使用CUDA添加大整数

11 c cuda gpgpu thrust

我一直在GPU上开发一种加密算法,目前坚持使用算法来执行大整数加法.大整数以通常的方式表示为一堆32位字.

例如,我们可以使用一个线程来添加两个32位字.为简单起见,假设要添加的数字具有相同的长度和每个块的线程数==字数.然后:

__global__ void add_kernel(int *C, const int *A, const int *B) {
     int x = A[threadIdx.x];
     int y = B[threadIdx.x];
     int z = x + y;
     int carry = (z < x);
     /** do carry propagation in parallel somehow ? */
     ............

     z = z + newcarry; // update the resulting words after carry propagation
     C[threadIdx.x] = z;
 }
Run Code Online (Sandbox Code Playgroud)

我很确定有一种方法可以通过一些棘手的减少程序来进行传播,但是无法弄明白.

我看了一下CUDA推力扩展但是大整数包似乎还没有实现.也许有人可以给我一个提示如何在CUDA上做到这一点?

小智 8

你是对的,进位传播可以通过前缀和计算完成,但是为这个操作定义二进制函数并证明它是关联的(并行前缀和需要)有点棘手.事实上,该算法(在理论上)用于Carry-lookahead加法器.

假设我们有两个大整数a [0..n-1]和b [0..n-1].然后我们计算(i = 0..n-1):

s[i] = a[i] + b[i]l;
carryin[i] = (s[i] < a[i]);
Run Code Online (Sandbox Code Playgroud)

我们定义了两个函数:

generate[i] = carryin[i];
propagate[i] = (s[i] == 0xffffffff);
Run Code Online (Sandbox Code Playgroud)

具有非常直观的含义:generate [i] == 1表示在位置i处生成进位,而传播[i] == 1表示进位将从位置(i-1)传播到(i + 1).我们的目标是计算用于更新结果和[0..n-1]的函数进位[0..n-1].carryout可以递归计算如下:

carryout[i] = generate[i] OR (propagate[i] AND carryout[i-1])
carryout[0] = 0
Run Code Online (Sandbox Code Playgroud)

这里,carryout [i] == 1如果在位置i生成进位,则有时更早生成并传播到位置i.最后,我们更新结果总和:

s[i] = s[i] + carryout[i-1];  for i = 1..n-1
carry = carryout[n-1];
Run Code Online (Sandbox Code Playgroud)

现在,证明进位函数确实是二进制关联并因此适用并行前缀和计算是非常简单的.要在CUDA上实现这一点,我们可以在单个变量中合并标记'generate'和'propagate',因为它们是互斥的,即:

cy[i] = (s[i] == -1u ? -1u : 0) | carryin[i];
Run Code Online (Sandbox Code Playgroud)

换一种说法,

cy[i] = 0xffffffff  if propagate[i]
cy[i] = 1           if generate[i]
cy[u] = 0           otherwise
Run Code Online (Sandbox Code Playgroud)

然后,可以验证以下公式计算进位函数的前缀和:

cy[i] = max((int)cy[i], (int)cy[k]) & cy[i];
Run Code Online (Sandbox Code Playgroud)

对于所有k <i.下面的示例代码显示了2048字整数的大量添加.这里我使用了512个线程的CUDA块:

// add & output carry flag
#define UADDO(c, a, b) \ 
     asm volatile("add.cc.u32 %0, %1, %2;" : "=r"(c) : "r"(a) , "r"(b));
// add with carry & output carry flag
#define UADDC(c, a, b) \ 
     asm volatile("addc.cc.u32 %0, %1, %2;" : "=r"(c) : "r"(a) , "r"(b));

#define WS 32

__global__ void bignum_add(unsigned *g_R, const unsigned *g_A,const unsigned *g_B) {

extern __shared__ unsigned shared[];
unsigned *r = shared; 

const unsigned N_THIDS = 512;
unsigned thid = threadIdx.x, thid_in_warp = thid & WS-1;
unsigned ofs, cf;

uint4 a = ((const uint4 *)g_A)[thid],
      b = ((const uint4 *)g_B)[thid];

UADDO(a.x, a.x, b.x) // adding 128-bit chunks with carry flag
UADDC(a.y, a.y, b.y)
UADDC(a.z, a.z, b.z)
UADDC(a.w, a.w, b.w)
UADDC(cf, 0, 0) // save carry-out

// memory consumption: 49 * N_THIDS / 64
// use "alternating" data layout for each pair of warps
volatile short *scan = (volatile short *)(r + 16 + thid_in_warp +
        49 * (thid / 64)) + ((thid / 32) & 1);

scan[-32] = -1; // put identity element
if(a.x == -1u && a.x == a.y && a.x == a.z && a.x == a.w)
    // this indicates that carry will propagate through the number
    cf = -1u;

// "Hillis-and-Steele-style" reduction 
scan[0] = cf;
cf = max((int)cf, (int)scan[-2]) & cf;
scan[0] = cf;
cf = max((int)cf, (int)scan[-4]) & cf;
scan[0] = cf;
cf = max((int)cf, (int)scan[-8]) & cf;
scan[0] = cf;
cf = max((int)cf, (int)scan[-16]) & cf;
scan[0] = cf;
cf = max((int)cf, (int)scan[-32]) & cf;
scan[0] = cf;

int *postscan = (int *)r + 16 + 49 * (N_THIDS / 64);
if(thid_in_warp == WS - 1) // scan leading carry-outs once again
    postscan[thid >> 5] = cf;

__syncthreads();

if(thid < N_THIDS / 32) {
    volatile int *t = (volatile int *)postscan + thid;
    t[-8] = -1; // load identity symbol
    cf = t[0];
    cf = max((int)cf, (int)t[-1]) & cf;
    t[0] = cf;
    cf = max((int)cf, (int)t[-2]) & cf;
    t[0] = cf;
    cf = max((int)cf, (int)t[-4]) & cf;
    t[0] = cf;
}
__syncthreads();

cf = scan[0];
int ps = postscan[(int)((thid >> 5) - 1)]; // postscan[-1] equals to -1
scan[0] = max((int)cf, ps) & cf; // update carry flags within warps
cf = scan[-2];

if(thid_in_warp == 0)
    cf = ps;
if((int)cf < 0)
    cf = 0;

UADDO(a.x, a.x, cf) // propagate carry flag if needed
UADDC(a.y, a.y, 0)
UADDC(a.z, a.z, 0)
UADDC(a.w, a.w, 0)
((uint4 *)g_R)[thid] = a;
}
Run Code Online (Sandbox Code Playgroud)

请注意,由于CUDA 4.0具有相应的内在函数,因此可能不再需要宏UADDO/UADDC(但我不完全确定).

还要注意的是,虽然并行缩减非常快,但如果需要连续添加几个大整数,最好使用一些冗余表示(在上面的注释中建议),即首先累加添加的结果64位字,然后在"一次扫描"的最后执行一次进位传播.