최우석
최우석

Reputation: 183

What is the role of cudaDeviceReset() in Cuda

#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <stdio.h>
__global__ void funct(void){
      printf("Hello from GPU!\n");
}

int main(void){
         funct << < 2, 4 >> >();

for (int i = 0; i < 10; i++){
    cudaDeviceReset();
    //cudaDeviceSynchronize();
    printf("Hello, World from CPU!\n");

    }
return 0;
}

I thought the role of cudaDeviceReset is cudamemcpy. this case we didn't have the result of number. So we were not able to use cudamemcpy. But We used cudaDeviceReset for returning four "Hello from GPU!" result from kernel.

Is that right?

and I replaced cudaDeviceRest() with cudaDeviceSynchronize(). I saw the same result. but I couldn't know the difference between them.

Upvotes: 18

Views: 18973

Answers (1)

Robert Crovella
Robert Crovella

Reputation: 151849

The role of cudaDeviceReset() is documented here

It is used to destroy a CUDA context, which means that all device allocations are removed.

I agree that it appears to have a synchronizing effect. However since the documentation states:

Note that this function will reset the device immediately.

I believe it is unsafe to rely on this behavior. Furthermore, the documentation also calls out the hazard of using this function in a multi-threaded app. Therefore, safe coding would dictate:

  1. Use of device synchronization (e.g. cudaDeviceSynchronize(), or cudaMemcpy(), etc.)

  2. Retrieve whatever data your application would like to preserve that may be in a device allocation, or that a recently running kernel may have updated (in device memory).

  3. Make sure that any host threads that may also have device activity associated with them, are also terminated

  4. Make sure that any C++ objects that may have device activity in their destructors are properly destroyed or out-of-scope

  5. call cudaDeviceReset() as part of application shut-down.

Note that calling cudaDeviceReset() as part of application shut-down should not be considered mandatory. Many applications will work fine without such an idiom.

This answer may also be of interest.

Upvotes: 20

Related Questions