假设我在设备(CUDA)上有一组数字,例如
float *d_x;
cudaMalloc(&x, N*sizeof(float));
其中x
将类似于[0,0,3,0,3,0,3,1,5,1,0]
。我在阵列上执行两项操作。细节无关紧要,但是第一个操作将作为一种预处理,对
x
的值进行置换并返回索引,第二个操作仅对数组的第一个n
值执行某些操作,其中n
是返回的值通过第一次操作。我的问题是,第二个操作本质上在计算上要昂贵得多,并且将花费更多的时间,而实际上只涉及到数组的第一个
n
值。所以,像
uint operation1(float* d_x)
{
// call some kernel and wait for the kernel to execute.
// The kernel reorders x into [3,3,1,5,1,0,0,0,0,0]
return n; // n in this case is 5, because there are 5 nonzero values in d_x
}
void operation2(float* d_x, int n)
{
// call another kernel, sorting the subarray [3,3,1,5,1], and never touching the values at index
// n or above
// In other words, sort the subarray of values *d_x, *(d_x + 1), ... *(d_x + n - 1) to get
// [1,1,3,3,5]
}
int main()
{
float* d_x;
// fill d_x with input data
int n = operation1(d_x);
// many many lines of code doing several other things with it.
operation2(d_x, n);
// more code.
}
我的问题是双重的:operation1
之后将不再使用的数组部分是个好主意吗? 最佳答案
Is it a good idea to deallocate the part of the array that will no longer be used after operation1?
这并不是一个完全不受支持的“好”主意。 CUDA API中没有重新分配样式的操作,并且考虑到GPU上内存分配的成本和同步性,从性能的 Angular 来看也不是一个好主意,即使有这样的事情(或您自己等效的免拷贝分配)实现)。
If yes, what is the safest and cleanest way to go about this?
看上面。
关于c++ - 在CUDA中取消分配数组的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64969767/