我尝试按如下方式分配17338896个浮点数元素(大约70 mb):

    state = cublasAlloc(theSim->Ndim*theSim->Ndim, 
                       sizeof(*(theSim->K0)), 
                       (void**)&K0cuda);
    if(state != CUBLAS_STATUS_SUCCESS) {
        printf("Error allocation video memory.\n");
        return -1;
    }

但是,我收到了变量状态 CUBLAS_STATUS_ALLOC_FAILED 的错误消息。这是否与机器上可用的显卡内存量(我的128 mb)有关,或者这是我可以使用cublasAlloc()函数分配的内存量的限制(即与数量无关)机器上可用的内存)?我尝试使用cudaMalloc()函数,我遇到了同样的问题。提前感谢您对此进行调查。

--------------添加错误再现----------------------------- --------

#include <cuda.h>
#include <stdio.h>
int main (int argc, char *argv[]) {

    // CUDA setup
    cublasStatus state;

    if(cublasInit() == CUBLAS_STATUS_NOT_INITIALIZED) {
        printf("CUBLAS init error.\n");
        return -1;
    }

    // Instantiate video memory pointers
    float *K0cuda;

    // Allocate video memory needed
    state = cublasAlloc(20000000, 
                        sizeof(float), 
                        (void**)&K0cuda);
    if(state != CUBLAS_STATUS_SUCCESS) {
        printf("Error allocation video memory.\n");
        return -1;
    }

    // Copy K0 from CPU memory to GPU memory
    // Note: before so, decide whether to integrate as a part of InsertionSim or
    //      CUDA content as a separate class
    //state = cublasSetMatrix(theSim->Ndim, theSim->Ndim, sizeof(*theSim->K0),
    //                      theSim->K0, theSim->Ndim, K0cuda, theSim->Ndim);
    //if(state != CUBLAS_STATUS_SUCCESS) {
    //  printf("Error copy to video memory.\n");
    //  return -1;
    //}

    // Free memory
    if(cublasFree(K0cuda) != CUBLAS_STATUS_SUCCESS) {
        printf("Error freeing video memory.\n");
        return -1;
    }

    // CUDA shutdown
    if(cublasShutdown() != CUBLAS_STATUS_SUCCESS) {
        printf("CUBLAS shutdown error.\n");
        return -1;
    }

    if(theSim != NULL) delete theSim;

    return 0;
}
有帮助吗?

解决方案

内存可以碎片化,这意味着您仍然可以分配多个较小的块但不能分配一个大块。您的显卡显然需要一些内存用于其正常的2D任务。如果这恰好将128 MB分成了几乎64 MB的2个块,那么你会看到这种失败。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top