【问题标题】:PyOpenCL 2D array kernel get_global_id(1) errorPyOpenCL 2D 数组内核 get_global_id(1) 错误
【发布时间】:2018-04-02 04:33:35
【问题描述】:

我真的是 OpenCL 的新手。我已经从这个网站上获取了示例代码:http://www.drdobbs.com/open-source/easy-opencl-with-python/240162614?pgno=2,并且我已经对其进行了一些定制。我的目标是向内核发送一个填充有 1 个数字的 4x4 矩阵并从内核中恢复它。我知道这是一个微不足道的代码,但我需要这样做才能了解 OpenCL 的工作原理。输入矩阵是这个:

 [[ 1.  1.  1.  1.]
 [ 1.  1.  1.  1.]
 [ 1.  1.  1.  1.]
 [ 1.  1.  1.  1.]]

但是,我从内核得到的输出是这个,应该和输入一样:

[[ 1.  1.  1.  1.]
 [ 0.  0.  0.  0.]
 [ 0.  0.  0.  0.]
 [ 0.  0.  0.  0.]]

这是我的完整代码:

import pyopencl as cl
from pyopencl import array
import numpy as np

## Step #1. Obtain an OpenCL platform.
platform = cl.get_platforms()[0]

## It would be necessary to add some code to check the check the support for
## the necessary platform extensions with platform.extensions

## Step #2. Obtain a device id for at least one device (accelerator).
device = platform.get_devices()[1]

## It would be necessary to add some code to check the check the support for
## the necessary device extensions with device.extensions

## Step #3. Create a context for the selected device.
context = cl.Context([device])

## Step #4. Create the accelerator program from source code.
## Step #5. Build the program.
## Step #6. Create one or more kernels from the program functions.
program = cl.Program(context, """
    __kernel void matrix_dot_vector(const unsigned int size, __global const float *matrix, __global float *result)
    {
        int x = get_global_id(0);
        int y = get_global_id(1);
        result[x + size * y] = matrix[x + size * y];
    }
    """).build()

matrix = np.ones((4,4), np.float32)

## Step #7. Create a command queue for the target device.
queue = cl.CommandQueue(context)

## Step #8. Allocate device memory and move input data from the host to the device memory.
mem_flags = cl.mem_flags
#matrix_buf = cl.Buffer(context, mem_flags.READ_ONLY | mem_flags.COPY_HOST_PTR, hostbuf=matrix)
matrix_buf = cl.Buffer(context, mem_flags.READ_ONLY | mem_flags.COPY_HOST_PTR, hostbuf=matrix)
destination_buf = cl.Buffer(context, mem_flags.WRITE_ONLY, matrix.nbytes)

## Step #9. Associate the arguments to the kernel with kernel object.
## Step #10. Deploy the kernel for device execution.
program.matrix_dot_vector(queue, matrix.shape, None, np.int32(matrix.size), matrix_buf, destination_buf)

## Step #11. Move the kernels output data to host memory.
matrix_dot_vector = np.ones((4,4), np.float32)
cl.enqueue_copy(queue, matrix_dot_vector, destination_buf)

## Step #12. Release context, program, kernels and memory.
## PyOpenCL performs this step for you, and therefore,
## you don't need to worry about cleanup code

print(matrix_dot_vector)

据我所见,int y = get_global_id(1); 的值始终为 0。这就是导致错误的原因,我不明白为什么它始终为 0,因为我将正确的形状传递给内核 @987654326 @这是第二个参数matrix.shape,等于(4,4)。

有人猜出什么问题了吗?

谢谢!

【问题讨论】:

    标签: python c multidimensional-array opencl pyopencl


    【解决方案1】:

    为第一个内核参数传递了错误的值 - 大小不应是总矩阵大小。将np.int32(matrix.size) 更改为np.int32(matrix.shape[0])

    【讨论】:

    • 完美!我知道它必须是这样的,但现在,你能解释一下我在做什么和你告诉我做什么之间的区别吗?
    • 在内核中,您正在计算元素在展平数组中的位置,因此对于x + size * y,大小不能是size=matrix.shape[0]*matrix.shape[1],而是size=matrix.shape[0],其中matrix.shape[0] 是矩阵第一个维度的大小。
    猜你喜欢
    • 2018-10-26
    • 2016-06-25
    • 2021-07-21
    • 1970-01-01
    • 1970-01-01
    • 2012-06-16
    • 1970-01-01
    • 2012-01-08
    • 1970-01-01
    相关资源
    最近更新 更多