本文整理汇总了Python中gputools.OCLArray.from_array方法的典型用法代码示例。如果您正苦于以下问题:Python OCLArray.from_array方法的具体用法?Python OCLArray.from_array怎么用?Python OCLArray.from_array使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类gputools.OCLArray
的用法示例。
在下文中一共展示了OCLArray.from_array方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _deconv_rl_np
# 需要导入模块: from gputools import OCLArray [as 别名]
# 或者: from gputools.OCLArray import from_array [as 别名]
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()
示例2: test_3d
# 需要导入模块: from gputools import OCLArray [as 别名]
# 或者: from gputools.OCLArray import from_array [as 别名]
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()
示例3: _convolve_sep2_numpy
# 需要导入模块: from gputools import OCLArray [as 别名]
# 或者: from gputools.OCLArray import from_array [as 别名]
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()
示例4: _fft_convolve_numpy
# 需要导入模块: from gputools import OCLArray [as 别名]
# 或者: from gputools.OCLArray import from_array [as 别名]
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
示例5: setup
# 需要导入模块: from gputools import OCLArray [as 别名]
# 或者: from gputools.OCLArray import from_array [as 别名]
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",
)
示例6: _convolve_np
# 需要导入模块: from gputools import OCLArray [as 别名]
# 或者: from gputools.OCLArray import from_array [as 别名]
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()
示例7: fftshift
# 需要导入模块: from gputools import OCLArray [as 别名]
# 或者: from gputools.OCLArray import from_array [as 别名]
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()
示例8: _deconv_rl_np_fft
# 需要导入模块: from gputools import OCLArray [as 别名]
# 或者: from gputools.OCLArray import from_array [as 别名]
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())
示例9: _deconv_rl_gpu_conv
# 需要导入模块: from gputools import OCLArray [as 别名]
# 或者: from gputools.OCLArray import from_array [as 别名]
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
示例10: gpu_kuwahara
# 需要导入模块: from gputools import OCLArray [as 别名]
# 或者: from gputools.OCLArray import from_array [as 别名]
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()
示例11: test_parseval
# 需要导入模块: from gputools import OCLArray [as 别名]
# 或者: from gputools.OCLArray import from_array [as 别名]
def test_parseval():
from time import time
Nx = 512
Nz = 10
d = np.random.uniform(-1,1,(Nx,Nx)).astype(np.complex64)
d_g = OCLArray.from_array(d.astype(np.complex64))
s1, s2 = [],[]
t = time()
for i in range(Nz):
print i
# myfunc(d_g)
# fft(d_g, inplace=True, fast_math=False)
# fft(d_g, inverse = True,inplace=True,fast_math=False)
fft(d_g, inplace=True)
# fft(d_g, inverse = True,inplace=True)
s1.append(np.sum(np.abs(d_g.get())**2))
print time()-t
for i in range(Nz):
print i
d = np.fft.fftn(d).astype(np.complex64)
d = np.fft.ifftn(d).astype(np.complex64)
s2.append(np.sum(np.abs(d)**2))
return s1, s2
示例12: create_dn_buffer
# 需要导入模块: from gputools import OCLArray [as 别名]
# 或者: from gputools.OCLArray import from_array [as 别名]
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
示例13: test_2d
# 需要导入模块: from gputools import OCLArray [as 别名]
# 或者: from gputools.OCLArray import from_array [as 别名]
def test_2d():
import time
data = np.zeros((100,)*2,np.float32)
data[50,50] = 1.
hx = 1./5*np.ones(5)
hy = 1./13*np.ones(13)
out = convolve_sep2(data,hx,hy)
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))
out_g = convolve_sep2(data_g,hx_g,hy_g)
return out, out_g.get()
示例14: test_bessel
# 需要导入模块: from gputools import OCLArray [as 别名]
# 或者: from gputools.OCLArray import from_array [as 别名]
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()
示例15: focus_field_debye_at
# 需要导入模块: from gputools import OCLArray [as 别名]
# 或者: from gputools.OCLArray import from_array [as 别名]
def focus_field_debye_at(x,y,z,lam, NA, n0 = 1., n_integration_steps = 200):
""" the same as focus_field_debye but for the coordinates given in x, y, z (arrays of same shape)
slower than focus_field_debye as it doesnt assume the coordinates to be on a grid
"""
print absPath("kernels/psf_debye.cl")
p = OCLProgram(absPath("kernels/psf_debye.cl"),
build_options = str("-I %s -D INT_STEPS=%s"%(absPath("."),n_integration_steps)))
if np.isscalar(NA):
NA = [0.,NA]
alphas = np.arcsin(np.array(NA)/n0)
assert len(alphas)%2 ==0
assert x.shape == y.shape == z.shape
dshape =x.shape
N = np.prod(dshape)
x_g = OCLArray.from_array(x.flatten().astype(np.float32))
y_g = OCLArray.from_array(y.flatten().astype(np.float32))
z_g = OCLArray.from_array(z.flatten().astype(np.float32))
u_g = OCLArray.empty(N,np.float32)
ex_g = OCLArray.empty(N,np.complex64)
ey_g = OCLArray.empty(N,np.complex64)
ez_g = OCLArray.empty(N,np.complex64)
alpha_g = OCLArray.from_array(alphas.astype(np.float32))
p.run_kernel("debye_wolf_at",(N,),None,
x_g.data,y_g.data,z_g.data,
ex_g.data,ey_g.data,ez_g.data, u_g.data,
np.float32(1.),np.float32(0.),
np.float32(lam/n0),
alpha_g.data, np.int32(len(alphas)))
u = u_g.get().reshape(dshape)
ex = ex_g.get().reshape(dshape)
ey = ey_g.get().reshape(dshape)
ez = ez_g.get().reshape(dshape)
return u, ex, ey, ez