1
我正在研究一個大的cuda內核,我發現內核每個線程使用43個寄存器。爲了瞭解發生了什麼,我編寫了一個較小的程序來計算註冊使用情況。我注意到,無論何時使用if
,註冊使用率都會增加。小代碼如下:額外的寄存器用法if if
#include <limits.h>
#include <stdio.h>
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <stdint.h>
using namespace std;
__global__ void test_ifs(unsigned int* result){
unsigned int k = 0;
for(int j=0;j<MAX_COMP;j++){
//if(j <= threadIdx.x%MAX_COMP){
k += j;
//}
}
result[threadIdx.x] = k;
}
int main(){
unsigned int* result;
cudaError_t e1 = cudaMalloc((void**) &result, THREADSPERBLOCK*sizeof(unsigned int));
if(e1 == cudaSuccess){
test_ifs<<<1, THREADSPERBLOCK>>>(result);
cudaError_t e2 = cudaGetLastError();
if(e2 == cudaSuccess){
}
else{
cout << "kernel failed to launch" << endl;
}
}
else{
cout << "Failed to allocate results memory" << endl;
}
}
當我編譯該代碼,每個線程使用5個寄存器
ptxas info : Compiling entry function '_Z8test_ifsPj' for 'sm_20'
ptxas info : Function properties for _Z8test_ifsPj
0 bytes stack frame, 0 bytes spill stores, 0 bytes spill loads
ptxas info : Used 5 registers, 40 bytes cmem[0]
但是,如果我去掉if
,每個線程使用8個寄存器。任何人都可以向我解釋發生了什麼事?
ptxas info : Compiling entry function '_Z8test_ifsPj' for 'sm_20'
ptxas info : Function properties for _Z8test_ifsPj
0 bytes stack frame, 0 bytes spill stores, 0 bytes spill loads
ptxas info : Used 8 registers, 40 bytes cmem[0]
MAX_COMP的值是多少?看來你的模運算'threadIdx.x%MAX_COMP'沒有被優化,只是在循環中使用一個寄存器。嘗試在循環外移動計算。 – djmj 2012-07-19 22:23:46
MAX_COMP的值是32. – gmemon 2012-07-20 00:16:06
當您將該計算移到循環之外時會發生什麼? – djmj 2012-07-20 04:43:02