Skip to content

Instantly share code, notes, and snippets.

@mrocklin
Created October 1, 2012 18:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mrocklin/3813386 to your computer and use it in GitHub Desktop.
Save mrocklin/3813386 to your computer and use it in GitHub Desktop.
A quick CUDA program to time the effectiveness of using asynchronous CPU-GPU memory transfers.
#include <stdio.h>
#include <sys/time.h>
const int n = 16000000;
// Print number of milliseconds between timevals
void printDuration(timeval a, timeval b, char* message)
{
double elapsedTime = (b.tv_sec - a.tv_sec) * 1000.0;
elapsedTime += (b.tv_usec - a.tv_usec) / 1000.0;
printf("%s: %lfms\n", message, elapsedTime);
}
int main()
{
timeval a, b;
int bytes = n * sizeof(float);
gettimeofday(&a, NULL);
float* hdata = (float*)malloc(bytes);
gettimeofday(&b, NULL);
printDuration(a, b, "Host Malloc time");
float* ddata;
// Here is a stream if you want to play with intra-GPU concurrency
cudaStream_t s;
if (cudaStreamCreate(&s) != cudaSuccess)
printf("Error!\n");
// Allocate array on GPU memory
gettimeofday(&a, NULL);
cudaMalloc( (void**)&ddata, bytes);
gettimeofday(&b, NULL);
printDuration(a, b, "Device Malloc time");
// Transfer CPU - GPU synchronously
gettimeofday(&a, NULL);
if (cudaMemcpy(ddata, hdata, bytes, cudaMemcpyHostToDevice) != cudaSuccess)
printf("Error!\n");
gettimeofday(&b, NULL);
printDuration(a, b, "cudaMemCpy Time");
// Transfer CPU - GPU asynchronously
gettimeofday(&a, NULL);
if (cudaMemcpyAsync(ddata, hdata, bytes, cudaMemcpyHostToDevice, s)
!= cudaSuccess)
printf("Error!\n");
gettimeofday(&b, NULL);
printDuration(a, b, "cudaMemCpyAsync Time");
cudaFree(ddata);
cudaDeviceProp dev;
cudaGetDeviceProperties(&dev, 0);
printf("Engine count %d\n", dev.asyncEngineCount);
printf("Gpu name %s\n", dev.name);
return EXIT_SUCCESS;
}
@mrocklin
Copy link
Author

mrocklin commented Oct 1, 2012

Run using

nvcc time_cudaMemcpyAsync.cu -o time_cudaMemcpyAsync.o
./time_cudaMemcpyAsync.o 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment