关于随机数生成的速度测试
随机数生成有两种方法,一种是主机上生成拷贝到设备上,另一种是直接主机上调用,在设备上生成:下面就是关于这两种方法的速度测试:
int main(){ clock_t start,finish; int *d_data; int m = 1000; CUDA_CALL(cudaMalloc((void **)&d_data, m*sizeof(int))); int rand1[1000]; start = clock(); memset(rand1,0,m*sizeof(int)); for(int i=0;i<m;i++) rand1[i] = RAND::Rand(100); CUDA_CALL(cudaMemcpy(d_data,rand1,m*sizeof(int),cudaMemcpyHostToDevice)); finish=clock(); printf("%d--%d --- %d\n",start,finish,finish-start); clock_t start1,finish1; start1=clock(); size_t n = 1000; size_t i; curandGenerator_t gen; float *devData; CUDA_CALL(cudaMalloc((void **)&devData, n*sizeof(float))); CURAND_CALL(curandCreateGenerator(&gen, CURAND_RNG_PSEUDO_DEFAULT)); CURAND_CALL(curandSetPseudoRandomGeneratorSeed(gen, 1234)); CURAND_CALL(curandGenerateUniform(gen, devData, n)); CURAND_CALL(curandDestroyGenerator(gen)); finish1=clock(); printf("%d --- %d",finish1,finish1-start1); CUDA_CALL(cudaFree(devData)); getchar(); return 0; }
速度测试结果:
1000000个点 | 1000个点 |
6 | 1 |
6 | 0 |
6 | 0 |
1000000个点 | 1000个点 |
85 | 87 |
85 | 83 |
84 | 83 |