本文整理汇总了Python中gputools.OCLArray类的典型用法代码示例。如果您正苦于以下问题:Python OCLArray类的具体用法?Python OCLArray怎么用?Python OCLArray使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了OCLArray类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _convolve_sep2_numpy
def _convolve_sep2_numpy(data,hx,hy):
hx_g = OCLArray.from_array(hx.astype(np.float32))
hy_g = OCLArray.from_array(hy.astype(np.float32))
data_g = OCLArray.from_array(data.astype(np.float32))
return _convolve_sep2_gpu(data_g,hx_g,hy_g).get()
示例2: _deconv_rl_gpu_conv
def _deconv_rl_gpu_conv(data_g, h_g, Niter = 10):
"""
using convolve
"""
#set up some gpu buffers
u_g = OCLArray.empty(data_g.shape,np.float32)
u_g.copy_buffer(data_g)
tmp_g = OCLArray.empty(data_g.shape,np.float32)
tmp2_g = OCLArray.empty(data_g.shape,np.float32)
#fix this
hflip_g = OCLArray.from_array((h_g.get()[::-1,::-1]).copy())
for i in range(Niter):
convolve(u_g, h_g,
res_g = tmp_g)
_divide_inplace(data_g,tmp_g)
# return data_g, tmp_g
convolve(tmp_g, hflip_g,
res_g = tmp2_g)
_multiply_inplace(u_g,tmp2_g)
return u_g
示例3: gpu_kuwahara
def gpu_kuwahara(data, N=5):
"""Function to convolve an imgage with the Kuwahara filter on GPU."""
# create numpy arrays
if (N%2==0):
raise ValueError("Data has to be a (2n+1)x(2n+1) array.")
data_g = OCLArray.from_array(data.astype(float32))
res_g = OCLArray.empty((data.shape[0],data.shape[1]),float32)
prog = OCLProgram("./OpenCL/gpu_kernels/gpu_kuwahara.cl")
# start kernel on gput
prog.run_kernel("kuwahara", # the name of the kernel in the cl file
data_g.shape[::-1], # global size, the number of threads e.g. (128,128,)
None, # local size, just leave it to None
data_g.data,res_g.data,
int32(N))
#
return res_g.get()
示例4: _deconv_rl_np
def _deconv_rl_np(data, h, Niter = 10, ):
"""
"""
d_g = OCLArray.from_array(data.astype(np.float32, copy = False))
h_g = OCLArray.from_array(h.astype(np.float32, copy = False))
res_g = _deconv_rl_gpu_conv(d_g,h_g,Niter)
return res_g.get()
示例5: _fft_convolve_numpy
def _fft_convolve_numpy(data, h, plan = None,
kernel_is_fft = False,
kernel_is_fftshifted = False):
""" convolving via opencl fft for numpy arrays
data and h must have the same size
"""
dev = get_device()
if data.shape != h.shape:
raise ValueError("data and kernel must have same size! %s vs %s "%(str(data.shape),str(h.shape)))
data_g = OCLArray.from_array(data.astype(np.complex64))
if not kernel_is_fftshifted:
h = np.fft.fftshift(h)
h_g = OCLArray.from_array(h.astype(np.complex64))
res_g = OCLArray.empty_like(data_g)
_fft_convolve_gpu(data_g,h_g,res_g = res_g,
plan = plan,
kernel_is_fft = kernel_is_fft)
res = abs(res_g.get())
del data_g
del h_g
del res_g
return res
示例6: setup
def setup(self, size, units, lam=0.5, n0=1.0, use_fresnel_approx=False):
"""
sets up the internal variables e.g. propagators etc...
:param size: the size of the geometry in pixels (Nx,Ny,Nz)
:param units: the phyiscal units of each voxel in microns (dx,dy,dz)
:param lam: the wavelength of light in microns
:param n0: the refractive index of the surrounding media
:param use_fresnel_approx: if True, uses fresnel approximation for propagator
"""
Bpm3d_Base.setup(self, size, units, lam=lam, n0=n0, use_fresnel_approx=use_fresnel_approx)
# setting up the gpu buffers and kernels
self.program = OCLProgram(absPath("kernels/bpm_3d_kernels.cl"))
Nx, Ny = self.size[:2]
plan = fft_plan(())
self._H_g = OCLArray.from_array(self._H.astype(np.complex64))
self.scatter_weights_g = OCLArray.from_array(self.scatter_weights.astype(np.float32))
self.gfactor_weights_g = OCLArray.from_array(self.gfactor_weights.astype(np.float32))
self.scatter_cross_sec_g = OCLArray.zeros(Nz, "float32")
self.gfactor_g = OCLArray.zeros(Nz, "float32")
self.reduce_kernel = OCLReductionKernel(
np.float32,
neutral="0",
reduce_expr="a+b",
map_expr="weights[i]*cfloat_abs(field[i]-(i==0)*plain)*cfloat_abs(field[i]-(i==0)*plain)",
arguments="__global cfloat_t *field, __global float * weights,cfloat_t plain",
)
示例7: test_3d
def test_3d():
from time import time
Niter = 10
data = np.zeros((128,)*3,np.float32)
data[30,30,30] = 1.
hx = 1./5*np.ones(5)
hy = 1./13*np.ones(13)
hz = 1./13*np.ones(11)
t = time()
for _ in range(Niter):
out = convolve_sep3(data,hx,hy, hz)
print "time: %.3f ms"%(1000.*(time()-t)/Niter)
data_g = OCLArray.from_array(data.astype(np.float32))
hx_g = OCLArray.from_array(hx.astype(np.float32))
hy_g = OCLArray.from_array(hy.astype(np.float32))
hz_g = OCLArray.from_array(hz.astype(np.float32))
t = time()
for _ in range(Niter):
out_g = convolve_sep3(data_g,hx_g,hy_g, hz_g)
out_g.get();
print "time: %.3f ms"%(1000.*(time()-t)/Niter)
return out, out_g.get()
示例8: create_dn_buffer
def create_dn_buffer(size, units,points,
dn_inner = .0, rad_inner = 0,
dn_outer = .1, rad_outer = .4):
Nx, Ny, Nz = size
dx, dy, dz = units
program = OCLProgram(absPath("kernels/bpm_3d_spheres.cl"))
dn_g = OCLArray.empty((Nz,Ny,Nx),dtype=np.float32)
# sort by z
ps = np.array(points)
ps = ps[np.argsort(ps[:,2]),:]
Np = ps.shape[0]
pointsBuf = OCLArray.from_array(ps.flatten().astype(np.float32))
program.run_kernel("fill_dn",(Nx,Ny,Nz),None,dn_g.data,
pointsBuf.data,np.int32(Np),
np.float32(dx),np.float32(dy),np.float32(dz),
np.float32(dn_inner),np.float32(rad_inner),
np.float32(dn_outer),np.float32(rad_outer))
return dn_g
示例9: test_bessel
def test_bessel(n,x):
x_g = OCLArray.from_array(x.astype(float32))
res_g = OCLArray.empty_like(x.astype(float32))
p = OCLProgram(absPath("kernels/bessel.cl"))
p.run_kernel("bessel_fill",x_g.shape,None,
x_g.data,res_g.data,int32(n))
return res_g.get()
示例10: _convolve_np
def _convolve_np(data, h):
"""
numpy variant
"""
data_g = OCLArray.from_array(data.astype(np.float32, copy = False))
h_g = OCLArray.from_array(h.astype(np.float32, copy = False))
return _convolve_buf(data_g, h_g).get()
示例11: fftshift
def fftshift(arr_obj, axes = None, res_g = None, return_buffer = False):
"""
gpu version of fftshift for numpy arrays or OCLArrays
Parameters
----------
arr_obj: numpy array or OCLArray (float32/complex64)
the array to be fftshifted
axes: list or None
the axes over which to shift (like np.fft.fftshift)
if None, all axes are taken
res_g:
if given, fills it with the result (has to be same shape and dtype as arr_obj)
else internally creates a new one
Returns
-------
if return_buffer, returns the result as (well :) OCLArray
else returns the result as numpy array
"""
if axes is None:
axes = range(arr_obj.ndim)
if isinstance(arr_obj, OCLArray):
if not arr_obj.dtype.type in DTYPE_KERNEL_NAMES.keys():
raise NotImplementedError("only works for float32 or complex64")
elif isinstance(arr_obj, np.ndarray):
if np.iscomplexobj(arr_obj):
arr_obj = OCLArray.from_array(arr_obj.astype(np.complex64,copy = False))
else:
arr_obj = OCLArray.from_array(arr_obj.astype(np.float32,copy = False))
else:
raise ValueError("unknown type (%s)"%(type(arr_obj)))
if not np.all([arr_obj.shape[a]%2==0 for a in axes]):
raise NotImplementedError("only works on axes of even dimensions")
if res_g is None:
res_g = OCLArray.empty_like(arr_obj)
# iterate over all axes
# FIXME: this is still rather inefficient
in_g = arr_obj
for ax in axes:
_fftshift_single(in_g, res_g, ax)
in_g = res_g
if return_buffer:
return res_g
else:
return res_g.get()
示例12: nlm3
def nlm3(data,sigma, size_filter = 2, size_search = 3):
"""for noise level of sigma_0, choose sigma = 1.5*sigma_0
"""
prog = OCLProgram(abspath("kernels/nlm3.cl"),
build_options="-D FS=%i -D BS=%i"%(size_filter,size_search))
data = data.astype(np.float32, copy = False)
img = OCLImage.from_array(data)
distImg = OCLImage.empty_like(data)
distImg = OCLImage.empty_like(data)
tmpImg = OCLImage.empty_like(data)
tmpImg2 = OCLImage.empty_like(data)
accBuf = OCLArray.zeros(data.shape,np.float32)
weightBuf = OCLArray.zeros(data.shape,np.float32)
for dx in range(size_search+1):
for dy in range(-size_search,size_search+1):
for dz in range(-size_search,size_search+1):
prog.run_kernel("dist",img.shape,None,
img,tmpImg,np.int32(dx),np.int32(dy),np.int32(dz))
prog.run_kernel("convolve",img.shape,None,
tmpImg,tmpImg2,np.int32(1))
prog.run_kernel("convolve",img.shape,None,
tmpImg2,tmpImg,np.int32(2))
prog.run_kernel("convolve",img.shape,None,
tmpImg,distImg,np.int32(4))
prog.run_kernel("computePlus",img.shape,None,
img,distImg,accBuf.data,weightBuf.data,
np.int32(img.shape[0]),
np.int32(img.shape[1]),
np.int32(img.shape[2]),
np.int32(dx),np.int32(dy),np.int32(dz),
np.float32(sigma))
if any([dx,dy,dz]):
prog.run_kernel("computeMinus",img.shape,None,
img,distImg,accBuf.data,weightBuf.data,
np.int32(img.shape[0]),
np.int32(img.shape[1]),
np.int32(img.shape[2]),
np.int32(dx),np.int32(dy),np.int32(dz),
np.float32(sigma))
acc = accBuf.get()
weights = weightBuf.get()
return acc/weights
示例13: _deconv_rl_np_fft
def _deconv_rl_np_fft(data, h, Niter = 10,
h_is_fftshifted = False):
""" deconvolves data with given psf (kernel) h
data and h have to be same shape
via lucy richardson deconvolution
"""
if data.shape != h.shape:
raise ValueError("data and h have to be same shape")
if not h_is_fftshifted:
h = np.fft.fftshift(h)
hflip = h[::-1,::-1]
#set up some gpu buffers
y_g = OCLArray.from_array(data.astype(np.complex64))
u_g = OCLArray.from_array(data.astype(np.complex64))
tmp_g = OCLArray.empty(data.shape,np.complex64)
hf_g = OCLArray.from_array(h.astype(np.complex64))
hflip_f_g = OCLArray.from_array(hflip.astype(np.complex64))
# hflipped_g = OCLArray.from_array(h.astype(np.complex64))
plan = fft_plan(data.shape)
#transform psf
fft(hf_g,inplace = True)
fft(hflip_f_g,inplace = True)
for i in range(Niter):
print i
fft_convolve(u_g, hf_g,
res_g = tmp_g,
kernel_is_fft = True)
_complex_divide_inplace(y_g,tmp_g)
fft_convolve(tmp_g,hflip_f_g,
inplace = True,
kernel_is_fft = True)
_complex_multiply_inplace(u_g,tmp_g)
return np.abs(u_g.get())
示例14: resample_buf
def resample_buf(data, new_shape):
"""resamples d"""
d1_g = OCLArray.from_array(data)
d2_g = OCLArray.empty(new_shape,data.dtype)
if data.dtype.type == np.float32:
im = OCLImage.empty(data.shape[::1],dtype = np.float32)
elif data.dtype.type == np.complex64:
im = OCLImage.empty(data.shape[::1],dtype = np.float32, num_channels=2)
im.copy_buffer(d1_g)
d2_g.copy_image_resampled(im)
return d2_g.get()
示例15: transfer
def transfer(data):
"""transfers data"""
d1_g = OCLArray.from_array(data)
d2_g = OCLArray.empty_like(data)
if data.dtype.type == np.float32:
im = OCLImage.empty(data.shape[::1],dtype = np.float32)
elif data.dtype.type == np.complex64:
im = OCLImage.empty(data.shape[::1],dtype = np.float32, num_channels=2)
im.copy_buffer(d1_g)
d2_g.copy_image(im)
return d2_g.get()