我正在编写一个简单的CUDA程序来进行性能测试。
这与向量计算无关,只是用于简单的(并行)字符串转换。
#include <stdio.h>
#include <string.h>
#include <cuda_runtime.h>
#define UCHAR unsigned char
#define UINT32 unsigned long int
#define CTX_SIZE sizeof(aes_context)
#define DOCU_SIZE 4096
#define TOTAL 100000
#define BBLOCK_SIZE 500
UCHAR pH_TXT[DOCU_SIZE * TOTAL];
UCHAR pH_ENC[DOCU_SIZE * TOTAL];
UCHAR* pD_TXT;
UCHAR* pD_ENC;
__global__
void TEST_Encode( UCHAR *a_input, UCHAR *a_output )
{
UCHAR *input;
UCHAR *output;
input = &(a_input[threadIdx.x * DOCU_SIZE]);
output = &(a_output[threadIdx.x * DOCU_SIZE]);
for ( int i = 0 ; i < 30 ; i++ ) {
if ( (input[i] >= 'a') && (input[i] <= 'z') ) {
output[i] = input[i] - 'a' + 'A';
}
else {
output[i] = input[i];
}
}
}
int main(int argc, char** argv)
{
struct cudaDeviceProp xCUDEV;
cudaGetDeviceProperties(&xCUDEV, 0);
// Prepare Source
memset(pH_TXT, 0x00, DOCU_SIZE * TOTAL);
for ( int i = 0 ; i < TOTAL ; i++ ) {
strcpy((char*)pH_TXT + (i * DOCU_SIZE), "hello world, i need an apple.");
}
// Allocate vectors in device memory
cudaMalloc((void**)&pD_TXT, DOCU_SIZE * TOTAL);
cudaMalloc((void**)&pD_ENC, DOCU_SIZE * TOTAL);
// Copy vectors from host memory to device memory
cudaMemcpy(pD_TXT, pH_TXT, DOCU_SIZE * TOTAL, cudaMemcpyHostToDevice);
// Invoke kernel
int threadsPerBlock = BLOCK_SIZE;
int blocksPerGrid = (TOTAL + threadsPerBlock - 1) / threadsPerBlock;
printf("Total Task is %d\n", TOTAL);
printf("block size is %d\n", threadsPerBlock);
printf("repeat cnt is %d\n", blocksPerGrid);
TEST_Encode<<<blocksPerGrid, threadsPerBlock>>>(pD_TXT, pD_ENC);
cudaMemcpy(pH_ENC, pD_ENC, DOCU_SIZE * TOTAL, cudaMemcpyDeviceToHost);
// Free device memory
if (pD_TXT) cudaFree(pD_TXT);
if (pD_ENC) cudaFree(pD_ENC);
cudaDeviceReset();
}当我将BLOCK_SIZE的值从2改为1000时,我得到了以下持续时间(来自NVIDIA Visual Profiler)
TOTAL BLOCKS BLOCK_SIZE Duration(ms)
100000 50000 2 28.22
100000 10000 10 22.223
100000 2000 50 12.3
100000 1000 100 9.624
100000 500 200 10.755
100000 250 400 29.824
100000 200 500 39.67
100000 100 1000 81.268我的图形处理器是GeForce GT520,最大threadsPerBlock值是1024,所以我预测当块是1000时我会获得最好的性能,但上表显示了不同的结果。
我不明白为什么持续时间不是线性的,以及我如何解决这个问题。(或者如何找到优化的块值(最小持续时间)
发布于 2012-05-15 23:28:50
它似乎2,10,50个线程没有利用gpu的能力,因为它的设计是启动更多的线程。
您的卡具有2.1计算能力。
<代码>F29
有两个问题:
1.
你试图在每个线程中占用太多的寄存器内存,如果你的块大小增加,它肯定会被外包给减慢本地内存空间。
2.
使用32的倍数执行测试,因为这是卡的翘曲大小,并且许多内存操作都针对线程大小进行了优化,并且具有倍数的翘曲大小。
因此,如果您每个块仅使用大约1024个线程(在您的情况下为1000个),那么33%的gpu是空闲的,因为每个SM只能分配一个块。
如果您使用以下100%占用大小,会发生什么?
3个数据块
https://stackoverflow.com/questions/10600088
复制相似问题