在GeForce GT540M上,执行以下代码示例大约需要750毫秒,而在GT330M上,相同的代码需要执行大约250毫秒。
在GT540M上,将dev_a和dev_b复制到CUDA设备内存需要大约350毫秒,大约需要250毫秒。在GT540M上执行“addCuda”并将其复制回主机需要花费大约400毫秒,而在GT330M上需要大约0毫秒。
这不是我所期望的,因此我检查了设备的属性,发现GT540M设备在所有方面都超过或等于GT330M,除了多处理器的数量之外-GT540M有2个,而GT330M有6个。真的可以吗?如果是这样,它真的会对执行时间产生如此大的影响吗?
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#define T 512
#define N 60000*T
__global__ void addCuda(double *a, double *b, double *c) {
int tid = threadIdx.x + blockIdx.x * blockDim.x;
if(tid < N) {
c[tid] = sqrt(fabs(a[tid] * b[tid] / 12.34567)) * cos(a[tid]);
}
}
int main() {
double *dev_a, *dev_b, *dev_c;
double* a = (double*)malloc(N*sizeof(double));
double* b = (double*)malloc(N*sizeof(double));
double* c = (double*)malloc(N*sizeof(double));
printf("Filling arrays (CPU)...\n\n");
int i;
for(i = 0; i < N; i++) {
a[i] = (double)-i;
b[i] = (double)i;
}
int timer = clock();
cudaMalloc((void**) &dev_a, N*sizeof(double));
cudaMalloc((void**) &dev_b, N*sizeof(double));
cudaMalloc((void**) &dev_c, N*sizeof(double));
cudaMemcpy(dev_a, a, N*sizeof(double), cudaMemcpyHostToDevice);
cudaMemcpy(dev_b, b, N*sizeof(double), cudaMemcpyHostToDevice);
printf("Memcpy time: %d\n", clock() - timer);
addCuda<<<(N+T-1)/T,T>>>(dev_a, dev_b, dev_c);
cudaMemcpy(c, dev_c, N*sizeof(double), cudaMemcpyDeviceToHost);
printf("Time elapsed: %d\n", clock() - timer);
cudaFree(dev_a);
cudaFree(dev_b);
cudaFree(dev_c);
free(a);
free(b);
free(c);
return 0;
}
设备的设备属性:
GT540M:
Major revision number: 2
Minor revision number: 1
Name: GeForce GT 540M
Total global memory: 1073741824
Total shared memory per block: 49152
Total registers per block: 32768
Warp size: 32
Maximum memory pitch: 2147483647
Maximum threads per block: 1024
Maximum dimension 0 of block: 1024
Maximum dimension 1 of block: 1024
Maximum dimension 2 of block: 64
Maximum dimension 0 of grid: 65535
Maximum dimension 1 of grid: 65535
Maximum dimension 2 of grid: 65535
Clock rate: 1344000
Total constant memory: 65536
Texture alignment: 512
Concurrent copy and execution: Yes
Number of multiprocessors: 2
Kernel execution timeout: Yes
GT330M
Major revision number: 1
Minor revision number: 2
Name: GeForce GT 330M
Total global memory: 268435456
Total shared memory per block: 16384
Total registers per block: 16384
Warp size: 32
Maximum memory pitch: 2147483647
Maximum threads per block: 512
Maximum dimension 0 of block: 512
Maximum dimension 1 of block: 512
Maximum dimension 2 of block: 64
Maximum dimension 0 of grid: 65535
Maximum dimension 1 of grid: 65535
Maximum dimension 2 of grid: 1
Clock rate: 1100000
Total constant memory: 65536
Texture alignment: 256
Concurrent copy and execution: Yes
Number of multiprocessors: 6
Kernel execution timeout: Yes
最佳答案
我认为从设备到主机的复制不可能是〜0ms。我建议检查该副本是否存在错误
关于c - GT540M上的低性能CUDA代码,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9258755/