2016-10-13 2 views
-3

サイズ4のベクトルで算術演算を行う場合、多くのPyPenCLの例があります.PeOpenCLを使ってMac上でAMD GPUを使用して100個の整数を一度に100個の整数に掛けなければならない場合は、最大ベクトルサイズは16であるので、16個以上の整数を並列処理する必要があるこの操作をGPUに求めるにはどうすればいいか知りたい。PyOpenCLを使って100個の整数を別の100個の整数にGPUで並列に乗算する方法は?

私はAMD D500 firepro GPUを持っています。 すべての作業項目(スレッド)が独立してタスクを実行しますか?はい、24個の計算単位があり、各計算単位には1次元の255個の作業項目があり、3次元の作業項目は255,255,255です。私のGPUには6120の独立した作業項目があるのでしょうか?

+0

OpenCLのメモリモデルでは、APIを使用する前に必ず読んでください。 – Dschoni

答えて

0

私は、2つの1次元整数配列のエントリーワイズ乗算の簡単な例を作成しました。 100個の値を乗算する予定がある場合は、データをコピーするなどのオーバーヘッドが大きいため、CPUでこれを行うよりも高速ではありません。 PyOpenCLのドキュメントについて

import pyopencl as cl 
import numpy as np 

#this is compiled by the GPU driver and will be executed on the GPU 
kernelsource = """ 
__kernel void multInt( __global int* res, 
         __global int* a, 
         __global int* b){ 
    int i = get_global_id(0); 
    int N = get_global_size(0); //this is the dimension given as second argument in the kernel execution 
    res[i] = a[i] * b[i]; 
} 
""" 

device = cl.get_platforms()[0].get_devices()[0] 
context = cl.Context([device]) 
program = cl.Program(context, kernelsource).build() 
queue = cl.CommandQueue(context) 

#preparing input data in numpy arrays in local memory (i.e. accessible by the CPU) 
N = 100 
a_local = np.array(range(N)).astype(np.int32) 
b_local = (np.ones(N)*10).astype(np.int32) 

#preparing result buffer in local memory 
res_local = np.zeros(N).astype(np.int32) 

#copy input data to GPU-memory 
a_buf = cl.Buffer(context, cl.mem_flags.READ_ONLY | cl.mem_flags.COPY_HOST_PTR, hostbuf=a_local) 
b_buf = cl.Buffer(context, cl.mem_flags.READ_ONLY | cl.mem_flags.COPY_HOST_PTR, hostbuf=b_local) 
#prepare result buffer in GPU-memory 
res_buf = cl.Buffer(context, cl.mem_flags.WRITE_ONLY, res_local.nbytes) 
#execute previously compiled kernel on GPU 
program.multInt(queue,(N,), None, res_buf, a_buf, b_buf) 
#copy the result from GPU-memory to CPU-memory 
cl.enqueue_copy(queue, res_local, res_buf) 

print("result: {}".format(res_local)) 

:あなたはGPGPUプログラミングとOpenCLののプログラミングの概念の動作原理を理解したら、PyOpenCLは非常に簡単です。

関連する問題