简体   繁体   中英

how can i overcome memory allocation in cuda

I have a question. I'm working with the 4.0 cuda. I have the following code:

in my cudaHeader.h:

#include <stdlib.h>

extern "C" void wrapperfunction(int* array);

in my cudaCpp.cpp:

#include <stdio.h>
......
int main()
{
  int array[50] = {0, 1, 2, ..........,49};
  ...........

  while(true)
  {
   ........

   wrapperfunction(array);

   ........

   }
  return 0;

}

in my cuda.CU:

__global__ void kernel(int *new_arrayG, int *arrayG,int *const_arrayG)
{
  int x = threadIdx.x;

  new_arrayG[x] = arrayG[x] + const_arrayG[x];
  __syncthreads();
}

extern "C" int wrapperfunction(int* array)
{

  static int const_array[50] = {0, 1, 2, ........., 49};  //any constant data
  int *arrayG, *new_arrayG, *const_arrayG;
  int size = 50 * sizeof(int);

  cudaMalloc((void**)&arrayG, size);
  cudaMalloc((void**)&new_arrayG, size);
  cudaMalloc((void**)&const_arrayG, size);

  cudaMemcpy(const_arrayG, const_array, size, cudaMemcpyHostToDevice);
  cudaMemcpy(arrayG, array, size, cudaMemcpyHostToDevice);

  Kernel<<<1, 50>>>(new_arrayG, arrayG, const_arrayG);

  cudaMemcpy(array, new_arrayG, size, cudaMemcpyDeviceToHost);

  cudaFree(arrayG);cudaFree(new_arrayG);cudaFree(const_arrayG);
}

this a sample from my code, i wanna to say each time i call the wrapperfunction from my .cpp code the program allocate the static array and free it at the end and it it takes much time and at real i deal with very large static array and each time call this function i take very much time. so i wanna to a way to allocate my static arrays one time at the begining of the program and free them at the end of my program(application). Plz. any helping.

Thanks.

Simply allocate your arrays once for all in your main(), moving it to some .cu file. And them, pass their addresses to the wrapping function.

Also, you should check the return of such calls.

Furthermore, try not to use any C header in your C++ program if you have C++ header equivalent. Use #include <cstdio> instead of #include <cstdlib> .

Finally, as you're only dealing with cpp code, you should drop your extern "C".

Or if you do want work with C code out of CUDA code, quit using .cpp file extension.

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