5

这是一个让我发疯的简单 OpenCL 矩阵乘法内核:

顺便说一句,我正在使用 pyopencl。

__kernel void matrixMul(  __global int* C,
                          __global int* A,
                          __global int* B,
                          int wA, int wB){

                int row = get_global_id(1); //2D Threas ID x
                int col = get_global_id(0); //2D Threas ID y

                //Perform dot-product accumulated into value
                int value = 0;
                for ( int k = 0; k < wA; k++ ){
                    value += A[row*wA + k] * B[k*wB+col];
                }
                C[row*wA+col] = value; //Write to the device memory
            }

哪里(输入)

A = [72 45
     75 61]
B = [26 53 
     46 76]
wA = wB = 2

我得到的输出:

有时我会得到:

C = [3942 0
     0 5472]

否则我得到:

C = [3942 7236
     3312 5472]

但输出应该是:

C = [3942 7236
     4756 8611]

我不知道我在这里犯了什么错误。我一整天都没有运气。

请在这件事上给予我帮助

这是完整的python代码:

import pyopencl as cl
import numpy as np
import os

ORDER = 2
LEN = ORDER*ORDER
ctx = cl.create_some_context()

commandQueue = cl.CommandQueue( ctx )

A = np.array((72, 45, 75, 61), dtype = np.int32)
B = np.array((26, 53, 46, 76), dtype = np.int32)
C = np.empty_like(A)

in_buf1 = cl.Buffer( ctx, cl.mem_flags.READ_ONLY | cl.mem_flags.COPY_HOST_PTR,
                 hostbuf = A )
in_buf2 = cl.Buffer( ctx, cl.mem_flags.READ_ONLY | cl.mem_flags.COPY_HOST_PTR,
                 hostbuf = B )
out_buf = cl.Buffer( ctx, cl.mem_flags.WRITE_ONLY, C.nbytes )

kernelSrc1 = """__kernel void
            matrixMul(  /*const int Mdim,
                        const int Ndim,
                        const int Pdim,*/
                        __global int* C,
                        __global int* A,
                        __global int* B,
                        int wA, int wB)
           {
                int row = get_global_id(1); //2D Threas ID x
                int col = get_global_id(0); //2D Threas ID y                

                //Perform dot-product accumulated into value
                int value = 0;
                for ( int k = 0; k < wA; k++ ){
                    value += A[row*wA + k] * B[k*wB+col];
                }
                C[row*wA+col] = value; //Write to the device memory
            }"""

program1 = cl.Program(ctx, kernelSrc1 ).build()
event1 = program1.matrixMul( commandQueue, (LEN, ), None,
                     out_buf, in_buf1, in_buf2, np.int32(ORDER), np.int32(ORDER));
event1.wait()

cl.enqueue_copy(commandQueue, C, out_buf)
print C

我正在使用 Python 2.7.x、pyopencl 2012.1、AMD APP SDK

4

1 回答 1

7

您错误地设置了全局大小参数。由于您在内核中使用了全局大小的两个维度,因此您需要将全局大小设置为 (ORDER,ORDER)。当你改变它时,你会得到:

[3942 7236
 4756 8611]
于 2012-10-21T19:02:34.257 回答