当前位置: 首页>>代码示例>>Python>>正文


Python pyopencl.create_some_context方法代码示例

本文整理汇总了Python中pyopencl.create_some_context方法的典型用法代码示例。如果您正苦于以下问题:Python pyopencl.create_some_context方法的具体用法?Python pyopencl.create_some_context怎么用?Python pyopencl.create_some_context使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在pyopencl的用法示例。


在下文中一共展示了pyopencl.create_some_context方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: main

# 需要导入模块: import pyopencl [as 别名]
# 或者: from pyopencl import create_some_context [as 别名]
def main():
    fn = "matmul.floopy"
    with open(fn, "r") as inf:
        source = inf.read()

    dgemm, = lp.parse_transformed_fortran(source, filename=fn)

    ctx = cl.create_some_context()
    queue = cl.CommandQueue(ctx)

    n = 2048
    a = cl.array.empty(queue, (n, n), dtype=np.float64, order="F")
    b = cl.array.empty(queue, (n, n), dtype=np.float64, order="F")
    c = cl.array.zeros(queue, (n, n), dtype=np.float64, order="F")
    cl.clrandom.fill_rand(a)
    cl.clrandom.fill_rand(b)

    dgemm = lp.set_options(dgemm, write_code=True)

    dgemm(queue, a=a, b=b, alpha=1, c=c)

    c_ref = (a.get() @ b.get())
    assert la.norm(c_ref - c.get())/la.norm(c_ref) < 1e-10 
开发者ID:inducer,项目名称:loopy,代码行数:25,代码来源:matmul-driver.py

示例2: __init_cl

# 需要导入模块: import pyopencl [as 别名]
# 或者: from pyopencl import create_some_context [as 别名]
def __init_cl(self, cl_context, extra_include_path):
        # create OpenCL context, queue, and memory
        # NOTE: Please set PYOPENCL_CTX=N (N is the device number you want to use)
        #       at first if it's in external_process mode, otherwise a exception
        #       will be thrown, since it's not in interactive mode.
        # TODO: Select a reliable device during runtime by default.
        self.__ctx = cl_context if cl_context is not None else cl.create_some_context()
        self.__queue = cl.CommandQueue(self.__ctx)
        self.__include_path = []

        ocl_kernel_path = os.path.join(os.path.dirname(os.path.abspath('../../' + __file__)), 'kernel').replace(' ', '\\ ')
        paths = extra_include_path + [ocl_kernel_path]
        for path in paths:
            escapedPath = path.replace(' ', '^ ') if sys.platform.startswith('win')\
                                                  else path.replace(' ', '\\ ')
            # After looking into the source code of pyopencl/__init__.py
            # '-I' and folder path should be sepearetd. And ' should not included in string path.
            self.__include_path.append('-I')
            self.__include_path.append(os.path.join(os.getcwd(), escapedPath)) 
开发者ID:PyOCL,项目名称:OpenCLGA,代码行数:21,代码来源:ocl_sa.py

示例3: __init_cl

# 需要导入模块: import pyopencl [as 别名]
# 或者: from pyopencl import create_some_context [as 别名]
def __init_cl(self, cl_context, extra_include_path):
        # create OpenCL context, queue, and memory
        # NOTE: Please set PYOPENCL_CTX=N (N is the device number you want to use)
        #       at first if it's in external_process mode, otherwise a exception
        #       will be thrown, since it's not in interactive mode.
        # TODO: Select a reliable device during runtime by default.
        self.__ctx = cl_context if cl_context is not None else cl.create_some_context()
        self.__queue = cl.CommandQueue(self.__ctx)
        self.__include_path = []
        kernel_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'kernel')
        paths = extra_include_path + [kernel_path]
        for path in paths:
            escapedPath = path.replace(' ', '^ ') if sys.platform.startswith('win')\
                                                  else path.replace(' ', '\\ ')
            # After looking into the source code of pyopencl/__init__.py
            # '-I' and folder path should be sepearetd. And ' should not included in string path.
            self.__include_path.append('-I')
            self.__include_path.append(os.path.join(os.getcwd(), escapedPath)) 
开发者ID:PyOCL,项目名称:OpenCLGA,代码行数:20,代码来源:ocl_ga.py

示例4: initialize_opencl

