public SparseTwinIndex <float> ProcessFlow(float[] Friction, SparseArray <float> O, SparseArray <float> D) { float[] o = O.GetFlatData(); float[] d = D.GetFlatData(); var oLength = o.Length; var dLength = d.Length; var squareSize = oLength * dLength; Stopwatch watch = new Stopwatch(); watch.Start(); gravityModelShader.NumberOfXThreads = length; gravityModelShader.NumberOfYThreads = 1; gravityModelShader.ThreadGroupSizeX = 64; gravityModelShader.ThreadGroupSizeY = 1; float[] balanced = new float[] { 0, this.Epsilon }; int iterations = 0; var step1 = new int[] { oLength, 0, this.MaxIterations }; var step2 = new int[] { oLength, 1, this.MaxIterations }; if (flows == null || flows.Length != o.Length * d.Length) { flows = new float[squareSize]; } SparseTwinIndex <float> ret = null; Task createReturn = new Task(delegate() { ret = O.CreateSquareTwinArray <float>(); }); createReturn.Start(); FillAndLoadBuffers(o, d, Friction, balanced); iterations = Balance(gpu, gravityModelShader, balancedBuffer, parameters, balanced, iterations, step1, step2); gpu.Read(flowsBuffer, flows); gravityModelShader.RemoveAllBuffers(); createReturn.Wait(); BuildDistribution(ret, O, D, oLength, flows); watch.Stop(); using (StreamWriter writer = new StreamWriter("GPUPerf.txt", true)) { writer.Write("Iterations:"); writer.WriteLine(iterations); writer.Write("Time(ms):"); writer.WriteLine(watch.ElapsedMilliseconds); } return(ret); }
public float Add(int bufferSize) { Adder.ThreadGroupSizeX = 64; Adder.RemoveAllBuffers(); var stream = new AddStream(Gpu, bufferSize, DataToAdd); int index = 0; var total = 0.0f; var constBuffer = Gpu.CreateConstantBuffer(16); var tempBuffer = new float[bufferSize / 512]; Adder.AddBuffer(constBuffer); stream.AttachBuffer(Adder); while (index < DataToAdd.Length) { // Load up the next set of data stream.LoadData(index); stream.ApplyPass(this.Adder, constBuffer, index); total += stream.GetAnswer(tempBuffer); index += bufferSize; } return(total); }
public SparseTwinIndex <float> ProcessFlow(SparseArray <float> O, SparseArray <float> D) { float[] o = O.GetFlatData(); float[] d = D.GetFlatData(); var oLength = o.Length; var dLength = d.Length; var squareSize = oLength * dLength; float[] flows = new float[squareSize]; float[] residules = new float[dLength]; GPU gpu = new GPU(); string programPath; var codeBase = Assembly.GetEntryAssembly().CodeBase; try { programPath = Path.GetFullPath(codeBase); } catch { programPath = codeBase.Replace("file:///", String.Empty); } // Since the modules are always located in the ~/Modules subdirectory for XTMF, // we can just go in there to find the script ComputeShader gravityModelShader = null; Task compile = new Task(delegate() { gravityModelShader = gpu.CompileComputeShader(Path.Combine(Path.GetDirectoryName(programPath), "Modules", "GravityModel.hlsl"), "CSMain"); gravityModelShader.NumberOfXThreads = oLength; gravityModelShader.NumberOfYThreads = 1; gravityModelShader.ThreadGroupSizeX = 64; gravityModelShader.ThreadGroupSizeY = 1; }); compile.Start(); GPUBuffer flowsBuffer = gpu.CreateBuffer(squareSize, 4, true); GPUBuffer attractionStarBuffer = gpu.CreateBuffer(oLength, 4, true); GPUBuffer balancedBuffer = gpu.CreateBuffer(2, 4, true); GPUBuffer productionBuffer = gpu.CreateBuffer(dLength, 4, false); GPUBuffer attractionBuffer = gpu.CreateBuffer(oLength, 4, false); GPUBuffer frictionBuffer = gpu.CreateBuffer(squareSize, 4, false); GPUBuffer parameters = gpu.CreateConstantBuffer(16); float[] balanced = new float[] { 0, this.Epsilon }; int iterations = 0; var step1 = new int[] { oLength, 0, this.MaxIterations }; var step2 = new int[] { oLength, 1, this.MaxIterations }; compile.Wait(); Stopwatch watch = new Stopwatch(); watch.Start(); FillAndLoadBuffers(o, d, Friction, gpu, gravityModelShader, flowsBuffer, attractionStarBuffer, balancedBuffer, productionBuffer, attractionBuffer, frictionBuffer, parameters, balanced); if (gravityModelShader == null) { throw new XTMF.XTMFRuntimeException("Unable to compile the GravityModel GPU Kernel!"); } iterations = Balance(gpu, gravityModelShader, balancedBuffer, parameters, balanced, iterations, step1, step2); gpu.Read(flowsBuffer, flows); gravityModelShader.RemoveAllBuffers(); watch.Stop(); using (StreamWriter writer = new StreamWriter("GPUPerf.txt", true)) { writer.Write("Iteraions:"); writer.WriteLine(iterations); writer.Write("Time(ms):"); writer.WriteLine(watch.ElapsedMilliseconds); } gravityModelShader.Dispose(); gpu.Release(); return(BuildDistribution(O, D, oLength, flows)); }
private void RunGPU(int dataLength) { float[] data = new float[dataLength]; for (int i = 0; i < data.Length; i++) { data[i] = 2; } ComputeShader shader = null; int reductionPerCall = 64; var codeBase = Assembly.GetEntryAssembly().CodeBase.Replace("file:///", ""); var compile = Task.Factory.StartNew(() => { var fileName = Path.Combine(Path.GetDirectoryName(codeBase), "Modules", "James.UTDM.TestGPU.hlsl"); shader = Gpu.CompileComputeShader(fileName, "CSMain"); if (shader == null) { throw new XTMFRuntimeException("We were unable to compile the compute shader!"); } shader.NumberOfXThreads = data.Length; shader.NumberOfYThreads = 1; shader.ThreadGroupSizeX = 64; shader.ThreadGroupSizeY = 1; }); var constBuffer = Gpu.CreateConstantBuffer(16); var originalBuffer = Gpu.CreateBuffer(data.Length, sizeof(float), true); var destinationBuffer = Gpu.CreateBuffer((data.Length / reductionPerCall) + 1, sizeof(float), true); try { this.Gpu.Write(originalBuffer, data); // wait now until the gpu has finished compiling the data compile.Wait(); // make sure the write has completed this.Gpu.Wait(); var watch = new Stopwatch(); watch.Start(); int size = data.Length; int remainder = 0; while (size > 0) { remainder = size % reductionPerCall; shader.NumberOfXThreads = size / reductionPerCall; shader.RemoveAllBuffers(); shader.AddBuffer(constBuffer); shader.AddBuffer(originalBuffer); shader.AddBuffer(destinationBuffer); // execute then flip the buffers this.Gpu.ExecuteComputeShader(shader); // compute the remainder var temp = destinationBuffer; destinationBuffer = originalBuffer; originalBuffer = temp; size = 0; } if (remainder > 0) { } Gpu.Wait(); watch.Stop(); using (var writer = new StreamWriter("gpu.txt", true)) { writer.WriteLine("{0}", (watch.ElapsedTicks / 10000f)); } } finally { this.Gpu.ReleaseBuffer(constBuffer); this.Gpu.ReleaseBuffer(originalBuffer); this.Gpu.ReleaseBuffer(destinationBuffer); if (shader != null) { shader.Dispose(); shader = null; } } }