2016-07-22 83 views
1

我有一個100個元素的數組,我想要做的就是將這100個元素複製到另一個數組的每個第n個元素中。OpenCL - 在數組的每n個元素中插入值

假設n爲3

新的陣列將有[VAL1 0 0 0值2 0 0 VAL3 0 ...]後的值複製到每個第n個元素。現在在opencl中,我嘗試創建一個指向當前索引的指針,並且我只是每次將n添加到該值。但是,目前的指數總是保持相同的價值。以下是我的代碼。對於CURRENTINDEX部分

__kernel void ddc(__global float *inputArray, __global float *outputArray, __const int interpolateFactor, __global int *currentIndex){ 
    int i = get_global_id(0); 
    outputArray[currentIndex[0]] = inputArray[i]; 
    currentIndex[0] = currentIndex[0] + (interpolateFactor - 1); 
    printf("index %i \n", currentIndex[0]);  
} 

主機代碼:

int *index; 
index = (int*)malloc(2*sizeof(int)); 
index[0] = 0; 

cl_mem currentIndex; 
currentIndex = clCreateBuffer(
    context, 
    CL_MEM_WRITE_ONLY, 
    2 * sizeof(int), 
    NULL, 
    &status); 
status = clEnqueueWriteBuffer(
    cmdQueue, 
    currentIndex, 
    CL_FALSE, 
    0, 
    2 * sizeof(int), 
    index, 
    0, 
    NULL, 
    NULL); 
printf("Index enqueueWriteBuffer status: %i \n", status); 
status |= clSetKernelArg(
    kernel, 
    4, 
    sizeof(cl_mem), 
    &currentIndex); 
printf("Kernel Arg currentIndex Factor status: %i \n", status); 

如果你想知道爲什麼我使用數組有兩個元素,那是因爲我不知道如何只引用一個變量。我只是以輸入和輸出數組工作的方式實現它。當我運行的3的interpolateFactor內核,CURRENTINDEX總是打印2.

回答

2

所以,如果我理解你想要做的是保存應該用來CURRENTINDEX下一個索引正確的。這不起作用。該值不會立即更新爲其他工作項目。如果你想這樣做,你將不得不按順序執行所有的內核。

你可以做的是

__kernel void ddc(__global float *inputArray, __global float *outputArray, __const int interpolateFactor, int start){ 
    int i = get_global_id(0); 
    outputArray[start+i*(interpolateFactor-1)] = inputArray[i]; 
} 

假設你可以從任何其他點比0開始。否則,你可能只是把它扔完全。

得到它的工作一樣,你做

int start = 0; 
status |= clSetKernelArg(
    kernel, 
    3, // This should be 3 right? Might have been the problem to begin with. 
    sizeof(int), 
    &start); 

希望這有助於。

+0

那樣容易。再次感謝,不敢相信我沒有嘗試過這種方式。另外,你說得對3,我只是爲了這個問題刪除了內核的其他不必要的參數,並且錯過了編輯。 –