ElementwiseKernelは一言でいえばお手軽カーネルツールです。引数や処理ロジックをC言語で記述しますが、それ以外はPythonのインターフェースをとるため、とっつきやすいかもしれません。
class pyopencl.elementwise.ElementwiseKernel( context, arguments, #(1) operation, #(2) name="kernel", preamble="", options=[])
参考までに実装例を以下に掲載します。
import numpy as np import pyopencl as cl import pyopencl.array as clarr import warnings from pyopencl.elementwise import ElementwiseKernel import os os.environ['PYOPENCL_COMPILER_OUTPUT'] = '1' a = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]).astype(np.float32) b = np.array([10, 9, 8, 7, 6, 5, 4, 3, 2, 1]).astype(np.float32) platforms = cl.get_platforms() ctx = cl.Context([platforms[0].get_devices(cl.device_type.GPU)[0]]) queue = cl.CommandQueue(ctx) a_mem = clarr.to_device(queue, a) b_mem = clarr.to_device(queue, b) multiply = ElementwiseKernel( ctx, "float* a, float* b, float* out", #(1) "out[i] = a[i] * b[i]", #(2) "multiply") #(3) out_mem = clarr.empty_like(a_mem) multiply(a_mem, b_mem, out_mem) print(out_mem)
arguments(引数)を宣言します。C99に準拠したOpenCL-C言語で記述します。 | |
Map処理を指定します。C99に準拠したOpenCL-C言語で記述します。 | |
カーネル関数の名前を指定します。(Cの名前空間で固有であれば何でも構いません。) |
出力.
/Library/Frameworks/Python.framework/Versions/3.5/bin/python3.5 /Users/komatsu/PycharmProjects/MyPythonProject/CLElementWiseKernelTest.py [ 10. 18. 24. 28. 30. 30. 28. 24. 18. 10.]
Copyright 2018-2019, by Masaki Komatsu