# 需要导入模块: import pyopencl [as 别名]
# 或者: from pyopencl import create_some_context [as 别名]
def initialize_opencl(self, cl_platform_index=None, cl_device_index=None, ctx=None, queue=None):
        assert HAVE_PYOPENCL, 'PyOpenCL is not installed'

        global default_platform_index
        global default_device_index
        
        if ctx is None and queue is None:
            if cl_platform_index is None:
                if default_platform_index is not None and default_device_index is not None:
                    self.cl_platform_index = default_platform_index
                    self.cl_device_index = default_device_index
                    self.devices = [pyopencl.get_platforms()[self.cl_platform_index].get_devices()[self.cl_device_index]]
                    self.ctx = pyopencl.Context(self.devices)
                else:
                    self.ctx = pyopencl.create_some_context(interactive=False)
            else:
                self.cl_platform_index = cl_platform_index
                self.cl_device_index = cl_device_index
                self.devices = [pyopencl.get_platforms()[self.cl_platform_index].get_devices()[self.cl_device_index]]
                self.ctx = pyopencl.Context(self.devices)
            self.queue = pyopencl.CommandQueue(self.ctx)
        else:
            assert cl_platform_index is None and cl_device_index is None
            self.ctx = ctx
            self.queue = queue
        
        self.max_wg_size = self.ctx.devices[0].get_info(pyopencl.device_info.MAX_WORK_GROUP_SIZE) 
开发者ID:tridesclous,项目名称:tridesclous,代码行数:29,代码来源:cltools.py

示例5: __init_cl

# 需要导入模块: import pyopencl [as 别名]
# 或者: from pyopencl import create_some_context [as 别名]
def __init_cl(self, cl_context=None):
        self.__ctx = cl_context if cl_context is not None else cl.create_some_context()
        self.__queue = cl.CommandQueue(self.__ctx) 
开发者ID:PyOCL,项目名称:OpenCLGA,代码行数:5,代码来源:ant_tsp.py

示例6: create_context

# 需要导入模块: import pyopencl [as 别名]
# 或者: from pyopencl import create_some_context [as 别名]
def create_context():
    global context
    global program
    context = cl.create_some_context()
    program = cl.Program(context, kernel).build(options="-cl-no-signed-zeros -cl-mad-enable -cl-fast-relaxed-math") 
开发者ID:libtangle,项目名称:qcgpu,代码行数:7,代码来源:backend.py

示例7: test_preamble_with_separate_temporaries

# 需要导入模块: import pyopencl [as 别名]
# 或者: from pyopencl import create_some_context [as 别名]
def test_preamble_with_separate_temporaries(ctx_factory):
    # create a function mangler

    # and finally create a test
    n = 10
    # for each entry come up with a random number of data points
    num_data = np.asarray(np.random.randint(2, 10, size=n), dtype=np.int32)
    # turn into offsets
    offsets = np.asarray(np.hstack(([0], np.cumsum(num_data))), dtype=np.int32)
    # create lookup data
    lookup = np.empty(0)
    for i in num_data:
        lookup = np.hstack((lookup, np.arange(i)))
    lookup = np.asarray(lookup, dtype=np.int32)
    # and create data array
    data = np.random.rand(np.product(num_data))

    # make kernel
    kernel = lp.make_kernel('{[i]: 0 <= i < n}',
    """
    for i
        <>ind = indirect(offsets[i], offsets[i + 1], 1)
        out[i] = data[ind]
    end
    """,
    [lp.GlobalArg('out', shape=('n',)),
     lp.TemporaryVariable(
        'offsets', shape=(offsets.size,), initializer=offsets,
        address_space=lp.AddressSpace.GLOBAL,
        read_only=True),
     lp.GlobalArg('data', shape=(data.size,), dtype=np.float64)],
    )

    # fixt params, and add manglers / preamble
    from testlib import (
            SeparateTemporariesPreambleTestMangler,
            SeparateTemporariesPreambleTestPreambleGenerator,
            )
    func_info = dict(
            func_name='indirect',
            func_arg_dtypes=(np.int32, np.int32, np.int32),
            func_result_dtypes=(np.int32,),
            arr=lookup
            )

    kernel = lp.fix_parameters(kernel, **{'n': n})
    kernel = lp.register_preamble_generators(
            kernel, [SeparateTemporariesPreambleTestPreambleGenerator(**func_info)])
    kernel = lp.register_function_manglers(
            kernel, [SeparateTemporariesPreambleTestMangler(**func_info)])

    print(lp.generate_code(kernel)[0])
    # and call (functionality unimportant, more that it compiles)
    ctx = cl.create_some_context()
    queue = cl.CommandQueue(ctx)
    # check that it actually performs the lookup correctly
    assert np.allclose(kernel(
        queue, data=data.flatten('C'))[1][0], data[offsets[:-1] + 1]) 
开发者ID:inducer,项目名称:loopy,代码行数:60,代码来源:test_loopy.py

示例8: __init__

