pyFAI/sandbox/profile_ocl_hist_pixelsplit.py

127 lines
3.7 KiB
Python
Raw Permalink Normal View History

2014-05-30 19:39:01 +08:00
# -*- coding: utf-8 -*-
"""
Created on Fri Mar 07 09:52:51 2014
@author: ashiotis
"""
2014-12-19 00:13:40 +08:00
from __future__ import absolute_import
from __future__ import print_function
import sys, numpy, time, os
2014-05-30 19:39:01 +08:00
2014-06-24 23:17:52 +08:00
import fabio
import pyopencl as cl
2014-05-30 19:39:01 +08:00
from pylab import *
from pyFAI.third_party import six
2014-12-19 00:13:40 +08:00
print("#"*50)
if __name__ == '__main__':
import pkgutil
__path__ = pkgutil.extend_path([os.path.dirname(__file__)], "pyFAI.test")
2018-01-10 23:56:07 +08:00
from pyFAI.test.utilstest import UtilsTest
2014-12-19 00:13:40 +08:00
2014-05-30 19:39:01 +08:00
pyFAI = sys.modules["pyFAI"]
from pyFAI import splitPixelFullLUT
from pyFAI import splitPixelFull
2014-05-30 19:39:01 +08:00
from pyFAI import ocl_hist_pixelsplit
2014-12-19 00:13:40 +08:00
# from pyFAI import splitBBoxLUT
# from pyFAI import splitBBoxCSR
2014-05-30 19:39:01 +08:00
2014-12-19 00:13:40 +08:00
os.chdir("testimages")
ai = pyFAI.load("halfccd.poni")
data = fabio.open("halfccd.edf").data
2014-05-30 19:39:01 +08:00
2014-06-24 23:17:52 +08:00
workgroup_size = 256
bins = 1000
2017-05-11 21:48:33 +08:00
pos_in = ai.array_from_unit(data.shape, "corner", unit="2th_deg", scale=False)
2014-05-30 23:01:13 +08:00
2014-12-19 00:13:40 +08:00
pos = pos_in.reshape(pos_in.size / 8, 4, 2)
2014-05-30 19:39:01 +08:00
2014-06-24 23:17:52 +08:00
pos_size = pos.size
size = data.size
ctx = cl.create_some_context()
queue = cl.CommandQueue(ctx)
mf = cl.mem_flags
2014-12-19 00:13:40 +08:00
d_pos = cl.array.to_device(queue, pos)
d_preresult = cl.Buffer(ctx, mf.READ_WRITE, 4 * 4 * workgroup_size)
d_minmax = cl.Buffer(ctx, mf.READ_WRITE, 4 * 4)
2014-06-24 23:17:52 +08:00
2014-12-19 00:13:40 +08:00
with open("../../openCL/ocl_hist_pixelsplit.cl", "r") as kernelFile:
2014-06-24 23:17:52 +08:00
kernel_src = kernelFile.read()
compile_options = "-D BINS=%i -D NIMAGE=%i -D WORKGROUP_SIZE=%i -D EPS=%f" % \
(bins, size, workgroup_size, numpy.finfo(numpy.float32).eps)
program = cl.Program(ctx, kernel_src).build(options=compile_options)
2014-12-19 00:13:40 +08:00
program.reduce1(queue, (workgroup_size * workgroup_size,), (workgroup_size,), d_pos.data, numpy.uint32(pos_size), d_preresult)
2014-06-24 23:17:52 +08:00
program.reduce2(queue, (workgroup_size,), (workgroup_size,), d_preresult, d_minmax)
2014-12-19 00:13:40 +08:00
result = numpy.ndarray(4, dtype=numpy.float32)
2014-06-24 23:17:52 +08:00
2014-12-19 00:13:40 +08:00
cl.enqueue_copy(queue, result, d_minmax)
2014-06-24 23:17:52 +08:00
min0 = pos[:, :, 0].min()
max0 = pos[:, :, 0].max()
min1 = pos[:, :, 1].min()
max1 = pos[:, :, 1].max()
2014-12-19 00:13:40 +08:00
minmax = (min0, max0, min1, max1)
2014-06-24 23:17:52 +08:00
2014-12-19 00:13:40 +08:00
print(minmax)
print(result)
2014-06-24 23:17:52 +08:00
2014-12-19 00:13:40 +08:00
d_outData = cl.Buffer(ctx, mf.READ_WRITE, 4 * bins)
d_outCount = cl.Buffer(ctx, mf.READ_WRITE, 4 * bins)
d_outMerge = cl.Buffer(ctx, mf.READ_WRITE, 4 * bins)
2014-06-24 23:17:52 +08:00
program.memset_out(queue, (1024,), (workgroup_size,), d_outData, d_outCount, d_outMerge)
2014-12-19 00:13:40 +08:00
outData = numpy.ndarray(bins, dtype=numpy.float32)
2014-06-24 23:17:52 +08:00
outCount = numpy.ndarray(bins, dtype=numpy.float32)
outMerge = numpy.ndarray(bins, dtype=numpy.float32)
2014-12-19 00:13:40 +08:00
cl.enqueue_copy(queue, outData, d_outData)
cl.enqueue_copy(queue, outCount, d_outCount)
cl.enqueue_copy(queue, outMerge, d_outMerge)
2014-06-24 23:17:52 +08:00
global_size = (data.size + workgroup_size - 1) & ~(workgroup_size - 1),
d_image = cl.array.to_device(queue, data)
2014-12-19 00:13:40 +08:00
d_image_float = cl.Buffer(ctx, mf.READ_WRITE, 4 * size)
2014-06-24 23:17:52 +08:00
2014-12-19 00:13:40 +08:00
# program.s32_to_float(queue, global_size, (workgroup_size,), d_image.data, d_image_float) # Pilatus1M
program.u16_to_float(queue, global_size, (workgroup_size,), d_image.data, d_image_float) # halfccd
2014-06-24 23:17:52 +08:00
2014-12-19 00:13:40 +08:00
program.integrate1(queue, global_size, (workgroup_size,), d_pos.data, d_image_float, d_minmax, numpy.int32(data.size), d_outData, d_outCount)
2014-06-24 23:17:52 +08:00
2014-12-19 00:13:40 +08:00
cl.enqueue_copy(queue, outData, d_outData)
cl.enqueue_copy(queue, outCount, d_outCount)
cl.enqueue_copy(queue, outMerge, d_outMerge)
2014-06-24 23:17:52 +08:00
program.integrate2(queue, (1024,), (workgroup_size,), d_outData, d_outCount, d_outMerge)
2014-12-19 00:13:40 +08:00
cl.enqueue_copy(queue, outData, d_outData)
cl.enqueue_copy(queue, outCount, d_outCount)
cl.enqueue_copy(queue, outMerge, d_outMerge)
2014-06-24 23:17:52 +08:00
ref = ai.xrpd_LUT(data, bins, correctSolidAngle=False)
test = splitPixelFull.fullSplit1D(pos, data, bins)
2014-05-30 19:39:01 +08:00
2014-12-19 00:13:40 +08:00
# assert(numpy.allclose(ref,outMerge))
2014-05-30 19:39:01 +08:00
2014-12-19 00:13:40 +08:00
# plot(outMerge, label="ocl_hist")
plot(ref[0], test[1], label="splitPixelFull")
plot(ref[0], ref[1], label="ref")
2014-12-19 00:13:40 +08:00
# plot(abs(ref-outMerge)/outMerge, label="ocl_csr_fullsplit")
2014-05-30 19:39:01 +08:00
legend()
show()
six.moves.input()