private static void FillAndLoadBuffers(float[] o, float[] d, float[] Friction, GPU gpu, ComputeShader gravityModelShader, GPUBuffer flowsBuffer, GPUBuffer attractionStarBuffer, GPUBuffer balancedBuffer, GPUBuffer productionBuffer, GPUBuffer attractionBuffer, GPUBuffer frictionBuffer, GPUBuffer parameters, float[] balanced) { gpu.Write(balancedBuffer, balanced); gpu.Write(productionBuffer, o); gpu.Write(attractionBuffer, d); gpu.Write(attractionStarBuffer, d); gpu.Write(frictionBuffer, Friction); // The order matters, needs to be the same as in the shader code!!! gravityModelShader.AddBuffer(parameters); gravityModelShader.AddBuffer(flowsBuffer); gravityModelShader.AddBuffer(attractionStarBuffer); gravityModelShader.AddBuffer(balancedBuffer); gravityModelShader.AddBuffer(productionBuffer); gravityModelShader.AddBuffer(attractionBuffer); gravityModelShader.AddBuffer(frictionBuffer); }
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); }
private static void FillAndLoadBuffers(float[] o, float[] d, float[] Friction, GPU gpu, ComputeShader gravityModelShader, GPUBuffer flowsBuffer, GPUBuffer attractionStarBuffer, GPUBuffer balancedBuffer, GPUBuffer productionBuffer, GPUBuffer attractionBuffer, GPUBuffer frictionBuffer, GPUBuffer parameters, float[] balanced) { gpu.Write( balancedBuffer, balanced ); gpu.Write( productionBuffer, o ); gpu.Write( attractionBuffer, d ); gpu.Write( attractionStarBuffer, d ); gpu.Write( frictionBuffer, Friction ); // The order matters, needs to be the same as in the shader code!!! gravityModelShader.AddBuffer( parameters ); gravityModelShader.AddBuffer( flowsBuffer ); gravityModelShader.AddBuffer( attractionStarBuffer ); gravityModelShader.AddBuffer( balancedBuffer ); gravityModelShader.AddBuffer( productionBuffer ); gravityModelShader.AddBuffer( attractionBuffer ); gravityModelShader.AddBuffer( frictionBuffer ); }
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; } } }
internal void AttachBuffer(ComputeShader adder) { adder.AddBuffer(Buffer); }