[PR]
×
[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。
プログラミング、3DCGとその他いろいろについて
[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。
size_t get_global_size(uint dimIndex)
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);Kernel kernel = new Kernel(program, "myKernelFunction");Buffer buffer = Buffer.FromCopiedHostMemory(context, new float[6]);kernel.SetArgument(0, buffer);commandQueue.EnqueueRange(kernel, new MultiDimension(2, 3), new MultiDimension(1, 1));var readBack = new float[6];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;using System.Linq;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();}public void EnqueueRange(Kernel kernel, MultiDimension globalWorkSize, MultiDimension localWorkSize){MultiDimension offset = new MultiDimension();OpenCLFunctions.clEnqueueNDRangeKernel(InternalPointer,kernel.InternalPointer,globalWorkSize.Dimension,ref offset,ref globalWorkSize,ref localWorkSize,0,null,IntPtr.Zero);}}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){int error = OpenCLFunctions.clBuildProgram(InternalPointer,devices.Length,devices,null,null,IntPtr.Zero);if (error != 0){int paramValueSize = 0;OpenCLFunctions.clGetProgramBuildInfo(InternalPointer,devices.First(),ProgramBuildInfoString.Log,0,null,out paramValueSize);System.Text.StringBuilder text = new System.Text.StringBuilder(paramValueSize);OpenCLFunctions.clGetProgramBuildInfo(InternalPointer,devices.First(),ProgramBuildInfoString.Log,paramValueSize,text,out paramValueSize);throw new Exception(text.ToString());}}}class Kernel{public IntPtr InternalPointer { get; private set; }public Kernel(Program program, string functionName){int errorCode;InternalPointer = OpenCLFunctions.clCreateKernel(program.InternalPointer,functionName,out errorCode);}~Kernel(){OpenCLFunctions.clReleaseKernel(InternalPointer);}public void SetArgument(int argumentIndex, Buffer buffer){IntPtr bufferPointer = buffer.InternalPointer;OpenCLFunctions.clSetKernelArg(InternalPointer,argumentIndex,Marshal.SizeOf(typeof(IntPtr)),ref bufferPointer);}}
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);[DllImport("OpenCL.dll")]public static extern IntPtr clCreateKernel(IntPtr kernel, string functionName, out int errorCode);[DllImport("OpenCL.dll")]public static extern int clReleaseKernel(IntPtr kernel);[DllImport("OpenCL.dll")]public static extern int clSetKernelArg(IntPtr kernel, int argumentIndex, int size, ref IntPtr value);[DllImport("OpenCL.dll")]public static extern int clEnqueueNDRangeKernel(IntPtr commandQueue,IntPtr kernel,int workDimension,ref MultiDimension globalWorkOffset,ref MultiDimension globalWorkSize,ref MultiDimension localWorkSize,int countOfEventsInWaitList,IntPtr[] eventList,IntPtr eventObject);[DllImport("OpenCL.dll")]public static extern int clGetProgramBuildInfo(IntPtr program,IntPtr device,ProgramBuildInfoString paramName,int paramValueSize,System.Text.StringBuilder paramValue,out int paramValueSizeReturn);}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)}struct MultiDimension{public int X;public int Y;public int Z;public int Dimension;public MultiDimension(int x){X = x;Y = 0;Z = 0;Dimension = 1;}public MultiDimension(int x, int y){X = x;Y = y;Z = 0;Dimension = 2;}}enum ProgramBuildInfoString{Options = 0x1182,Log = 0x1183}
__kernel void myKernelFunction(__global float* items){items[get_global_id(0)] =get_global_size(0);//get_global_size(1);}
size_t get_local_size(uint dimIndex)
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);Kernel kernel = new Kernel(program, "myKernelFunction");Buffer buffer = Buffer.FromCopiedHostMemory(context, new float[24]);kernel.SetArgument(0, buffer);commandQueue.EnqueueRange(kernel, new MultiDimension(4, 6), new MultiDimension(2, 3));var readBack = new float[24];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;}}
__kernel void myKernelFunction(__global float* items){items[get_global_id(0)] =get_local_size(0);//get_local_size(1);}
size_t get_num_groups(uint dimIndex)
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);Kernel kernel = new Kernel(program, "myKernelFunction");Buffer buffer = Buffer.FromCopiedHostMemory(context, new float[24]);kernel.SetArgument(0, buffer);commandQueue.EnqueueRange(kernel, new MultiDimension(2, 12), new MultiDimension(2, 3));var readBack = new float[24];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;}}
__kernel void myKernelFunction(__global float* items){items[get_global_id(0)] =get_num_groups(0);//get_num_groups(1);}