private static async Task ExecuteOnGpu(GraphicsDevice device, int index, TestShader shader) { ShaderGeneratorContext context = new ShaderGeneratorContext(device); context.Visit(shader); PipelineState pipelineState = await context.CreateComputePipelineStateAsync(); DescriptorSet?descriptorSet = context.CreateShaderResourceViewDescriptorSet(); using (CommandList commandList = new CommandList(device, CommandListType.Compute)) { commandList.SetPipelineState(pipelineState); if (descriptorSet != null) { commandList.SetComputeRootDescriptorTable(0, descriptorSet); } commandList.Dispatch(1, 1, 1); await commandList.FlushAsync(); } float sum = shader.DestinationBuffer.Resource.GetArray <float>().Sum(); Console.WriteLine($"Origin: GPU, Thread: {index}, sum: {sum}."); }
public static async Task RunAsync(GraphicsDevice device) { int count = 100; int sumCount = 10; List <Task> tasks = new List <Task>(); for (int i = 0; i < count; i++) { int index = i; Console.WriteLine($"Scheduling task {index}"); tasks.Add(Task.Run(async() => { GraphicsBuffer <float> buffer = GraphicsBuffer.Create <float>(device, sumCount, ResourceFlags.AllowUnorderedAccess); TestShader shader = new TestShader(buffer, Sigmoid); ShaderGeneratorContext context = new ShaderGeneratorContext(device); context.Visit(shader); ShaderGeneratorResult result = new ShaderGenerator(shader).GenerateShader(); PipelineState pipelineState = await context.CreateComputePipelineStateAsync(); DescriptorSet?descriptorSet = context.CreateShaderResourceViewDescriptorSet(); using (CommandList commandList = new CommandList(device, CommandListType.Compute)) { commandList.SetPipelineState(pipelineState); if (descriptorSet != null) { commandList.SetComputeRootDescriptorTable(0, descriptorSet); } commandList.Dispatch(1, 1, 1); await commandList.FlushAsync(); } float sum = buffer.GetData().Sum(); Console.WriteLine($"Thread: {index}, sum: {sum}."); })); } Console.WriteLine("Awaiting tasks..."); Console.WriteLine($"Task count: {tasks.Count}"); await Task.WhenAll(tasks); Console.WriteLine("DONE!"); }