[PR]
×
[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。
プログラミング、3DCGとその他いろいろについて
[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。
このプログラムは実はあまりGPUの並列処理っぽくはない、シングルスレッドなプログラムです。__kernel void myKernelFunction(__global float* numbers){numbers[0] = 3;numbers[1] = 4;numbers[2] = 5;}
cl_program clCreateProgramWithSource( cl_context context, cl_uint count, const char **strings, const size_t *lengths, cl_int *errcode_ret )
cl_int clReleaseProgram(cl_program program)
cl_int clBuildProgram( cl_program program, cl_uint num_devices, const cl_device_id *device_list, const char *options, void (CL_CALLBACK *pfn_notify)(cl_program program, void *user_data), void *user_data )
using System;class MyProgram{static void Main(){IntPtr device = getDevices(getPlatforms()[0], DeviceType.Default)[0];Context context = new Context(device);CommandQueue commandQueue = new CommandQueue(context, device);Program program = new Program(context, System.IO.File.ReadAllText("myKernelProgram.cl"));program.Build(device);Buffer buffer = Buffer.FromCopiedHostMemory(context, new float[] { 1, 2, 3 });float[] readBack = new float[3];commandQueue.ReadBuffer(buffer, readBack);foreach (var number in readBack){Console.WriteLine(number);}}private static IntPtr[] getDevices(IntPtr platform, DeviceType deviceType){int deviceCount;OpenCLFunctions.clGetDeviceIDs(platform, deviceType, 0, null, out deviceCount);IntPtr[] result = new IntPtr[deviceCount];OpenCLFunctions.clGetDeviceIDs(platform, deviceType, deviceCount, result, out deviceCount);return result;}private static IntPtr[] getPlatforms(){int platformCount;OpenCLFunctions.clGetPlatformIDs(0, null, out platformCount);IntPtr[] result = new IntPtr[platformCount];OpenCLFunctions.clGetPlatformIDs(platformCount, result, out platformCount);return result;}}
using System;using System.Runtime.InteropServices;class Context{public IntPtr InternalPointer { get; private set; }public Context(params IntPtr[] devices){int error;InternalPointer = OpenCLFunctions.clCreateContext(null,devices.Length,devices,null,IntPtr.Zero,out error);}~Context(){OpenCLFunctions.clReleaseContext(InternalPointer);}}class CommandQueue{public IntPtr InternalPointer { get; private set; }public CommandQueue(Context context, IntPtr device){int error;InternalPointer = OpenCLFunctions.clCreateCommandQueue(context.InternalPointer,device,0,out error);}~CommandQueue(){OpenCLFunctions.clReleaseCommandQueue(InternalPointer);}public void ReadBuffer<T>(Buffer buffer, T[] systemBuffer) where T : struct{GCHandle handle = GCHandle.Alloc(systemBuffer, GCHandleType.Pinned);OpenCLFunctions.clEnqueueReadBuffer(InternalPointer,buffer.InternalPointer,true,0,Math.Min(buffer.SizeInBytes, Marshal.SizeOf(typeof(T)) * systemBuffer.Length),handle.AddrOfPinnedObject(),0,IntPtr.Zero,IntPtr.Zero);handle.Free();}}class Buffer{public IntPtr InternalPointer { get; private set; }public int SizeInBytes { get; private set; }private Buffer() { }~Buffer(){OpenCLFunctions.clReleaseMemObject(InternalPointer);}public static Buffer FromCopiedHostMemory<T>(Context context, T[] initialData) where T : struct{Buffer result = new Buffer();result.SizeInBytes = Marshal.SizeOf(typeof(T)) * initialData.Length;int errorCode;GCHandle handle = GCHandle.Alloc(initialData, GCHandleType.Pinned);result.InternalPointer = OpenCLFunctions.clCreateBuffer(context.InternalPointer,MemoryFlags.CopyHostMemory,result.SizeInBytes,handle.AddrOfPinnedObject(),out errorCode);handle.Free();return result;}}class Program{public IntPtr InternalPointer { get; private set; }public Program(Context context, params string[] sources){int errorCode;InternalPointer = OpenCLFunctions.clCreateProgramWithSource(context.InternalPointer,sources.Length,sources,null,out errorCode);}~Program(){OpenCLFunctions.clReleaseProgram(InternalPointer);}public void Build(params IntPtr[] devices){OpenCLFunctions.clBuildProgram(InternalPointer,devices.Length,devices,null,null,IntPtr.Zero);}}
using System;using System.Runtime.InteropServices;static class OpenCLFunctions{[DllImport("OpenCL.dll")]public static extern int clGetPlatformIDs(int entryCount, IntPtr[] platforms, out int platformCount);[DllImport("OpenCL.dll")]public static extern int clGetDeviceIDs(IntPtr platform,DeviceType deviceType,int entryCount,IntPtr[] devices,out int deviceCount);[DllImport("OpenCL.dll")]public static extern IntPtr clCreateContext(IntPtr[] properties,int deviceCount,IntPtr[] devices,NotifyContextCreated pfnNotify,IntPtr userData,out int errorCode);[DllImport("OpenCL.dll")]public static extern int clReleaseContext(IntPtr context);[DllImport("OpenCL.dll")]public static extern IntPtr clCreateCommandQueue(IntPtr context,IntPtr device,long properties,out int errorCodeReturn);[DllImport("OpenCL.dll")]public static extern int clReleaseCommandQueue(IntPtr commandQueue);[DllImport("OpenCL.dll")]public static extern IntPtr clCreateBuffer(IntPtr context,MemoryFlags allocationAndUsage,int sizeInBytes,IntPtr hostPtr,//out int errorCodeReturn);[DllImport("OpenCL.dll")]public static extern int clReleaseMemObject(IntPtr memoryObject);[DllImport("OpenCL.dll")]public static extern int clEnqueueReadBuffer(IntPtr commandQueue,IntPtr buffer,bool isBlocking,int offset,int sizeInBytes,IntPtr result,int numberOfEventsInWaitList,IntPtr eventWaitList,IntPtr eventObjectOut);[DllImport("OpenCL.dll")]public static extern IntPtr clCreateProgramWithSource(IntPtr context,int count,string[] programSources,int[] sourceLengths,out int errorCode);[DllImport("OpenCL.dll")]public static extern int clBuildProgram(IntPtr program,int deviceCount,IntPtr[] deviceList,string buildOptions,NotifyProgramBuilt notify,IntPtr userData);[DllImport("OpenCL.dll")]public static extern int clReleaseProgram(IntPtr program);}delegate void NotifyContextCreated(string errorInfo, IntPtr privateInfoSize, int cb, IntPtr userData);delegate void NotifyProgramBuilt(IntPtr program, IntPtr userData);enum DeviceType : long{Default = (1 << 0),Cpu = (1 << 1),Gpu = (1 << 2),Accelerator = (1 << 3),All = 0xFFFFFFFF}enum MemoryFlags : long{ReadWrite = (1 << 0),WriteOnly = (1 << 1),ReadOnly = (1 << 2),UseHostMemory = (1 << 3),HostAccessible = (1 << 4),CopyHostMemory = (1 << 5)}
__kernel void myKernelFunction(__global float* numbers){numbers[0] = 3;numbers[1] = 4;numbers[2] = 5;}
cl_mem clCreateBuffer( cl_context context, cl_mem_flags flags, size_t size, void *host_ptr, cl_int *errcode_ret )
cl_mem_flags | 値 | 解説 |
CL_MEM_READ_WRITE | 1 << 0 | メモリがGPUによって読み込まれもするし書きこまれもすることを表します。これがデフォルトです。 |
CL_MEM_WRITE_ONLY | 1 << 1 | メモリがGPUによって書きこまれはするけれど読み込まれることはないということを表します。これを指定してデータを読み込もうとしたらどうなるかは未定義です。 |
CL_MEM_READ_ONLY | 1 << 2 | メモリが読み込み専用であることを表します。これを指定してデータを書きこもうとしたらどうなるかは未定義です。 |
CL_MEM_USE_HOST_PTR | 1 << 3 | このフラグはhost_ptrがNULL出ないときに限り有効です。もしこの値が指定されたら、アプリケーションはOpenCLの実装に対してhost_ptrで指定されたメモリをこのメモリオブジェクト(バッファ)のストレージとして使用して欲しいということを意味します。 |
CL_MEM_ALLOC_HOST_PTR | 1 << 4 | CPUからアクセス可能なメモリとしてメモリを生成してほしいことを表します。 この値とCL_MEM_USE_HOST_PTRは同時に指定できません。 |
CL_MEM_COPY_HOST_PTR | 1 << 5 | このフラグはhost_ptrがNULL出ないときに限って有効です。この値が指定されたら、host_ptrに書かれたデータをコピーしてメモリがGPUに確保されます。 この値とCL_MEM_USE_HOST_PTRは同時に指定できません。 この値はCL_MEM_ALLOC_HOST_PTRと一緒に使うことができます。 |
cl_int clReleaseMemObject(cl_mem memobj)
cl_int clEnqueueReadBuffer( cl_command_queue command_queue, cl_mem buffer, cl_bool blocking_read, size_t offset, size_t cb, void *ptr, cl_uint num_events_in_wait_list, const cl_event *event_wait_list, cl_event *event )
using System;class Program{static void Main(){IntPtr device = getDevices(getPlatforms()[0], DeviceType.Default)[0];Context context = new Context(device);CommandQueue commandQueue = new CommandQueue(context, device);Buffer buffer = Buffer.FromCopiedHostMemory(context, new float[] { 1, 2, 3 });float[] readBack = new float[3];commandQueue.ReadBuffer(buffer, readBack);foreach (var number in readBack){Console.WriteLine(number);}}private static IntPtr[] getDevices(IntPtr platform, DeviceType deviceType){int deviceCount;OpenCLFunctions.clGetDeviceIDs(platform, deviceType, 0, null, out deviceCount);IntPtr[] result = new IntPtr[deviceCount];OpenCLFunctions.clGetDeviceIDs(platform, deviceType, deviceCount, result, out deviceCount);return result;}private static IntPtr[] getPlatforms(){int platformCount;OpenCLFunctions.clGetPlatformIDs(0, null, out platformCount);IntPtr[] result = new IntPtr[platformCount];OpenCLFunctions.clGetPlatformIDs(platformCount, result, out platformCount);return result;}}
using System;using System.Runtime.InteropServices;class Context{public IntPtr InternalPointer { get; private set; }public Context(params IntPtr[] devices){int error;InternalPointer = OpenCLFunctions.clCreateContext(null,devices.Length,devices,null,IntPtr.Zero,out error);}~Context(){OpenCLFunctions.clReleaseContext(InternalPointer);}}class CommandQueue{public IntPtr InternalPointer { get; private set; }public CommandQueue(Context context, IntPtr device){int error;InternalPointer = OpenCLFunctions.clCreateCommandQueue(context.InternalPointer,device,0,out error);}~CommandQueue(){OpenCLFunctions.clReleaseCommandQueue(InternalPointer);}public void ReadBuffer<T>(Buffer buffer, T[] systemBuffer) where T : struct{GCHandle handle = GCHandle.Alloc(systemBuffer, GCHandleType.Pinned);OpenCLFunctions.clEnqueueReadBuffer(InternalPointer,buffer.InternalPointer,true,0,Math.Min(buffer.SizeInBytes, Marshal.SizeOf(typeof(T)) * systemBuffer.Length),handle.AddrOfPinnedObject(),0,IntPtr.Zero,IntPtr.Zero);handle.Free();}}class Buffer{public IntPtr InternalPointer { get; private set; }public int SizeInBytes { get; private set; }private Buffer() { }~Buffer(){OpenCLFunctions.clReleaseMemObject(InternalPointer);}public static Buffer FromCopiedHostMemory<T>(Context context, T[] initialData) where T : struct{Buffer result = new Buffer();result.SizeInBytes = Marshal.SizeOf(typeof(T)) * initialData.Length;int errorCode;GCHandle handle = GCHandle.Alloc(initialData, GCHandleType.Pinned);result.InternalPointer = OpenCLFunctions.clCreateBuffer(context.InternalPointer,MemoryFlags.CopyHostMemory,result.SizeInBytes,handle.AddrOfPinnedObject(),out errorCode);handle.Free();return result;}}
using System;using System.Runtime.InteropServices;static class OpenCLFunctions{[DllImport("OpenCL.dll")]public static extern int clGetPlatformIDs(int entryCount, IntPtr[] platforms, out int platformCount);[DllImport("OpenCL.dll")]public static extern int clGetDeviceIDs(IntPtr platform,DeviceType deviceType,int entryCount,IntPtr[] devices,out int deviceCount);[DllImport("OpenCL.dll")]public static extern IntPtr clCreateContext(IntPtr[] properties,int deviceCount,IntPtr[] devices,NotifyCallback pfnNotify,IntPtr userData,out int errorCode);[DllImport("OpenCL.dll")]public static extern int clReleaseContext(IntPtr context);[DllImport("OpenCL.dll")]public static extern IntPtr clCreateCommandQueue(IntPtr context,IntPtr device,long properties,out int errorCodeReturn);[DllImport("OpenCL.dll")]public static extern int clReleaseCommandQueue(IntPtr commandQueue);[DllImport("OpenCL.dll")]public static extern IntPtr clCreateBuffer(IntPtr context,MemoryFlags allocationAndUsage,int sizeInBytes,IntPtr hostPtr,out int errorCodeReturn);[DllImport("OpenCL.dll")]public static extern int clReleaseMemObject(IntPtr memoryObject);[DllImport("OpenCL.dll")]public static extern int clEnqueueReadBuffer(IntPtr commandQueue,IntPtr buffer,bool isBlocking,int offset,int sizeInBytes,IntPtr result,int numberOfEventsInWaitList,IntPtr eventWaitList,IntPtr eventObjectOut);}delegate void NotifyCallback(string errorInfo, IntPtr privateInfoSize, int cb, IntPtr userData);enum DeviceType : long{Default = (1 << 0),Cpu = (1 << 1),Gpu = (1 << 2),Accelerator = (1 << 3),All = 0xFFFFFFFF}enum MemoryFlags : long{ReadWrite = (1 << 0),WriteOnly = (1 << 1),ReadOnly = (1 << 2),UseHostMemory = (1 << 3),HostAccessible = (1 << 4),CopyHostMemory = (1 << 5)}
cl_command_queue clCreateCommandQueue( cl_context context, cl_device_id device, cl_command_queue_properties properties, cl_int *errcode_ret )
コマンドキュープロパティ | 解説 |
CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE | コマンドキュー中のコマンド実行をout-of-order(並列実行)にします。そうでない場合はin-orderで、これはコマンド実行の順番を入れ替え無いモードです。 |
CL_QUEUE_PROFILING_ENABLE | コマンドキュー内のcommandのプロファイリングを有効にします。詳細はclGetEventProfilingInfo関数を見てください。 |
名前 | 値 | 解説 |
CL_SUCCESS | 0 | 関数は成功しました。 |
CL_INVALID_CONTEXT | -34 | contextが無効です。 |
CL_INVALID_DEVICE | -33 | deviceが無効です。あるいはdeviceがcontextに結び付けられていません。 |
CL_INVALID_VALUE | -30 | propertiesが無効です。 |
CL_INVALID_QUEUE_PROPERTIES | -35 | propertiesは有効ですが、デバイスにサポートされていません。 |
CL_OUT_OF_RESOURCES | -5 | デバイスがリソースを確保するのに失敗しました。 |
CL_OUT_OF_HOST_MEMORY | -6 | メモリが足りません。 |
cl_int clReleaseCommandQueue( cl_command_queue command_queue )
名前 | 値 | 解説 |
CL_SUCCESS | 0 | 関数は成功しました。 |
CL_INVALID_COMMAND_QUEUE | -36 | command_queueが無効です。 |
CL_OUT_OF_RESOURCES | -5 | デバイスがリソースを確保できませんでした。 |
CL_OUT_OF_HOST_MEMORY | -6 | メモリが足りません。 |
using System;class Program{static void Main(){IntPtr device = getDevices(getPlatforms()[0], DeviceType.Default)[0];Context context = new Context(device);CommandQueue commandQueue = new CommandQueue(context, device);}private static IntPtr[] getDevices(IntPtr platform, DeviceType deviceType){int deviceCount;OpenCLFunctions.clGetDeviceIDs(platform, deviceType, 0, null, out deviceCount);IntPtr[] result = new IntPtr[deviceCount];OpenCLFunctions.clGetDeviceIDs(platform, deviceType, deviceCount, result, out deviceCount);return result;}private static IntPtr[] getPlatforms(){int platformCount;OpenCLFunctions.clGetPlatformIDs(0, null, out platformCount);IntPtr[] result = new IntPtr[platformCount];OpenCLFunctions.clGetPlatformIDs(platformCount, result, out platformCount);return result;}}
using System;class Context{public IntPtr InternalPointer { get; private set; }public Context(params IntPtr[] devices){int error;InternalPointer = OpenCLFunctions.clCreateContext(null,devices.Length,devices,null,IntPtr.Zero,out error);}~Context(){OpenCLFunctions.clReleaseContext(InternalPointer);}}class CommandQueue{public IntPtr InternalPointer { get; private set; }public CommandQueue(Context context, IntPtr device){int error;InternalPointer = OpenCLFunctions.clCreateCommandQueue(context.InternalPointer,device,0,out error);}~CommandQueue(){OpenCLFunctions.clReleaseCommandQueue(InternalPointer);}}
using System;using System.Runtime.InteropServices;static class OpenCLFunctions{[DllImport("OpenCL.dll")]public static extern int clGetPlatformIDs(int entryCount, IntPtr[] platforms, out int platformCount);[DllImport("OpenCL.dll")]public static extern int clGetDeviceIDs(IntPtr platform,DeviceType deviceType,int entryCount,IntPtr[] devices,out int deviceCount);[DllImport("OpenCL.dll")]public static extern IntPtr clCreateContext(IntPtr[] properties,int deviceCount,IntPtr[] devices,NotifyCallback pfnNotify,IntPtr userData,out int errorCode);[DllImport("OpenCL.dll")]public static extern int clReleaseContext(IntPtr context);[DllImport("OpenCL.dll")]public static extern IntPtr clCreateCommandQueue(IntPtr context,IntPtr device,long properties,out int errorCodeReturn);[DllImport("OpenCL.dll")]public static extern int clReleaseCommandQueue(IntPtr commandQueue);}delegate void NotifyCallback(string errorInfo, IntPtr privateInfoSize, int cb, IntPtr userData);enum DeviceType:long{Default = (1 << 0),Cpu = (1 << 1),Gpu = (1 << 2),Accelerator = (1 << 3),All = 0xFFFFFFFF}
cl_context clCreateContext( const cl_context_properties *properties, cl_uint num_devices, const cl_device_id *devices, (void CL_CALLBACK *pfn_notify)( const char *errinfo, const void *private_info, size_t cb, void *user_data ), void *user_data, cl_int *errcode_ret )
cl_context_properties | 値 | プロパティ値 | 解説 |
CL_CONTEXT_PLATFORM | 0x1084 | cl_platform_id | 使うプラットフォームです。 |
CL_CONTEXT_D3D10_DEVICE_KHR | 0x4014 | ID3D10Device* | cl_khr_d3d10_sharing拡張が有効なときに使えます。Direct3D10との相互運用が可能です。デフォルトではNULLです。 |
CL_GL_CONTEXT_KHR | 0x2008 | cl_khr_gl_sharing拡張が有効なときに使えます。 |
|
CL_EGL_DISPLAY_KHR | 0x2009 | ||
CL_GLX_DISPLAY_KHR | 0x200A | ||
CL_WGL_HDC_KHR | 0x200B | ||
CL_CGL_SHAREGROUP_KHR | 0x200C |
cl_int clReleaseContext(cl_context context);
名前 | 値 | 解説 |
CL_SUCCESS | 0 | 関数は成功しました。 |
CL_INVALID_CONTEXT | -34 | contextが無効な値です。 |
CL_OUT_OF_RESOURCES | -5 | デバイスがリソースを作れませんでした。 |
CL_OUT_OF_HOST_MEMORY | -6 | メモリが足りません。 |
using System;class Program{static void Main(){Context context = new Context(getDevices(getPlatforms()[0], DeviceType.Default));}private static IntPtr[] getDevices(IntPtr platform, DeviceType deviceType){int deviceCount;OpenCLFunctions.clGetDeviceIDs(platform, deviceType, 0, null, out deviceCount);IntPtr[] result = new IntPtr[deviceCount];OpenCLFunctions.clGetDeviceIDs(platform, deviceType, deviceCount, result, out deviceCount);return result;}private static IntPtr[] getPlatforms(){int platformCount;OpenCLFunctions.clGetPlatformIDs(0, null, out platformCount);IntPtr[] result = new IntPtr[platformCount];OpenCLFunctions.clGetPlatformIDs(platformCount, result, out platformCount);return result;}}
using System;class Context{public IntPtr InternalPointer { get; private set; }public Context(IntPtr[] devices){int error;InternalPointer = OpenCLFunctions.clCreateContext(null,devices.Length,devices,null,IntPtr.Zero,out error);}~Context(){OpenCLFunctions.clReleaseContext(InternalPointer);}}
using System;using System.Runtime.InteropServices;static class OpenCLFunctions{[DllImport("OpenCL.dll")]public static extern int clGetPlatformIDs(int entryCount, IntPtr[] platforms, out int platformCount);[DllImport("OpenCL.dll")]public static extern int clGetDeviceIDs(IntPtr platform,DeviceType deviceType,int entryCount,IntPtr[] devices,out int deviceCount);[DllImport("OpenCL.dll")]public static extern IntPtr clCreateContext(IntPtr[] properties,int deviceCount,IntPtr[] devices,NotifyCallback pfnNotify,IntPtr userData,out int errorCode);[DllImport("OpenCL.dll")]public static extern int clReleaseContext(IntPtr context);}delegate void NotifyCallback(string errorInfo, IntPtr privateInfoSize, int cb, IntPtr userData);enum DeviceType:long{Default = (1 << 0),Cpu = (1 << 1),Gpu = (1 << 2),Accelerator = (1 << 3),All = 0xFFFFFFFF}
cl_int clGetDeviceIDs( cl_platform_id platform, cl_device_type device_type, cl_uint num_entries, cl_device_id *devices, cl_uint *num_devices )
cl_device_type | 値 | 解説 |
CL_DEVICE_TYPE_CPU | (1 << 1) | CPUです。 |
CL_DEVICE_TYPE_GPU | (1 << 2) | GPUです。 |
CL_DEVICE_TYPE_ACCELERATOR | (1 << 3) | IBM CELL(PS3に使われている)などです。 |
CL_DEVICE_TYPE_DEFAULT | (1 << 0) | デフォルトのデバイスです。 |
CL_DEVICE_TYPE_ALL | 0xFFFFFFFF | 使用可能な全てのOpenCLデバイスです。 |
名前 | 値 | 解説 |
CL_SUCCESS | 0 | 関数が成功しました。 |
CL_INVALID_PLATFORM | -32 | platformが無効な値です。 |
CL_INVALID_DEVICE_TYPE | -31 | device_typeが無効な値です。 |
CL_INVALID_VALUE | -30 | num_entriesが0なのにdevicesがNULLではありません。あるいは、num_devicesとdevicesが両方共NULLです。 |
CL_DEVICE_NOT_FOUND | -1 | device_typeで指定されたタイプのデバイスが存在しません。 |
CL_OUT_OF_RESOURCES | -5 | デバイスでリソースを確保するのに失敗しました。 |
CL_OUT_OF_HOST_MEMORY | -6 | メモリが足りません。 |
using System;class Program{static void Main(){foreach (var platform in getPlatforms()){foreach (var device in getDevices(platform, DeviceType.Default)){Console.WriteLine(device);}}}private static IntPtr[] getDevices(IntPtr platform, DeviceType deviceType){int deviceCount;OpenCLFunctions.clGetDeviceIDs(platform, deviceType, 0, null, out deviceCount);IntPtr[] result = new IntPtr[deviceCount];OpenCLFunctions.clGetDeviceIDs(platform, deviceType, deviceCount, result, out deviceCount);return result;}private static IntPtr[] getPlatforms(){int platformCount;OpenCLFunctions.clGetPlatformIDs(0, null, out platformCount);IntPtr[] result = new IntPtr[platformCount];OpenCLFunctions.clGetPlatformIDs(platformCount, result, out platformCount);return result;}}
using System;using System.Runtime.InteropServices;static class OpenCLFunctions{[DllImport("OpenCL.dll")]public static extern int clGetPlatformIDs(int entryCount, IntPtr[] platforms, out int platformCount);[DllImport("OpenCL.dll")]public static extern int clGetDeviceIDs(IntPtr platform,DeviceType deviceType,int entryCount,IntPtr[] devices,out int deviceCount);}enum DeviceType:long{Default = (1 << 0),Cpu = (1 << 1),Gpu = (1 << 2),Accelerator = (1 << 3),All = 0xFFFFFFFF}
cl_int clGetDeviceInfo( cl_device_id device, cl_device_info param_name, size_t param_value_size, void *param_value, size_t *param_value_size_ret )
cl_device_info | 型 | 値 | |
CL_DEVICE_ADDRESS_BITS | cl_uint | 0x100D | |
CL_DEVICE_AVAILABLE | cl_bool | 0x1027 | |
CL_DEVICE_COMPILER_AVAILABLE | cl_bool | 0x1028 | |
CL_DEVICE_DOUBLE_FP_CONFIG | cl_device_fp_config | 0x1032 | |
CL_DEVICE_ENDIAN_LITTLE | cl_bool | 0x1026 | |
CL_DEVICE_ERROR_CORRECTION_SUPPORT | cl_bool | 0x1024 | |
CL_DEVICE_EXECUTION_CAPABILITIES | cl_device_exec_capabilities | 0x1029 | |
CL_DEVICE_EXTENSIONS | char[] | 0x1030 | |
CL_DEVICE_GLOBAL_MEM_CACHE_SIZE | cl_ulong | 0x101E | |
CL_DEVICE_GLOBAL_MEM_CACHE_TYPE | cl_device_mem_cache_type | 0x101C | |
CL_DEVICE_GLOBAL_MEM_CACHELINE_SIZE | cl_uint | 0x101D | |
CL_DEVICE_GLOBAL_MEM_SIZE | cl_ulong | 0x101F | |
CL_DEVICE_HALF_FP_CONFIG | cl_device_fp_config | 0x1033 | |
CL_DEVICE_HOST_UNIFIED_MEMORY | cl_bool | 0x1035 | |
CL_DEVICE_IMAGE_SUPPORT | cl_bool | 0x1016 | |
CL_DEVICE_IMAGE2D_MAX_HEIGHT | size_t | 0x1012 | |
CL_DEVICE_IMAGE2D_MAX_WIDTH | size_t | 0x1011 | |
CL_DEVICE_IMAGE3D_MAX_DEPTH | size_t | 0x1015 | |
CL_DEVICE_IMAGE3D_MAX_HEIGHT | size_t | 0x1014 | |
CL_DEVICE_IMAGE3D_MAX_WIDTH | size_t | 0x1013 | |
CL_DEVICE_LOCAL_MEM_SIZE | cl_ulong | 0x1023 | |
CL_DEVICE_LOCAL_MEM_TYPE | cl_device_local_mem_type | 0x1022 | |
CL_DEVICE_MAX_CLOCK_FREQUENCY | cl_uint | 0x100C | |
CL_DEVICE_MAX_COMPUTE_UNITS | cl_uint | 0x1002 | |
CL_DEVICE_MAX_CONSTANT_ARGS | cl_uint | 0x1021 | |
CL_DEVICE_MAX_CONSTANT_BUFFER_SIZE | cl_ulong | 0x1020 | |
CL_DEVICE_MAX_MEM_ALLOC_SIZE | cl_ulong | 0x1010 | |
CL_DEVICE_MAX_PARAMETER_SIZE | size_t | 0x1017 | |
CL_DEVICE_MAX_READ_IMAGE_ARGS | cl_uint | 0x100E | |
CL_DEVICE_MAX_SAMPLERS | cl_uint | 0x1018 | |
CL_DEVICE_MAX_WORK_GROUP_SIZE | size_t | 0x1004 | |
CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS | cl_uint | 0x1003 | |
CL_DEVICE_MAX_WORK_ITEM_SIZES | size_t[] | 0x1005 | |
CL_DEVICE_MAX_WRITE_IMAGE_ARGS | cl_uint | 0x100F | |
CL_DEVICE_MEM_BASE_ADDR_ALIGN | cl_uint | 0x1019 | |
CL_DEVICE_MIN_DATA_TYPE_ALIGN_SIZE | cl_uint | 0x101A | |
CL_DEVICE_NAME | char[] | 0x102B | |
CL_DEVICE_NATIVE_VECTOR_WIDTH_CHAR | cl_uint | 0x1036 | |
CL_DEVICE_NATIVE_VECTOR_WIDTH_SHORT | cl_uint | 0x1037 | |
CL_DEVICE_NATIVE_VECTOR_WIDTH_INT | cl_uint | 0x1038 | |
CL_DEVICE_NATIVE_VECTOR_WIDTH_LONG | cl_uint | 0x1039 | |
CL_DEVICE_NATIVE_VECTOR_WIDTH_FLOAT | cl_uint | 0x103A | |
CL_DEVICE_NATIVE_VECTOR_WIDTH_DOUBLE | cl_uint | 0x103B | |
CL_DEVICE_NATIVE_VECTOR_WIDTH_HALF | cl_uint | 0x103C | |
CL_DEVICE_OPENCL_C_VERSION | char[] | 0x103D | |
CL_DEVICE_PLATFORM | cl_platform_id | 0x1031 | |
CL_DEVICE_PREFERRED_VECTOR_WIDTH_CHAR | cl_uint | 0x1006 | |
CL_DEVICE_PREFERRED_VECTOR_WIDTH_SHORT | cl_uint | 0x1007 | |
CL_DEVICE_PREFERRED_VECTOR_WIDTH_INT | cl_uint | 0x1008 | |
CL_DEVICE_PREFERRED_VECTOR_WIDTH_LONG | cl_uint | 0x1009 | |
CL_DEVICE_PREFERRED_VECTOR_WIDTH_FLOAT | cl_uint | 0x100A | |
CL_DEVICE_PREFERRED_VECTOR_WIDTH_DOUBLE | cl_uint | 0x100B | |
CL_DEVICE_PREFERRED_VECTOR_WIDTH_HALF | cl_uint | 0x1034 | |
CL_DEVICE_PROFILE | char[] | 0x102E | |
CL_DEVICE_PROFILING_TIMER_RESOLUTION | size_t | 0x1025 | |
CL_DEVICE_QUEUE_PROPERTIES | cl_command_queue_properties | 0x102A | |
CL_DEVICE_SINGLE_FP_CONFIG | cl_device_fp_config | 0x101B | |
CL_DEVICE_TYPE | cl_device_type | 0x1000 | |
CL_DEVICE_VENDOR | char[] | 0x102C | |
CL_DEVICE_VENDOR_ID | cl_uint | 0x1001 | |
CL_DEVICE_VERSION | char[] | 0x102F | |
CL_DRIVER_VERSION | char[] | 0x102D |
名前 | 値 | 解説 |
CL_SUCCESS | 0 | 関数は成功しました |
CL_INVALID_DEVICE | -33 | deviceが無効な値です。 |
CL_INVALID_VALUE | -30 | param_nameが無効な値であるか、param_value_sizeが小さすぎ、param_valueがNULLではありません。 |
CL_OUT_OF_RESOURCES | -5 | デバイスでリソースを確保するのに失敗しました。 |
CL_OUT_OF_HOST_MEMORY | -6 | メモリが足りません。 |
using System;class Program{static void Main(){foreach (var platform in getPlatforms()){foreach (var device in getDevices(platform, DeviceType.Default)){Console.WriteLine(getDeviceInfo(device, DeviceInfoString.Name));}}}private static string getDeviceInfo(IntPtr device, DeviceInfoString deviceInfoName){int valueSize;OpenCLFunctions.clGetDeviceInfo(device, deviceInfoName, 0, null, out valueSize);System.Text.StringBuilder result = new System.Text.StringBuilder(valueSize);OpenCLFunctions.clGetDeviceInfo(device, deviceInfoName, valueSize, result, out valueSize);return result.ToString();}private static IntPtr[] getDevices(IntPtr platform, DeviceType deviceType){int deviceCount;OpenCLFunctions.clGetDeviceIDs(platform, deviceType, 0, null, out deviceCount);IntPtr[] result = new IntPtr[deviceCount];OpenCLFunctions.clGetDeviceIDs(platform, deviceType, deviceCount, result, out deviceCount);return result;}private static IntPtr[] getPlatforms(){int platformCount;OpenCLFunctions.clGetPlatformIDs(0, null, out platformCount);IntPtr[] result = new IntPtr[platformCount];OpenCLFunctions.clGetPlatformIDs(platformCount, result, out platformCount);return result;}}
using System;using System.Runtime.InteropServices;static class OpenCLFunctions{[DllImport("OpenCL.dll")]public static extern int clGetPlatformIDs(int entryCount, IntPtr[] platforms, out int platformCount);[DllImport("OpenCL.dll")]public static extern int clGetDeviceIDs(IntPtr platform,DeviceType deviceType,int entryCount,IntPtr[] devices,out int deviceCount);[DllImport("OpenCL.dll")]public static extern int clGetDeviceInfo(IntPtr device,DeviceInfoString paramName,int paramValueSize,System.Text.StringBuilder paramValue,out int paramValueSizeReturn);}enum DeviceType:long{Default = (1 << 0),Cpu = (1 << 1),Gpu = (1 << 2),Accelerator = (1 << 3),All = 0xFFFFFFFF}enum DeviceInfoString{Name = 0x102B,Vendor = 0x102C,DriverVersion = 0x102D,Profile = 0x102E,Version = 0x102F,Extensions = 0x1030}