Skip to content

Correct usage of barriers #920

Description

@apothecarius

I'm experimenting with ComputeSharp (https://github.com/apothecarius/ComputeSharpTestbed) and am trying to control flow by using Barriers:

ReadWriteBuffer<int> bufferA;
ReadWriteBuffer<int> bufferB;
ReadWriteBuffer<bool> bufferC;
using (ComputeContext ctx = GraphicsDevice.GetDefault().CreateComputeContext())
{
      bufferA = ctx.GraphicsDevice.AllocateReadWriteBuffer<int>(quarterOfNums*4);
      bufferB = ctx.GraphicsDevice.AllocateReadWriteBuffer<int>(quarterOfNums);
      bufferC = ctx.GraphicsDevice.AllocateReadWriteBuffer<bool>(quarterOfNums);
      ctx.For(quarterOfNums*4,new SetterKernel1(bufferA));
      ctx.Barrier(bufferA);
      ctx.For(quarterOfNums*4,new MultiplierKernel2(bufferA));
      ctx.Barrier(bufferA);
      ctx.For(quarterOfNums*4,new AddThreadIdKernel3(bufferA));
      ctx.Barrier(bufferA);
      ctx.For(quarterOfNums,new CollectFourKernel4(bufferA,bufferB));
      ctx.Barrier(bufferA);
      ctx.Barrier(bufferB);
      ctx.For(quarterOfNums,new VerificationKernel5(bufferB,bufferC));
      ctx.Barrier(bufferC);
}
bool[] downloadedResults = new bool[quarterOfNums];
bufferC.CopyTo(downloadedResults);
trues = downloadedResults.Count(x => x);

Basically it goes through 3 buffers and 5 computing kernels and it should write only true into the final buffer. But the count of "true" in it is not stable from one execution to another. It appears that the barriers are not having an effect.

Am I using Barriers correctly? I cannot find it mentioned in the wiki and in the samples they seem to be used similarly. The samples use [AutoConstructor] decorators extensively, but I can't deduce whether they hide some special effects.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    question ❓Further information is requesteduntriaged 🧰A new issue that needs initial triage

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions