odd-even sort in OpenCL - Stack Overflow

admin2025-04-18  1

i have the next task: write opencl program, which will sort massive using odd-even sort method. i wrote it, but have some troubles and i don't know how to solve it because i'm new in opencl

here is my kernel code:

const char* kernelSource =
"__kernel void odd_even_sort(__global ulong * arr, const unsigned int n) {\n"
"    unsigned int id = get_global_id(0);\n"
"    for (unsigned int phase = 0; phase < n; phase++) {\n"
"        unsigned int swap_idx;\n"
"        if (phase % 2 == 0) {\n"
"            swap_idx = id * 2;\n"
"        }\n"
"        else {\n"
"            swap_idx = id * 2 + 1;\n"
"        }\n"
"        if (swap_idx + 1 < n) {\n"
"            if (arr[swap_idx] > arr[swap_idx + 1]) {\n"
"                ulong temp = arr[swap_idx];\n"
"                arr[swap_idx] = arr[swap_idx + 1];\n"
"                arr[swap_idx + 1] = temp;\n"
"            }\n"
"        }\n"
"        barrier(CLK_GLOBAL_MEM_FENCE);\n"
"    }\n"
"}\n";

and here is how i run it:

clSetKernelArg(kernel, 0, sizeof(cl_mem), (cl_ulong*)&A_mem);
clSetKernelArg(kernel, 1, sizeof(unsigned int), &nums);
size_t global_work_size = nums / 2;
ret = clEnqueueNDRangeKernel(command_queue, kernel, 1, NULL, &global_work_size, NULL, 0, NULL, NULL);

nums is the length of massive, it is a power of 2

i don't know why, but when i try to print sorted massive, i can't find some numbers from the unsorted. instead of these numbers there can be dublicates of random numbers, here is the example:

  1. 3669687509
  2. 3670134728
  3. 3672631890
  4. 3674672863
  5. 3674672863
  6. 3674672863
  7. 3674672863
  8. 3674672863
  9. 3678491299
  10. 3678977840
  11. 3682679700

the unsorted massive is guaranteed to contain no duplicate numbers.

there may also be a problem that the number is out of place (example with another array):

  1. 4011496893
  2. 4013708165
  3. 4016925868
  4. 4116700792
  5. 4016953184
  6. 4020082302

i hope for someone's help

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1744951267a276350.html

最新回复(0)