Minor clean up and testing using a low-end GPU

This commit is contained in:
Jerome Kieffer 2012-06-14 23:32:36 +02:00
parent 22b7745cb6
commit 70a13047bf
8 changed files with 7559 additions and 5578 deletions

4
.gitignore vendored
View File

@ -25,3 +25,7 @@ pip-log.txt
#Mr Developer #Mr Developer
.mr.developer.cfg .mr.developer.cfg
#Shared libraries
*.so

View File

@ -1 +0,0 @@
#error Do not use this file, it is the result of a failed Cython compilation.

File diff suppressed because it is too large Load Diff

View File

@ -36,6 +36,8 @@ cimport numpy
import numpy import numpy
import threading import threading
import cython import cython
from stdlib cimport free, malloc
from libc.string cimport memcpy
@ -45,9 +47,11 @@ cdef class Integrator1d:
""" """
cdef ocl_xrpd1d.ocl_xrpd1D_fullsplit* cpp_integrator cdef ocl_xrpd1d.ocl_xrpd1D_fullsplit* cpp_integrator
cdef char* _devicetype cdef char* _devicetype
cdef int _nBins, _nData, _platformid, _devid, _tth_id, _dtth_id, _mask_id, _solid_angle_id cdef char* filename
cdef int _nBins, _nData, _platformid, _devid,
cdef cpp_bool _useFp64 cdef cpp_bool _useFp64
cdef float tth_min, tth_max,_tth_min, _tth_max cdef float tth_min, tth_max,_tth_min, _tth_max
cdef float* ctth_out
def __cinit__(self, filename=None): def __cinit__(self, filename=None):
""" """
@ -55,28 +59,26 @@ cdef class Integrator1d:
""" """
self._nBins = -1 self._nBins = -1
self._nData = -1 self._nData = -1
self._platformid = -1
self._devid = -1
self._useFp64 = False self._useFp64 = False
self._tth_id = 0
self._dtth_id = 0
self._mask_id = 0
self._solid_angle_id = 0
self._devicetype = "gpu" self._devicetype = "gpu"
if filename is None: if filename is None:
self.cpp_integrator = new ocl_xrpd1d.ocl_xrpd1D_fullsplit() self.cpp_integrator = new ocl_xrpd1d.ocl_xrpd1D_fullsplit()
else: else:
name = str(filename)
self.cpp_integrator = new ocl_xrpd1d.ocl_xrpd1D_fullsplit(name)
def __init__(self,filename=None):
"""
Python constructor
"""
self.filename = filename self.filename = filename
self.tth_out = None self.cpp_integrator = new ocl_xrpd1d.ocl_xrpd1D_fullsplit(self.filename)
def __dealloc__(self): def __dealloc__(self):
if self.ctth_out:
free(self.ctth_out)
del self.cpp_integrator del self.cpp_integrator
def __repr__(self):
return os.linesep.join(["Cython wrapper for ocl_xrpd1d.ocl_xrpd1D_fullsplit C++ class. Logging in %s"%self.filename,
"device: %s, platform %s device %s 64bits:%s image size: %s histogram size: %s"%(self._devicetype,self._platformid,self._devid, self._useFp64, self._nData,self._nBins),
",\t ".join(["%s: %s"%(k,v) for k,v in self.get_status().items()])])
@cython.cdivision(True) @cython.cdivision(True)
@cython.boundscheck(False) @cython.boundscheck(False)
@cython.wraparound(False) @cython.wraparound(False)
@ -86,12 +88,15 @@ cdef class Integrator1d:
""" """
self.tth_min = lower self.tth_min = lower
self.tth_max = upper self.tth_max = upper
cdef numpy.ndarray[numpy.float32_t, ndim = 1] tth_out = numpy.empty(self._nBins,dtype=numpy.float32) # cdef numpy.ndarray[numpy.float32_t, ndim = 1] tth_out = numpy.empty(self._nBins,dtype=numpy.float32)
if self.ctth_out:
free(self.ctth_out)
self.ctth_out= <float*> malloc(self._nBins*sizeof(float))
cdef float delta = (upper - lower ) / (< float > (self._nBins)) cdef float delta = (upper - lower ) / (< float > (self._nBins))
with nogil: with nogil:
for i in range(self._nBins): for i in range(self._nBins):
tth_out[i] = self.tth_min + (0.5 +< float > i) * delta self.ctth_out[i] = <float>( self.tth_min + (0.5 +< float > i) * delta)
self.tth_out = tth_out # self.tth_out = tth_out
def getConfiguration(self, int Nimage, int Nbins, useFp64=None): def getConfiguration(self, int Nimage, int Nbins, useFp64=None):
@ -244,17 +249,19 @@ cdef class Integrator1d:
set / unset and loadTth methods have a direct impact on the execute() method. set / unset and loadTth methods have a direct impact on the execute() method.
All the rest of the methods will require at least a new configuration via configure()""" All the rest of the methods will require at least a new configuration via configure()"""
cdef int rc,i cdef int rc,i
cdef numpy.ndarray[numpy.float32_t, ndim = 1] cimage, histogram, bins cdef numpy.ndarray[numpy.float32_t, ndim = 1] cimage, histogram, bins,tth_out
cimage = numpy.ascontiguousarray(image.ravel(),dtype=numpy.float32) cimage = numpy.ascontiguousarray(image.ravel(),dtype=numpy.float32)
outPos = numpy.zeros(self._nBins,dtype=numpy.float32) histogram = numpy.empty(self._nBins,dtype=numpy.float32)
histogram = numpy.zeros(self._nBins,dtype=numpy.float32) bins = numpy.empty(self._nBins,dtype=numpy.float32)
bins = numpy.zeros(self._nBins,dtype=numpy.float32) tth_out = numpy.empty(self._nBins,dtype=numpy.float32)
assert cimage.size == self._nData assert cimage.size == self._nData
with nogil: with nogil:
rc = self.cpp_integrator.execute(<float*> cimage.data, <float*> histogram.data, <float*> bins.data) rc = self.cpp_integrator.execute(<float*> cimage.data, <float*> histogram.data, <float*> bins.data)
if rc!=0: if rc!=0:
raise RuntimeError("OpenCL integrator failed with RC=%s"%rc) raise RuntimeError("OpenCL integrator failed with RC=%s"%rc)
return self.tth_out,histogram,bins
memcpy(tth_out.data,self.ctth_out,self._nBins*sizeof(float))
return tth_out,histogram,bins
def clean(self, int preserve_context=0): def clean(self, int preserve_context=0):
"""Free OpenCL related resources. """Free OpenCL related resources.

