Last active
February 1, 2021 21:58
-
-
Save DanWBR/82f773be14b6ddb60715867baa6cfd71 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using ILGPU; | |
using ILGPU.Algorithms; | |
using ILGPU.Runtime; | |
using ILGPU.Runtime.Cuda; | |
using ILGPU.Runtime.OpenCL; | |
namespace NestedLoopsGPU | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
int n = 500; | |
float[] output = new float[n]; | |
float[] output2 = new float[n]; | |
float[,] a = new float[n, n]; | |
float[] ai = new float[n]; | |
float[,] vkij = new float[n, n]; | |
int i, j, k; | |
Random r = new Random(0); | |
Stopwatch sw = new Stopwatch(); | |
sw.Start(); | |
for (i = 0; (i < n); i++) | |
{ | |
ai[i] = (float)r.NextDouble(); | |
for (j = 0; (j < n); j++) | |
{ | |
vkij[i, j] = (float)r.NextDouble(); | |
} | |
} | |
sw.Stop(); | |
Console.WriteLine(sw.Elapsed.ToString()); | |
sw.Reset(); | |
sw.Start(); | |
for (k = 0; (k < n); k++) | |
{ | |
output[k] = 0.0f; | |
for (i = 0; (i < n); i++) | |
{ | |
for (j = 0; (j < n); j++) | |
{ | |
output[k] += (float)Math.Sqrt(ai[i] * ai[j]) * (1 - vkij[i, j]); | |
} | |
} | |
} | |
Console.WriteLine("CPU Loop Vector Sum: {0}", output.Sum()); | |
sw.Stop(); | |
Console.WriteLine(sw.Elapsed.ToString()); | |
sw.Reset(); | |
sw.Start(); | |
using (var context = new Context()) | |
{ | |
var allSupportedAccelerators = Accelerator.Accelerators; | |
var cudaAccelerators = CudaAccelerator.CudaAccelerators; | |
var supportedCLAccelerators = CLAccelerator.CLAccelerators; | |
var allCLAccelerators = CLAccelerator.AllCLAccelerators; | |
// Enable the algorithms library | |
context.EnableAlgorithms(); | |
using (var accelerator = Accelerator.Create(context, allSupportedAccelerators[1])) | |
{ | |
output2 = VectorMultiplyAccelerated(accelerator, ai, vkij, output2); | |
Console.WriteLine("GPU Loop Vector Sum: {0}", output2.Sum()); | |
} | |
} | |
sw.Stop(); | |
Console.WriteLine(sw.Elapsed.ToString()); | |
Console.ReadKey(); | |
} | |
static float[] VectorMultiplyAccelerated(Accelerator accelerator, float[] a, float[,] kij, float[] output) | |
{ | |
var n = a.GetLength(0); | |
var kernel = accelerator.LoadAutoGroupedStreamKernel<Index3, ArrayView<float>, ArrayView2D<float>, ArrayView<float>>(VectorMultiplyAcceleratedKernel); | |
using (var aBuffer = accelerator.Allocate<float>(n)) | |
using (var kBuffer = accelerator.Allocate<float>(n, n)) | |
using (var oBuffer = accelerator.Allocate<float>(n)) | |
{ | |
aBuffer.CopyFrom(a, Index1.Zero, Index1.Zero, aBuffer.Extent); | |
kBuffer.CopyFrom(kij, Index2.Zero, Index2.Zero, kBuffer.Extent); | |
oBuffer.CopyFrom(output, Index1.Zero, Index1.Zero, oBuffer.Extent); | |
kernel(new Index3(n, n, n), aBuffer, kBuffer, oBuffer); | |
accelerator.Synchronize(); | |
return oBuffer.GetAsArray(); | |
} | |
} | |
static void VectorMultiplyAcceleratedKernel(Index3 indexk, ArrayView<float> aView, ArrayView2D<float> kView, ArrayView<float> oView) | |
{ | |
oView[indexk.Z] += (float)Math.Sqrt(aView[indexk.X] * aView[indexk.Y]) * (1.0f - kView[indexk.X, indexk.Y]); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment