Skip to content

Thread Private Data

Matt Norman edited this page Dec 10, 2021 · 2 revisions

How do I use thread-private data?

All data you declare inside a parallel_for is inherently thread-private, and each thread will have its own copy of that variable. All data you declare outside a parallel_for is inherently shared between threads.

For instance, supposed you want to gather a stencil of data in a kernel, the following code will fail:

// The following code is incorrect!
SArray<float,3> stencil; // This will be share between threads
parallel_for( nx , YAKL_LAMBDA (int i) {
  // Threads will be overwriting each others' data here in a data race
  for (int ii=0; ii < 3; ii++) { stencil(ii) = data(i+ii); }
  ...
});

Instead, thread-private data must be declared inside the the kernels:

// The following code is correct
parallel_for( nx , YAKL_LAMBDA (int i) {
  SArray<float,3> stencil; // Each thread has its own copy of "stencil"
  for (int ii=0; ii < 3; ii++) { stencil(ii) = data(i+ii); }
  ...
});
Clone this wiki locally