Commit ba5733cc authored by Martin Bauer's avatar Martin Bauer

Python export for GPUFields and interface to pycuda

parent dd28a536
......@@ -2,13 +2,18 @@
if ( WALBERLA_BUILD_WITH_PYTHON_MODULE )
set(PYTHON_MODULE_DEPENDENCIES blockforest boundary domain_decomposition core field geometry lbm postprocessing python_coupling timeloop vtk)
if (WALBERLA_BUILD_WITH_CUDA)
set(PYTHON_MODULE_DEPENDENCIES ${PYTHON_MODULE_DEPENDENCIES} cuda)
endif()
if( WALBERLA_CXX_COMPILER_IS_MSVC )
set ( pythonModules blockforest boundary domain_decomposition core field geometry lbm postprocessing python_coupling timeloop vtk)
set ( pythonModules ${PYTHON_MODULE_DEPENDENCIES})
elseif( APPLE )
set ( pythonModules "-Wl,-force_load" blockforest boundary domain_decomposition core field geometry lbm postprocessing python_coupling timeloop vtk)
set ( pythonModules "-Wl,-force_load" ${PYTHON_MODULE_DEPENDENCIES})
else()
set ( pythonModules "-Wl,-whole-archive" blockforest boundary domain_decomposition core field geometry lbm postprocessing python_coupling timeloop vtk "-Wl,-no-whole-archive" )
set ( pythonModules "-Wl,-whole-archive" ${PYTHON_MODULE_DEPENDENCIES} "-Wl,-no-whole-archive" )
endif()
if( WALBERLA_BUILD_WITH_PYTHON_LBM )
......
......@@ -30,10 +30,15 @@
#include "timeloop/python/Exports.h"
#include "vtk/python/Exports.h"
#ifdef WALBERLA_BUILD_WITH_CUDA
#include "cuda/python/Exports.h"
#endif
#include <boost/mpl/vector.hpp>
#include <boost/mpl/insert_range.hpp>
namespace bmpl = boost::mpl;
using namespace walberla;
......@@ -111,6 +116,14 @@ struct InitObject
// Timeloop
pythonManager->addExporterFunction( timeloop::exportModuleToPython );
#ifdef WALBERLA_BUILD_WITH_CUDA
using walberla::cuda::GPUField;
typedef bmpl::vector<GPUField<double>, GPUField<float>, GPUField<int>, GPUField<uint8_t>, GPUField<uint16_t> > GPUFields;
pythonManager->addExporterFunction( cuda::exportModuleToPython<GPUFields> );
pythonManager->addBlockDataConversion<GPUFields>();
#endif
python_coupling::initWalberlaForPythonModule();
}
};
......
......@@ -28,7 +28,10 @@ if cpp_available:
# extend the C++ module with some python functions
from .field_extension import extend as extend_field
extend_field( field )
if 'cuda' in globals():
sys.modules[__name__ + '.cuda'] = cuda
from .cuda_extension import extend as extend_cuda
extend_cuda( cuda )
if 'geometry' in globals():
sys.modules[__name__ + '.geometry'] = geometry
if 'lbm' in globals():
......
from pycuda.gpuarray import GPUArray
import numpy as np
def toGpuArray(f):
"""Converts a waLBerla GPUField to a pycuda GPUArray"""
if not f:
return None
dtype = np.dtype(f.dtypeStr)
strides = [dtype.itemsize*a for a in f.strides]
return GPUArray(f.sizeWithGhostLayers, dtype, gpudata=f.ptr, strides=strides)
def extend(cppCudaModule):
cppCudaModule.toGpuArray = toGpuArray
......@@ -21,6 +21,7 @@
#pragma once
#include "cuda/FieldCopy.h"
namespace walberla {
namespace cuda {
......
......@@ -4,6 +4,6 @@
#
###################################################################################################
waLBerla_add_module( DEPENDS core communication domain_decomposition field stencil BUILD_ONLY_IF_FOUND CUDA )
waLBerla_add_module( DEPENDS core communication domain_decomposition python_coupling field stencil BUILD_ONLY_IF_FOUND CUDA )
###################################################################################################
\ No newline at end of file
......@@ -91,4 +91,5 @@ namespace cuda {
} // namespace cuda
} // namespace walberla
#include "FieldIndexing.impl.h"
......@@ -20,7 +20,6 @@
//======================================================================================================================
#include "FieldIndexing.h"
#include "GPUTypesExplicitInstantiation.h"
#include "GPUField.h"
#include "core/cell/CellInterval.h"
......@@ -224,10 +223,6 @@ FieldIndexing<T> FieldIndexing<T>::all ( const GPUField<T> & f, const cell::Cell
GPU_CLASS_TEMPLATE_INSTANTIATION( FieldIndexing )
} // namespace cuda
} // namespace walberla
......
......@@ -103,3 +103,4 @@ namespace cuda {
} // namespace walberla
#include "FieldIndexing.impl.h"
\ No newline at end of file
......@@ -20,7 +20,6 @@
//======================================================================================================================
#include "FieldIndexing3D.h"
#include "GPUTypesExplicitInstantiation.h"
#include "GPUField.h"
#include "core/cell/CellInterval.h"
......@@ -165,10 +164,6 @@ FieldIndexing3D<T> FieldIndexing3D<T>::intervalXYZ( const GPUField<T> & f, const
GPU_CLASS_TEMPLATE_INSTANTIATION( FieldIndexing3D )
} // namespace cuda
} // namespace walberla
......
......@@ -77,3 +77,4 @@ template< typename T> class GPUField;
} // namespace walberla
#include "FieldIndexingXYZ.impl.h"
\ No newline at end of file
......@@ -20,7 +20,6 @@
//======================================================================================================================
#include "FieldIndexingXYZ.h"
#include "GPUTypesExplicitInstantiation.h"
#include "GPUField.h"
#include "core/cell/CellInterval.h"
......@@ -114,8 +113,6 @@ FieldIndexingXYZ<T> FieldIndexingXYZ<T>::withGhostLayerXYZ( const GPUField<T> &
}
GPU_CLASS_TEMPLATE_INSTANTIATION( FieldIndexingXYZ )
} // namespace cuda
} // namespace walberla
......
......@@ -79,11 +79,27 @@ namespace cuda {
inline uint_t zSize() const { return zSize_; }
inline uint_t fSize() const { return fSize_; }
inline uint_t size() const { return fSize() * xSize() * ySize() * zSize(); }
inline uint_t size( uint_t coord ) const;
inline uint_t xSizeWithGhostLayer() const { return xSize() + uint_c(2)*nrOfGhostLayers_; }
inline uint_t ySizeWithGhostLayer() const { return ySize() + uint_c(2)*nrOfGhostLayers_; }
inline uint_t zSizeWithGhostLayer() const { return zSize() + uint_c(2)*nrOfGhostLayers_; }
inline uint_t sizeWithGhostLayer(uint_t i) const { return i==3 ? fSize_ :
size(i) + uint_c(2)*nrOfGhostLayers_; }
cell_idx_t xOff() const { return cell_idx_c( nrOfGhostLayers_ ); }
cell_idx_t yOff() const { return cell_idx_c( nrOfGhostLayers_ ); }
cell_idx_t zOff() const { return cell_idx_c( nrOfGhostLayers_ ); }
cell_idx_t xStride() const { return (layout_ == fzyx) ? cell_idx_t(1) :
cell_idx_c(fAllocSize()); }
cell_idx_t yStride() const { return (layout_ == fzyx) ? cell_idx_t(xAllocSize()) :
cell_idx_c(fAllocSize() * xAllocSize()); }
cell_idx_t zStride() const { return (layout_ == fzyx) ? cell_idx_t(xAllocSize() * yAllocSize()) :
cell_idx_c(fAllocSize() * xAllocSize() * yAllocSize()); }
cell_idx_t fStride() const { return (layout_ == fzyx) ? cell_idx_t(xAllocSize() * yAllocSize() * zAllocSize()) :
cell_idx_c(1); }
uint_t xAllocSize() const;
uint_t yAllocSize() const;
......@@ -91,8 +107,8 @@ namespace cuda {
uint_t fAllocSize() const;
inline uint_t allocSize() const { return fAllocSize() * xAllocSize() * yAllocSize() * zAllocSize(); }
inline bool hasSameAllocSize( const GPUField<T> & other ) const;
inline bool hasSameSize( const GPUField<T> & other ) const;
bool hasSameAllocSize( const GPUField<T> & other ) const;
bool hasSameSize( const GPUField<T> & other ) const;
GPUField<T> * cloneUninitialized() const;
......@@ -133,3 +149,4 @@ namespace cuda {
} // namespace walberla
#include "GPUField.impl.h"
\ No newline at end of file
......@@ -21,7 +21,6 @@
#include "GPUField.h"
#include "ErrorChecking.h"
#include "GPUTypesExplicitInstantiation.h"
#include "core/logging/Logging.h"
......@@ -124,12 +123,23 @@ void GPUField<T>::getSlice(stencil::Direction d, CellInterval & ci,
}
}
template<typename T>
inline uint_t GPUField<T>::size( uint_t coord ) const
{
switch (coord) {
case 0: return this->xSize();
case 1: return this->ySize();
case 2: return this->zSize();
case 3: return this->fSize();
default: WALBERLA_ASSERT(false); return 0;
}
}
//*******************************************************************************************************************
/*! True if sizes of all dimensions match
*******************************************************************************************************************/
template<typename T>
inline bool GPUField<T>::hasSameSize( const GPUField<T> & other ) const
bool GPUField<T>::hasSameSize( const GPUField<T> & other ) const
{
return xSize() == other.xSize() &&
ySize() == other.ySize() &&
......@@ -140,7 +150,7 @@ inline bool GPUField<T>::hasSameSize( const GPUField<T> & other ) const
/*! True if allocation sizes of all dimensions match
*******************************************************************************************************************/
template<typename T>
inline bool GPUField<T>::hasSameAllocSize( const GPUField<T> & other ) const
bool GPUField<T>::hasSameAllocSize( const GPUField<T> & other ) const
{
return xAllocSize() == other.xAllocSize() &&
yAllocSize() == other.yAllocSize() &&
......@@ -238,9 +248,6 @@ void GPUField<T>::swapDataPointers( GPUField<T> & other )
GPU_CLASS_TEMPLATE_INSTANTIATION( GPUField )
} // namespace cuda
} // namespace walberla
......
#define GPU_CLASS_TEMPLATE_INSTANTIATION(ClassName)\
template class ClassName< double >;\
template class ClassName< float >;\
template class ClassName< int >;\
template class ClassName< uint8_t >;\
template class ClassName< uint16_t >;
//======================================================================================================================
//
// This file is part of waLBerla. waLBerla is free software: you can
// redistribute it and/or modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation, either version 3 of
// the License, or (at your option) any later version.
//
// waLBerla is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details.
//
// You should have received a copy of the GNU General Public License along
// with waLBerla (see COPYING.txt). If not, see <http://www.gnu.org/licenses/>.
//
//! \file FieldExport.h
//! \ingroup cuda
//! \author Martin Bauer <martin.bauer@fau.de>
//
//======================================================================================================================
#pragma once
#ifdef WALBERLA_BUILD_WITH_PYTHON
#include <string>
namespace walberla {
namespace cuda {
template<typename GpuFields >
void exportModuleToPython();
} // namespace cuda
} // namespace walberla
#include "Exports.impl.h"
#endif //WALBERLA_BUILD_WITH_PYTHON
This diff is collapsed.
......@@ -29,6 +29,8 @@
#cmakedefine WALBERLA_BUILD_WITH_OPENMESH
#cmakedefine WALBERLA_BUILD_WITH_CUDA
#cmakedefine WALBERLA_BUFFER_DEBUG
#cmakedefine WALBERLA_THREAD_SAFE_LOGGING
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment