Quantcast
Channel: Active questions tagged kernel - Stack Overflow
Viewing all articles
Browse latest Browse all 6334

C++ class destructor using cuda function

$
0
0

If I implemented a c++ class ClassName. It contains an array. The element of the array is linked list and the memory space is allocated on GPU using CUDA. For example:

//classname.cuh//data structure for linked liststruct Node{    Node* next;    int data;};class ClassName{public:    ClassName();//constructor    ~ClassName();//destructorprivate:    Node list_[1000];    //// the data structure is shown as below:    //// |-----------------------------|    //// | [node] [node] [node] [node] |    //// |   ↓↓↓    |    //// | [node]        [node] [node] |    //// |   ↓↓           |    //// | [node]        [node]        |    //// |-----------------------------|};

In the constructor ClassName(), I can use CUDA to allocate memory on GPU parallelly.

//classname.cu//cuda kernel function: allocate memory on GPU__global__ void allocate_memory_cuda_kernel(Node* list_, int size){    unsigned int idx = blockDim.x * blockIdx.x + threadIdx.x;    if( idx >= size ) return;    Node* new_node = new Node;//allocate a node on gpu    //add new_node to list_[idx]    new_node->next = list_[idx];    list_[idx] = new_node; }////constructor, call cuda kernel functionClassName::ClassName(){    int block_size;    int grid_size;    ////determine block_size and grid_size    //launch CUDA kernel function    allocate_memory_cuda_kernel<<<grid_size, block_size>>>(this->list_, 1000);    cudaDeviceSynchronize();}

However, I need to destroy the memory in destructor ~ClassName(). Can I call a CUDA function to free list_[1000]?

//classname.cu__global__void clear_data_cuda(Node* list_, int size){    unsigned int idx = blockIdx.x * blockDim.x + threadIdx.x;    if(idx >= size) return;    //free list_[idx]    while(list_[idx])    {        Node* temp = list_[idx];        list_[idx] = temp->next;//let list_[idx] point to next node        delete temp;        temp = nullptr;    }}ClassName::~ClassName(){    int block_size, grid_size;    //determine block_size and grid_size;    ////call clear_data_cuda(this->list_array, 1000);    clear_data_cuda<<<grid_size, block_size>>>(list_, 1000);    cudaDeviceSynchronize();}

Is it safe to call a CUDA kernel function in the destructor?


Viewing all articles
Browse latest Browse all 6334

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>