执行以下代码示例在GeForce GT540M上需要约750毫秒,而在GT330M上相同的代码执行时间为约250毫秒。
将dev_a和dev_b复制到CUDA设备内存需要在GT540M上花费约350毫秒,在GT330M上花费约250毫秒。执行“addCuda”并将其复制回主机在GT540M上需要另外约400毫秒,在GT330M上则为0毫秒。
这不是我预期的结果,所以我检查了设备的属性,并发现GT540M设备在除了多处理器数量外的每个方面都超过或等于GT330M - GT540M有2个,而GT330M有6个。这真的可能吗?如果是这样,它真的会对执行时间产生如此巨大的影响吗?
设备属性如下:
GT540M:
将dev_a和dev_b复制到CUDA设备内存需要在GT540M上花费约350毫秒,在GT330M上花费约250毫秒。执行“addCuda”并将其复制回主机在GT540M上需要另外约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