public OpenCLProxy(bool useSoftware = false) { HardwareAccelerationEnabled = ComputePlatform.Platforms.Count != 0 && !useSoftware; if (HardwareAccelerationEnabled) { ComputePlatform platform = ComputePlatform.Platforms[0]; var devices = new List<ComputeDevice> { platform.Devices[0] }; var properties = new ComputeContextPropertyList(platform); _context = new ComputeContext(devices, properties, null, IntPtr.Zero); _commands = new ComputeCommandQueue(_context, _context.Devices[0], ComputeCommandQueueFlags.None); _intComputeBuffers = new Dictionary<string, ComputeBuffer<int>>(); _floatComputeBuffers = new Dictionary<string, ComputeBuffer<float>>(); AcceleratorName = platform.Name; } else { AcceleratorName = "CPU"; } _intArguments = new Dictionary<string, int>(); _intBuffers = new Dictionary<string, int[]>(); _floatArguments = new Dictionary<string, float>(); _floatBuffers = new Dictionary<string, float[]>(); _doubleArguments = new Dictionary<string, double>(); }
protected override void RunInternal() { ComputeCommandQueue commands = new ComputeCommandQueue(context, context.Devices[0], ComputeCommandQueueFlags.Profiling); Console.WriteLine("Original content:"); Random rand = new Random(); int count = 6; long[] bufferContent = new long[count]; for (int i = 0; i < count; i++) { bufferContent[i] = (long)(rand.NextDouble() * long.MaxValue); Console.WriteLine("\t" + bufferContent[i]); } ComputeBuffer<long> buffer = new ComputeBuffer<long>(context, ComputeMemoryFlags.CopyHostPointer, bufferContent); IntPtr mappedPtr = commands.Map(buffer, false, ComputeMemoryMappingFlags.Read, 0, bufferContent.Length, null); commands.Finish(); Console.WriteLine("Mapped content:"); for (int i = 0; i < bufferContent.Length; i++) { IntPtr ptr = new IntPtr(mappedPtr.ToInt64() + i * sizeof(long)); Console.WriteLine("\t" + Marshal.ReadInt64(ptr)); } commands.Unmap(buffer, ref mappedPtr, null); }
public MaterialCache(ComputeCommandQueue commandQueue) { _commandQueue = commandQueue; _materialArray = new Material[DefaultCacheSize]; Buffer = new ComputeBuffer<Material>(commandQueue.Context, ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.UseHostPointer, _materialArray); }
public GpuBvhTree(ComputeCommandQueue commandQueue, List<Triangle> prims, List<SimplePointLight> lights, int maxPrimsPerNode) : base(prims, maxPrimsPerNode) { _commandQueue = commandQueue; _lights = lights; initBuffers(); }
internal OpenCLQueue(OpenCLContext context, ComputeCommandQueue computeCommandQueue) { Contract.Requires(context != null); Contract.Requires(computeCommandQueue != null); Context = context; ComputeCommandQueue = computeCommandQueue; }
internal ComputeEvent(IntPtr handle, ComputeCommandQueue queue) { unsafe { Handle = handle; commandQueue = queue; commandType = (ComputeCommandType)GetInfo<ComputeEventInfo, uint>( ComputeEventInfo.CommandType, CL10.GetEventInfo); } }
internal ComputeEvent(CLEventHandle handle, ComputeCommandQueue queue) { Handle = handle; SetID(Handle.Value); CommandQueue = queue; Type = (ComputeCommandType)GetInfo<CLEventHandle, ComputeEventInfo, int>(Handle, ComputeEventInfo.CommandType, CLInterface.CL10.GetEventInfo); Context = queue.Context; if (ComputeTools.ParseVersionString(CommandQueue.Device.Platform.Version, 1) > new Version(1, 0)) HookNotifier(); }
public static void Run(TextWriter log, ComputeContext context) { StartTest(log, "Image test"); try { log.Write("Creating command queue... "); ComputeCommandQueue commands = new ComputeCommandQueue(context, context.Devices[0], ComputeCommandQueueFlags.None); log.WriteLine("done."); int width = 16; int height = 16; log.Write("Creating first bitmap and drawing shapes... "); Bitmap firstBitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb); Graphics graphics = Graphics.FromImage(firstBitmap); graphics.FillEllipse(Brushes.Red, 0, 0, width / 2, height / 2); graphics.FillRectangle(Brushes.Green, width / 2 + 1, 0, width / 2, height / 2); graphics.FillRectangle(Brushes.Blue, width / 2 + 1, height / 2 + 1, width / 2, height / 2); log.WriteLine("done."); log.Write("Creating OpenCL image object from first bitmap... "); ComputeImage2D clImage = new ComputeImage2D(context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.CopyHostPointer, firstBitmap); log.WriteLine("done."); log.Write("Creating second bitmap... "); Bitmap secondBitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb); BitmapData bmpData = secondBitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, secondBitmap.PixelFormat); log.WriteLine("done."); log.Write("Reading from OpenCL image object... "); commands.ReadFromImage(clImage, bmpData.Scan0, true, null); log.WriteLine("done."); secondBitmap.UnlockBits(bmpData); log.Write("Comparing bitmaps... "); for (int i = 0; i < width; i++) for (int j = 0; j < height; j++) if (firstBitmap.GetPixel(i, j) != secondBitmap.GetPixel(i, j)) throw new Exception("Image data mismatch!"); log.WriteLine("passed."); } catch (Exception e) { log.WriteLine(e.ToString()); } EndTest(log, "Image test"); }
internal ComputeEvent(CLEventHandle handle, ComputeCommandQueue queue) { Handle = handle; SetID(Handle.Value); CommandQueue = queue; Type = (ComputeCommandType)GetInfo<CLEventHandle, ComputeEventInfo, int>(Handle, ComputeEventInfo.CommandType, CL10.GetEventInfo); Context = queue.Context; if (ComputeTools.ParseVersionString(CommandQueue.Device.Platform.Version, 1) > new Version(1, 0)) HookNotifier(); Trace.WriteLine("Create " + this + " in Thread(" + Thread.CurrentThread.ManagedThreadId + ").", "Information"); }
public Bitmap GetScreenshot(CameraConfig camera, int screenshotHeight, int slowRender) { var screenshotWidth = (int)(screenshotHeight * ScreenshotAspectRatio); var computeBuffer = new ComputeBuffer <Vector4>(_program.Context, ComputeMemoryFlags.ReadWrite, screenshotWidth * screenshotHeight); var queue = new ComputeCommandQueue(_program.Context, _program.Context.Devices[0], ComputeCommandQueueFlags.None); var globalSize = GlobalLaunchsizeFor(screenshotWidth, screenshotHeight); for (var i = 0; i < slowRender; i++) { CoreRender(computeBuffer, queue, _kernels, new Vector4((Vector3)camera.Position), new Vector4((Vector3)camera.Lookat), new Vector4((Vector3)camera.Up), i, camera.Fov, slowRender, camera.FocalDistance, screenshotWidth, screenshotHeight, globalSize, _localSize); } for (var i = 0; i < camera.Frame * slowRender; i++) { CoreRender(computeBuffer, queue, _kernels, new Vector4((Vector3)camera.Position), new Vector4((Vector3)camera.Lookat), new Vector4((Vector3)camera.Up), i, camera.Fov, slowRender, camera.FocalDistance, screenshotWidth, screenshotHeight, globalSize, _localSize); } var pixels = new Vector4[screenshotWidth * screenshotHeight]; queue.ReadFromBuffer(computeBuffer, ref pixels, true, null); queue.Finish(); computeBuffer.Dispose(); queue.Dispose(); var bmp = new Bitmap(screenshotWidth, screenshotHeight); var destBuffer = new int[screenshotWidth * screenshotHeight]; for (var y = 0; y < screenshotHeight; y++) { for (var x = 0; x < screenshotWidth; x++) { var pixel = pixels[x + y * screenshotWidth]; if (float.IsNaN(pixel.X) || float.IsNaN(pixel.Y) || float.IsNaN(pixel.Z)) { Console.WriteLine("Warning! Caught NAN pixel while taking screenshot!"); continue; } destBuffer[y * screenshotWidth + x] = (byte)(pixel.X * 255) << 16 | (byte)(pixel.Y * 255) << 8 | (byte)(pixel.Z * 255); } } var bmpData = bmp.LockBits(new Rectangle(0, 0, screenshotWidth, screenshotHeight), ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb); Marshal.Copy(destBuffer, 0, bmpData.Scan0, destBuffer.Length); bmp.UnlockBits(bmpData); return(bmp); }
//ComputeBuffer<int> input5_dev; public override void Init() { // pick first platform ComputePlatform platform = ComputePlatform.Platforms[0]; // create context with all gpu devices ComputeContext context = new ComputeContext(ComputeDeviceTypes.Gpu, new ComputeContextPropertyList(platform), null, IntPtr.Zero); // create a command queue with first gpu found queue = new ComputeCommandQueue(context, context.Devices[0], ComputeCommandQueueFlags.None); // load opencl source StreamReader streamReader = new StreamReader("main.cl"); string clSource = streamReader.ReadToEnd(); streamReader.Close(); // create program with opencl source ComputeProgram program = new ComputeProgram(context, clSource); // compile opencl source program.Build(null, null, null, IntPtr.Zero); result_dev = new ComputeBuffer <byte>(context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.AllocateHostPointer, results.Length); resultCalc_dev = new ComputeBuffer <double>(context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.AllocateHostPointer, calculatables.Length); // init input parameters input1_dev = new ComputeBuffer <int>(context, ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.CopyHostPointer, DataGenerator.In1); input2_dev = new ComputeBuffer <int>(context, ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.CopyHostPointer, DataGenerator.In2); input3_dev = new ComputeBuffer <double>(context, ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.CopyHostPointer, DataGenerator.In3); input4_dev = new ComputeBuffer <byte>(context, ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.CopyHostPointer, DataGenerator.In4_3_bytes); //input5_dev = new ComputeBuffer<int>(context, ComputeMemoryFlags.ReadOnly, DataFeeder.width); // load chosen kernel from program kernel = program.CreateKernel("Proccess"); int i = 0; kernel.SetMemoryArgument(i++, result_dev); kernel.SetMemoryArgument(i++, resultCalc_dev); kernel.SetMemoryArgument(i++, input1_dev); kernel.SetMemoryArgument(i++, input2_dev); kernel.SetMemoryArgument(i++, input3_dev); kernel.SetMemoryArgument(i++, input4_dev); kernel.SetValueArgument(i++, DataGenerator.Width); kernel.SetValueArgument(i++, DataGenerator.Height); }
private static void ConductSearch(ComputeContext context, ComputeKernel kernel) { var todos = GetQueenTaskPartition(NumQueens, 4); var done = new List <QueenTask>(); ComputeEventList eventList = new ComputeEventList(); var commands = new ComputeCommandQueue(context, context.Devices[1], ComputeCommandQueueFlags.None); Console.WriteLine("Starting {0} tasks, and working {1} at a time.", todos.Count, Spread); QueenTask[] inProgress = GetNextAssignment(new QueenTask[] {}, todos, done); var sw = new Stopwatch(); sw.Start(); while (inProgress.Any()) { var taskBuffer = new ComputeBuffer <QueenTask>(context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.CopyHostPointer, inProgress); kernel.SetMemoryArgument(0, taskBuffer); commands.WriteToBuffer(inProgress, taskBuffer, false, null); for (int i = 0; i < 12; i++) { commands.Execute(kernel, null, new long[] { inProgress.Length }, null, eventList); } commands.ReadFromBuffer(taskBuffer, ref inProgress, false, eventList); commands.Finish(); inProgress = GetNextAssignment(inProgress, todos, done); } sw.Stop(); Console.WriteLine(sw.ElapsedMilliseconds / 1000.0); ulong sum = done.Select(state => state.solutions) .Aggregate((total, next) => total + next); Console.WriteLine("Q({0})={1}", NumQueens, sum); }
//////////////////////////////////////////////////////////////////////////////////////////////////// /// <summary> Initializes this object. </summary> /// /// <param name="selectedComputeDeviceTypes"> The selected compute device types. </param> /// <param name="platformId"> (Optional) Identifier for the platform. </param> /// <param name="deviceIndex"> (Optional) Zero-based index of the device. </param> //////////////////////////////////////////////////////////////////////////////////////////////////// public static void Initialize(ComputeDeviceTypes selectedComputeDeviceTypes, int platformId = 0, int deviceIndex = 0) { Platform = ComputePlatform.Platforms?[platformId]; Devices = Platform?.Devices .Where(d => (long)d.Type == (long)selectedComputeDeviceTypes) .ToArray(); DeviceIndex = deviceIndex; if (Devices.Length > 0) { Context = new ComputeContext(Devices, new ComputeContextPropertyList(Platform), null, IntPtr.Zero); CommandQueue = new ComputeCommandQueue(Context, Devices[DeviceIndex], ComputeCommandQueueFlags.None); Enable = true; } }
private static void CoreRender(ComputeMemory buffer, ComputeCommandQueue queue, IEnumerable <ComputeKernel> kernels, Vector4 position, Vector4 lookat, Vector4 up, int frame, float fov, int slowRenderCount, float focalDistance, int width, int height, long[] globalSize, long[] localSize) { foreach (var kernel in kernels) { kernel.SetMemoryArgument(0, buffer); kernel.SetValueArgument(1, width); kernel.SetValueArgument(2, height); kernel.SetValueArgument(3, position); kernel.SetValueArgument(4, lookat); kernel.SetValueArgument(5, up); kernel.SetValueArgument(6, frame); kernel.SetValueArgument(7, fov); kernel.SetValueArgument(8, slowRenderCount); kernel.SetValueArgument(9, focalDistance); queue.Execute(kernel, LaunchSize, globalSize, localSize, null); } }
internal ComputeEvent(IntPtr handle, ComputeCommandQueue queue) { unsafe { Handle = handle; CommandQueue = queue; Type = (ComputeCommandType)GetInfo<ComputeEventInfo, uint>( ComputeEventInfo.CommandType, CL10.GetEventInfo); Context = queue.Context; if (CommandQueue.Device.Version == new Version(1, 1)) HookNotifier(); Completed += new ComputeCommandStatusChanged(ComputeEvent_Fired); Aborted += new ComputeCommandStatusChanged(ComputeEvent_Fired); } }
public void FindPoints(byte[] baseImage, byte[] nextImage, int[] X, int[] Y, int searchDelta, int subsetDelta, int BitmapWidth, int BitmapHeight, int PointsinX, int PointsinY) { using var commands = new ComputeCommandQueue(context, context.Devices[0], ComputeCommandQueueFlags.None); using ComputeBuffer <byte> baseImageBuffer = new ComputeBuffer <byte>(context, ComputeMemoryFlags.WriteOnly | ComputeMemoryFlags.CopyHostPointer, baseImage); using ComputeBuffer <byte> nextImageBuffer = new ComputeBuffer <byte>(context, ComputeMemoryFlags.WriteOnly | ComputeMemoryFlags.CopyHostPointer, nextImage); using ComputeBuffer <int> XBuffer = new ComputeBuffer <int>(context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.UseHostPointer, X); using ComputeBuffer <int> YBuffer = new ComputeBuffer <int>(context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.UseHostPointer, Y); kernel.SetMemoryArgument(0, baseImageBuffer); kernel.SetMemoryArgument(1, nextImageBuffer); kernel.SetMemoryArgument(2, XBuffer); kernel.SetMemoryArgument(3, YBuffer); kernel.SetValueArgument(4, searchDelta); kernel.SetValueArgument(5, subsetDelta); kernel.SetValueArgument(6, BitmapHeight); kernel.SetValueArgument(7, PointsinX); commands.Execute(kernel, null, new long[] { PointsinX, PointsinY }, null, null); commands.Finish(); }
public void SetupDevice(params string[] kernelNames) { try { this.program.Build(new[] { device }, string.Empty, null, IntPtr.Zero); } catch (Exception) { Tracer.TraceLine(this.program.GetBuildLog(ComputePlatform.Platforms[0].Devices[0])); throw; } if (kernelNames.Length > 1) { kernels = program.CreateAllKernels().ToDictionary(item => item.FunctionName); } else { kernel = program.CreateKernel(kernelNames[0]); } commands = new ComputeCommandQueue(context, context.Devices[0], ComputeCommandQueueFlags.None); }
private static void ConductSearch(ComputeContext context, ComputeKernel kernel) { var todos = GetQueenTaskPartition(NumQueens, 4); var done = new List<QueenTask>(); ComputeEventList eventList = new ComputeEventList(); var commands = new ComputeCommandQueue(context, context.Devices[1], ComputeCommandQueueFlags.None); Console.WriteLine("Starting {0} tasks, and working {1} at a time.", todos.Count, Spread); QueenTask[] inProgress = GetNextAssignment(new QueenTask[] {}, todos, done); var sw = new Stopwatch(); sw.Start(); while (inProgress.Any()) { var taskBuffer = new ComputeBuffer<QueenTask>(context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.CopyHostPointer, inProgress); kernel.SetMemoryArgument(0, taskBuffer); commands.WriteToBuffer(inProgress, taskBuffer, false, null); for (int i = 0; i < 12; i++) commands.Execute(kernel, null, new long[] { inProgress.Length }, null, eventList); commands.ReadFromBuffer(taskBuffer, ref inProgress, false, eventList); commands.Finish(); inProgress = GetNextAssignment(inProgress, todos, done); } sw.Stop(); Console.WriteLine(sw.ElapsedMilliseconds / 1000.0); ulong sum = done.Select(state => state.solutions) .Aggregate((total, next) => total + next); Console.WriteLine("Q({0})={1}", NumQueens, sum); }
/// <summary> /// Executes the specified kernel function name. /// </summary> /// <typeparam name="TSource">The type of the source.</typeparam> /// <param name="functionName">Name of the function.</param> /// <param name="args"></param> /// <exception cref="ExecutionException"> /// </exception> public override void Execute(string functionName, params object[] args) { ValidateArgs(functionName, args); ComputeKernel kernel = _compiledKernels.FirstOrDefault(x => (x.FunctionName == functionName)); ComputeCommandQueue commands = new ComputeCommandQueue(_context, _defaultDevice, ComputeCommandQueueFlags.None); if (kernel == null) { throw new ExecutionException(string.Format("Kernal function {0} not found", functionName)); } try { var ndobject = (Array)args.FirstOrDefault(x => (x.GetType().IsArray)); long length = ndobject != null ? ndobject.Length : 1; var buffers = BuildKernelArguments(args, kernel, length); commands.Execute(kernel, null, new long[] { length }, null, null); for (int i = 0; i < args.Length; i++) { if (!args[i].GetType().IsArray) { continue; } Array r = (Array)args[i]; commands.ReadFromMemory(buffers[i], ref r, true, 0, null); buffers[i].Dispose(); } commands.Finish(); } catch (Exception ex) { throw new ExecutionException(ex.Message); } finally { commands.Dispose(); } }
public void Run(ComputeContext context, TextWriter log) { try { ComputeCommandQueue commands = new ComputeCommandQueue(context, context.Devices[0], ComputeCommandQueueFlags.None); log.WriteLine("Original content:"); Random rand = new Random(); int count = 6; long[] bufferContent = new long[count]; for (int i = 0; i < count; i++) { bufferContent[i] = (long)(rand.NextDouble() * long.MaxValue); log.WriteLine("\t" + bufferContent[i]); } ComputeBuffer <long> buffer = new ComputeBuffer <long>(context, ComputeMemoryFlags.CopyHostPointer, bufferContent); IntPtr mappedPtr = commands.Map(buffer, true, ComputeMemoryMappingFlags.Read, 0, bufferContent.Length, null); log.WriteLine("Mapped content:"); for (int i = 0; i < bufferContent.Length; i++) { IntPtr ptr = new IntPtr(mappedPtr.ToInt64() + i * sizeof(long)); log.WriteLine("\t" + Marshal.ReadInt64(ptr)); } commands.Unmap(buffer, ref mappedPtr, null); // wait for the unmap to happen commands.Finish(); // cleanup buffer buffer.Dispose(); // cleanup commands commands.Dispose(); } catch (Exception e) { log.WriteLine(e.ToString()); } }
internal ComputeEvent(IntPtr handle, ComputeCommandQueue queue) { unsafe { Handle = handle; CommandQueue = queue; Type = (ComputeCommandType)GetInfo<ComputeEventInfo, uint>( ComputeEventInfo.CommandType, CL10.GetEventInfo); Context = queue.Context; if (CommandQueue.Device.Version == new Version(1, 1)) HookNotifier(); Completed += new ComputeCommandStatusChanged(ComputeEvent_Fired); Aborted += new ComputeCommandStatusChanged(ComputeEvent_Fired); } Trace.WriteLine("Created " + this + " in Thread(" + Thread.CurrentThread.ManagedThreadId + ")."); }
public DoubleMatrixOpenCL(int deviceID = 0) { Func <int, string, Version, bool> deviceSelector = (i, n, v) => i == deviceID; deviceSelector = deviceSelector ?? ((i, d, v) => true); var device = ComputePlatform.Platforms.SelectMany(p => p.Devices).Where((d, i) => deviceSelector(i, $"{d.Name} {d.DriverVersion}", d.Version)).First(); var properties = new ComputeContextPropertyList(device.Platform); context = new ComputeContext(new[] { device }, properties, null, IntPtr.Zero); // Create the kernel function and set its arguments. program = new ComputeProgram(context, clProgramSourceMultiplyMatrix); // Create and build the opencl program. program.Build(null, null, null, IntPtr.Zero); kernel = program.CreateKernel("MatrixMultiply"); // Create the command queue. This is used to control kernel execution and manage read/write/copy operations. commands = new ComputeCommandQueue(context, context.Devices[0], ComputeCommandQueueFlags.None); }
public void Run(ComputeContext context, TextWriter log) { try { ComputeCommandQueue commands = new ComputeCommandQueue(context, context.Devices[0], ComputeCommandQueueFlags.None); log.WriteLine("Original content:"); Random rand = new Random(); int count = 6; long[] bufferContent = new long[count]; for (int i = 0; i < count; i++) { bufferContent[i] = (long)(rand.NextDouble() * long.MaxValue); log.WriteLine("\t" + bufferContent[i]); } ComputeBuffer<long> buffer = new ComputeBuffer<long>(context, ComputeMemoryFlags.CopyHostPointer, bufferContent); IntPtr mappedPtr = commands.Map(buffer, true, ComputeMemoryMappingFlags.Read, 0, bufferContent.Length, null); log.WriteLine("Mapped content:"); for (int i = 0; i < bufferContent.Length; i++) { IntPtr ptr = new IntPtr(mappedPtr.ToInt64() + i * sizeof(long)); log.WriteLine("\t" + Marshal.ReadInt64(ptr)); } commands.Unmap(buffer, ref mappedPtr, null); // wait for the unmap to happen commands.Finish(); // cleanup buffer buffer.Dispose(); // cleanup commands commands.Dispose(); } catch (Exception e) { log.WriteLine(e.ToString()); } }
public void OpenClMul() { //ѡȡ�豸 var platform = ComputePlatform.Platforms.FirstOrDefault(); var device = platform.Devices.FirstOrDefault(); var properties = new ComputeContextPropertyList(platform); var context = new ComputeContext(new[] { device }, properties, null, IntPtr.Zero); ComputeCommandQueue commands = new ComputeCommandQueue(context, context.Devices[0], ComputeCommandQueueFlags.None); var code = File.ReadAllText(@"demos\cls\matrix_mul.cl"); var program = new ComputeProgram(context, code); try { program.Build(new [] { device }, null, null, IntPtr.Zero); } catch (Exception ex) { throw; } var kernel = program.CreateKernel("MatrixMul"); int rank = Rank; var result = new ComputeBuffer <int>(context, ComputeMemoryFlags.WriteOnly, rank * rank); var matrix = CreateMatrix(context, rank); kernel.SetMemoryArgument(0, result); kernel.SetMemoryArgument(1, matrix); kernel.SetValueArgument(2, rank); Console.WriteLine($"Platform: {platform.Name}\n Device: {device.Name}\n Size: {rank}x{rank}"); Stopwatch sw = Stopwatch.StartNew(); commands.Execute(kernel, null, new long[] { rank, rank }, null, null); int[] resultArray = new int[rank * rank]; var arrHandle = GCHandle.Alloc(resultArray, GCHandleType.Pinned); commands.Read(result, true, 0, rank * rank, arrHandle.AddrOfPinnedObject(), null); var elapsed = sw.Elapsed; Console.WriteLine($"using: {elapsed.TotalMilliseconds} ms\n"); arrHandle.Free(); kernel.Dispose(); }
private void InitClooApi() { try { CvInvoke.UseOpenCL = true; // pick first platform ComputePlatform platform = ComputePlatform.Platforms[1]; // create context with all gpu devices clooCtx = new ComputeContext(ComputeDeviceTypes.Gpu, new ComputeContextPropertyList(platform), null, IntPtr.Zero); // load opencl source StreamReader streamReader = new StreamReader(LASER_CL_PATH); string clSource = streamReader.ReadToEnd(); streamReader.Close(); // build program. ctxLaserCL = new ComputeProgram(clooCtx, clSource); // compile opencl source ctxLaserCL.Build(null, null, null, IntPtr.Zero); // load chosen kernel from program ctxMinMaxKernel = ctxLaserCL.CreateKernel("minMaxValues"); ctxMaskImageKernel = ctxLaserCL.CreateKernel("maskImage"); ctxCenterMassKernel = ctxLaserCL.CreateKernel("centerMass"); ctxTransform3DKernel = ctxLaserCL.CreateKernel("transformPixelsTo3D"); // create a command queue with first gpu found queue = new ComputeCommandQueue(clooCtx, clooCtx.Devices[0], ComputeCommandQueueFlags.None); // execute kernel events = new ComputeEventList(); } catch (Exception ex) { MessageBox.Show(ex.Message); MessageBox.Show(ctxLaserCL.GetBuildLog(clooCtx.Devices[0])); } }
public override void RunKernel(ComputeContext context, ComputeKernel kernel, ComputeCommandQueue commands, long[] dimensions) { var tradeprofitsBuffer = new ComputeBuffer <short>(context, ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.CopyHostPointer, TradeProfits); var tradearbitrageBuffer = new ComputeBuffer <byte>(context, ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.CopyHostPointer, TradeArbitrage); var fit_functionBuffer = new ComputeBuffer <float>(context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.CopyHostPointer, FitFunction); kernel.SetMemoryArgument(0, allvarsBuffer); kernel.SetMemoryArgument(1, loboundsBuffer); kernel.SetMemoryArgument(2, upboundsBuffer); kernel.SetMemoryArgument(3, tradeprofitsBuffer); kernel.SetMemoryArgument(4, tradearbitrageBuffer); kernel.SetMemoryArgument(5, fit_functionBuffer); kernel.SetValueArgument <int>(6, VariablesCount); var eventList = new ComputeEventList(); commands.Execute(kernel, null, dimensions, null, eventList); commands.ReadFromBuffer(fit_functionBuffer, ref FitFunction, true, null); commands.Finish(); }
// -- Methods ------------------------------------------------------------ public void BuildOpenCL() { cl_Source = File.ReadAllText(Environment.CurrentDirectory + "/OpenClKernel/sha256_kernal.cl"); cl_Properties = new ComputeContextPropertyList(cl_Platform); cl_Context = new ComputeContext(cl_Devices, cl_Properties, null, IntPtr.Zero); // Create the command queue. This is used to control kernel execution and manage read/write/copy operations. cl_Commands = new ComputeCommandQueue(cl_Context, cl_Context.Devices[0], ComputeCommandQueueFlags.None); // Create program object cl_Program = new ComputeProgram(cl_Context, cl_Source); //Compiles the source codes. cl_Program.Build(null, null, null, IntPtr.Zero); // Create the kernel function cl_Kernel = cl_Program.CreateKernel("search"); }
public OpenCLContext() { platform = ComputePlatform.Platforms[0]; Context = new ComputeContext(ComputeDeviceTypes.Gpu, new ComputeContextPropertyList(platform), null, IntPtr.Zero); queue = new ComputeCommandQueue(Context, Context.Devices[0], ComputeCommandQueueFlags.None); string clSource; using (StreamReader sr = new StreamReader("displayKernels.cl")) { clSource = sr.ReadToEnd(); } using (StreamReader sr = new StreamReader("simulationKernels.cl")) { clSource += sr.ReadToEnd(); } program = new ComputeProgram(Context, clSource); program.Build(null, null, null, IntPtr.Zero); }
public List <Vector2> CalculateSquareDistance(List <Vector2> positions) { // Create data var xResultData = new float[positions.Count]; var yResultData = new float[positions.Count]; var xData = positions.Select(x => x.X).ToArray(); var yData = positions.Select(x => x.Y).ToArray(); // Put data on buffers var xResultBuffer = new ComputeBuffer <float>(context, ComputeMemoryFlags.WriteOnly | ComputeMemoryFlags.CopyHostPointer, xResultData); var yResultBuffer = new ComputeBuffer <float>(context, ComputeMemoryFlags.WriteOnly | ComputeMemoryFlags.CopyHostPointer, yResultData); var xBuffer = new ComputeBuffer <float>(context, ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.CopyHostPointer, xData); var yBuffer = new ComputeBuffer <float>(context, ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.CopyHostPointer, yData); // Set memory arguments to kernel kernel.SetMemoryArgument(0, xResultBuffer); kernel.SetMemoryArgument(1, yResultBuffer); kernel.SetMemoryArgument(2, xBuffer); kernel.SetMemoryArgument(3, yBuffer); // Create queue var queue = new ComputeCommandQueue(context, device, ComputeCommandQueueFlags.None); queue.Execute(kernel, null, new long[] { positions.Count }, null, null); // Read data queue.ReadFromBuffer(xResultBuffer, ref xResultData, true, null); queue.ReadFromBuffer(yResultBuffer, ref yResultData, true, null); var result = new List <Vector2>(); for (int i = 0; i < xResultData.Length; i++) { result.Add(new Vector2(xResultData[i], xResultData[i])); } return(result); }
public VoxelGrid(ComputeCommandQueue commandQueue, float gridWidth, int gridResolution) { _commandQueue = commandQueue; _gridWidth = gridWidth; GridResolution = gridResolution; CellSize = gridWidth / gridResolution; Vector3 halfGridWidth = new Vector3(gridWidth/2.0f, gridWidth/2.0f, gridWidth/2.0f); _gridOrigin = -halfGridWidth; // Create voxel grid. gridResolution^3 cells int cellCount = gridResolution * gridResolution * gridResolution; _voxelArray = new Voxel[cellCount]; unsafe { fixed (Voxel* gridData = _voxelArray) { _grid = new ComputeImage3D(_commandQueue.Context, ComputeMemoryFlags.CopyHostPointer | ComputeMemoryFlags.ReadOnly, _imageFormat, GridResolution, GridResolution, GridResolution, 0, 0, (IntPtr)gridData); } } // Create array to hold primitives. VectorsPerVoxel = 16; // Low value for testing; _geometryArray = new Triangle[cellCount * VectorsPerVoxel]; Geometry = new ComputeBuffer<Triangle>(_commandQueue.Context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.UseHostPointer, _geometryArray); // Create array for lights _pointLightArray = new SimplePointLight[InitialPointLightArraySize]; PointLightCount = 0; _pointLightBuffer = new ComputeBuffer<SimplePointLight>(_commandQueue.Context, ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.UseHostPointer, _pointLightArray); syncBuffers(); }
public static void Run(TextWriter log, ComputeContext context) { StartTest(log, "Dummy test"); try { ComputeCommandQueue commands = new ComputeCommandQueue(context, context.Devices[0], ComputeCommandQueueFlags.None); log.WriteLine("Original content:"); Random rand = new Random(); int count = 6; long[] bufferContent = new long[count]; for (int i = 0; i < count; i++) { bufferContent[i] = (long)(rand.NextDouble() * long.MaxValue); log.WriteLine("\t" + bufferContent[i]); } ComputeBuffer<long> buffer = new ComputeBuffer<long>(context, ComputeMemoryFlags.CopyHostPointer, bufferContent); IntPtr mappedPtr = commands.Map(buffer, false, ComputeMemoryMappingFlags.Read, 0, bufferContent.Length, null); commands.Finish(); log.WriteLine("Mapped content:"); for (int i = 0; i < bufferContent.Length; i++) { IntPtr ptr = new IntPtr(mappedPtr.ToInt64() + i * sizeof(long)); log.WriteLine("\t" + Marshal.ReadInt64(ptr)); } commands.Unmap(buffer, ref mappedPtr, null); } catch (Exception e) { log.WriteLine(e.ToString()); } EndTest(log, "Dummy test"); }
/// <summary> /// Creates an instance of OpenCLProgram, consisting of all kernel methods in all the specified files. /// </summary> public OpenCLProgram(params string[] filePaths) { if (filePaths.Length < 1) { throw new ArgumentException("Specify at least one source file to compile."); } // Pick first platform SelectBestDevice(); // Load OpenCL code string clSource = ""; foreach (string filePath in filePaths) { try { var streamReader = new StreamReader(filePath); clSource += streamReader.ReadToEnd(); streamReader.Close(); } catch { throw new Exception("File not found:\n" + filePath); } } // Create program with OpenCL code program = new ComputeProgram(context, clSource); // Compile OpenCL code try { program.Build(null, null, null, IntPtr.Zero); } catch { throw new Exception("Error in kernel code:\n" + program.GetBuildLog(context.Devices[0])); } // create a command queue with first gpu found queue = new ComputeCommandQueue(context, context.Devices[0], 0); }
public static void Initialize(ComputeDeviceTypes selectedComputeDeviceTypes, int platformId = 0, int deviceIndex = 0) { Platform = ComputePlatform.Platforms[platformId]; Devices = Platform .Devices .Where(d => (long)d.Type == (long)selectedComputeDeviceTypes) .ToArray(); DeviceIndex = deviceIndex; int id = 0; foreach (var item in Devices) { Console.WriteLine($"Compute[{item.Type},{id}], {item.Name}, {item.VersionString}, Driver:{item.DriverVersion}"); id++; } if (Devices.Length > 0) { Context = new ComputeContext( Devices, new ComputeContextPropertyList(Platform), null, IntPtr.Zero ); CommandQueue = new ComputeCommandQueue( Context, Devices[DeviceIndex], ComputeCommandQueueFlags.None ); Enable = true; Console.WriteLine("Device Initialized"); } }
public GraphicsInterop() { var glHandle = ((IGraphicsContextInternal)GraphicsContext.CurrentContext).Context.Handle; var wglHandle = wglGetCurrentDC(); _device = ComputePlatform.Platforms[0].Devices[0]; var p1 = new ComputeContextProperty(ComputeContextPropertyName.Platform, Device.Platform.Handle.Value); var p2 = new ComputeContextProperty(ComputeContextPropertyName.CL_GL_CONTEXT_KHR, glHandle); var p3 = new ComputeContextProperty(ComputeContextPropertyName.CL_WGL_HDC_KHR, wglHandle); var cpl = new ComputeContextPropertyList(new[] { p1, p2, p3 }); _context = new ComputeContext(ComputeDeviceTypes.Gpu, cpl, null, IntPtr.Zero); _queue = new ComputeCommandQueue(Context, Device, ComputeCommandQueueFlags.None); GL.ClearColor(0f, 0f, 1f, 1f); GL.MatrixMode(MatrixMode.Projection); GL.LoadIdentity(); GL.Ortho(0, 1.0f, 0, 1.0f, -1.0f, 1.0f); GL.MatrixMode(MatrixMode.Modelview); GL.LoadIdentity(); GL.GenBuffers(1, out _pub); GL.Enable(EnableCap.Texture2D); _texture = GL.GenTexture(); }
static void Main(string[] args) { int[] r1 = new int[] {1, 2, 3, 4}; int[] r2 = new int[] {4, 3, 2, 1}; int rowSize = r1.Length; // pick first platform ComputePlatform platform = ComputePlatform.Platforms[0]; // create context with all gpu devices ComputeContext context = new ComputeContext(ComputeDeviceTypes.Gpu, new ComputeContextPropertyList(platform), null, IntPtr.Zero); // create a command queue with first gpu found ComputeCommandQueue queue = new ComputeCommandQueue(context, context.Devices[0], ComputeCommandQueueFlags.None); // load opencl source and // create program with opencl source ComputeProgram program = new ComputeProgram(context, CalculateKernel); // compile opencl source program.Build(null, null, null, IntPtr.Zero); // load chosen kernel from program ComputeKernel kernel = program.CreateKernel("Calc"); // allocate a memory buffer with the message (the int array) ComputeBuffer<int> row1Buffer = new ComputeBuffer<int>(context, ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.UseHostPointer, r1); // allocate a memory buffer with the message (the int array) ComputeBuffer<int> row2Buffer = new ComputeBuffer<int>(context, ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.UseHostPointer, r2); kernel.SetMemoryArgument(0, row1Buffer); // set the integer array kernel.SetMemoryArgument(1, row2Buffer); // set the integer array kernel.SetValueArgument(2, rowSize); // set the array size // execute kernel queue.ExecuteTask(kernel, null); // wait for completion queue.Finish(); Console.WriteLine("Finished"); Console.ReadKey(); }
/// <summary> /// Executes the specified kernel function name. /// </summary> /// <typeparam name="TSource">The type of the source.</typeparam> /// <param name="functionName">Name of the function.</param> /// <param name="inputs">The inputs.</param> /// <param name="returnInputVariable">The return result.</param> /// <returns></returns> /// <exception cref="ExecutionException"> /// </exception> public override void Execute <TSource>(string functionName, params object[] args) { ComputeKernel kernel = _compiledKernels.FirstOrDefault(x => (x.FunctionName == functionName)); ComputeCommandQueue commands = new ComputeCommandQueue(_context, _defaultDevice, ComputeCommandQueueFlags.None); if (kernel == null) { throw new ExecutionException(string.Format("Kernal function {0} not found", functionName)); } try { var ndobject = (TSource[])args.FirstOrDefault(x => (x.GetType() == typeof(TSource[]))); long length = ndobject != null ? ndobject.Length : 1; var buffers = BuildKernelArguments <TSource>(args, kernel, length); commands.Execute(kernel, null, new long[] { length }, null, null); foreach (var item in buffers) { TSource[] r = (TSource[])args[item.Key]; commands.ReadFromBuffer(item.Value, ref r, true, null); //args[item.Key] = r; item.Value.Dispose(); } commands.Finish(); } catch (Exception ex) { throw new ExecutionException(ex.Message); } finally { commands.Dispose(); } }
/// <summary> /// Run kernel against all elements. /// </summary> /// <typeparam name="TSource">Struct type that corresponds to kernel function type</typeparam> /// <param name="array">Array of elements to process</param> /// <param name="kernelCode">The code of kernel function</param> /// <param name="kernelSelector">Method that selects kernel by function name; if null uses first</param> /// <param name="deviceSelector">Method that selects device by index, description, OpenCL version; if null uses first</param> public static void ClooForEach <TSource>(this TSource[] array, string kernelCode, Func <string, bool> kernelSelector = null, Func <int, string, Version, bool> deviceSelector = null) where TSource : struct { kernelSelector = kernelSelector ?? ((k) => true); deviceSelector = deviceSelector ?? ((i, d, v) => true); var device = ComputePlatform.Platforms.SelectMany(p => p.Devices).Where((d, i) => deviceSelector(i, $"{d.Name} {d.DriverVersion}", d.Version)).First(); var properties = new ComputeContextPropertyList(device.Platform); using (var context = new ComputeContext(new[] { device }, properties, null, IntPtr.Zero)) using (var program = new ComputeProgram(context, kernelCode)) { program.Build(new[] { device }, null, null, IntPtr.Zero); var kernels = program.CreateAllKernels().ToList(); try { var kernel = kernels.First((k) => kernelSelector(k.FunctionName)); using (var primesBuffer = new ComputeBuffer <TSource>(context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.UseHostPointer, array)) { kernel.SetMemoryArgument(0, primesBuffer); using (var queue = new ComputeCommandQueue(context, context.Devices[0], 0)) { queue.Execute(kernel, null, new long[] { primesBuffer.Count }, null, null); queue.Finish(); queue.ReadFromBuffer(primesBuffer, ref array, true, null); } } } finally { kernels.ForEach(k => k.Dispose()); } } }
public GraphicsInterop() { var glHandle = ((IGraphicsContextInternal)GraphicsContext.CurrentContext).Context.Handle; var wglHandle = wglGetCurrentDC(); _device = ComputePlatform.Platforms[0].Devices[0]; var p1 = new ComputeContextProperty(ComputeContextPropertyName.Platform, Device.Platform.Handle.Value); var p2 = new ComputeContextProperty(ComputeContextPropertyName.CL_GL_CONTEXT_KHR, glHandle); var p3 = new ComputeContextProperty(ComputeContextPropertyName.CL_WGL_HDC_KHR, wglHandle); var cpl = new ComputeContextPropertyList(new[] { p1, p2, p3 }); _context = new ComputeContext(ComputeDeviceTypes.Gpu, cpl, null, IntPtr.Zero); _queue = new ComputeCommandQueue(Context, Device, ComputeCommandQueueFlags.None); GL.ClearColor(0f, 0f, 1f, 1f); GL.MatrixMode(MatrixMode.Projection); GL.LoadIdentity(); GL.Ortho(0, 1.0f, 0, 1.0f, -1.0f, 1.0f); GL.MatrixMode(MatrixMode.Modelview); GL.LoadIdentity(); GL.GenBuffers(1, out _pub); GL.Enable(EnableCap.Texture2D); _texture = GL.GenTexture(); }
// 26 ms 4096x4096@512 iter with 1024 cores static void SetupCUDA(string sourceFile) { //var watch = System.Diagnostics.Stopwatch.StartNew(); // pick first platform platform = ComputePlatform.Platforms[0]; // create context with all gpu devices context = new ComputeContext(ComputeDeviceTypes.Gpu, new ComputeContextPropertyList(platform), null, IntPtr.Zero); // LEAK // create a command queue with first gpu found queue = new ComputeCommandQueue(context, context.Devices[0], ComputeCommandQueueFlags.None); // load opencl source using (var streamReader = new StreamReader(sourceFile)) { string clSource = streamReader.ReadToEnd(); // create program with opencl source program = new ComputeProgram(context, clSource); // compile opencl source program.Build(null, null, null, IntPtr.Zero); // load chosen kernel from program kernel = program.CreateKernel("mandel"); // allocate a memory buffer with the message messageBuffer = new ComputeBuffer <int>(context, ComputeMemoryFlags.WriteOnly | ComputeMemoryFlags.UseHostPointer, message); gradientBuffer = new ComputeBuffer <int>(context, ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.UseHostPointer, gradient); streamReader.Close(); } }
// -- Methods ------------------------------------------------------------ public void BuildOpenCL() { //String x = Environment.CurrentDirectory; //cl_Source = File.ReadAllText(x + "\\" + OPENCL_CUSTOM); //cl_Source = File.ReadAllText("..\\..\\..\\OpenCL\\" + OPENCL_CUSTOM); if (KernelSelection == OPENCL_CUSTOM) { cl_Source = Encoding.UTF8.GetString(Properties.Resources.sha256_kernal); } else if (KernelSelection == OPENCL_CUSTOM_MIN) { cl_Source = Encoding.UTF8.GetString(Properties.Resources.sha256_kernal_wMin); } else { cl_Source = Encoding.UTF8.GetString(Properties.Resources.sha256_kernal); } cl_Properties = new ComputeContextPropertyList(cl_Platform); cl_Context = new ComputeContext(cl_Devices, cl_Properties, null, IntPtr.Zero); // Create the command queue. This is used to control kernel execution and manage read/write/copy operations. cl_Commands = new ComputeCommandQueue(cl_Context, cl_Context.Devices[0], ComputeCommandQueueFlags.None); // Create program object cl_Program = new ComputeProgram(cl_Context, cl_Source); //Compiles the source codes. cl_Program.Build(null, null, null, IntPtr.Zero); // Create the kernel function cl_Kernel = cl_Program.CreateKernel("search"); }
public OpenCLBuffer(OpenCLProgram ocl, T[] buffer, int flags = ON_DEVICE + ON_HOST + READ_WRITE) { _queue = ocl.queue; int clflags = (int)ComputeMemoryFlags.UseHostPointer; if ((flags & READ_ONLY) > 0) { clflags += (int)ComputeMemoryFlags.ReadOnly; } if ((flags & WRITE_ONLY) > 0) { clflags += (int)ComputeMemoryFlags.WriteOnly; } if ((flags & READ_WRITE) > 0) { clflags += (int)ComputeMemoryFlags.ReadWrite; } _cpubuffer = buffer; if ((flags & ON_DEVICE) > 0) { _gpubuffer = new ComputeBuffer <T>(ocl.context, (ComputeMemoryFlags)clflags, _cpubuffer); CopyToDevice(); } }
void DestroyClBuffers() { if (clImage != null) { clImage.Dispose(); clImage = null; } if (result != null) { result.Dispose(); result = null; } if (cmap != null) { cmap.Dispose(); cmap = null; } if (clCommands != null) { clCommands.Dispose(); clCommands = null; } if (clKernel != null) { clKernel.Dispose(); clKernel = null; } if (clProgram != null) { clProgram.Dispose(); clProgram = null; } clDirty = true; }
/// <summary> /// Computes the potential field at all points. /// </summary> /// <param name="queue">The queue to execute the kernel on.</param> /// <param name="field">The calculated field points.</param> private void ComputeField(ComputeCommandQueue queue, float[] field) { var ball = new Vector2((float)currentEnvironment.CurrentBall.Position.X - currentEnvironment.FieldBounds.Left, (float)currentEnvironment.CurrentBall.Position.Y - currentEnvironment.FieldBounds.Bottom ); var goalTarget = new Vector2((97.3632f) - currentEnvironment.FieldBounds.Left, (33.932f + 49.6801f) / 2.0f - currentEnvironment.FieldBounds.Bottom); var ballvel = new Vector2((float)currentEnvironment.CurrentBall.Position.X - goalTarget.X - currentEnvironment.FieldBounds.Left, (float)currentEnvironment.CurrentBall.Position.Y - goalTarget.Y - currentEnvironment.FieldBounds.Bottom); // Collect together all the points that will repel the robot var repulsers = currentEnvironment.Opponents.Select(o => o.Position).Concat( currentEnvironment.Home.Select(h => h.Position)).Select( p => new Vector2((float)p.X - currentEnvironment.FieldBounds.Left, (float)p.Y - currentEnvironment.FieldBounds.Bottom)).ToArray(); using (var inRepulsers = new ComputeBuffer <Vector2>(context, ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.CopyHostPointer, repulsers)) { kernel.SetValueArgument(0, ball); kernel.SetValueArgument(1, GridResolution); kernel.SetMemoryArgument(2, inRepulsers); kernel.SetMemoryArgument(3, outCl); kernel.SetValueArgument(4, ballvel); queue.Execute(kernel, new long[] { 0 }, new long[] { gridWidth, gridHeight }, new long[] { WorkGroupSize, WorkGroupSize }, null); queue.ReadFromBuffer(outCl, ref field, true, null); } }
/// <summary> /// Entry point for a standard work thread. /// </summary> private void WorkThread() { InitializeOpenCL(); try { // continue working until canceled while (!cts.IsCancellationRequested) Work(Context.GetWork(this, GetType().Name)); } catch (OperationCanceledException) { // ignore } clQueue.Finish(); clKernel.Dispose(); clKernel = null; clBuffer0.Dispose(); clBuffer0 = null; clBuffer1.Dispose(); clBuffer1 = null; clQueue.Dispose(); clQueue = null; clDevice = null; clProgram.Dispose(); clProgram = null; clContext.Dispose(); clContext = null; }
protected override void RunInternal() { int count = 10; float[] arrA = new float[count]; float[] arrB = new float[count]; float[] arrC = new float[count]; Random rand = new Random(); for (int i = 0; i < count; i++) { arrA[i] = (float)(rand.NextDouble() * 100); arrB[i] = (float)(rand.NextDouble() * 100); } ComputeBuffer<float> a = new ComputeBuffer<float>(context, ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.CopyHostPointer, arrA); ComputeBuffer<float> b = new ComputeBuffer<float>(context, ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.CopyHostPointer, arrB); ComputeBuffer<float> c = new ComputeBuffer<float>(context, ComputeMemoryFlags.WriteOnly, arrC.Length); ComputeProgram program = new ComputeProgram(context, new string[] { kernelSource }); program.Build(null, null, null, IntPtr.Zero); ComputeKernel kernel = program.CreateKernel("VectorAdd"); kernel.SetMemoryArgument(0, a); kernel.SetMemoryArgument(1, b); kernel.SetMemoryArgument(2, c); ComputeCommandQueue commands = new ComputeCommandQueue(context, context.Devices[0], ComputeCommandQueueFlags.None); ComputeEventList events = new ComputeEventList(); commands.Execute(kernel, null, new long[] { count }, null, events); arrC = new float[count]; GCHandle arrCHandle = GCHandle.Alloc(arrC, GCHandleType.Pinned); commands.Read(c, false, 0, count, arrCHandle.AddrOfPinnedObject(), events); commands.Finish(); arrCHandle.Free(); for (int i = 0; i < count; i++) Console.WriteLine("{0} + {1} = {2}", arrA[i], arrB[i], arrC[i]); }
protected OpenCLMiner(Device aDevice, String aAlgorithmName) : base(aDevice, aAlgorithmName) { mDevice = aDevice; mQueue = new ComputeCommandQueue(Context, ComputeDevice, ComputeCommandQueueFlags.None); }
/// <summary> /// Renders the image based on the <see cref="latestEnvironment"/>. /// </summary> /// <param name="sender">The sender.</param> /// <param name="doWorkEventArgs">The <see cref="System.ComponentModel.DoWorkEventArgs"/> instance containing the event data.</param> /// <remarks> /// Buffers <see cref="latestEnvironment"/> into <see cref="currentEnvironment"/> before rendering, to ensure new incoming /// data doesn't interfere, even if it comes in while still rendering. /// </remarks> private void RenderImage(object sender, DoWorkEventArgs doWorkEventArgs) { int type; // Buffer the environment data lock (latestEnvironmentLocker) { currentEnvironment = latestEnvironment; currentBallVelocity = latestBallVelocity; type = fieldType; } // Create the computation queue (this can't be saved and reused, not sure why) var queue = new ComputeCommandQueue(context, context.Devices[0], ComputeCommandQueueFlags.None); switch (type) { case 0: // Calculate the potential field ComputeField(queue, points); break; case 1: ComputeFinalApproachField(queue, points); break; case 2: ComputePosessionField(queue, points); break; } // Draw a picture of it var bitmap = RenderPoints(queue, points, outCl); // Calculate the field gradients ComputeGradient(queue, gradientPoints); // Draw a picture of them var bitmap2 = RenderPoints(queue, gradientPoints, outGradient); // Anonymous function which refreshes the images shown on the UI. Has to be called on the UI thread to allow access // to the UI controls. Action refresh = () => { using (var stream = new MemoryStream()) { bitmap.Save(stream, ImageFormat.Png); stream.Seek(0, SeekOrigin.Begin); var decoder = BitmapDecoder.Create(stream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad); var writeable = new WriteableBitmap(decoder.Frames.Single()); writeable.Freeze(); FieldImage.Source = writeable; } using (var stream = new MemoryStream()) { bitmap2.Save(stream, ImageFormat.Png); stream.Seek(0, SeekOrigin.Begin); var decoder = BitmapDecoder.Create(stream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad); var writeable = new WriteableBitmap(decoder.Frames.Single()); writeable.Freeze(); GradientImage.Source = writeable; } }; Dispatcher.Invoke(refresh); queue.Finish(); queue.Dispose(); }
public TerrainGen() { #if CPU_DEBUG var platform = ComputePlatform.Platforms[1]; #else var platform = ComputePlatform.Platforms[0]; #endif _devices = new List<ComputeDevice>(); _devices.Add(platform.Devices[0]); _properties = new ComputeContextPropertyList(platform); _context = new ComputeContext(_devices, _properties, null, IntPtr.Zero); _cmdQueue = new ComputeCommandQueue(_context, _devices[0], ComputeCommandQueueFlags.None); #region setup generator kernel bool loadFromSource = Gbl.HasRawHashChanged[Gbl.RawDir.Scripts]; loadFromSource = true; _chunkWidthInBlocks = Gbl.LoadContent<int>("TGen_ChunkWidthInBlocks"); _chunkWidthInVerts = _chunkWidthInBlocks + 1; _blockWidth = Gbl.LoadContent<int>("TGen_BlockWidthInMeters"); float lacunarity = Gbl.LoadContent<float>("TGen_Lacunarity"); float gain = Gbl.LoadContent<float>("TGen_Gain"); int octaves = Gbl.LoadContent<int>("TGen_Octaves"); float offset = Gbl.LoadContent<float>("TGen_Offset"); float hScale = Gbl.LoadContent<float>("TGen_HScale"); float vScale = Gbl.LoadContent<float>("TGen_VScale"); _genConstants = new ComputeBuffer<float>(_context, ComputeMemoryFlags.ReadOnly, 8); var genArr = new[]{ lacunarity, gain, offset, octaves, hScale, vScale, _blockWidth, _chunkWidthInBlocks }; _cmdQueue.WriteToBuffer(genArr, _genConstants, false, null); if (loadFromSource){ _generationPrgm = new ComputeProgram(_context, Gbl.LoadScript("TGen_Generator")); #if CPU_DEBUG _generationPrgm.Build(null, @"-g -s D:\Projects\Gondola\Scripts\GenTerrain.cl", null, IntPtr.Zero); //use option -I + scriptDir for header search #else _generationPrgm.Build(null, "", null, IntPtr.Zero);//use option -I + scriptDir for header search #endif Gbl.SaveBinary(_generationPrgm.Binaries, "TGen_Generator"); } else{ var binary = Gbl.LoadBinary("TGen_Generator"); _generationPrgm = new ComputeProgram(_context, binary, _devices); _generationPrgm.Build(null, "", null, IntPtr.Zero); } //loadFromSource = false; _terrainGenKernel = _generationPrgm.CreateKernel("GenTerrain"); _normalGenKernel = _generationPrgm.CreateKernel("GenNormals"); //despite the script using float3 for these fields, we need to consider it to be float4 because the //implementation is basically a float4 wrapper that uses zero for the last variable _geometry = new ComputeBuffer<float>(_context, ComputeMemoryFlags.None, _chunkWidthInVerts*_chunkWidthInVerts*4); _normals = new ComputeBuffer<ushort>(_context, ComputeMemoryFlags.None, _chunkWidthInVerts * _chunkWidthInVerts * 4); _binormals = new ComputeBuffer<byte>(_context, ComputeMemoryFlags.None, _chunkWidthInVerts*_chunkWidthInVerts*4); _tangents = new ComputeBuffer<byte>(_context, ComputeMemoryFlags.None, _chunkWidthInVerts*_chunkWidthInVerts*4); _uvCoords = new ComputeBuffer<float>(_context, ComputeMemoryFlags.None, _chunkWidthInVerts*_chunkWidthInVerts*2); _terrainGenKernel.SetMemoryArgument(0, _genConstants); _terrainGenKernel.SetMemoryArgument(3, _geometry); _terrainGenKernel.SetMemoryArgument(4, _uvCoords); _normalGenKernel.SetMemoryArgument(0, _genConstants); _normalGenKernel.SetMemoryArgument(3, _geometry); _normalGenKernel.SetMemoryArgument(4, _normals); _normalGenKernel.SetMemoryArgument(5, _binormals); _normalGenKernel.SetMemoryArgument(6, _tangents); #endregion #region setup quadtree kernel if (loadFromSource){ _qTreePrgm = new ComputeProgram(_context, Gbl.LoadScript("TGen_QTree")); #if CPU_DEBUG _qTreePrgm.Build(null, @"-g -s D:\Projects\Gondola\Scripts\Quadtree.cl", null, IntPtr.Zero); #else _qTreePrgm.Build(null, "", null, IntPtr.Zero); #endif Gbl.SaveBinary(_qTreePrgm.Binaries, "TGen_QTree"); } else{ var binary = Gbl.LoadBinary("TGen_QTree"); _qTreePrgm = new ComputeProgram(_context, binary, _devices); _qTreePrgm.Build(null, "", null, IntPtr.Zero); } _qTreeKernel = _qTreePrgm.CreateKernel("QuadTree"); _crossCullKernel = _qTreePrgm.CreateKernel("CrossCull"); _activeVerts = new ComputeBuffer<byte>(_context, ComputeMemoryFlags.None, _chunkWidthInVerts*_chunkWidthInVerts); _dummy = new ComputeBuffer<int>(_context, ComputeMemoryFlags.None, 50); var rawNormals = new ushort[_chunkWidthInVerts * _chunkWidthInVerts * 4]; _emptyVerts = new byte[_chunkWidthInVerts*_chunkWidthInVerts]; for (int i = 0; i < _emptyVerts.Length; i++){ _emptyVerts[i] = 1; } _cmdQueue.WriteToBuffer(rawNormals, _normals, true, null); _cmdQueue.WriteToBuffer(_emptyVerts, _activeVerts, true, null); _qTreeKernel.SetValueArgument(1, _chunkWidthInBlocks); _qTreeKernel.SetMemoryArgument(2, _normals); _qTreeKernel.SetMemoryArgument(3, _activeVerts); _qTreeKernel.SetMemoryArgument(4, _dummy); _crossCullKernel.SetValueArgument(1, _chunkWidthInBlocks); _crossCullKernel.SetMemoryArgument(2, _normals); _crossCullKernel.SetMemoryArgument(3, _activeVerts); _crossCullKernel.SetMemoryArgument(4, _dummy); #endregion #region setup winding kernel if (loadFromSource){ _winderPrgm = new ComputeProgram(_context, Gbl.LoadScript("TGen_VertexWinder")); #if CPU_DEBUG _winderPrgm.Build(null, @"-g -s D:\Projects\Gondola\Scripts\VertexWinder.cl", null, IntPtr.Zero); #else _winderPrgm.Build(null, "", null, IntPtr.Zero); #endif Gbl.SaveBinary(_winderPrgm.Binaries, "TGen_VertexWinder"); } else{ var binary = Gbl.LoadBinary("TGen_VertexWinder"); _winderPrgm = new ComputeProgram(_context, binary, _devices); _winderPrgm.Build(null, "", null, IntPtr.Zero); } _winderKernel = _winderPrgm.CreateKernel("VertexWinder"); _indicies = new ComputeBuffer<int>(_context, ComputeMemoryFlags.None, (_chunkWidthInBlocks)*(_chunkWidthInBlocks)*8); _winderKernel.SetMemoryArgument(0, _activeVerts); _winderKernel.SetMemoryArgument(1, _indicies); _emptyIndices = new int[(_chunkWidthInBlocks)*(_chunkWidthInBlocks)*8]; for (int i = 0; i < (_chunkWidthInBlocks)*(_chunkWidthInBlocks)*8; i++){ _emptyIndices[i] = 0; } _cmdQueue.WriteToBuffer(_emptyIndices, _indicies, true, null); #endregion if (loadFromSource){ Gbl.AllowMD5Refresh[Gbl.RawDir.Scripts] = true; } _cmdQueue.Finish(); }
public static void Run(TextWriter log, ComputeContext context) { StartTest(log, "Vector addition test"); try { int count = 10; float[] arrA = new float[count]; float[] arrB = new float[count]; float[] arrC = new float[count]; Random rand = new Random(); for (int i = 0; i < count; i++) { arrA[i] = (float)(rand.NextDouble() * 100); arrB[i] = (float)(rand.NextDouble() * 100); } ComputeBuffer<float> a = new ComputeBuffer<float>(context, ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.CopyHostPointer, arrA); ComputeBuffer<float> b = new ComputeBuffer<float>(context, ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.CopyHostPointer, arrB); ComputeBuffer<float> c = new ComputeBuffer<float>(context, ComputeMemoryFlags.WriteOnly, arrC.Length); ComputeProgram program = new ComputeProgram(context, kernelSource); program.Build(null, null, null, IntPtr.Zero); ComputeKernel kernel = program.CreateKernel("VectorAdd"); kernel.SetMemoryArgument(0, a); kernel.SetMemoryArgument(1, b); kernel.SetMemoryArgument(2, c); ComputeCommandQueue commands = new ComputeCommandQueue(context, context.Devices[0], ComputeCommandQueueFlags.None); ICollection<ComputeEventBase> events = new Collection<ComputeEventBase>(); // BUG: ATI Stream v2.2 crash if event list not null. commands.Execute(kernel, null, new long[] { count }, null, events); //commands.Execute(kernel, null, new long[] { count }, null, null); arrC = new float[count]; GCHandle arrCHandle = GCHandle.Alloc(arrC, GCHandleType.Pinned); commands.Read(c, true, 0, count, arrCHandle.AddrOfPinnedObject(), events); arrCHandle.Free(); for (int i = 0; i < count; i++) log.WriteLine("{0} + {1} = {2}", arrA[i], arrB[i], arrC[i]); } catch (Exception e) { log.WriteLine(e.ToString()); } EndTest(log, "Vector addition test"); }
private unsafe void notify(CLProgramHandle programHandle, IntPtr userDataPtr) { uint[] dst = new uint[16]; fixed (uint* dstPtr = dst) { using (var queue = new ComputeCommandQueue(ccontext, device, ComputeCommandQueueFlags.None)) { var buf = new ComputeBuffer<uint>(ccontext, ComputeMemoryFlags.WriteOnly, 16); var kernel = program.CreateKernel("test"); kernel.SetValueArgument(0, 1443351125U); kernel.SetMemoryArgument(1, buf); var eventList = new ComputeEventList(); queue.Execute(kernel, null, new long[] { 16L, 256L, 1048576L }, null, null); queue.Finish(); queue.Read<uint>(buf, true, 0, 16, (IntPtr)dstPtr, null); queue.Finish(); queue.Finish(); } } }
public void Run(ComputeContext context, TextWriter log) { try { // Create the arrays and fill them with random data. int count = 640*480; // float[] arrA = new float[count]; float[] arrB = new float[count]; float[] arrC = new float[count]; Random rand = new Random(); for (int i = 0; i < count; i++) { arrA[i] = (float)(rand.NextDouble() * 100); arrB[i] = (float)(rand.NextDouble() * 100); } // Create the input buffers and fill them with data from the arrays. // Access modifiers should match those in a kernel. // CopyHostPointer means the buffer should be filled with the data provided in the last argument. program = new ComputeProgram(context, clProgramSource); program.Build(null, null, null, IntPtr.Zero); ComputeBuffer<float> a = new ComputeBuffer<float>(context, ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.CopyHostPointer, arrA); //ComputeBuffer<float> b = new ComputeBuffer<float>(context, ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.CopyHostPointer, arrB); // The output buffer doesn't need any data from the host. Only its size is specified (arrC.Length). ComputeBuffer<float> c = new ComputeBuffer<float>(context, ComputeMemoryFlags.WriteOnly, arrC.Length); // Create and build the opencl program. // Create the kernel function and set its arguments. ComputeKernel kernel = program.CreateKernel("CompareGPUCPU"); DateTime ExecutionStartTime; //Var will hold Execution Starting Time DateTime ExecutionStopTime;//Var will hold Execution Stopped Time TimeSpan ExecutionTime;//Var will count Total Execution Time-Our Main Hero ComputeCommandQueue commands = new ComputeCommandQueue(context, context.Devices[0], ComputeCommandQueueFlags.None); ExecutionStartTime = DateTime.Now; //Gets the system Current date time expressed as local time int repeatTimes = 100; for (int repeatCounter = 0; repeatCounter < repeatTimes; repeatCounter++) { kernel.SetMemoryArgument(0, a); //kernel.SetMemoryArgument(1, b); //kernel.SetMemoryArgument(2, c); kernel.SetMemoryArgument(1, c); // Create the event wait list. An event list is not really needed for this example but it is important to see how it works. // Note that events (like everything else) consume OpenCL resources and creating a lot of them may slow down execution. // For this reason their use should be avoided if possible. //ComputeEventList eventList = new ComputeEventList(); // Create the command queue. This is used to control kernel execution and manage read/write/copy operations. // Execute the kernel "count" times. After this call returns, "eventList" will contain an event associated with this command. // If eventList == null or typeof(eventList) == ReadOnlyCollection<ComputeEventBase>, a new event will not be created. //commands.Execute(kernel, null, new long[] { count }, null, eventList); commands.Execute(kernel, null, new long[] { count }, null, null); // Read back the results. If the command-queue has out-of-order execution enabled (default is off), ReadFromBuffer // will not execute until any previous events in eventList (in our case only eventList[0]) are marked as complete // by OpenCL. By default the command-queue will execute the commands in the same order as they are issued from the host. // eventList will contain two events after this method returns. //commands.ReadFromBuffer(c, ref arrC, false, eventList); commands.ReadFromBuffer(c, ref arrC, false, null); // A blocking "ReadFromBuffer" (if 3rd argument is true) will wait for itself and any previous commands // in the command queue or eventList to finish execution. Otherwise an explicit wait for all the opencl commands // to finish has to be issued before "arrC" can be used. // This explicit synchronization can be achieved in two ways: // 1) Wait for the events in the list to finish, //eventList.Wait(); // 2) Or simply use commands.Finish(); } ExecutionStopTime = DateTime.Now; ExecutionTime = ExecutionStopTime - ExecutionStartTime; double perTaskTime = ExecutionTime.TotalMilliseconds / repeatTimes; log.WriteLine("Use {0} ms using GPU", perTaskTime); // Do that using CPU /* ExecutionStartTime = DateTime.Now; //Gets the system Current date time expressed as local time for (int repeatCounter = 0; repeatCounter < repeatTimes; repeatCounter++) { for (int i = 0; i < count; i++) { //arrC[i] = arrA[i] + arrB[i]; int j; for (j = 0; j < 330 * 10; j++) arrC[i] = arrA[i] + j; } } ExecutionStopTime = DateTime.Now; ExecutionTime = ExecutionStopTime - ExecutionStartTime; perTaskTime = ExecutionTime.TotalMilliseconds / repeatTimes; log.WriteLine("Use {0} ms using CPU", ExecutionTime.TotalMilliseconds.ToString()); */ log.WriteLine("arrA[0]:{0}, arrC[0]:{1}", arrA[0], arrC[0]); } catch (Exception e) { log.WriteLine(e.ToString()); } }
public String SearchPassword (byte[] hash, HashType type, int maxLength, String[] keySpace) { if (type != HashType.MD5) { throw new NotImplementedException ("sums other than MD5 not supported"); } if (maxLength > 6) { throw new NotImplementedException ("doesn't support longer passwords than 7"); } var joinedKeySpace = new List<byte> (); foreach (var k in keySpace) { if (k.Length > 1) { throw new NotImplementedException ("doesn't support longer keyspaces than 1"); } joinedKeySpace.AddRange (Encoding.ASCII.GetBytes (k)); } byte[] resultData = new byte[20]; byte[] keyspaceJoined = joinedKeySpace.ToArray (); var resultBuffer = new ComputeBuffer<byte> (Context, ComputeMemoryFlags.WriteOnly | ComputeMemoryFlags.CopyHostPointer, resultData); var hashBuffer = new ComputeBuffer<byte> (Context, ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.CopyHostPointer, hash); var keyspaceBuffer = new ComputeBuffer<byte> (Context, ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.CopyHostPointer, keyspaceJoined); var passLenBuffer = new ComputeBuffer<byte> (Context, ComputeMemoryFlags.WriteOnly, 1); var flagBuffer = new ComputeBuffer<int> (Context, ComputeMemoryFlags.None, 1); Kernel.SetMemoryArgument (0, hashBuffer); Kernel.SetMemoryArgument (1, keyspaceBuffer); Kernel.SetMemoryArgument (2, resultBuffer); Kernel.SetMemoryArgument (3, passLenBuffer); Kernel.SetMemoryArgument (4, flagBuffer); // execute kernel var queue = new ComputeCommandQueue (Context, Device, ComputeCommandQueueFlags.None); long firstDim = joinedKeySpace.Count; var globalWorksize = new long[] { firstDim, 57 * 57, 57 * 57 }; queue.Execute (Kernel, new long[] { 0, 0, 0 }, globalWorksize, null, null); byte[] passLen = new byte[1]; queue.ReadFromBuffer (resultBuffer, ref resultData, true, null); queue.ReadFromBuffer (passLenBuffer, ref passLen, true, null); String password = null; if (passLen [0] > 0) { logger.Info ("pass len {0}", passLen [0]); password = Encoding.ASCII.GetString (resultData, 0, passLen [0]); logger.Info ("Found password: \"{0}\"", password); } else { logger.Info ("Password not found."); } queue.Finish (); return password; }
public GraphicsInterop(IGraphicsContext context, ComputeDevice device) { var os = Environment.OSVersion.Platform; ComputeContextProperty platformDependantCcp; switch (os) { case PlatformID.Unix: platformDependantCcp = GetUnixCcp(); break; case PlatformID.MacOSX: platformDependantCcp = GetMacCcp(context); break; default: platformDependantCcp = GetWindowsCcp(); break; } var glHandle = ((IGraphicsContextInternal)context).Context.Handle; var p1 = new ComputeContextProperty(ComputeContextPropertyName.Platform, device.Platform.Handle.Value); var p2 = new ComputeContextProperty(ComputeContextPropertyName.CL_GL_CONTEXT_KHR, glHandle); var cpl = new ComputeContextPropertyList(new[] { p1, p2, platformDependantCcp }); _context = new ComputeContext(ComputeDeviceTypes.Gpu, cpl, null, IntPtr.Zero); _queue = new ComputeCommandQueue(Context, device, ComputeCommandQueueFlags.None); GL.ClearColor(0f, 0f, 1f, 1f); GL.MatrixMode(MatrixMode.Projection); GL.LoadIdentity(); GL.Ortho(0, 1.0f, 0, 1.0f, -1.0f, 1.0f); GL.MatrixMode(MatrixMode.Modelview); GL.LoadIdentity(); GL.GenBuffers(1, out _pub); GL.Enable(EnableCap.Texture2D); _texture = GL.GenTexture(); }
/// <summary> /// Initializes local fields and the underlying compute context. /// </summary> public void Initialize() { if (this.context == null) { var devices = ComputePlatform.Platforms.SelectMany(a => a.Devices).Where(a => a.Extensions.Contains("cl_khr_fp64")).Take(1).ToArray(); ComputeContextPropertyList list = new ComputeContextPropertyList(devices[0].Platform); this.context = new ComputeContext(devices, list, null, IntPtr.Zero); } this.program = new ComputeProgram(this.context, File.ReadAllText("Mandelbrot.cl")); this.program.Build(null, null, null, IntPtr.Zero); this.mandelbrot = this.program.CreateKernel("Mandelbrot"); this.toBitmap = this.program.CreateKernel("ToBitmap"); this.resultBuffer = new ComputeBuffer<int>(this.context, ComputeMemoryFlags.ReadWrite, this.ImageWidth * this.ImageHeight); this.bitmapBuffer = new ComputeBuffer<byte>(this.context, ComputeMemoryFlags.ReadWrite, this.ImageWidth * this.ImageHeight * 4); this.mandelbrot.SetMemoryArgument(7, this.resultBuffer); this.toBitmap.SetMemoryArgument(1, this.resultBuffer); this.toBitmap.SetMemoryArgument(2, this.bitmapBuffer); this.commandQueue = new ComputeCommandQueue(this.context, this.context.Devices.OrderBy(a => a.Type).Where(a => a.Extensions.Contains("cl_khr_fp64")).First(), ComputeCommandQueueFlags.None); }
/// <summary> /// Attempts to initialize OpenCL for the selected GPU. /// </summary> private void InitializeOpenCL() { // only initialize once if (clKernel != null) return; // select the device we've been instructed to use clDevice = ComputePlatform.Platforms .SelectMany(i => i.Devices) .SingleOrDefault(i => i.Handle.Value == Gpu.CLDeviceHandle.Value); // context we'll be working underneath clContext = new ComputeContext(new ComputeDevice[] { clDevice }, new ComputeContextPropertyList(clDevice.Platform), null, IntPtr.Zero); // queue to control device clQueue = new ComputeCommandQueue(clContext, clDevice, ComputeCommandQueueFlags.None); // buffers to store kernel output clBuffer0 = new ComputeBuffer<uint>(clContext, ComputeMemoryFlags.ReadOnly, 16); clBuffer1 = new ComputeBuffer<uint>(clContext, ComputeMemoryFlags.ReadOnly, 16); // kernel code string kernelCode; using (var rdr = new StreamReader(GetType().Assembly.GetManifestResourceStream("BitMaker.Miner.Gpu.DiabloMiner.cl"))) kernelCode = rdr.ReadToEnd(); clProgram = new ComputeProgram(clContext, kernelCode); try { // build kernel for device clProgram.Build(new ComputeDevice[] { clDevice }, "-D WORKSIZE=" + clDevice.MaxWorkGroupSize, null, IntPtr.Zero); } catch (ComputeException) { throw new Exception(clProgram.GetBuildLog(clDevice)); } clKernel = clProgram.CreateKernel("search"); }
public void Run(IComputeContext context, TextWriter log) { try { log.Write("Creating command queue... "); var commands = new ComputeCommandQueue(context, context.Devices[0], ComputeCommandQueueFlags.None); log.WriteLine("done."); log.Write("Generating data... "); int linearSize = 24; SysIntX2 rectSize = new SysIntX2(4, 6); SysIntX3 cubicSize = new SysIntX3(2, 3, 4); float[] linearIn = new float[linearSize]; float[] linearOut = new float[linearSize]; float[,] rectIn = new float[(int)rectSize.Y, (int)rectSize.X]; float[,] rectOut = new float[(int)rectSize.Y, (int)rectSize.X]; float[,,] cubicIn = new float[(int)cubicSize.Z, (int)cubicSize.Y, (int)cubicSize.X]; float[,,] cubicOut = new float[(int)cubicSize.Z, (int)cubicSize.Y, (int)cubicSize.X]; for (int i = 0; i < linearSize; i++) { linearIn[i] = i; } for (int i = 0; i < (int)rectSize.X; i++) { for (int j = 0; j < (int)rectSize.Y; j++) { rectIn[j, i] = (float)(rectSize.X.ToInt32() * j + i); } } for (int i = 0; i < (int)cubicSize.X; i++) { for (int j = 0; j < (int)cubicSize.Y; j++) { for (int k = 0; k < (int)cubicSize.Z; k++) { cubicIn[k, j, i] = (float)(k * cubicSize.Y.ToInt32() * cubicSize.X.ToInt32() + cubicSize.X.ToInt32() * j + i); } } } log.WriteLine("done."); log.Write("Creating buffer... "); var buffer = new ComputeBuffer <float>(context, ComputeMemoryFlags.ReadWrite, linearSize); log.WriteLine("done."); GC.Collect(); log.Write("Writing to buffer (linear)... "); commands.WriteToBuffer(linearIn, buffer, false, null); log.WriteLine("done."); log.Write("Reading from buffer (linear)... "); commands.ReadFromBuffer(buffer, ref linearOut, false, null); log.WriteLine("done."); GC.Collect(); commands.Finish(); log.Write("Comparing data... "); Compare(linearIn, linearOut); log.WriteLine("passed."); GC.Collect(); log.Write("Writing to buffer (rectangular)... "); commands.WriteToBuffer(rectIn, buffer, false, new SysIntX2(), new SysIntX2(), rectSize, null); log.WriteLine("done."); GC.Collect(); log.Write("Reading from buffer (rectangular)... "); commands.ReadFromBuffer(buffer, ref rectOut, false, new SysIntX2(), new SysIntX2(), rectSize, null); log.WriteLine("done."); GC.Collect(); commands.Finish(); log.Write("Comparing data... "); Compare(rectIn, rectOut); log.WriteLine("passed."); GC.Collect(); log.Write("Writing to buffer (cubic)... "); commands.WriteToBuffer(cubicIn, buffer, false, new SysIntX3(), new SysIntX3(), cubicSize, null); log.WriteLine("done."); GC.Collect(); log.Write("Reading from buffer (cubic)... "); commands.ReadFromBuffer(buffer, ref cubicOut, false, new SysIntX3(), new SysIntX3(), cubicSize, null); log.WriteLine("done."); GC.Collect(); commands.Finish(); log.Write("Comparing data... "); Compare(cubicIn, cubicOut); log.WriteLine("passed."); } catch (Exception e) { log.WriteLine(e.ToString()); } }
bool useGPU = true; // GPU code enabled (from commandline) #endregion Fields #region Methods // initialize renderer: takes in command line parameters passed by template code public void Init( int rt, bool gpu, int platformIdx ) { // pass command line parameters runningTime = rt; useGPU = gpu; gpuPlatform = platformIdx; // initialize accumulator accumulator = new Vector3[screen.width * screen.height]; ClearAccumulator(); // setup scene scene = new Scene(); // setup camera camera = new Camera( screen.width, screen.height ); // Generate randoms Console.Write("Generating randoms....\t"); randoms = new float[1000]; Random r = RTTools.GetRNG(); for (int i = 0; i < 1000; i++) randoms[i] = (float)r.NextDouble(); int variable = r.Next(); Console.WriteLine("Done!"); // initialize required opencl things if gpu is used if (useGPU) { StreamReader streamReader = new StreamReader("../../kernel.cl"); string clSource = streamReader.ReadToEnd(); streamReader.Close(); platform = ComputePlatform.Platforms[0]; context = new ComputeContext(ComputeDeviceTypes.Gpu, new ComputeContextPropertyList(platform), null, IntPtr.Zero); queue = new ComputeCommandQueue(context, context.Devices[0], ComputeCommandQueueFlags.None); program = new ComputeProgram(context, clSource); try { program.Build(null, null, null, IntPtr.Zero); kernel = program.CreateKernel("Main"); sceneBuffer = new ComputeBuffer<Vector4>(context, ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.UseHostPointer, scene.toCL()); rndBuffer = new ComputeBuffer<float>(context, ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.UseHostPointer, randoms); cameraBuffer = new ComputeBuffer<Vector3>(context, ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.UseHostPointer, camera.toCL()); outputBuffer = new ComputeBuffer<int>(context, ComputeMemoryFlags.WriteOnly | ComputeMemoryFlags.UseHostPointer, screen.pixels); skydome = new ComputeBuffer<float>(context, ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.UseHostPointer, scene.Skydome); kernel.SetMemoryArgument(0, outputBuffer); kernel.SetValueArgument(1, screen.width); kernel.SetValueArgument(2, screen.height); kernel.SetMemoryArgument(3, sceneBuffer); kernel.SetValueArgument(4, scene.toCL().Length); kernel.SetMemoryArgument(5, skydome); kernel.SetMemoryArgument(6, cameraBuffer); kernel.SetMemoryArgument(7, rndBuffer); } catch (ComputeException e) { Console.WriteLine("Error in kernel code: {0}", program.GetBuildLog(context.Devices[0])); Console.ReadLine(); useGPU = false; } } else { return; } }
public unsafe void EndSend() { for (int i = 0; i < points.Count; i++) { inx[i].x = (float)points[i].Item3.Real; inx[i].y = (float)points[i].Item3.Imaginary; inc[i].x = (float)points[i].Item4.Real; inc[i].y = (float)points[i].Item4.Imaginary; } _krnl.SetMemoryArgument(0, x); _krnl.SetMemoryArgument(1, c); for (int i = 0; i < _ld.Count; i++) { _krnl.SetMemoryArgument(2 + i, outp[i]); } ComputeCommandQueue command = new ComputeCommandQueue(_context, _context.Devices[0], ComputeCommandQueueFlags.None); command.WriteToBuffer(inx, x, false, null); command.WriteToBuffer(inc, c, false, null); command.Execute(_krnl, null, new long[] { points.Count }, null, null); for (int i = 0; i < _ld.Count; i++) command.ReadFromBuffer(outp[i], ref opl[i], false, null); command.Finish(); output = new Queue<Tuple<int, int, List<ProcessLayer>>>(); for (int i = 0; i < points.Count; i++) { List<ProcessLayer> pl = new List<ProcessLayer>(); for (int ii = 0; ii < _ld.Count; ii++) { ProcessLayer p = _ld[ii].Clone(); p.c_active = opl[ii][i].c_active != 0; p.c_calc = opl[ii][i].c_calc; p.c_cmean = opl[ii][i].c_cmean; p.c_cvariance = opl[ii][i].c_cvariance; p.c_cvarsx = opl[ii][i].c_cvarsx; p.c_isin = opl[ii][i].c_isin != 0; p.c_n = opl[ii][i].c_n; p.c_old2x = new Complex(opl[ii][i].c_old2x.x,opl[ii][i].c_old2x.y); p.c_oldx = new Complex(opl[ii][i].c_oldx.x,opl[ii][i].c_oldx.y); p.c_resn = opl[ii][i].c_resn; p.c_resx = new Complex(opl[ii][i].c_resx.x,opl[ii][i].c_resx.y); p.c_x = new Complex(opl[ii][i].c_x.x,opl[ii][i].c_x.y); pl.Add(p); } output.Enqueue(Tuple.Create(points[i].Item1, points[i].Item2, pl)); } }
/// <summary> /// Executes the specified kernel function name. /// </summary> /// <typeparam name="TSource">The type of the source.</typeparam> /// <param name="functionName">Name of the function.</param> /// <param name="args"></param> /// <exception cref="ExecutionException"> /// </exception> public override void Execute(string functionName, params object[] args) { ValidateArgs(functionName, args); ComputeKernel kernel = _compiledKernels.FirstOrDefault(x => (x.FunctionName == functionName)); ComputeCommandQueue commands = new ComputeCommandQueue(_context, _defaultDevice, ComputeCommandQueueFlags.None); if (kernel == null) { throw new ExecutionException(string.Format("Kernal function {0} not found", functionName)); } try { Array ndobject = (Array)args.FirstOrDefault(x => (x.GetType().IsArray)); List <long> length = new List <long>(); long totalLength = 0; if (ndobject == null) { var xarrayList = args.Where(x => (x.GetType().Name == "XArray" || x.GetType().BaseType.Name == "XArray")).ToList(); foreach (var item in xarrayList) { var xarrayobj = (XArray)item; if (xarrayobj.Direction == Direction.Output) { totalLength = xarrayobj.Count; if (!xarrayobj.IsElementWise) { length = xarrayobj.Sizes.ToList(); } else { length.Add(totalLength); } } } if (totalLength == 0) { var xarrayobj = (XArray)xarrayList[0]; totalLength = xarrayobj.Count; if (!xarrayobj.IsElementWise) { length = xarrayobj.Sizes.ToList(); } else { length.Add(totalLength); } } } else { totalLength = ndobject.Length; for (int i = 0; i < ndobject.Rank; i++) { length.Add(ndobject.GetLength(i)); } } var method = KernelFunctions.FirstOrDefault(x => (x.Name == functionName)); var buffers = BuildKernelArguments(method, args, kernel, totalLength); commands.Execute(kernel, null, length.ToArray(), null, null); for (int i = 0; i < args.Length; i++) { if (args[i].GetType().IsArray) { var ioMode = method.Parameters.ElementAt(i).Value.IOMode; if (ioMode == IOMode.InOut || ioMode == IOMode.Out) { Array r = (Array)args[i]; commands.ReadFromMemory(buffers[i], ref r, true, 0, null); } buffers[i].Dispose(); } else if (args[i].GetType().Name == "XArray" || args[i].GetType().BaseType.Name == "XArray") { var ioMode = method.Parameters.ElementAt(i).Value.IOMode; if (ioMode == IOMode.InOut || ioMode == IOMode.Out) { XArray r = (XArray)args[i]; commands.ReadFromMemory(buffers[i], ref r, true, 0, null); } buffers[i].Dispose(); } } } catch (Exception ex) { throw new ExecutionException(ex.Message); } finally { commands.Finish(); commands.Dispose(); } }
public void InitGPU(int platformIdx) { platform = ComputePlatform.Platforms[platformIdx]; context = new ComputeContext(ComputeDeviceTypes.Gpu, new ComputeContextPropertyList(platform), null, IntPtr.Zero); StreamReader streamReader = new StreamReader("../../program.cl"); string clSource = streamReader.ReadToEnd(); streamReader.Close(); ComputeProgram program = new ComputeProgram(context, clSource); program.Build(null, null, null, IntPtr.Zero); ComputeKernel kernelInit = program.CreateKernel("init"); ComputeKernel kernelUpdate = program.CreateKernel("update"); var flags = ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.UseHostPointer; int aantalPixels = screen.height * screen.width; rays = new GPURay[aantalPixels]; pixels = new Vector3[aantalPixels]; setGPUCameraToCamera(); GPUCamera[] gpuCamArray = { gpuCamera }; ComputeBuffer<GPURay> bufferRays = new ComputeBuffer<GPURay>(context, flags, rays); ComputeBuffer<Vector3> bufferPixels = new ComputeBuffer<Vector3>(context, flags, pixels); ComputeBuffer<GPUCamera> bufferCamera = new ComputeBuffer<GPUCamera>(context, flags, gpuCamArray); kernelUpdate.SetMemoryArgument(0, bufferRays); kernelUpdate.SetMemoryArgument(1, bufferPixels); kernelUpdate.SetMemoryArgument(2, bufferCamera); ComputeCommandQueue queue = new ComputeCommandQueue(context, context.Devices[0], 0); }