简体   繁体   中英

Allocate a 2D vector in unified memory CUDA C/C++

So I need to create a matrix with different row lengths, and this is how it looks like in normal C/C++

int** MpesosT = (int**)malloc(N * sizeof(int*));
    for (int i = 0; i < N; i++)
    {
        MpesosT[i] = (int*)malloc(vecinosT[i] * sizeof(int));
    }

However, I don't know how to do this using the CUDA function to allocate memory:

int* Vector;    cudaMallocManaged(&Vector, VectorSize* sizeof(int));

I can't just use a vector of size N*N or something, because every row has a different size, so how could I do that?

Took a couple of hours, but I found the way to do it. In case anyone has the same problem:

double** Matrix;
cudaMallocManaged((double***)&Matrix, N * sizeof(double*));
for (i = 0; i < N; i++)
{
    cudaMallocManaged((double**)&Matrix[i], rowlength[i] * sizeof(double));
}

This way, every row has a different length

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM