// Alea Parallel.For! internal static Image Render1(Bounds bounds) { bounds.AdjustAspectRatio(); var width = bounds.Width; var height = bounds.Height; var scale = (bounds.XMax - bounds.XMin) / width; var resultLength = ColorComponents * width * height; var resultMemory = Gpu.Default.AllocateDevice <byte>(resultLength); var resultDevPtr = new deviceptr <byte>(resultMemory.Handle); Gpu.Default.For(0, width * height, i => { var x = i % width; var y = i / width; var offset = ColorComponents * i; if (offset < resultLength) { // ReSharper disable once PossibleLossOfFraction var c = new Complex { Real = bounds.XMin + x * scale, Imaginary = bounds.YMin + y * scale, }; ComputeMandelbrotAtOffset(resultDevPtr, c, offset); } }); return(BitmapUtility.FromByteArray(Gpu.CopyToHost(resultMemory), width, height)); }
// Custom! internal static Image Render2(Bounds bounds) { bounds.AdjustAspectRatio(); var width = bounds.Width; var height = bounds.Height; var scale = (bounds.XMax - bounds.XMin) / width; var resultLength = ColorComponents * width * height; var resultMemory = Gpu.Default.AllocateDevice <byte>(resultLength); var resultDevPtr = new deviceptr <byte>(resultMemory.Handle); var lp = ComputeLaunchParameters(bounds); Gpu.Default.Launch(() => { var i = blockDim.x * blockIdx.x + threadIdx.x; var x = i % width; var y = i / width; var offset = ColorComponents * i; if (offset < resultLength) { var c = new Complex { Real = bounds.XMin + x * scale, Imaginary = bounds.YMin + y * scale, }; ComputeMandelbrotAtOffset(resultDevPtr, c, offset); } }, lp); return(BitmapUtility.FromByteArray(Gpu.CopyToHost(resultMemory), width, height)); }
// Byte Array! internal static Image Render2(Bounds bounds) { bounds.AdjustAspectRatio(); var width = bounds.Width; var height = bounds.Height; var scale = (bounds.XMax - bounds.XMin) / width; var result = new byte[ColorComponents * width * height]; Parallel.For(0, height, y => { for (var x = 0; x < width; ++x) { var c = new Complex(bounds.XMin + x * scale, bounds.YMin + y * scale); ComputeMandelbrotAtOffset(c, i => { // ReSharper disable once AccessToModifiedClosure var offset = ColorComponents * (y * width + x); result[offset + 0] = i; result[offset + 1] = i; result[offset + 2] = i; }); } }); return(BitmapUtility.FromByteArray(result, width, height)); }