• 検索結果がありません。

nagasaki 最近の更新履歴 Ocean and Climate Change Lab

N/A
N/A
Protected

Academic year: 2018

シェア "nagasaki 最近の更新履歴 Ocean and Climate Change Lab"

Copied!
81
0
0

読み込み中.... (全文を見る)

全文

(1)

CUDA, OpenCL 実習

濱田 剛

長崎大学工学部

超高速 コン セン

hamada@cis.nagasaki-u.ac.jp

(2)

14451615 •CUDA, OpenCL 実習CUDA OpenCL環境構築

•CUDA入門

•CUDA 使い方

•OpenCL入門

Contents

(3)

実習環境 い

C/C++ 言語* 64-bit inux (CentOS 5)

ン用端 シン (80 )

Core2Duo

GPU

Happy Hacking Keyboard

計算用 シン

GPU (128 1 )

Core2Quad

GeForce9800GTX+ x 2GPUs

(4)

CUDA ( ), OpenCL

CUDA: (Compute unified device architecture)

NVIDIA 提供 NVIDIA GPU 開発環境

無料

C/C++ 文法 GPU

SPMD

セス , gather/scatter operation

OpenCL

AMD, NVIDIA GPU, Cell, CPU 利用 開発

環境

AUBA 汎用的 環境 NVIDIA GPU 以外

利用 う 群

当然対象 階層 深い 複雑

(5)

始 前

GPGPU習得 奥義

ぶ 挫折

(6)

(奥義そ 1)

ュ 最初 べ

ぶ 挫折

(7)

(奥義そ 2)

SDK サン

最初 べ

CUDA 学習 関係

(8)

(奥義そ 3)

理屈 学ぶ前

あえ 動 .

CUDA 理解

(9)

CUDA学習用コ

例題 500個sqrt

// 関数 (GPU 処理部 )

__global__

void calc_on_gpu (float *y)

{

int i = threadIdx.x;

y[i] = sqrt(y[i]);

(10)

CUDA学習用コ

例題 500個sqrt

// 処理 int main() {

int n = 500;

int nb = sizeof(float) * n; float *x = (float*) malloc(nb); float *y;

cudaMalloc( (void**) &y, nb); for(int i=0; i<n; i++) x[i] = i;

cudaMemcpy( y, x, nb, cudaMemcpyHostToDevice);

calc_on_gpu <<<1, 500 >>> ( y ); // 関数 (500 処理) cudaMemcpy(x, y, nb, cudaMemcpyDeviceToHost);

return 0; }

(11)

CUDAコ コン 実行

CUDA SDK CUDA Toolkit

& ンス &後 )

例題 保存 (test.cu)

例題 場所

~/GPGPU-seminar-CUDA/sample1

コン

 nvcc test.cu

(12)

コ 何 い ?

&動作 仕組 い 明'

(13)

CUDA

移動 ス ッ 並列処理

GPU 移動 処理

処理 関数 呼ぶ

Host GPU GPU

(14)

CUDA学習用コ

// 処理 int main() {

int n = 500;

int nb = sizeof(float) * n; float *x = (float*) malloc(nb); float *y;

cudaMalloc( (void**) &y, nb); for(int i=0; i<n; i++) x[i] = i;

cudaMemcpy( y, x, nb, cudaMemcpyHostToDevice); calc_on_gpu <<<1, 500 >>> ( y );

cudaMemcpy(x, y, nb, cudaMemcpyDeviceToHost); return 0;

}

// ~/GPGPU-seminar-CUDA/sample1/test.cu

GNU 確保

