Memory CUDA内存限制

Memory CUDA内存限制,memory,memory-management,cuda,Memory,Memory Management,Cuda,如果我试图向CUDA设备发送比可用内存大的结构,CUDA会给我任何警告或错误吗 我这样问是因为我的GPU有1024兆字节(1073414144字节)的全局内存总量,但我不知道该如何处理和最终的问题 这是我的密码: #define VECSIZE 2250000 #define WIDTH 1500 #define HEIGHT 1500 // Matrices are stored in row-major order: // M(row, col) = *(M.elements + ro

如果我试图向CUDA设备发送比可用内存大的结构,CUDA会给我任何警告或错误吗

我这样问是因为我的GPU有1024兆字节(1073414144字节)的全局内存总量,但我不知道该如何处理和最终的问题

这是我的密码:

#define VECSIZE 2250000
#define WIDTH 1500
#define HEIGHT 1500



// Matrices are stored in row-major order:
// M(row, col) = *(M.elements + row * M.width + col)
struct Matrix
{

    int width;
    int height;
    int* elements;

};


   int main()
   {


Matrix M;
M.width = WIDTH;
M.height = HEIGHT;
M.elements = (int *) calloc(VECSIZE,sizeof(int));   

int row, col;   


// define Matrix M
// Matrix generator:
for (int i = 0; i < M.height; i++)
    for(int j = 0; j < M.width; j++)
    {
    row = i;
    col = j; 

    if (i == j)
    M.elements[row * M.width + col] = INFINITY;
        else
        {
        M.elements[row * M.width + col] = (rand() % 2); // because 'rand() % 1' just does not seems to work ta all.
        if (M.elements[row * M.width + col] == 0)  // can't have zero weight.
            M.elements[row * M.width + col] = INFINITY;
            else if (M.elements[row * M.width + col] == 2)
                M.elements[row * M.width + col] = 1;    

        }

    }





// Declare & send device Matrix to Device.
Matrix d_M;
d_M.width = M.width;
d_M.height = M.height;
size_t size = M.width * M.height * sizeof(int);
cudaMalloc(&d_M.elements, size);
cudaMemcpy(d_M.elements, M.elements, size, cudaMemcpyHostToDevice);

int *d_k=  (int*) malloc(sizeof(int));
cudaMalloc((void**) &d_k, sizeof (int));



int *d_width=(int*)malloc(sizeof(int));
cudaMalloc((void**) &d_width, sizeof(int));
unsigned int *width=(unsigned int*)malloc(sizeof(unsigned int));
width[0] = M.width;
cudaMemcpy(d_width, width, sizeof(int), cudaMemcpyHostToDevice);

int *d_height=(int*)malloc(sizeof(int));
cudaMalloc((void**) &d_height, sizeof(int));
unsigned int *height=(unsigned int*)malloc(sizeof(unsigned int));
height[0] = M.height;   
cudaMemcpy(d_height, height, sizeof(int), cudaMemcpyHostToDevice);
    /*

        et cetera .. */
#定义向量大小225000
#定义宽度1500
#定义高度1500
//矩阵按行的主要顺序存储:
//M(行,列)=*(M.元素+行*M.宽度+列)
结构矩阵
{
整数宽度;
内部高度;
int*元素;
};
int main()
{
矩阵M;
M.宽度=宽度;
M.高度=高度;
M.elements=(int*)calloc(VECSIZE,sizeof(int));
int row,col;
//定义矩阵M
//矩阵生成器:
对于(int i=0;i
当您当前可能没有向GPU发送足够的数据以最大限度地利用其内存时,您的
cudaMalloc
将返回错误代码
CudaErrorMemoryLocation
,这表示内存分配失败。我注意到,在您的示例代码中,您没有检查cuda的返回值调用。需要检查这些返回代码以确保程序正确运行。cuda api不会引发异常:您必须检查返回代码。有关检查错误和获取有关错误的有意义消息的信息,请参阅。如果您使用的是
cutil.h
,则它提供了两个非常有用的宏:
CUDA\u SAFE\u调用
(在发出cudamaloc、cudaMemcpy等函数时使用)

CUT\u CHECK\u ERROR
(在执行内核后用于检查内核执行中的错误)。

如果有错误,他们会使用文章中详细介绍的错误检查机制来处理。

200万个元素*4个字节只有8MB。您有1GB,即1024MB可玩!是的,我不在这个示例中,但我希望随着时间的推移使用更大的矩阵。