我正在使用cusp::bicgstab求解线性系统Ax=b,其中A是 MxNxP 网格上的 3D Poisson,x是未知数,b是 RHS。我有一个K40m Tesla,它有 12GB 内存。
我用M=2000, N=2000, P=20(8000 万未知数)进行了测试,变量类型是double;因此使用的总内存(用于A、x、b和其他)约为5.5GB。代码工作正常。
然后我将M或者N的值增加到2500(使用的内存还远小于12GB),程序遇到如下错误:
在抛出 'thrust::system::detail::bad_alloc' 的实例后调用终止
what(): std::bad_alloc: out of memory
Aborted (core dumped)
我看到错误是“设备内存不足”。因此,我想知道cusp库中的内存管理。在求解系统的迭代过程中,它是否为额外变量(用于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;
}