Added OpenCL CIC code

This commit is contained in:
Guilhem Lavaux 2014-09-23 11:54:44 +02:00
parent 1e733f2318
commit f6ad248f75
8 changed files with 297 additions and 78 deletions

View file

@ -21,6 +21,7 @@ cdef extern from "project_tool.hpp" namespace "":
@cython.boundscheck(False)
@cython.cdivision(True)
@cython.wraparound(False)
cdef int interp3d_INTERNAL_periodic(DTYPE_t x, DTYPE_t y,
DTYPE_t z,
DTYPE_t[:,:,:] d, DTYPE_t Lbox, DTYPE_t *retval) nogil:
@ -45,6 +46,10 @@ cdef int interp3d_INTERNAL_periodic(DTYPE_t x, DTYPE_t y,
ry -= iy
rz -= iz
ix = ix % Ngrid
iy = iy % Ngrid
iz = iz % Ngrid
jx = (ix+1)%Ngrid
jy = (iy+1)%Ngrid
jz = (iz+1)%Ngrid
@ -53,13 +58,6 @@ cdef int interp3d_INTERNAL_periodic(DTYPE_t x, DTYPE_t y,
iy = iy%Ngrid
iz = iz%Ngrid
if (ix < 0) or (jx >= Ngrid):
return -1
if (iy < 0) or (jy >= Ngrid):
return -2
if (iz < 0) or (jz >= Ngrid):
return -3
f[0][0][0] = (1-rx)*(1-ry)*(1-rz)
f[1][0][0] = ( rx)*(1-ry)*(1-rz)
f[0][1][0] = (1-rx)*( ry)*(1-rz)
@ -80,10 +78,9 @@ cdef int interp3d_INTERNAL_periodic(DTYPE_t x, DTYPE_t y,
d[ix ,jy ,jz ] * f[0][1][1] + \
d[jx ,jy ,jz ] * f[1][1][1]
return 0
@cython.boundscheck(False)
@cython.cdivision(True)
@cython.wraparound(False)
cdef int ngp3d_INTERNAL_periodic(DTYPE_t x, DTYPE_t y,
DTYPE_t z,
DTYPE_t[:,:,:] d, DTYPE_t Lbox, DTYPE_t *retval) nogil:
@ -110,14 +107,13 @@ cdef int ngp3d_INTERNAL_periodic(DTYPE_t x, DTYPE_t y,
retval[0] = d[ix ,iy ,iz ]
return 0
@cython.boundscheck(False)
@cython.cdivision(True)
@cython.wraparound(False)
cdef int ngp3d_INTERNAL(DTYPE_t x, DTYPE_t y,
DTYPE_t z,
DTYPE_t[:,:,:] d, DTYPE_t Lbox, DTYPE_t *retval) nogil:
DTYPE_t[:,:,:] d, DTYPE_t Lbox, DTYPE_t *retval, DTYPE_t inval) nogil:
cdef int Ngrid = d.shape[0]
cdef DTYPE_t inv_delta = Ngrid/Lbox
@ -134,23 +130,18 @@ cdef int ngp3d_INTERNAL(DTYPE_t x, DTYPE_t y,
iy = int(round(ry))
iz = int(round(rz))
if (ix < 0 or ix >= Ngrid):
return -1
if (iy < 0 or iy >= Ngrid):
return -2
if (iz < 0 or iz >= Ngrid):
return -3
if ((ix < 0) or (ix+1) >= Ngrid or (iy < 0) or (iy+1) >= Ngrid or (iz < 0) or (iz+1) >= Ngrid):
retval[0] = inval
retval[0] = d[ix ,iy ,iz ]
return 0
@cython.boundscheck(False)
@cython.cdivision(True)
@cython.wraparound(False)
cdef int interp3d_INTERNAL(DTYPE_t x, DTYPE_t y,
DTYPE_t z,
DTYPE_t[:,:,:] d, DTYPE_t Lbox, DTYPE_t *retval) nogil:
DTYPE_t[:,:,:] d, DTYPE_t Lbox, DTYPE_t *retval, DTYPE_t inval) nogil:
cdef int Ngrid = d.shape[0]
cdef DTYPE_t inv_delta = Ngrid/Lbox
@ -170,14 +161,8 @@ cdef int interp3d_INTERNAL(DTYPE_t x, DTYPE_t y,
ry -= iy
rz -= iz
if ((ix < 0) or (ix+1) >= Ngrid):
return -1
if ((iy < 0) or (iy+1) >= Ngrid):
return -2
if ((iz < 0) or (iz+1) >= Ngrid):
return -3
if ((ix < 0) or (ix+1) >= Ngrid or (iy < 0) or (iy+1) >= Ngrid or (iz < 0) or (iz+1) >= Ngrid):
retval[0] = inval
# assert ((ix >= 0) and ((ix+1) < Ngrid))
# assert ((iy >= 0) and ((iy+1) < Ngrid))
# assert ((iz >= 0) and ((iz+1) < Ngrid))
@ -202,13 +187,11 @@ cdef int interp3d_INTERNAL(DTYPE_t x, DTYPE_t y,
d[ix ,iy+1,iz+1] * f[0][1][1] + \
d[ix+1,iy+1,iz+1] * f[1][1][1]
return 0
@cython.boundscheck(False)
def interp3d(x not None, y not None,
z not None,
npx.ndarray[DTYPE_t, ndim=3] d not None, DTYPE_t Lbox,
bool periodic=False, bool centered=True, bool ngp=False):
bool periodic=False, bool centered=True, bool ngp=False, DTYPE_t inval = 0):
""" interp3d(x,y,z,d,Lbox,periodic=False,centered=True,ngp=False) -> interpolated values
Compute the tri-linear interpolation of the given field (d) at the given position (x,y,z). It assumes that they are box-centered coordinates. So (x,y,z) == (0,0,0) is equivalent to the pixel at (Nx/2,Ny/2,Nz/2) with Nx,Ny,Nz = d.shape. If periodic is set, it assumes the box is periodic
@ -253,41 +236,29 @@ def interp3d(x not None, y not None,
if not myngp:
if myperiodic:
for i in prange(Nelt):
if interp3d_INTERNAL_periodic(shifter+ax[i], shifter+ay[i], shifter+az[i], in_slice, Lbox, &out_slice[i]) < 0:
with gil:
raise ierror
interp3d_INTERNAL_periodic(shifter+ax[i], shifter+ay[i], shifter+az[i], in_slice, Lbox, &out_slice[i])
else:
for i in prange(Nelt):
if interp3d_INTERNAL(shifter+ax[i], shifter+ay[i], shifter+az[i], in_slice, Lbox, &out_slice[i]) < 0:
with gil:
raise ierror
interp3d_INTERNAL(shifter+ax[i], shifter+ay[i], shifter+az[i], in_slice, Lbox, &out_slice[i], inval)
else:
if myperiodic:
for i in prange(Nelt):
if ngp3d_INTERNAL_periodic(shifter+ax[i], shifter+ay[i], shifter+az[i], in_slice, Lbox, &out_slice[i]) < 0:
with gil:
raise ierror
ngp3d_INTERNAL_periodic(shifter+ax[i], shifter+ay[i], shifter+az[i], in_slice, Lbox, &out_slice[i])
else:
for i in prange(Nelt):
if ngp3d_INTERNAL(shifter+ax[i], shifter+ay[i], shifter+az[i], in_slice, Lbox, &out_slice[i]) < 0:
with gil:
raise ierror
ngp3d_INTERNAL(shifter+ax[i], shifter+ay[i], shifter+az[i], in_slice, Lbox, &out_slice[i], inval)
return out
else:
if not myngp:
if periodic:
if interp3d_INTERNAL_periodic(shifter+x, shifter+y, shifter+z, d, Lbox, &retval) < 0:
raise ierror
interp3d_INTERNAL_periodic(shifter+x, shifter+y, shifter+z, d, Lbox, &retval)
else:
if interp3d_INTERNAL(shifter+x, shifter+y, shifter+z, d, Lbox, &retval) < 0:
raise ierror
interp3d_INTERNAL(shifter+x, shifter+y, shifter+z, d, Lbox, &retval, inval)
else:
if periodic:
if ngp3d_INTERNAL_periodic(shifter+x, shifter+y, shifter+z, d, Lbox, &retval) < 0:
raise ierror
ngp3d_INTERNAL_periodic(shifter+x, shifter+y, shifter+z, d, Lbox, &retval)
else:
if ngp3d_INTERNAL(shifter+x, shifter+y, shifter+z, d, Lbox, &retval) < 0:
raise ierror
ngp3d_INTERNAL(shifter+x, shifter+y, shifter+z, d, Lbox, &retval, inval)
return retval
@cython.boundscheck(False)
@ -410,8 +381,7 @@ def interp2d(x not None, y not None,
@cython.boundscheck(False)
@cython.cdivision(True)
cdef void INTERNAL_project_cic_no_mass(npx.ndarray[DTYPE_t, ndim=3] g,
npx.ndarray[DTYPE_t, ndim=2] x, int Ngrid, float Lbox):
cdef double half_Box = 0.5*Lbox
npx.ndarray[DTYPE_t, ndim=2] x, int Ngrid, double Lbox, double shifter):
cdef double delta_Box = Ngrid/Lbox
cdef int i
cdef double a[3], c[3]
@ -422,7 +392,7 @@ cdef void INTERNAL_project_cic_no_mass(npx.ndarray[DTYPE_t, ndim=3] g,
do_not_put = 0
for j in range(3):
a[j] = (x[i,j]+half_Box)*delta_Box
a[j] = (x[i,j]+shifter)*delta_Box
b[j] = int(floor(a[j]))
a[j] -= b[j]
c[j] = 1-a[j]
@ -446,8 +416,7 @@ cdef void INTERNAL_project_cic_no_mass(npx.ndarray[DTYPE_t, ndim=3] g,
@cython.boundscheck(False)
@cython.cdivision(True)
cdef void INTERNAL_project_cic_no_mass_periodic(npx.ndarray[DTYPE_t, ndim=3] g,
npx.ndarray[DTYPE_t, ndim=2] x, int Ngrid, double Lbox):
cdef double half_Box = 0.5*Lbox
npx.ndarray[DTYPE_t, ndim=2] x, int Ngrid, double Lbox, double shifter):
cdef double delta_Box = Ngrid/Lbox
cdef int i
cdef double a[3], c[3]
@ -463,7 +432,7 @@ cdef void INTERNAL_project_cic_no_mass_periodic(npx.ndarray[DTYPE_t, ndim=3] g,
do_not_put = 0
for j in range(3):
a[j] = (ax[i,j]+half_Box)*delta_Box
a[j] = (ax[i,j]+shifter)*delta_Box
b[j] = int(floor(a[j]))
b1[j] = (b[j]+1) % Ngrid
@ -488,8 +457,7 @@ cdef void INTERNAL_project_cic_no_mass_periodic(npx.ndarray[DTYPE_t, ndim=3] g,
cdef void INTERNAL_project_cic_with_mass(npx.ndarray[DTYPE_t, ndim=3] g,
npx.ndarray[DTYPE_t, ndim=2] x,
npx.ndarray[DTYPE_t, ndim=1] mass,
int Ngrid, double Lbox):
cdef double half_Box = 0.5*Lbox
int Ngrid, double Lbox, double shifter):
cdef double delta_Box = Ngrid/Lbox
cdef int i
cdef double a[3], c[3]
@ -500,7 +468,7 @@ cdef void INTERNAL_project_cic_with_mass(npx.ndarray[DTYPE_t, ndim=3] g,
do_not_put = False
for j in range(3):
a[j] = (x[i,j]+half_Box)*delta_Box
a[j] = (x[i,j]+shifter)*delta_Box
b[j] = int(a[j])
a[j] -= b[j]
c[j] = 1-a[j]
@ -525,7 +493,7 @@ cdef void INTERNAL_project_cic_with_mass(npx.ndarray[DTYPE_t, ndim=3] g,
cdef void INTERNAL_project_cic_with_mass_periodic(npx.ndarray[DTYPE_t, ndim=3] g,
npx.ndarray[DTYPE_t, ndim=2] x,
npx.ndarray[DTYPE_t, ndim=1] mass,
int Ngrid, double Lbox):
int Ngrid, double Lbox, double shifter):
cdef double half_Box = 0.5*Lbox, m0
cdef double delta_Box = Ngrid/Lbox
cdef int i
@ -535,7 +503,7 @@ cdef void INTERNAL_project_cic_with_mass_periodic(npx.ndarray[DTYPE_t, ndim=3] g
for i in range(x.shape[0]):
for j in range(3):
a[j] = (x[i,j]+half_Box)*delta_Box
a[j] = (x[i,j]+shifter)*delta_Box
b[j] = int(floor(a[j]))
b1[j] = b[j]+1
while b1[j] < 0:
@ -559,14 +527,20 @@ cdef void INTERNAL_project_cic_with_mass_periodic(npx.ndarray[DTYPE_t, ndim=3] g
def project_cic(npx.ndarray[DTYPE_t, ndim=2] x not None, npx.ndarray[DTYPE_t, ndim=1] mass, int Ngrid,
double Lbox, bool periodic = False):
double Lbox, bool periodic = False, centered=True):
"""
project_cic(x array (N,3), mass (may be None), Ngrid, Lbox, periodict)
project_cic(x array (N,3), mass (may be None), Ngrid, Lbox, periodict, centered=True)
This function does a Cloud-In-Cell projection of a 3d unstructured dataset. First argument is a Nx3 array of coordinates.
Second argument is an optinal mass. Ngrid is the size output grid and Lbox is the physical size of the grid.
"""
cdef npx.ndarray[DTYPE_t, ndim=3] g
cdef double shifter
if centered:
shifter = 0.5*Lbox
else:
shifter = 0
if x.shape[1] != 3:
raise ValueError("Invalid shape for x array")
@ -578,14 +552,14 @@ def project_cic(npx.ndarray[DTYPE_t, ndim=2] x not None, npx.ndarray[DTYPE_t, nd
if not periodic:
if mass == None:
INTERNAL_project_cic_no_mass(g, x, Ngrid, Lbox)
INTERNAL_project_cic_no_mass(g, x, Ngrid, Lbox, shifter)
else:
INTERNAL_project_cic_with_mass(g, x, mass, Ngrid, Lbox)
INTERNAL_project_cic_with_mass(g, x, mass, Ngrid, Lbox, shifter)
else:
if mass == None:
INTERNAL_project_cic_no_mass_periodic(g, x, Ngrid, Lbox)
INTERNAL_project_cic_no_mass_periodic(g, x, Ngrid, Lbox, shifter)
else:
INTERNAL_project_cic_with_mass_periodic(g, x, mass, Ngrid, Lbox)
INTERNAL_project_cic_with_mass_periodic(g, x, mass, Ngrid, Lbox, shifter)
return g