15.4. Context

Contextオブジェクトは、クラスツリー内の中心にあり各コンポーネント間が連携を行う中間点となります。

クラス宣言は以下のようになります。

class pyopencl.Context(
    devices=None, #(1)
    properties=None, #(2)
    dev_type=None, #(3)
    cache_dir=None) #(4)

(1)

デバイスリストを指定。

(2)

propertiesはキーがcontext_propertiesのタプル(tuple)です。

(3)

デバイスの種類(device_typeのプロパティ)を指定。

(4)

プログラムインスタンスのビルドの既定キャッシュディレクトリを指定。

Contextオブジェクトのコンストラクタの直接呼び出しについては、platformのインスタンスをpropertiesで指定しないと動作しないバグがあるとされており、次の項目のcreate_some_contextを使ってContextのインスタンスを生成することが推奨されています。

15.4.1. create_some_context

create_some_context関数はOpenCLコンテキストを生成します。

Note

詳細は「表: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.")

15.4.2. context_properties

Note

詳しくは「表: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

15.4.3. get_info

get_info関数はコンテキストについての情報の取得をします。

Note

詳しくは「表:clGetContextInfo」(Table B.21, “表:clGetContextInfo”)と「表:cl_context_info」(Table B.22, “表:cl_context_info”)を参照ください。

get_info(param) #(1)

(1)

context_info.NUM_DEVICESやcontext_info.DEVICES等のcl_context_infoを指定します。

15.4.4. context_info

Note

詳細は「表:cl_context_info」(Table B.22, “表:cl_context_info”)を参照ください。

class pyopencl.context_info

Table 15.2. pyopencl.context_info

DEVICES
INTEROP_USER_SYNC
NUM_DEVICES
PROPERTIES

Copyright 2018-2019, by Masaki Komatsu