本文介绍了是内联PTX汇编代码强大吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到一些代码示例,人们在C代码中使用内联PTX汇编代码。 Doc在CUDA工具包中提到PTX是强大的,为什么会这样?

I saw some code samples where people use inline PTX assembly code in C code. Doc in CUDA toolkit mentions that PTX is powerful, why is it so? What advantage we get if we use such codes in our C code?

推荐答案

Inline PTX允许您访问不通过CUDA公开的指令intrinsincs,并允许应用编译器缺少或语言规范禁止的优化。对于使用内联PTX的有用示例,请参阅:

Inline PTX gives you access to instructions not exposed via CUDA intrinsincs, and lets you apply optimizations that are either lacking in the compiler or prohibited by language specifications. For a worked example where use of inline PTX is advantageous, see:128 bit integer on cuda?

使用内联PTX的128位添加只需要四个指令,因为它可以直接访问进位标志。作为HLL,C / C ++不具有用于进位标志的表示,因为给定的硬件平台可以没有进位标志(例如MIPS),单个进位标志(例如x86,sm_2x)或甚至多个进位标志。与128位加法和减法的4指令PTX版本相反,这些操作可以在C中编码如下:

The 128-bit addition using inline PTX requires just four instructions, since it has direct access to the carry flag. As a HLL, C/C++ does not have a representation for a carry flag, as a given hardware platform may have no carry flag (e.g. MIPS), a single carry flag (e.g. x86, sm_2x), or even multiple carry flags. In contrast to the 4-instruction PTX versions of 128-bit addition and subtraction, these operations might be coded in C as follows:

#define SUBCcc(a,b,cy,t0,t1,t2) \
  (t0=(b)+cy, t1=(a), cy=t0<cy, t2=t1<t0, cy=cy+t2, t1-t0)
#define SUBcc(a,b,cy,t0,t1) \
  (t0=(b), t1=(a), cy=t1<t0, t1-t0)
#define SUBC(a,b,cy,t0,t1) \
  (t0=(b)+cy, t1=(a), t1-t0)
#define ADDCcc(a,b,cy,t0,t1) \
  (t0=(b)+cy, t1=(a), cy=t0<cy, t0=t0+t1, t1=t0<t1, cy=cy+t1, t0=t0)
#define ADDcc(a,b,cy,t0,t1) \
  (t0=(b), t1=(a), t0=t0+t1, cy=t0<t1, t0=t0)
#define ADDC(a,b,cy,t0,t1) \
  (t0=(b)+cy, t1=(a), t0+t1)

unsigned int cy, t0, t1, t2;

res.x = ADDcc  (augend.x, addend.x, cy, t0, t1);
res.y = ADDCcc (augend.y, addend.y, cy, t0, t1);
res.z = ADDCcc (augend.z, addend.z, cy, t0, t1);
res.w = ADDC   (augend.w, addend.w, cy, t0, t1);

res.x = SUBcc  (minuend.x, subtrahend.x, cy, t0, t1);
res.y = SUBCcc (minuend.y, subtrahend.y, cy, t0, t1, t2);
res.z = SUBCcc (minuend.z, subtrahend.z, cy, t0, t1, t2);
res.w = SUBC   (minuend.w, subtrahend.w, cy, t0, t1);

上面的加法和减法可能编译为相应指令使用的指令数的三到四倍内联PTX版本。

The addition and subtraction above probably compile to about three to four times the number of instructions used by the corresponding inline PTX version.

这篇关于是内联PTX汇编代码强大吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-11 04:55