cuda尖点线性求解器中的内存使用

Memory use in cuda cusp linear solver

我正在使用 cusp::bicgstab 求解线性系统 Ax=b,其中 A 是 MxNxP 网格上的 3D 泊松,x 是未知数,b 是 RHS。我有一个 K40m Tesla 有 12GB 内存。

我用M=2000,N=2000,P=20(8000万个未知数)进行测试,变量类型是double;所以使用的总内存(A、x、b、 和其他)大约是 5.5GB。代码运行良好。

然后我把M或者N的值增加到2500(使用的内存还是远远小于12GB),程序遇到如下错误:

terminate called after throwing an instance of 'thrust::system::detail::bad_alloc'

what(): std::bad_alloc: out of memory
Aborted (core dumped)

我看到错误是“设备内存不足”。因此,我想知道 cusp 库中的内存管理。在求解系统的迭代过程中,它是否使用大约相同的内存 space 用于额外变量(如用于 A,x,b)?

下面是我的代码:

#include <iostream>
#include <cuda.h>
#include <cuda_runtime_api.h>

#include <cusp/monitor.h>
#include <cusp/krylov/bicgstab.h>
#include <cusp/gallery/poisson.h>
#include <cusp/print.h>

// where to perform the computation
typedef cusp::device_memory MemorySpace;

// which floating point type to use
typedef double ValueType;

int main(int argc, char **argv)
{
    size_t avail, total;                // Available and Total memory count
    int N = 2500, M = 2000, P = 20;     // Dimension
    
    // create a matrix for a 3D Poisson problem on a MxNxP grid
    cusp::dia_matrix<int, ValueType, MemorySpace> A;
    cusp::gallery::poisson7pt(A, N, M, P);

    // allocate storage for solution (x) and right hand side (b)
    cusp::array1d<ValueType, MemorySpace> x(N*M*P, 0.0);
    cusp::array1d<ValueType, MemorySpace> b(N*M*P, 1.0);
    
    // set preconditioner (identity)
    cusp::identity_operator<ValueType, MemorySpace> ID(A.num_rows, A.num_rows);
    
    // Set stopping criteria:
    // ... iteration_limit    = 100
    // ... relative_tolerance = 1e-9
    // ... absolute_tolerance = 0
    cusp::default_monitor <ValueType> monitor(b, 100, 1e-9);

    // solve the linear system A x = b
    cusp::krylov::bicgstab(A, x, b, monitor, ID);

    // Get device memory usage
    cudaMemGetInfo( &avail, &total );
    size_t used = total - avail;
    std::cout << "Device memory used: " << used/(1024.*1024.*1024.) << " Gb " << std::endl;
    
    return 0;
}

您可以自己阅读 bicgstab 解算器的 source,但看起来有八个临时数组,每个数组的条目数与矩阵中的行数相同。如果我正确阅读了您的代码,这意味着您需要至少 8 * N * M * P * sizeof(double) 字节的可用 GPU 内存才能进入 bicgstab 调用求解器 运行.