當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。