# 需要导入模块: import pyopencl [as 别名]
# 或者: from pyopencl import create_some_context [as 别名]
def __init__(self, step=1, patch_search_width=7, patch_data_width=7, past_frames=4, future_frames=4, search_width=21, input_dtype=np.float32):
        """
        step: nearest neighbors should be computed every step pixels
        patch_search_width: width of the square patches used to compute the matches
        patch_data_width: width of the square area to fetch at the neighbors positions.
            for example 1 means return only the value of the centers.
        past_frames: How many frames of the past should be used for the search.
        future_frames: How many frames of the future should be used for the search.
        search_width: width of the square region of the center pixels of all compared
            patches on one image. past_frames * future_frames * search_width patches
            are compared. the number of neighbors is 1 + past_frames + future_frames.
        input_dtype: type of the videos on which the search will occur.
        """
        num_neighbors = 1 + past_frames + future_frames
        ctx = cl.create_some_context()
        queue = cl.CommandQueue(ctx)
        f = open('video_patch_search.cl', 'r')
        fstr = "".join(f.readlines())
        build_options = '-cl-mad-enable -cl-unsafe-math-optimizations -cl-fast-relaxed-math -cl-no-signed-zeros'
        build_options += ' -DPATCH_AGGREGATION_STEP='  + ("%d" % step)
        build_options += ' -DPATCH_WIDTH='  + ("%d" % patch_search_width)
        build_options += ' -DNUM_NEIGHBORS=' + ("%d" % num_neighbors)
        build_options += ' -DWINDOW_SEARCH_WIDTH=' + ("%d" % search_width)
        build_options += ' -DWINDOW_SEARCH_FRAMES_PAST=' + ("%d" % past_frames)
        build_options += ' -DWINDOW_SEARCH_FRAMES_FUTURE=' + ("%d" % future_frames)
        build_options += ' ' + src_type_parameter[input_dtype] #video.dtype.type
        if search_width <= 256 and not(is_cpu(ctx)):
            wksize = 128
            build_options += ' -DUSE_CACHE -DWK_SIZE=' + ("%d" %  wksize)
        else:
            wksize = 64
            build_options += ' -DWK_SIZE=64'
        assert(wksize > patch_search_width)
        program = cl.Program(ctx, fstr).build(options=build_options)

        self.compute_nn = program.compute_nearest_neighbors_by_convolution
        self.compute_nn.set_scalar_arg_dtypes([None, None, np.int32, np.int32, np.int32, np.int32, np.int32, np.int32, np.int32, np.int32])

        self.ctx = ctx
        self.queue = queue
        self.step = step
        self.patch_search_width = patch_search_width
        self.patch_data_width = patch_data_width
        self.num_neighbors = num_neighbors
        self.past_frames = past_frames
        self.future_frames = future_frames
        self.input_dtype = input_dtype
        self.wksize = wksize 
开发者ID:axeldavy,项目名称:vnlnet,代码行数:50,代码来源:video_patch_search.py

示例9: run

# 需要导入模块: import pyopencl [as 别名]
# 或者: from pyopencl import create_some_context [as 别名]
def run(vector):
    ctx = cl.create_some_context()
    queue = cl.CommandQueue(ctx, properties=cl.command_queue_properties.PROFILING_ENABLE)
    f = open('casting_vector.c' if vector else 'casting.c', 'r')
    fstr = ''.join(f.readlines())
    f.close()

    data_size = 100;
    global_size = int(data_size / 4) if vector else data_size

    if vector:
        struct = 'typedef struct {\n'
        code = '  switch(id) {\n'
        codeTemp = '    case {0}:\n      tsR->data{0} = tsA->data{0} + tsB->data{0};\n    break;\n'
        for i in range(global_size):
            struct += '  float4 data' + str(i) + ';\n'
            code += codeTemp.format(i)
        struct += '} TypedStruct2;\n'
        code += '  }\n'
        fstr = fstr.replace('%code_generation%', code);
        fstr = '#define GLOBAL_SIZE ' + str(global_size) + '\n' + struct + fstr
    else:
        fstr = '#define GLOBAL_SIZE ' + str(global_size) + '\n' + fstr;

    print('=' * 50)
    print(fstr)
    print('-' * 50)

    a_np = np.random.rand(data_size).astype(np.float32)
    b_np = np.random.rand(data_size).astype(np.float32)

    mf = cl.mem_flags
    a_g = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=a_np)
    b_g = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=b_np)

    res_g = cl.Buffer(ctx, mf.WRITE_ONLY, a_np.nbytes)

    prg = cl.Program(ctx, fstr).build();
    exec_evt = prg.casting_test(queue, (global_size,), None, a_g, b_g, res_g)
    exec_evt.wait()

    res_np = np.empty_like(a_np)
    cl.enqueue_copy(queue, res_np, res_g).wait()
    print(res_np)

    elapsed = 1e-9 * (exec_evt.profile.end - exec_evt.profile.start)
    print('Vector: {0} => Execution time of test: {1}s'.format(vector, elapsed)) 
开发者ID:PyOCL,项目名称:OpenCLGA,代码行数:49,代码来源:casting.py


注:本文中的pyopencl.create_some_context方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。