本文整理匯總了Python中pyopencl.Program方法的典型用法代碼示例。如果您正苦於以下問題:Python pyopencl.Program方法的具體用法?Python pyopencl.Program怎麽用?Python pyopencl.Program使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類pyopencl
的用法示例。
在下文中一共展示了pyopencl.Program方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: compile
# 需要導入模塊: import pyopencl [as 別名]
# 或者: from pyopencl import Program [as 別名]
def compile(self, kernel_instance):
"""call the OpenCL compiler to compile the kernel, return the device function
:param kernel_name: The name of the kernel to be compiled, used to lookup the
function after compilation.
:type kernel_name: string
:param kernel_string: The OpenCL kernel code that contains the function `kernel_name`
:type kernel_string: string
:returns: An OpenCL kernel that can be called directly.
:rtype: pyopencl.Kernel
"""
prg = cl.Program(self.ctx, kernel_instance.kernel_string).build(options=self.compiler_options)
func = getattr(prg, kernel_instance.name)
return func
示例2: compile
# 需要導入模塊: import pyopencl [as 別名]
# 或者: from pyopencl import Program [as 別名]
def compile(self,type):
fname = ""
self.type=type
if (self.type == 'sha1'):
fname = os.path.join("Library","sha1.cl")
elif (self.type == 'sha256'):
fname = os.path.join("Library","sha256.cl")
elif (self.type == 'pbkdf2_sha1'):
fname = os.path.join("Library","pbkdf2_sha1.cl")
elif (self.type == 'pbkdf2_sha256'):
fname = os.path.join("Library","pbkdf2_sha256.cl")
else:
print('Type: ' + self.type + ' not supported!')
exit(0)
with open(fname, "r") as rf:
src = rf.read()
# Kernel function instantiation
self.prg = cl.Program(self.ctx, src).build()
示例3: __create_program
# 需要導入模塊: import pyopencl [as 別名]
# 或者: from pyopencl import Program [as 別名]
def __create_program(self):
self.__include_code = self.sas.kernelize()
codes = self.__include_code + '\n'
# codes = self.__args_codes + '\n' +\
# self.__populate_codes + '\n' +\
# self.__evaluate_code + '\n' +\
# self.__include_code + '\n' +\
# self.__fitness_kernel_str
f = open('ocl_sa.cl', 'r')
fstr = ''.join(f.readlines())
f.close()
if self.__debug_mode:
fdbg = open('final.cl', 'w')
fdbg.write(codes + fstr)
fdbg.close()
self.__prg = cl.Program(self.__ctx, codes + fstr).build(self.__include_path);
示例4: __create_program
# 需要導入模塊: import pyopencl [as 別名]
# 或者: from pyopencl import Program [as 別名]
def __create_program(self):
codes = self.__args_codes + '\n' +\
self.__populate_codes + '\n' +\
self.__evaluate_code + '\n' +\
self.__include_code + '\n' +\
self.__fitness_kernel_str
kernel_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'kernel')
f = open(os.path.join(kernel_path, 'ocl_ga.cl'), 'r')
fstr = ''.join(f.readlines())
f.close()
if self.__debug_mode:
fdbg = open('final.cl', 'w')
fdbg.write(codes + fstr)
fdbg.close()
self.__prg = cl.Program(self.__ctx, codes + fstr).build(self.__include_path);
示例5: _compile_kernels
# 需要導入模塊: import pyopencl [as 別名]
# 或者: from pyopencl import Program [as 別名]
def _compile_kernels(self):
# TODO: global cache, so every instantiation doesn't necessitate a
# full compile
self.compiled_kernels = {}
for domain in self.kernels.keys():
self.compiled_kernels[domain] = {}
for op_type in self.kernels[domain]:
kernel = self.kernels[domain][op_type]
if domain is backend.OpenCLMemory and isinstance(kernel, str):
prg = cl.Program(self.context.sub_context, kernel)
prg = prg.build()
try:
built_kernel = prg.all_kernels()[0]
except IndexError:
log.warning("Failed to build kernel for domain %s and operand types %s" % (domain, op_type))
built_kernel = noop
self.compiled_kernels[domain][op_type] = built_kernel
else:
self.compiled_kernels[domain][op_type] = kernel
示例6: change_params
# 需要導入模塊: import pyopencl [as 別名]
# 或者: from pyopencl import Program [as 別名]
def change_params(self, cl_platform_index=None, cl_device_index=None, **kargs):
BasePeakDetector.change_params(self, **kargs)
OpenCL_Helper.initialize_opencl(self, cl_platform_index=cl_platform_index, cl_device_index=cl_device_index)
if self.chunksize > self.max_wg_size:
n = int(np.ceil(self.chunksize / self.max_wg_size))
self.global_size = (self.max_wg_size * n, )
self.local_size = (self.max_wg_size,)
else:
self.global_size = (self.chunksize, )
self.local_size = (self.chunksize, )
chunksize2=self.chunksize+2*self.n_span
self.sum_rectified = np.zeros((self.chunksize), dtype=self.dtype)
self.peak_mask = np.zeros((self.chunksize), dtype='uint8')
ring_sum = np.zeros((chunksize2), dtype=self.dtype)
#GPU buffers
self.sigs_cl = pyopencl.Buffer(self.ctx, mf.READ_WRITE, size=self.nb_channel*self.chunksize*self.dtype.itemsize)
self.ring_sum_cl = pyopencl.Buffer(self.ctx, mf.READ_WRITE| mf.COPY_HOST_PTR, hostbuf=ring_sum)
self.peak_mask_cl = pyopencl.Buffer(self.ctx, mf.READ_WRITE | mf.COPY_HOST_PTR, hostbuf=self.peak_mask)
kernel = self.kernel%dict(chunksize=self.chunksize, nb_channel=self.nb_channel, n_span=self.n_span,
relative_threshold=self.relative_threshold, peak_sign={'+':1, '-':-1}[self.peak_sign])
prg = pyopencl.Program(self.ctx, kernel)
self.opencl_prg = prg.build(options='-cl-mad-enable')
self.kern_detect_peaks = getattr(self.opencl_prg, 'detect_peaks')
示例7: _get_kernels
# 需要導入模塊: import pyopencl [as 別名]
# 或者: from pyopencl import Program [as 別名]
def _get_kernels(self):
"""Return KNN and range search OpenCL kernels."""
kernel_source = open(self.kernel_location).read()
program = cl.Program(self.context, kernel_source).build()
kNN_kernel = program.kernelKNNshared
kNN_kernel.set_scalar_arg_dtypes([None, None, None, np.int32,
np.int32, np.int32, np.int32,
np.int32, None])
RS_kernel = program.kernelBFRSAllshared
RS_kernel.set_scalar_arg_dtypes([None, None, None, None,
np.int32, np.int32, np.int32,
np.int32, None])
return (kNN_kernel, RS_kernel)
示例8: kernel_info
# 需要導入模塊: import pyopencl [as 別名]
# 或者: from pyopencl import Program [as 別名]
def kernel_info(self, arg_to_dtype_set=frozenset(), all_kwargs=None):
kernel = self.get_typed_and_scheduled_kernel(arg_to_dtype_set)
from loopy.codegen import generate_code_v2
from loopy.target.execution import get_highlighted_code
codegen_result = generate_code_v2(kernel)
dev_code = codegen_result.device_code()
if self.kernel.options.write_cl:
output = dev_code
if self.kernel.options.highlight_cl:
output = get_highlighted_code(output)
if self.kernel.options.write_cl is True:
print(output)
else:
with open(self.kernel.options.write_cl, "w") as outf:
outf.write(output)
if self.kernel.options.edit_cl:
from pytools import invoke_editor
dev_code = invoke_editor(dev_code, "code.cl")
import pyopencl as cl
cl_program = (
cl.Program(self.context, dev_code)
.build(options=kernel.options.cl_build_options))
cl_kernels = _Kernels()
for dp in codegen_result.device_programs:
setattr(cl_kernels, dp.name, getattr(cl_program, dp.name))
return _KernelInfo(
kernel=kernel,
cl_kernels=cl_kernels,
implemented_data_info=codegen_result.implemented_data_info,
invoker=self.get_invoker(kernel, codegen_result))
示例9: test_gpu_vector_sum
# 需要導入模塊: import pyopencl [as 別名]
# 或者: from pyopencl import Program [as 別名]
def test_gpu_vector_sum(a, b):
#define the PyOpenCL Context
platform = cl.get_platforms()[0]
device = platform.get_devices()[0]
context = cl.Context([device])
queue = cl.CommandQueue(context, \
properties=cl.command_queue_properties.PROFILING_ENABLE)
#prepare the data structure
a_buffer = cl.Buffer\
(context, cl.mem_flags.READ_ONLY | cl.mem_flags.COPY_HOST_PTR, hostbuf=a)
b_buffer = cl.Buffer\
(context, cl.mem_flags.READ_ONLY | cl.mem_flags.COPY_HOST_PTR, hostbuf=b)
c_buffer = cl.Buffer\
(context, cl.mem_flags.WRITE_ONLY, b.nbytes)
program = cl.Program(context, """
__kernel void sum(__global const float *a, __global const float *b, __global float *c)
{
int i = get_global_id(0);
int j;
for(j = 0; j < 10000; j++)
{
c[i] = a[i] + b[i];
}
}""").build()
#start the gpu test
gpu_start_time = time()
event = program.sum(queue, a.shape, None, a_buffer, b_buffer, c_buffer)
event.wait()
elapsed = 1e-9*(event.profile.end - event.profile.start)
print("GPU Kernel evaluation Time: {0} s".format(elapsed))
c_gpu = np.empty_like(a)
cl._enqueue_read_buffer(queue, c_buffer, c_gpu).wait()
gpu_end_time = time()
print("GPU Time: {0} s".format(gpu_end_time - gpu_start_time))
return c_gpu
#start the test
開發者ID:PacktPublishing,項目名稱:Python-Parallel-Programming-Cookbook-Second-Edition,代碼行數:39,代碼來源:testApplicationPyopencl.py
示例10: build_program
# 需要導入模塊: import pyopencl [as 別名]
# 或者: from pyopencl import Program [as 別名]
def build_program(ctx, filename):
prog = None
try:
f = open(filename, 'r')
fstr = ''.join(f.readlines())
f.close()
# -Werror : Make all warnings into errors.
# https://www.khronos.org/registry/OpenCL/sdk/2.0/docs/man/xhtml/clBuildProgram.html
prog = cl.Program(ctx, fstr).build(options=['-Werror'], cache_dir=None);
except:
import traceback
traceback.print_exc()
return prog
示例11: __create_program
# 需要導入模塊: import pyopencl [as 別名]
# 或者: from pyopencl import Program [as 別名]
def __create_program(self):
f = open('ant_tsp.cl', 'r');
fstr = ''.join(f.readlines())
f.close()
ocl_kernel_path = os.path.join(os.path.dirname(os.path.abspath('../../' + __file__)), 'kernel').replace(' ', '\\ ')
options = [
'-D', 'ANT_COUNT={}'.format(self.__ants),
'-D', 'NODE_COUNT={}'.format(self.__node_count),
'-D', 'ALPHA={}'.format(self.__alpha),
'-D', 'BETA={}'.format(self.__beta),
'-D', 'EVAPORATION={}'.format(self.__evaporation),
'-D', 'Q={}'.format(self.__q),
'-I', ocl_kernel_path
]
self.__prg = cl.Program(self.__ctx, fstr).build(options);
示例12: tile_cl
# 需要導入模塊: import pyopencl [as 別名]
# 或者: from pyopencl import Program [as 別名]
def tile_cl(queue, a, shape, dim=4):
"""Horizontally and vertically tile a GPUArray.
Parameters
----------
queue
PyOpenCL queue.
a : gpuarray
GPUArray to tile.
shape : tuple
Number of vertical and horizontal tiles.
Returns
-------
gpuarray
Tiled GPUArray.
"""
m, n = a.shape
repy, repx = shape
b = cl_array.empty(queue, (m * repy, n * repx), dtype=float32)
kernel = cl.Program(queue.context, """
__kernel void tile_cl(__global float *a, __global float *b, unsigned m, unsigned n, unsigned repx, unsigned repy)
{
int idx = get_global_id(0);
int idy = get_global_id(1);
int id = idy * (n * repx) + idx;
b[id] = a[(idy % m) * n + (idx % n)];
}
""").build()
kernel.tile_cl(queue, (n * repx, m * repy), None, a.data, b.data, uint32(m), uint32(n), uint32(repx), uint32(repy))
return b
示例13: __getitem__
# 需要導入模塊: import pyopencl [as 別名]
# 或者: from pyopencl import Program [as 別名]
def __getitem__(self, index):
if self.vcl_sub_context.has_program(index):
program = self.vcl_sub_context.get_program_from_string(index)
return get_pyopencl_object(program)
else:
raise IndexError("Program %s not in context" % index)
示例14: __setitem__
# 需要導入模塊: import pyopencl [as 別名]
# 或者: from pyopencl import Program [as 別名]
def __setitem__(self, index, program):
if self.vcl_sub_context.has_program(index):
log.warn("Program %s already in context, so overwriting")
del self[index]
self.vcl_sub_context.add_program(program.int_ptr, index)
示例15: __delitem__
# 需要導入模塊: import pyopencl [as 別名]
# 或者: from pyopencl import Program [as 別名]
def __delitem__(self, index):
if self.vcl_sub_context.has_program(index):
self.vcl_sub_context.delete_program(index)
else:
raise IndexError("Program %s not in context" % index)