-
-
Save improve100/a93fbe598f8d460a6205722fdb8dff15 to your computer and use it in GitHub Desktop.
Benchmark of cudaMalloc. Allocate 1MB of memory totally with several block sizes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <sys/time.h> | |
#include <cuda_runtime.h> | |
#include <stdio.h> | |
inline double seconds() | |
{ | |
struct timeval tp; | |
struct timezone tzp; | |
int i = gettimeofday(&tp, &tzp); | |
return ((double)tp.tv_sec + (double)tp.tv_usec * 1.e-6); | |
} | |
int total_size = 1024 * 1024; // 1MB | |
void test(int size) | |
{ | |
double iStart, iElaps; | |
int num = total_size / size; | |
float *d[num]; | |
iStart = seconds(); | |
for (int i = 0; i < num; i++) { | |
cudaMalloc((float**)&d[i], size); | |
} | |
iElaps = seconds() - iStart; | |
printf("cudaMalloc(%d) x %d Time elapsed %f sec\n", size, num, iElaps); | |
iStart = seconds(); | |
for (int i = 0; i < num; i++) { | |
cudaFree(d[i]); | |
} | |
iElaps = seconds() - iStart; | |
printf("cudaFree(%d) x %d Time elapsed %f sec\n", size, num, iElaps); | |
} | |
int main(int argc, char **argv) | |
{ | |
printf("%s Starting...\n", argv[0]); | |
// set up device | |
int dev = 0; | |
cudaDeviceProp deviceProp; | |
cudaGetDeviceProperties(&deviceProp, dev); | |
printf("Using Device %d: %s\n", dev, deviceProp.name); | |
cudaSetDevice(dev); | |
int size = atoi(argv[1]); | |
test(size); | |
return(0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment