Skip to content
Snippets Groups Projects
Commit 3cdcef4d authored by Martin Bauer's avatar Martin Bauer
Browse files

Python: accessing GPU fields from python - ghost layer info

parent ed4fe2aa
Branches
Tags
No related merge requests found
from pycuda.gpuarray import GPUArray
import numpy as np
from .field_extension import normalizeGhostlayerInfo
def toGpuArray(f):
def toGpuArray(f, withGhostLayers=True):
"""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)
res = GPUArray(f.sizeWithGhostLayers, dtype, gpudata=f.ptr, strides=strides)
ghostLayers = normalizeGhostlayerInfo(f, withGhostLayers)
glCutoff = [ f.nrOfGhostLayers - gl for gl in ghostLayers ]
res = res[ glCutoff[0]:-glCutoff[0] if glCutoff[0] > 0 else None,
glCutoff[1]:-glCutoff[1] if glCutoff[1] > 0 else None,
glCutoff[2]:-glCutoff[2] if glCutoff[2] > 0 else None,
: ]
return res
def extend(cppCudaModule):
cppCudaModule.toGpuArray = toGpuArray
......
......@@ -7,6 +7,25 @@ except ImportError:
# ----------------------------- Python functions to extend the C++ field module ---------------------------------
def normalizeGhostlayerInfo( field, withGhostLayers):
"""Takes one ghost layer parameter and returns an integer:
True -> all ghost layers, False->no ghost layers"""
def normalizeComponent(gl):
if gl == False:
return 0
if gl == True:
return field.nrOfGhostLayers
if gl > field.nrOfGhostLayers:
raise ValueError("Field only has %d ghost layers (requested %d)" % ( field.nrOfGhostLayers, gl ) )
return gl
if hasattr( withGhostLayers, "__len__") and len(withGhostLayers) == 3:
ghostLayers = [ normalizeComponent(gl) for gl in withGhostLayers ]
else:
ghostLayers = [ normalizeComponent(withGhostLayers) ] * 3
return ghostLayers
def npArrayFromWaLBerlaField(field, withGhostLayers=False):
""" Creates a numpy array view on the waLBerla field data
@field: the waLBerla field
......@@ -19,24 +38,9 @@ def npArrayFromWaLBerlaField(field, withGhostLayers=False):
if not field:
return None
def normalizeGhostlayerInfo( field, gl ):
"""Takes one ghost layer parameter and returns an integer:
True -> all ghost layers, False->no ghost layers"""
if gl == False:
return 0
if gl == True:
return field.nrOfGhostLayers
if gl > field.nrOfGhostLayers:
raise ValueError("Field only has %d ghost layers (requested %d)" % ( field.nrOfGhostLayers, gl ) )
return gl
if hasattr( withGhostLayers, "__len__") and len(withGhostLayers) == 3:
ghostLayers = [ normalizeGhostlayerInfo(field, gl) for gl in withGhostLayers ]
else:
ghostLayers = [ normalizeGhostlayerInfo(field, withGhostLayers) ] * 3
ghostLayers = normalizeGhostlayerInfo(field, withGhostLayers)
if not hasattr(field, 'buffer'): # Field adaptor -> create field with adapted values
field = field.copyToField()
......
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