私は大規模な cuda カーネルで作業していましたが、カーネルがスレッドごとに 43 個のレジスタを使用していることに気付きました。何が起こっているのかを理解するために、レジスタの使用法を把握する小さなプログラムを作成しました。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]