public void Dispose() { if (Kernel != null) { Kernel.Dispose(); Kernel = null; } if (CommandQueue != null) { CommandQueue.Dispose(); CommandQueue = null; } System.GC.SuppressFinalize(this); }
internal static void CallOpenCLNet(int[] WorkSize, HlGraphEntry CacheEntry, InvokeContext ctx, HighLevel.HlGraph HLgraph, OpenCLNet.Device device) { // We can invoke the kernel using the arguments from ctx now :) if (device == null) { device = GetFirstGpu(); if (device == null) { device = GetFirstCpu(); } if (device == null) { throw new ArgumentNullException("device"); } } OpenCLNet.Platform Platform = device.Platform; OpenCLNet.Context context; OpenCLNet.Program program; lock (CacheEntry) { context = CacheEntry.Context; if (context == null) { IntPtr[] properties = new IntPtr[] { new IntPtr((long)OpenCLNet.ContextProperties.PLATFORM), Platform.PlatformID, IntPtr.Zero, }; context = CacheEntry.Context = Platform.CreateContext(properties, new OpenCLNet.Device[] { device }, null, IntPtr.Zero); } program = CacheEntry.Program; if (program == null) { StringBuilder source = new StringBuilder(); source.Append(GetOpenCLSourceHeader(Platform, device, CacheEntry)); source.Append(CacheEntry.Source); source.Append(GetOpenCLSourceFooter(Platform, device)); program = context.CreateProgramWithSource(source.ToString()); try { program.Build(); } catch (Exception ex) { string err = program.GetBuildLog(device); throw new Exception(err, ex); } CacheEntry.Program = program; } } using (CallContext CallContext = new CallContext(context, device, OpenCLNet.CommandQueueProperties.PROFILING_ENABLE, program.CreateKernel(HLgraph.MethodName))) { OpenCLNet.CommandQueue commandQueue = CallContext.CommandQueue; for (int i = 0; i < ctx.Arguments.Count; i++) { ctx.Arguments[i].WriteToKernel(CallContext, i); } //OpenCLNet.Event StartEvent, EndEvent; //commandQueue.EnqueueMarker(out StartEvent); IntPtr[] GlobalWorkSize = new IntPtr[WorkSize.Length]; for (int i = 0; i < WorkSize.Length; i++) { GlobalWorkSize[i] = new IntPtr(WorkSize[i]); } commandQueue.EnqueueNDRangeKernel(CallContext.Kernel, (uint)GlobalWorkSize.Length, null, GlobalWorkSize, null); for (int i = 0; i < ctx.Arguments.Count; i++) { ctx.Arguments[i].ReadFromKernel(CallContext, i); } commandQueue.Finish(); //commandQueue.EnqueueMarker(out EndEvent); //commandQueue.Finish(); //ulong StartTime, EndTime; //StartEvent.GetEventProfilingInfo(OpenCLNet.ProfilingInfo.QUEUED, out StartTime); //EndEvent.GetEventProfilingInfo(OpenCLNet.ProfilingInfo.END, out EndTime); } }
public CallContext(OpenCLNet.Context Context, OpenCLNet.Device Device, OpenCLNet.CommandQueueProperties CqProperties, OpenCLNet.Kernel Kernel) { this.Context = Context; this.CommandQueue = Context.CreateCommandQueue(Device, CqProperties); this.Kernel = Kernel; }