転送 ( → GPU

転送

(GPU )

500

関数 処理

(15)

ス ッ 番号

// 関数 (GPU 処理部 )

__global__

void calc_on_gpu (float *y)

{

int i = threadIdx.x; // 各ス 番号

y[i] = sqrt(y[i]);

(16)

CUDA学習用コ

3..個 ス ッ 関数(calc_on_gpu) 並列実行!!

(17)

Block( )

// 処理 int main() {

int n = 500;

int nb = sizeof(float) * n; float *x = (float*) malloc(nb); float *y;

cudaMalloc( (void**) &y, nb); for(int i=0; i<n; i++) x[i] = i;

cudaMemcpy( y, x, nb, cudaMemcpyHostToDevice);

<<<1, 500 >>>

// ~/GPGPU-seminar-CUDA/sample1/test.cu

何???

……… 答え = block

(18)

GPU

1 2 GPU

構成

GPU

30 SM

(Streaming Multiprocessor) 構成

SM, SP

8 SP

(Streaming processor) 構成.

SP 浮動小数点

積和算器

NVIDIA GT200

SP SP SP SP

SP SP SP SP Shared Memory

SM SM SM SM SM SM SM SM SM SM SM SM SM SM SM SM SM SM SM SM SM SM SM SM SM SM SM SM SM SM

GPU

DRAM DRAM DRAM

×

(19)

<<<1, 500>>> 1 500ス

ッ いう意味

学習コ

500 1 SM(Stream

Multiprocessor) 動作

SM SM SM SM SM SM SM SM SM SM SM SM SM SM SM SM SM SM SM SM SM SM SM SM SM SM SM SM SM SM

(20)

多数 ッ 使用 方法

// 処理 int main() {

int n = 60000; // 120 block , 500 thread/block; int nb = sizeof(float) * n;

float *x = (float*) malloc(nb); float *y;

cudaMalloc( (void**) &y, nb); for(int i=0; i<n; i++) x[i] = i;

cudaMemcpy( y, x, nb, cudaMemcpyHostToDevice); calc_on_gpu

<<<120, 500 >>>

( y );

cudaMemcpy(x, y, nb, cudaMemcpyDeviceToHost); return 0;

}

// ~/GPGPU-seminar-CUDA/sample2/test.cu

計算 増や

GPU

ッ 数 増や

(21)

若干修正

__global__

void calc_on_gpu (float *y)

{

int tid = threadIdx.x; // 各ス 番号

int bid = blockIdx.x; // 番号

int i = 500 * bid + tid;

y[i] = sqrt(y[i]);

(22)

<<<120, 500>>>

120 500ス ッ いう意味

SM 6 替え 実行

GPU 全体 /4...ス 動作

SM SM SM SM SM SM SM SM SM SM SM SM SM SM SM SM SM SM SM SM SM SM SM SM SM SM SM SM SM SM

SM SM SM SM SM SM SM SM SM SM SM SM SM SM SM SM SM SM SM SM SM SM SM SM SM SM SM SM SM SM

(23)

CUDA/OpenCL

GPU ンス

(実演)

自身

GPGPU環境 構築

(24)

CUDA /OpenCL ンス

Initlevel 3 変更

X

(25)

CUDA /OpenCL ンス

ンス 起動

(26)

CUDA /OpenCL ンス

(27)

CUDA /OpenCL ンス

(28)

CUDA /OpenCL ンス

(29)

CUDA /OpenCL ンス

(30)

CUDA /OpenCL ンス

(31)

CUDA /OpenCL ンス

(32)

CUDA /OpenCL ンス

(33)

CUDA /OpenCL ンス

GPU ンス 終了 .

明 簡略化 * 再起動

(34)

CUDA

Toolkit & SDK

ンス

(35)

CUDA Toolkit ンス

CUDA SDK ンス 起動

Root

(36)

CUDA ンス

環境設定 開く

以 う CUDA 必要 環境変数 追加

(37)

CUDA SDK ンス

CUDA SDK ンス 起動

追加 環境変数 効

(38)

CUDA SDK ンス

Enter (標準 場所 ンス 場合)

Enter (標準 場所 CUDA Toolkit ンス 場合)

(39)

CUDA SDK ンス

ンス 終了 様子

(40)

CUDA

SDKサン

コン

(41)

サン コ *SBI

コン

多少無駄

cd ~NVIDIA_CUDA_SDK/

make

*サン コ コン

べ 自動 行わ .

最低限必要 コン 作業

Common

(42)

CUDA

(CUDA-GDB)

入門

(43)

CUDA-GDB

GDB CUDA

能拡張

Host GPU 対象

CUDA-GDB 2.2 Linux 64 bit 対応

CUDA-GDB 2.2 公開

(44)

コン 起動

ション付 コン

nvcc -g -G foo.cu -o foo

CUDA-GDB 起動

cuda-gdb foo

(45)

(cuda-gdb) break myFirstKernel

(cuda-gdb) run

(cuda-gdb) next

(cuda-gdb) print idx

(cuda-gdb) finish

(cuda-gdb) quit

(46)

GDB 拡張コ

現在 focus 表示

(cuda-gdb) thread

替え

: (0, 0) ス ッ (1, 0, 0) 替え

(cuda-gdb) thread <<<(0, 0), (1, 0, 0)>>>

全ス 状態一覧

(cuda-gdb) info cuda threads all

(47)

起動 breakpoint設定 実行

(48)

ス ッ 情報表示 ス ッ 実行

(49)

ス ッ 替え ス ッ 実行

(50)

nvcc ション

--debug (-g)

Generate debug information for host code.

--device-debug <level> (-G)

Generate debug information for device code, plus

also specify the optimization level for the device

code in order to control its 'debuggability.

Allowed values for this option: 0,1,2,3.

(51)

OpenCL 入門

(52)

OpenCL

OpenCL

AMD, NVIDIA GPU, Cell, CPU 利用 開発

環境

業界標準 API 策定 行う非営利団体 OpenCLKhronos

Group 仕様策定

現在 Nvidia 実装 公開 & 無料 利用

AUBA 汎用的 環境 NVIDIA GPU 以外

利用 う 群

当然対象 階層 深い 複雑

*考え方 CUDA

* く ス ッ

(53)

CUDA OpenCL 対応

CUDA OpenCL

GPU 移動 必要

多数 実行

識別番号 用い 動作 指定

CUDA OpenCL

OpenCL 手続 多い

(54)

CUDA OpenCL 対応

&ス ッ 識別'

CUDA OpenCL

threadIdx.x get_local_id(0)

blockIdx.x get_group_id(0)

blockDim.x get_local_size(0)

blockIdx.x * blockDim.x + threadIdx.x get_global_size(0)

CUDA OpenCL 識別子 取得

⇒単一 複数 並列実行

CUDA

(55)

サン 明

work-item 識別子IB 使

列 IB番目 要素 IB 値 入 サン

く単純 動作

&以 実演 交え 明 '

(56)

MpenAJ関係 変数

// OpenCL Vars

cl_context cxGPUContext; // OpenCL context

cl_command_queue cqCommandQue; // OpenCL command que cl_device_id* cdDevices; // OpenCL device list

cl_program cpProgram; // OpenCL program cl_kernel ckKernel; // OpenCL kernel

cl_mem cmDevSrcA; // OpenCL device source buffer A cl_mem cmDevSrcB; // OpenCL device source buffer B cl_mem cmDevDst; // OpenCL device destination buffer size_t szGlobalWorkSize; // 1D var for Total # of work items

size_t szLocalWorkSize; // 1D var for # of work items in the work group size_t szParmDataBytes; // Byte size of context information

size_t szKernelLength; // Byte size of kernel code cl_int ciErr1, ciErr2; // Error code var

char* cPathAndName = NULL; // var for full paths to data, src, etc. char* cSourceCL = NULL; // Buffer to hold source for compilation

(57)

work-item数 指定

// Length of float arrays to process int iNumElements = 11444777;

// set and log Global and Local work size dimensions SzLocalWorkSize = 256;

// rounded up to the nearest multiple of the LocalWorkSize

szGlobalWorkSize = shrRoundUp((int)szLocalWorkSize, iNumElements);

(58)

処理内容

// OpenCL Kernel Function for element by element vector addition

__kernel void VectorAdd(__global float* c, __global int iNumElements) {

// get index into global data array int iGID = get_global_id(0);

// bound check (equivalent to the limit on a 'for' loop for standard/serial C code

if (iGID >= iNumElements) {

return; }

// add the vector elements c[iGID] = iGID;

}

(59)

Host側 確保

// Allocate and initialize host arrays

dst = (void *)malloc(sizeof(cl_float) * szGlobalWorkSize);

cl_mem_flags Description

CL_MEM_READ_WRITE カ ー ネ っ て 読み 書き さ CL_MEM_WRITE_ONLY カ ー ネ write only CL_MEM_READ_ONLY カ ー ネ read only

CL_MEM_USE_HOST_PTR ー タ の 格納先と し て ホ ス の を 指定

(60)

スコン ス コ ン ュ

作成

// Create the OpenCL context on a GPU device

cxGPUContext = clCreateContextFromType(0, CL_DEVICE_TYPE_GPU, NULL, NULL, &ciErr1);

// Get the list of GPU devices associated with context

clGetContextInfo( cxGPUContext, CL_CONTEXT_DEVICES, 0, NULL, &szParmDataBytes);

cdDevices = (cl_device_id*)malloc(szParmDataBytes);

clGetContextInfo( cxGPUContext, CL_CONTEXT_DEVICES,

szParmDataBytes, cdDevices, NULL);

// Create a command-queue CqCommandQue =

clCreateCommandQueue(cxGPUContext, cdDevices[0], 0, &ciErr1);

(61)

ス側 確保

// Allocate the OpenCL buffer memory objects on the device GMEM. cmDevDst = clCreateBuffer(cxGPUContext, CL_MEM_WRITE_ONLY,

sizeof(cl_float) * szGlobalWorkSize, NULL, &ciErr2);

(62)

込 コン

// Name of the file with the source code for the computation kernel const char* cSourceFile = "VectorInit.cl";

// Read the OpenCL kernel in from source file

cPathAndName = shrFindFilePath(cSourceFile, argv[0]);

cSourceCL = oclLoadProgSource(cPathAndName, "", &szKernelLength);

// Create the program CpProgram =

ClCreateProgramWithSource( cxGPUContext, 1, (const char **)&cSourceCL,

&szKernelLength, &ciErr1);

// Build the program

clBuildProgram(cpProgram, 0, NULL, NULL, NULL, NULL); // Create the kernel

ckKernel = clCreateKernel(cpProgram, "VectorAdd", &ciErr1);

(63)

引数 指定

起動

// Set the Argument values

clSetKernelArg(ckKernel, 0, sizeof(cl_mem), (void*)&cmDevDst); clSetKernelArg(ckKernel, 1, sizeof(cl_int), (void*)&iNumElements);

// Launch kernel

clEnqueueNDRangeKernel(cqCommandQue, ckKernel, 1, NULL,

&szGlobalWorkSize, &szLocalWorkSize, 0, NULL, NULL);

// Synchronous/blocking read of results, and check accumulated errors ClEnqueueReadBuffer(cqCommandQue, cmDevDst, CL_TRUE, 0,

sizeof(cl_float) * szGlobalWorkSize, dst,

(64)

OpenCL入門

OpenCL CUDA 比べ 非常 柔軟 仕様

言い方 複雑

考え方 CUDA

CUDA 学習時間 無駄

必要

CUDA 一通 学習

OpenCL 近道 思い

(65)

CUDA/OpenCL

く く基 的 入門 行い

機会 あ

高速化編 行い い

思い

(66)

Enjoy the CUDA and OpenCL !

長崎大学 う そ!

(67)

付録 :

CUDA-GDB ンス

(68)

CUDA-GDB ンス

(69)

付録 :

OpenCL ンス

(70)

注意事項

OpenCL 64-bit Linux

正規 方法 動作

(Ubuntu8.1以外 )

以 方法

CentOS5.3-x86_64

裏技 入 い !!

(71)

OpenCL ンス

(72)

OpenCL ンス

(73)

OpenCL ンス

(74)

OpenCL ンス

(75)

OpenCL ンス

(76)

OpenCL ンス

(77)

OpenCL ンス

(78)

OpenCL ンス

(79)

OpenCL ンス

(80)

OpenCL ンス

(81)

OpenCL ンス

参照

関連したドキュメント

It is suggested by our method that most of the quadratic algebras for all St¨ ackel equivalence classes of 3D second order quantum superintegrable systems on conformally flat

BOUNDARY INVARIANTS AND THE BERGMAN KERNEL 153 defining function r = r F , which was constructed in [F2] as a smooth approx- imate solution to the (complex) Monge-Amp` ere

Abstract. Recently, the Riemann problem in the interior domain of a smooth Jordan curve was solved by transforming its boundary condition to a Fredholm integral equation of the

We present sufficient conditions for the existence of solutions to Neu- mann and periodic boundary-value problems for some class of quasilinear ordinary differential equations.. We

It is well known that the inverse problems for the parabolic equations are ill- posed apart from this the inverse problems considered here are not easy to handle due to the

The main problem upon which most of the geometric topology is based is that of classifying and comparing the various supplementary structures that can be imposed on a

Mugnai; Carleman estimates, observability inequalities and null controlla- bility for interior degenerate non smooth parabolic equations, Mem.. Imanuvilov; Controllability of

Then it follows immediately from a suitable version of “Hensel’s Lemma” [cf., e.g., the argument of [4], Lemma 2.1] that S may be obtained, as the notation suggests, as the m A