问题
I have an algorithm and I've been trying to accelerate it using OpenCL on my nVidia.
It has to process a large amount of data (let's say 100k to milions), where for each one datum: a matrix (on the device) has to be updated first (using the datum and two vectors); and only after the whole matrix has been updated, the two vectors (also on the device) are updated using the same datum. So, my host code looks something like this
for (int i = 0; i < milions; i++) {
clSetKernelArg(kernel_matrixUpdate, 7, sizeof(int), (void *)&i);
clSetKernelArg(kernel_vectorsUpdate, 4, sizeof(int), (void *)&i);
clEnqueueNDRangeKernel(command_queue, kernel_matrixUpdate, 1, NULL, &global_item_size_Matrix, NULL, 0, NULL, NULL);
clEnqueueNDRangeKernel(command_queue, kernel_vectorsUpdate, 1, NULL, &global_item_size_Vectors, NULL, 0, NULL, NULL);}
Unfortunately, this loop takes longer to execute than the kernels themselves. So my questions are:
- Is there any way to enqueue N kernels more efficiently?
- Is there any way to update the whole matrix first and then update the vectors, without using separate kernels? E.g. does the device run the kernels in order, (i.e. first workgroup with 0,1,...,63; second workgroup with 64,...)? But I guess that would be a bad practice anyway...
- Any other ideas? :D
Every feedback or opinion will be appreciated. Thank you.
回答1:
You need to upload all your data to GPU and then call a kernel with one work item per element, instead of the for loop.
Generally, when going from CPU to GPU, the outermost "for" loop becomes a kernel invocation.
来源:https://stackoverflow.com/questions/51786300/how-to-reduce-opencl-enqueue-time-any-other-ideas