syghの新フラグメント置き場

プログラミングTipsやコード断片の保管場所です。お絵描きもときどき載せます。

CUDAでhalf型

VRAM 使用量や帯域の節約目的で、CUDA や OpenCLカーネルに FP16 半精度浮動小数点数(half)型のデータを渡す場合の話です。
通例 GPGPU で使われる実数は FP32 単精度浮動小数点数(float)型なんですが、half だとその半分のデータ量で済むため、帯域幅の比較的狭いシステム RAM ⇔ VRAM 間の転送負荷を減らすことができます。大容量データを GPU に転送してまとめて大量並列処理してもらう GPGPU では効果を発揮しやすいです。
OpenGLDirect3D で FP16 浮動小数テクスチャを使って HDR レンダリング(High Dynamic Range Rendering)をやったことがある人はすぐに理解できるでしょう。
といっても 16bit 幅の浮動小数型は C/C++ にも CUDA にもない*1ので(OpenCLNVIDIA Cg は拡張機能で組み込みの half 型をサポートしてますが)、入れ物としては 16bit の符号なし整数型(unsigned short, ushort)を代わりに使います。考え方としては int と float の共用体みたいなものです。

CUDA 5.0 は half 型の直接サポートはないものの、組み込みの half(ushort) ⇔ float 変換命令のための関数を持っているので、それをカーネル コード(デバイス コード)にて使用します。
half(ushort) → float 変換には __half2float() を、
float → half(ushort) 変換には __float2half_rn() を使います。
つまり、あくまで直接演算には float を使います。half はストレージ用途のみ。ちなみに DirectX 10 以降の HLSL では演算用の half 型のサポートはなくなっていますOpenGL ES やDirectX 11.1 には低精度シェーダーなんてのもありますが……)。

C++ 側の half(ushort) ⇔ float 変換処理には、Industrial Light & Magic が開発している OpenEXR 用の half 型サポート ライブラリを使うと楽です。
2014年1月時点での同ライブラリの最新版は 2.1.0 となっていますが、half 型を使うだけであれば、
"openexr-2.1.0.tar.gz" のほうではなく、
"ilmbase-2.1.0.tar.gz" のほうをダウンロードして、
Visual Studio を使ってビルドします。
付属の VC プロジェクト&ソリューションは VC7/VC8 向けの古い形式ですが、コンバートすれば VC10 (VC 2010) や VC11 (VC 2012) でもコンパイル&ビルドできる模様。
なお、今回使うのは Half.dll だけなのですが、同梱されている一部のプロジェクトはなぜか "config.windows" ディレクトリへのパスが通っていなかったり、OPENEXR_DLL シンボルや PLATFORM_WINDOWS シンボルが定義されていないせいでビルドが通らないものがあるため、他のライブラリも使いたい場合はプロジェクトファイルの修正が必要となります。
また、HALF_EXPORT 修飾などがメソッド宣言部だけでなく実装部にも施されているため、DLL エクスポートまわりでの警告(LNK4197)もいくつか出ますが、これはとりあえず一応無視できます。
※ちなみに VC の場合クラスのメソッド1つ1つを DLL エクスポートのために declspec で修飾する必要はなく、class を修飾するだけでよいのですが、天下の ILM にしては脇が甘いですね……
なお、IlmBase の float ⇔ half 変換処理の AVX 対応はされていないようなので、そのあたりの高速化の余地はあります。
Intel Core の Ivy Bridge 世代では Half Precision Floating Point 命令をサポートしているらしいです。
http://www.isus.jp/article/performance-special/half-precision-floats/

他にも XNA Math (DirectX Math) には XMConvertFloatToHalf(), XMConvertHalfToFloat() という変換関数が用意されています。XNA Math は別に DirectX アプリケーションでなくても使える独立した算術ライブラリで、SSEによるSIMD演算にも対応しているので積極的に利用すると良いでしょう。

