Skip to content

Atomics

Matt Norman edited this page Mar 7, 2022 · 3 revisions

YAKL includes sum, min, max atomics, using hardware atomics where available on GPU devices.

parallel_for( Bounds<3>(nz,ny,nx) , YAKL_LAMBDA (int k, int j, int i) {
  // column_average(k) = column_average(k) + data(k,j,i)
  yakl::atomicAdd( column_sum(k) , data(k,j,i) );

  // column_average(k) = min( column_average(k) , data(k,j,i) )
  yakl::atomicMin( column_min(k) , data(k,j,i) );

  // column_average(k) = max( column_average(k) , data(k,j,i) )
  yakl::atomicMax( column_max(k) , data(k,j,i) );
});

As a rule, if you ever see anything on the left-hand-side of an = with fewer indices than you have surrounding loops, then you're going to have a race condition that requires an atomic access.

Clone this wiki locally