579
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <stdlib.h>
#include <cutil_inline.h>
int main(int argc, char* argv[])
{
int cudaDeviceCount;
cudaGetDeviceCount(&cudaDeviceCount);
int cudaDevice = 0;
int maxSps = 0;
struct cudaDeviceProp dp;
for (int i = 0; i < cudaDeviceCount; i++) {
cudaGetDeviceProperties(&dp, i);
if (dp.multiProcessorCount > maxSps) {
maxSps = dp.multiProcessorCount;
cudaDevice = i;
}
}
cudaGetDeviceProperties(&dp, cudaDevice);
printf("Using cuda device %d: %s\n", cudaDevice, dp.name);
cudaSetDevice(cudaDevice);
int length = 1<<26;
unsigned int timer = 0;
CUT_SAFE_CALL( cutCreateTimer(&timer) );
CUT_SAFE_CALL( cutStartTimer(timer) );
float* h_Data;
h_Data = (float*)malloc(sizeof(float)*length);
CUT_SAFE_CALL( cutStopTimer(timer) );
printf("Time 1: %f (ms)\n", cutGetTimerValue(timer));
CUT_SAFE_CALL( cutStartTimer(timer) );
for(int i=0; i<length; i++) {
h_Data[i] = i;
}
CUT_SAFE_CALL( cutStopTimer(timer) );
printf("Time 2: %f (ms)\n", cutGetTimerValue(timer));
CUT_SAFE_CALL( cutStartTimer(timer) );
float* d_Data;
CUDA_SAFE_CALL(cudaMalloc(&d_Data, sizeof(float)*length));
CUT_SAFE_CALL( cutStopTimer(timer) );
printf("Time 3: %f (ms)\n", cutGetTimerValue(timer));
CUT_SAFE_CALL( cutStartTimer(timer) );
CUDA_SAFE_CALL(cudaMemcpy(d_Data, h_Data, sizeof(float)*length, cudaMemcpyHostToDevice));
cutilSafeCall( cudaThreadSynchronize() );
CUT_SAFE_CALL( cutStopTimer(timer) );
printf("Time 4: %f (ms)\n", cutGetTimerValue(timer));
CUT_SAFE_CALL( cutStartTimer(timer) );
cudaFree(d_Data);
free(h_Data);
CUT_SAFE_CALL( cutStopTimer(timer) );
printf("Time 5: %f (ms)\n", cutGetTimerValue(timer));
CUT_SAFE_CALL( cutDeleteTimer(timer));
cudaThreadExit();
}