Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You have to be aware where your memory resides. malloc allocates host memory, cudaMalloc allocates memory on the device and returns a pointer to that memory back. However, this pointer is only valid in device functions. </p> <p>What you want could be achived as followed:</p> <pre><code>typedef struct { int width; int height; float* elements; } Matrix; int main void() { int rows, cols, numMat = 2; // These are actually determined at run-time Matrix* data = (Matrix*)malloc(numMat * sizeof(Matrix)); // ... Successfully read from file into "data" ... Matrix* h_data = (Matrix*)malloc(numMat * sizeof(Matrix)); memcpy(h_data, data, numMat * sizeof(Matrix); for (int i=0; i&lt;numMat; i++){ cudaMalloc(&amp;(h_data[i].elements), rows*cols*sizeof(float)); cudaMemcpy(h_data[i].elements, data[i].elements, rows*cols*sizeof(float)), cudaMemcpyHostToDevice); }// matrix data is now on the gpu, now copy the "meta" data to gpu Matrix* d_data; cudaMalloc(&amp;d_data, numMat*sizeof(Matrix)); cudaMemcpy(d_data, h_data, numMat*sizeof(Matrix)); // ... Do other things ... } </code></pre> <p>To make things clear: <code>Matrix* data</code> contains the data on the host. <code>Matrix* h_data</code> contains a pointer to the device memory in elements which can be passed to the kernels as parameters. The memory is on the GPU. <code>Matrix* d_data</code> is completly on the GPU and can be used like data on the host.</p> <p>in your kernel code you kann now access the matrix values, e.g.,</p> <pre><code>__global__ void doThings(Matrix* matrices) { matrices[i].elements[0] = 42; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload