c++ - 在CUDA中取消分配数组的一部分

标签 c++ cuda

假设我在设备(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/

    相关文章:

    cudaEventSynchronize 与 cudaDeviceSynchronize

    c++ - 具有共享内存的 CUDA 矩阵转置

    c++ - 如何编译 thrust 和 c++ 项目?

    while-loop - 在迭代循环中避免 CudaMemcpy

    c++ - 错误 LNK2019 : unresolved external symbol "public: void __thiscall

    C++ LibcURL IMAP 获取主题行的变量是什么?

    c++ - CGAL 不计算完整的 delaunay 三角剖分

    C++ 使用 libssh libary 通过 SSH 检索数据失败

    c++ - 管理标准容器中的抽象类

    c++ - CUDA 中的递归返回非法内存访问