我怎么知道内存映射在 OpenCL 中是否成功

How do I know the memory mapping is successful in OpenCL

我是 OpenCL 的新手。 目前我正在研究一个大的一维数组。数组的大小约为800万。以下是我的部分代码:

//allocate opencl hosted memory for input
int[] Counts = new int[8000000];

//get device and create context....

CLBuffer<Integer> memIn1 = context.createIntBuffer(Usage.Input, 8000000);   
Pointer<Integer> a = memIn1.map(queue, MapFlags.Write);
a.setInts(Counts);

//memory allocation for the second parameter memIn2

CLKernel kernel = program.createKernel("gpuScoring", memIn1, memIn2, 8000000, memOut);
kernel.enqueueNDRange(queue, new int[] {8000000}, null);

下面是我的内核代码:

__kernel void gpuScoring(__global int *Counts, __global int *value, int width, int height, __global int *output){

    int gid = get_global_id(0);
    int x = gid % width;
    int y = gid / width;
    int count = Counts[y * width + x];
    if(count != 0){
        //need to do something here...
    }   
}

然而,问题是我发现我永远无法进入 if(count != 0) 的真正分支。我很确定我的 Java 代码中的 Counts 数组有一些不为 0 的索引值。是否因为我错误地使用了内存映射?请帮忙。谢谢。

映射缓冲区后,您必须在那里写入数据,然后取消映射。您的用法更像是创建缓冲区并将主机数据复制到其中。