View File

@ -389,7 +389,7 @@ class AzimuthalIntegrator(Geometry):
with self._ocl_sem: with self._ocl_sem:
if self._ocl is None: if self._ocl is None:
size = data.size size = data.size
fd, tmpfile = tempfile.mkstemp(".log", "pyfai-opencl") fd, tmpfile = tempfile.mkstemp(".log", "pyfai-opencl-")
os.close(fd) os.close(fd)
integr = ocl_azim.Integrator1d(tmpfile) integr = ocl_azim.Integrator1d(tmpfile)
if platformid and deviceid: if platformid and deviceid:

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -79,22 +79,21 @@ def histoBBox1d(numpy.ndarray weights not None,
assert pos0.size == size assert pos0.size == size
assert delta_pos0.size == size assert delta_pos0.size == size
assert bins > 1 assert bins > 1
print pos0Range
cdef long bin0_max, bin0_min, bin = 0 cdef long bin0_max, bin0_min, bin = 0
cdef float data, deltaR, deltaL, deltaA,p1, epsilon = 1e-10, cdummy, ddummy cdef float data, deltaR, deltaL, deltaA,p1, epsilon = 1e-10, cdummy, ddummy
cdef float pos0_min, pos0_max, pos0_maxin, pos1_min, pos1_max, pos1_maxin, min0, max0, fbin0_min, fbin0_max cdef float pos0_min, pos0_max, pos0_maxin, pos1_min, pos1_max, pos1_maxin, min0, max0, fbin0_min, fbin0_max
cdef int checkpos1 = 0, check_mask = 0, check_dummy = 0 cdef long checkpos1 = 0, check_mask = 0, check_dummy = 0
cdef numpy.ndarray[numpy.float32_t, ndim = 1] cdata = numpy.ascontiguousarray(weights.ravel(),dtype="float32") cdef numpy.ndarray[numpy.float32_t, ndim = 1] cdata = numpy.ascontiguousarray(weights.ravel(),dtype=numpy.float32)
cdef numpy.ndarray[numpy.float32_t, ndim = 1] cpos0, dpos0, cpos1, dpos1,cpos0_lower, cpos0_upper cdef numpy.ndarray[numpy.float32_t, ndim = 1] cpos0, dpos0, cpos1, dpos1,cpos0_lower, cpos0_upper
cpos0 = numpy.ascontiguousarray(pos0.ravel(), dtype="float32") cpos0 = numpy.ascontiguousarray(pos0.ravel(), dtype=numpy.float32)
dpos0 = numpy.ascontiguousarray(delta_pos0.ravel(), dtype="float32") dpos0 = numpy.ascontiguousarray(delta_pos0.ravel(), dtype=numpy.float32)
cdef numpy.ndarray[numpy.float64_t, ndim = 1] outData = numpy.zeros(bins, dtype="float64") cdef numpy.ndarray[numpy.float64_t, ndim = 1] outData = numpy.zeros(bins, dtype=numpy.float64)
cdef numpy.ndarray[numpy.float64_t, ndim = 1] outCount = numpy.zeros(bins, dtype="float64") cdef numpy.ndarray[numpy.float64_t, ndim = 1] outCount = numpy.zeros(bins, dtype=numpy.float64)
cdef numpy.ndarray[numpy.float32_t, ndim = 1] outMerge = numpy.zeros(bins, dtype="float32") cdef numpy.ndarray[numpy.float32_t, ndim = 1] outMerge = numpy.zeros(bins, dtype=numpy.float32)
cdef numpy.ndarray[numpy.float32_t, ndim = 1] outPos = numpy.zeros(bins, dtype="float32") cdef numpy.ndarray[numpy.float32_t, ndim = 1] outPos = numpy.zeros(bins, dtype=numpy.float32)
cdef numpy.ndarray[numpy.int8_t, ndim = 1] cmask cdef numpy.ndarray[numpy.int8_t, ndim = 1] cmask
if mask is not None: if mask is not None:
@ -111,8 +110,8 @@ def histoBBox1d(numpy.ndarray weights not None,
else: else:
cdummy=0.0 cdummy=0.0
cpos0_lower = numpy.zeros(size, dtype="float32") cpos0_lower = numpy.zeros(size, dtype=numpy.float32)
cpos0_upper = numpy.zeros(size, dtype="float32") cpos0_upper = numpy.zeros(size, dtype=numpy.float32)
pos0_min=cpos0[0] pos0_min=cpos0[0]
pos0_max=cpos0[0] pos0_max=cpos0[0]
with nogil: with nogil:
@ -138,8 +137,8 @@ def histoBBox1d(numpy.ndarray weights not None,
assert pos1.size == size assert pos1.size == size
assert delta_pos1.size == size assert delta_pos1.size == size
checkpos1 = 1 checkpos1 = 1
cpos1 = numpy.ascontiguousarray(pos1.ravel(),dtype="float32") cpos1 = numpy.ascontiguousarray(pos1.ravel(),dtype=numpy.float32)
dpos1 = numpy.ascontiguousarray(delta_pos1.ravel(),dtype="float32") dpos1 = numpy.ascontiguousarray(delta_pos1.ravel(),dtype=numpy.float32)
pos1_min = min(pos1Range) pos1_min = min(pos1Range)
pos1_maxin = max(pos1Range) pos1_maxin = max(pos1Range)
pos1_max = pos1_maxin * (1 + numpy.finfo(numpy.float32).eps) pos1_max = pos1_maxin * (1 + numpy.finfo(numpy.float32).eps)
@ -255,32 +254,54 @@ def histoBBox2d(numpy.ndarray weights not None,
bins0 = 1 bins0 = 1
if bins1 <= 0: if bins1 <= 0:
bins1 = 1 bins1 = 1
cdef numpy.ndarray[numpy.float32_t, ndim = 1] cdata = numpy.ascontiguousarray(weights.ravel(),dtype="float32") cdef numpy.ndarray[numpy.float32_t, ndim = 1] cdata = numpy.ascontiguousarray(weights.ravel(),dtype=numpy.float32)
cdef numpy.ndarray[numpy.float32_t, ndim = 1] cpos0 = numpy.ascontiguousarray(pos0.ravel(),dtype="float32") cdef numpy.ndarray[numpy.float32_t, ndim = 1] cpos0 = numpy.ascontiguousarray(pos0.ravel(),dtype=numpy.float32)
cdef numpy.ndarray[numpy.float32_t, ndim = 1] dpos0 = numpy.ascontiguousarray(delta_pos0.ravel(),dtype="float32") cdef numpy.ndarray[numpy.float32_t, ndim = 1] dpos0 = numpy.ascontiguousarray(delta_pos0.ravel(),dtype=numpy.float32)
# cdef numpy.ndarray[numpy.float32_t, ndim = 1] cpos0_inf = cpos0 - dpos0 cdef numpy.ndarray[numpy.float32_t, ndim = 1] cpos1 = numpy.ascontiguousarray(pos1.ravel(),dtype=numpy.float32)
# cdef numpy.ndarray[numpy.float32_t, ndim = 1] cpos0_sup = cpos0 + dpos0 cdef numpy.ndarray[numpy.float32_t, ndim = 1] dpos1 = numpy.ascontiguousarray(delta_pos1.ravel(),dtype=numpy.float32)
cdef numpy.ndarray[numpy.float32_t, ndim = 1] cpos1 = numpy.ascontiguousarray(pos1.ravel(),dtype="float32") cdef numpy.ndarray[numpy.float32_t, ndim = 1] cpos0_upper = numpy.zeros(size, dtype=numpy.float32)
cdef numpy.ndarray[numpy.float32_t, ndim = 1] dpos1 = numpy.ascontiguousarray(delta_pos1.ravel(),dtype="float32") cdef numpy.ndarray[numpy.float32_t, ndim = 1] cpos0_lower = numpy.zeros(size, dtype=numpy.float32)
# cdef numpy.ndarray[numpy.float32_t, ndim = 1] cpos1_inf = cpos1 - dpos1 # cdef numpy.ndarray[numpy.float32_t, ndim = 1] cpos1_upper = numpy.zeros(size, dtype=numpy.float32)
# cdef numpy.ndarray[numpy.float32_t, ndim = 1] cpos1_sup = cpos1 + dpos1 # cdef numpy.ndarray[numpy.float32_t, ndim = 1] cpos1_lower = numpy.zeros(size, dtype=numpy.float32)
cdef numpy.ndarray[numpy.float64_t, ndim = 2] outData = numpy.zeros((bins0, bins1), dtype="float64") cdef numpy.ndarray[numpy.float64_t, ndim = 2] outData = numpy.zeros((bins0, bins1), dtype=numpy.float64)
cdef numpy.ndarray[numpy.float64_t, ndim = 2] outCount = numpy.zeros((bins0, bins1), dtype="float64") cdef numpy.ndarray[numpy.float64_t, ndim = 2] outCount = numpy.zeros((bins0, bins1), dtype=numpy.float64)
cdef numpy.ndarray[numpy.float32_t, ndim = 2] outMerge = numpy.zeros((bins0, bins1), dtype="float32") cdef numpy.ndarray[numpy.float32_t, ndim = 2] outMerge = numpy.zeros((bins0, bins1), dtype=numpy.float32)
cdef numpy.ndarray[numpy.float32_t, ndim = 1] edges0 = numpy.zeros(bins0, dtype="float32") cdef numpy.ndarray[numpy.float32_t, ndim = 1] edges0 = numpy.zeros(bins0, dtype=numpy.float32)
cdef numpy.ndarray[numpy.float32_t, ndim = 1] edges1 = numpy.zeros(bins1, dtype="float32") cdef numpy.ndarray[numpy.float32_t, ndim = 1] edges1 = numpy.zeros(bins1, dtype=numpy.float32)
cdef float min0, max0, min1, max1, deltaR, deltaL, deltaU, deltaD, deltaA, tmp, delta0, delta1 cdef float min0, max0, min1, max1, deltaR, deltaL, deltaU, deltaD, deltaA, tmp, delta0, delta1
cdef float pos0_min, pos0_max, pos1_min, pos1_max, pos0_maxin, pos1_maxin cdef float pos0_min, pos0_max, pos1_min, pos1_max, pos0_maxin, pos1_maxin
cdef float fbin0_min, fbin0_max, fbin1_min, fbin1_max, data, epsilon = 1e-10 cdef float fbin0_min, fbin0_max, fbin1_min, fbin1_max, data, epsilon = 1e-10
cdef long bin0_max, bin0_min, bin1_max, bin1_min cdef long bin0_max, bin0_min, bin1_max, bin1_min
pos0_min=cpos0[0]
pos0_max=cpos0[0]
with nogil:
for idx in range(size):
min0 = cpos0[idx] - dpos0[idx]
max0 = cpos0[idx] + dpos0[idx]
cpos0_upper[idx]=max0
cpos0_lower[idx]=min0
if max0>pos0_max:
pos0_max=max0
if min0<pos0_min:
pos0_min=min0
# for idx in range(size):
# min1 = cpos1[idx] - dpos1[idx]
# max1 = cpos1[idx] + dpos1[idx]
# cpos1_upper[idx]=max1
# cpos1_lower[idx]=min1
# if max1>pos1_max:
# pos1_max=max1
# if min1<pos1_min:
# pos1_min=min1
if (pos0Range is not None) and (len(pos0Range) == 2): if (pos0Range is not None) and (len(pos0Range) == 2):
pos0_min = min(pos0Range) pos0_min = min(pos0Range)
pos0_maxin = max(pos0Range) pos0_maxin = max(pos0Range)
else: else:
pos0_min = cpos0.min() pos0_min = pos0_min
pos0_maxin = cpos0.max() pos0_maxin = pos0_max
if pos0_min<0: if pos0_min<0:
pos0_min=0 pos0_min=0
pos0_max = pos0_maxin * (1 + numpy.finfo(numpy.float32).eps) pos0_max = pos0_maxin * (1 + numpy.finfo(numpy.float32).eps)
@ -289,7 +310,6 @@ def histoBBox2d(numpy.ndarray weights not None,
pos1_min = min(pos1Range) pos1_min = min(pos1Range)
pos1_maxin = max(pos1Range) pos1_maxin = max(pos1Range)
else: else:
# tmp = cdelta_pos1.min()
pos1_min = cpos1.min() pos1_min = cpos1.min()
pos1_maxin = cpos1.max() pos1_maxin = cpos1.max()
pos1_max = pos1_maxin * (1 + numpy.finfo(numpy.float32).eps) pos1_max = pos1_maxin * (1 + numpy.finfo(numpy.float32).eps)
@ -305,8 +325,8 @@ def histoBBox2d(numpy.ndarray weights not None,
for idx in range(size): for idx in range(size):
data = cdata[idx] data = cdata[idx]
min0 = cpos0[idx] - dpos0[idx] min0 = cpos0_lower[idx]
max0 = cpos0[idx] + dpos0[idx] max0 = cpos0_upper[idx]
min1 = cpos1[idx] - dpos1[idx] min1 = cpos1[idx] - dpos1[idx]
max1 = cpos1[idx] + dpos1[idx] max1 = cpos1[idx] + dpos1[idx]
@ -404,7 +424,7 @@ def histoBBox2d(numpy.ndarray weights not None,
for i in range(bins0): for i in range(bins0):
for j in range(bins1): for j in range(bins1):
if outCount[i, j] > epsilon: if outCount[i, j] > epsilon:
outMerge[i, j] = outData[i, j] / outCount[i, j] outMerge[i, j] = <float> (outData[i, j] / outCount[i, j])
else: else:
outMerge[i, j] = dummy outMerge[i, j] = dummy
return outMerge.T, edges0, edges1, outData.T, outCount.T return outMerge.T, edges0, edges1, outData.T, outCount.T