下記は CUDA 5.0 + Visual C++ 2010 SP1 で検証したサンプルコードです。ウィザードが生成したコードを half 型を使って置き換えてるだけですが、ここで注意すべきは half 型の仮数部有効桁数で、2進数で10(+1)桁、10進数で3桁程度になってしまうため、整数部が大きい数値はすぐに桁落ちしてしまいます。試しにサンプル中で指定している入力値の浮動小数リテラルに大きな数(50000.0fくらい)と小さな数(1.0fくらい)を与えてみれば分かるでしょう。half 型は直接演算に使う型ではなく、演算の入出力をダイナミックレンジかつ省メモリで格納する用途(最終的な演算結果が低精度でも十分な用途)にのみ限定して使うようにするべきです。

#include "cuda.h"
#include "cuda_runtime.h"
//#include "cuda_texture_types.h"
#include "device_launch_parameters.h"

#include <cstdio>
#include <cstdint>
#include <conio.h>

#define OPENEXR_DLL // dllimport を有効にするために定義しておく。
#include "OpenEXR_ILMBaseLib/Half/half.h"

#if defined(_M_IX86)
#pragma comment(lib, "OpenEXR_ILMBaseLib\\Win32\\half.lib")
#elif defined(_M_AMD64)
#pragma comment(lib, "OpenEXR_ILMBaseLib\\x64\\half.lib")
#endif

// FP16 を保持するデータ型。
// C11/C++11 では uint16_t を使うべきだが、
// ILM の half ライブラリは unsigned short を使っている。
typedef unsigned short ushort;

inline float ConvertHalf2FloatViaUShort(ushort internalVal)
{
    half newVal;
    newVal.setBits(internalVal);
    return newVal;
}

#if 0
cudaError_t addWithCuda(int *c, const int *a, const int *b, size_t size);
#else
cudaError_t addWithCuda(ushort *c, const ushort *a, const ushort *b, size_t size);
#endif

#if 0
__global__ void addKernel(int *c, const int *a, const int *b)
#else
__global__ void addKernel(ushort *c, const ushort *a, const ushort *b)
#endif
{
    int i = threadIdx.x;
#if 0
    c[i] = a[i] + b[i];
#else
    float fa = __half2float(a[i]);
    float fb = __half2float(b[i]);
    float fc = fa + fb;
    c[i] = __float2half_rn(fc);
#endif

    // CUDA には組み込みの half ⇔ float 変換命令がある。
    // OpenGL/Direct3D の FP16 テクスチャのフェッチ/レンダリングと同様らしい。
    // ちなみに末尾の _rn が何を意味しているかは不明。real number のことなのか?
    // OpenCL の場合は vload_halfN() と vstore_halfN() になる。

#if 0
    ushort fp16Data = 0;
    float fp32Data = __half2float(fp16Data);
    fp16Data = __float2half_rn(fp32Data);
#endif
}

int main()
{
    const int arraySize = 5;
#if 0
    const int a[arraySize] = { 1, 2, 3, 4, 5 };
    const int b[arraySize] = { 10, 20, 30, 40, 50 };
    int c[arraySize] = { 0 };
#else
    // 半精度浮動小数点数の有効桁数は10進数で3桁程度なので、
    // 大きい数と小さい数をそのまま加減算したりすると簡単に桁落ちする。
    // ダイナミックレンジのストレージ用として割り切って使うべき。
    const ushort a[arraySize] =
    {
        half(1.1f).bits(),
        half(2.2f).bits(),
        half(3.3f).bits(),
        half(4.4f).bits(),
        half(5.5f).bits(),
    };
    const ushort b[arraySize] =
    {
        half(10.0f).bits(),
        half(20.0f).bits(),
        half(30.0f).bits(),
        half(40.0f).bits(),
        half(50.0f).bits(),
    };
    ushort c[arraySize] = { 0 };
    //static_assert((sizeof(half) == sizeof(ushort)), "Invalid size!!"); // 構造体パディングの影響があるので保証されない。
#endif

#if 0
    const half myhalf1(1.0f);
    const half myhalf2(10.0f);
    const half myhalf3 = myhalf1 + myhalf2;
    const float result = (float)myhalf3;
#endif

    // Add vectors in parallel.
    cudaError_t cudaStatus = addWithCuda(c, a, b, arraySize);
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "addWithCuda failed!");
        return 1;
    }

