Contextオブジェクトは、クラスツリー内の中心にあり各コンポーネント間が連携を行う中間点となります。
クラス宣言は以下のようになります。
class pyopencl.Context( devices=None, #(1) properties=None, #(2) dev_type=None, #(3) cache_dir=None) #(4)
デバイスリストを指定。 | |
propertiesはキーがcontext_propertiesのタプル(tuple)です。 | |
デバイスの種類(device_typeのプロパティ)を指定。 | |
プログラムインスタンスのビルドの既定キャッシュディレクトリを指定。 |
Contextオブジェクトのコンストラクタの直接呼び出しについては、platformのインスタンスをpropertiesで指定しないと動作しないバグがあるとされており、次の項目のcreate_some_contextを使ってContextのインスタンスを生成することが推奨されています。
create_some_context関数はOpenCLコンテキストを生成します。
詳細は「表:clCreateContext」(Table B.16, “表:clCreateContext”)を参照ください。
pyopencl.create_some_context( interactive=True, answers=None, cache_dir=None)
この関数はContextを生成するためのユーティリティです。初期化を可能な限り省きたい場合に使うことができます。
clCreateContextを使ってOpenCLサポートデバイス情報取得するためのプログラムをコーディングしてみましょう。
import pyopencl as cl platforms = cl.get_platforms() ## not recommended ctx_often_fail = cl.Context( dev_type=cl.device_type.ALL) ## highly recommended ctx_rec = cl.create_some_context() ## recommended ctx = cl.Context( dev_type=cl.device_type.ALL, properties=[(cl.context_properties.PLATFORM, platforms[0])]) ## check with the context object ndev = ctx.get_info(cl.context_info.NUM_DEVICES) print("num_devices: " + str(ndev)) devices = ctx.get_info(cl.context_info.DEVICES) vendor = devices[0].get_info(cl.device_info.VENDOR) print("Vendor: " + str(vendor)) ctxref = ctx.from_int_ptr(ctx.int_ptr) if ctxref == ctx and type(ctxref) is type(ctx): print("pointer identity is confirmed.")
詳しくは「表:cl_context_properties」(Table B.17, “表:cl_context_properties”)を参照ください。
コンテキストに対応するプラットフォームや拡張プラグインを指定できます。
class pyopencl.context_properties
Table 15.1. pyopencl.context_properties
CGL_SHAREGROUP_KHR(cl_khr_gl_sharing) |
EGL_DISPLAY_KHR(cl_khr_gl_sharing) |
GLX_DISPLAY_KHR(cl_khr_gl_sharing) |
GL_CONTEXT_KHR |
OFFLINE_DEVICES_AMD(cl_amd_offline_devices) |
PLATFORM |
WGL_HDC_KHR(cl_khr_gl_sharing) |
context_properties.CL_GL_CONTEXT_KHR context_properties.CL_EGL_DISPLAY_KHR context_properties.CL_GLX_DISPLAY_KHR context_properties.CL_WGL_HDC_KHR context_properties.CL_CGL_SHAREGROUP_KHR context_properties.CL_CGL_SHAREGROUP_APPLE
get_info関数はコンテキストについての情報の取得をします。
詳しくは「表:clGetContextInfo」(Table B.21, “表:clGetContextInfo”)と「表:cl_context_info」(Table B.22, “表:cl_context_info”)を参照ください。
get_info(param) #(1)
context_info.NUM_DEVICESやcontext_info.DEVICES等のcl_context_infoを指定します。 |
Copyright 2018-2019, by Masaki Komatsu