If statement 使用if的额外寄存器用法

If statement 使用if的额外寄存器用法,if-statement,cuda,gpu,register-allocation,ptxas,If Statement,Cuda,Gpu,Register Allocation,Ptxas,我在一个大型cuda内核上工作,我注意到该内核每个线程使用43个寄存器。为了了解发生了什么,我编写了一个较小的程序来计算寄存器的使用情况。我注意到每当我使用if时,寄存器的使用就会增加。小代码如下: #include <limits.h> #include <stdio.h> #include <fstream> #include <iostream> #include <cstdlib> #include <stdint.h&g

我在一个大型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;
  }
}
但是,如果我取消注释
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]

您在本例中看到的行为是由于编译器优化造成的。在简单循环的情况下,可以在编译时计算循环的结果,并用常量替换整个循环代码,而在包含if语句的循环中,循环的结果取决于编译器不知道其值的变量,并且循环必须保持不变

为了证明这一点,让我们看一下内核的一个稍加修改的版本:

#define MAX_COMP (32)

template<unsigned int s>
__global__ void test_ifs(unsigned int * result){
    unsigned int k = 0;
    for(int j=0;j<MAX_COMP;j++){
        switch (s) {
            case 1:
                if (j <= threadIdx.x%MAX_COMP){ k += j; }
                break;            

            case 0:
                { k += j; }
        }
    }
    result[threadIdx.x] = k;
}

template __global__ void test_ifs<0>(unsigned int *);
template __global__ void test_ifs<1>(unsigned int *);
#定义最大补偿(32)
模板
__全局无效测试ifs(无符号整数*结果){
无符号整数k=0;

对于(int j=0;j您在本例中看到的行为是由于编译器优化。在简单循环的情况下,可以在编译时计算循环的结果,并将整个循环代码替换为常量,而在包含if语句的循环中,循环的结果取决于一个变量,该变量的值对于编译器来说是未知的编译器,循环必须保持不变

为了证明这一点,让我们看一下内核的一个稍加修改的版本:

#define MAX_COMP (32)

template<unsigned int s>
__global__ void test_ifs(unsigned int * result){
    unsigned int k = 0;
    for(int j=0;j<MAX_COMP;j++){
        switch (s) {
            case 1:
                if (j <= threadIdx.x%MAX_COMP){ k += j; }
                break;            

            case 0:
                { k += j; }
        }
    }
    result[threadIdx.x] = k;
}

template __global__ void test_ifs<0>(unsigned int *);
template __global__ void test_ifs<1>(unsigned int *);
#定义最大补偿(32)
模板
__全局无效测试ifs(无符号整数*结果){
无符号整数k=0;

对于(int j=0;jmax\u COMP的值是多少?
threadIdx.x%MAX\u COMP
的模运算似乎没有优化到只在循环内使用一个寄存器。尝试将计算移到循环外。当将该计算移到循环外时会发生什么?
MAX\u COMP的值是多少?
odulo操作
threadIdx.x%MAX\u COMP
未优化为仅在循环内使用一个寄存器。请尝试将计算移到循环外。当将该计算移到循环外时会发生什么情况?
    .entry _Z8test_ifsILj0EEvPj (
        .param .u32 __cudaparm__Z8test_ifsILj0EEvPj_result)
    {
    .reg .u16 %rh<3>;
    .reg .u32 %r<6>;
    .loc    14  4   0
$LDWbegin__Z8test_ifsILj0EEvPj:
    .loc    14  16  0
    mov.u32     %r1, 496;  <--- here the loop has been replaced with 496
    ld.param.u32    %r2, [__cudaparm__Z8test_ifsILj0EEvPj_result];
    mov.u16     %rh1, %tid.x;
    mul.wide.u16    %r3, %rh1, 4;
    add.u32     %r4, %r2, %r3;
    st.global.u32   [%r4+0], %r1;
    .loc    14  17  0
    exit;
$LDWend__Z8test_ifsILj0EEvPj:
    } // _Z8test_ifsILj0EEvPj
    .entry _Z8test_ifsILj1EEvPj (
        .param .u32 __cudaparm__Z8test_ifsILj1EEvPj_result)
    {
    .reg .u32 %r<11>;
    .reg .pred %p<4>;
    .loc    14  4   0
$LDWbegin__Z8test_ifsILj1EEvPj:
    cvt.u32.u16     %r1, %tid.x;
    and.b32     %r2, %r1, 31;
    mov.s32     %r3, 0;
    mov.u32     %r4, 0;
$Lt_1_3842:
 //<loop> Loop body line 4, nesting depth: 1, iterations: 32
    .loc    14  7   0
    add.u32     %r5, %r3, %r4;
    setp.le.u32     %p1, %r3, %r2;
    selp.u32    %r4, %r5, %r4, %p1;
    add.s32     %r3, %r3, 1;
    mov.u32     %r6, 32;
    setp.ne.s32     %p2, %r3, %r6;
    @%p2 bra    $Lt_1_3842;
    .loc    14  16  0
    ld.param.u32    %r7, [__cudaparm__Z8test_ifsILj1EEvPj_result];
    mul24.lo.u32    %r8, %r1, 4;
    add.u32     %r9, %r7, %r8;
    st.global.u32   [%r9+0], %r4;
    .loc    14  17  0
    exit;
$LDWend__Z8test_ifsILj1EEvPj:
    } // _Z8test_ifsILj1EEvPj