#if 0
    printf("{1,2,3,4,5} + {10,20,30,40,50} = {%d,%d,%d,%d,%d}\n",
        c[0], c[1], c[2], c[3], c[4]);
#else
    printf("{1,2,3,4,5} + {10,20,30,40,50} = {%.1f,%.1f,%.1f,%.1f,%.1f}\n",
        ConvertHalf2FloatViaUShort(c[0]),
        ConvertHalf2FloatViaUShort(c[1]),
        ConvertHalf2FloatViaUShort(c[2]),
        ConvertHalf2FloatViaUShort(c[3]),
        ConvertHalf2FloatViaUShort(c[4]));
#endif

    // cudaDeviceReset must be called before exiting in order for profiling and
    // tracing tools such as Nsight and Visual Profiler to show complete traces.
    cudaStatus = cudaDeviceReset();
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaDeviceReset failed!");
        return 1;
    }

    puts("Press any...");
    _getch();

    return 0;
}

// Helper function for using CUDA to add vectors in parallel.
template<typename T> cudaError_t addWithCudaImpl(T *c, const T *a, const T *b, size_t size)
{
    T *dev_a = nullptr;
    T *dev_b = nullptr;
    T *dev_c = nullptr;
    cudaError_t cudaStatus;

    // Choose which GPU to run on, change this on a multi-GPU system.
    cudaStatus = cudaSetDevice(0);
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaSetDevice failed!  Do you have a CUDA-capable GPU installed?");
        goto Error;
    }

    // Allocate GPU buffers for three vectors (two input, one output).
    cudaStatus = cudaMalloc((void**)&dev_c, size * sizeof(T));
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaMalloc failed!");
        goto Error;
    }

    cudaStatus = cudaMalloc((void**)&dev_a, size * sizeof(T));
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaMalloc failed!");
        goto Error;
    }

    cudaStatus = cudaMalloc((void**)&dev_b, size * sizeof(T));
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaMalloc failed!");
        goto Error;
    }

    // Copy input vectors from host memory to GPU buffers.
    cudaStatus = cudaMemcpy(dev_a, a, size * sizeof(T), cudaMemcpyHostToDevice);
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaMemcpy failed!");
        goto Error;
    }

    cudaStatus = cudaMemcpy(dev_b, b, size * sizeof(T), cudaMemcpyHostToDevice);
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaMemcpy failed!");
        goto Error;
    }

    // Launch a kernel on the GPU with one thread for each element.
    addKernel<<<1, static_cast<uint32_t>(size)>>>(dev_c, dev_a, dev_b);

    // cudaDeviceSynchronize waits for the kernel to finish, and returns
    // any errors encountered during the launch.
    cudaStatus = cudaDeviceSynchronize();
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaDeviceSynchronize returned error code %d after launching addKernel!\n", cudaStatus);
        goto Error;
    }

    // Copy output vector from GPU buffer to host memory.
    cudaStatus = cudaMemcpy(c, dev_c, size * sizeof(T), cudaMemcpyDeviceToHost);
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaMemcpy failed!");
        goto Error;
    }

Error:
    cudaFree(dev_c);
    cudaFree(dev_a);
    cudaFree(dev_b);
    
    return cudaStatus;
}

#if 0
cudaError_t addWithCuda(int *c, const int *a, const int *b, size_t size)
{
    return addWithCudaImpl(c, a, b, size);
}
#else
cudaError_t addWithCuda(ushort *c, const ushort *a, const ushort *b, size_t size)
{
    return addWithCudaImpl(c, a, b, size);
}
#endif

*1:CUDA 7.5でhalf/half2型のサポートが追加されたようです。