How to call current_worker_id().

How to call current_worker_id().

Could anyone please give me some advice on how to call current_worker_id() in Cilk++ code? I read the manual, and the header file cilk.h. Unfortunately, all I found is the following:

Manual page 76

Getting the Worker Id
The runtime system provides one additional function that is not part of cilk::context but is convenient to describe here. int current_worker_id() returns the worker ID for the worker currently executing the calling Cilk++ strand. A worker ID is a small integer. Each worker within a cilk::context has a unique worker ID. Note that this function is provided purely for informational purposes. No other runtime API accepts a worker ID. In general, Cilk++ code should not care which worker a strand is running on.

cilk.h header file

extern "C++" {


namespace cilk {
namespace stub {
class context;
inline context *create_context(bool force_serial = true);
inline void destroy_context(context *ctx);
inline int current_worker_id();
inline unsigned current_worker_count();

template class monoid_base;
template class legacy_monoid_adapter;
template class reducer;
template class hyperobject;

} /* namespace cilk::stub */

using namespace cilk::stub;

} /* namespace cilk */

Thank you for the help in advance.


3 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

int iWorker = cilk::current_worker_id();

If you're working on Windows, you can look at the QuickDemo example of using Cilk with MFC for sample code that calls current_worker_id(). Unfortunately there is no equivalent example for Linux.

- Barry

Just out of curiosity, what are you using the worker ID for? Our experience is that 90% of the time, the use of a worker ID is just plain the wrong way to use Cilk. Cilk workers migrate around, stealing work from one another. A worker ID is ephemeral and should not, in general, be relied on for anything other than figuring out/demonstrating how Cilk works. The quicksort example for Windows uses worker ID for just such a demonstration.
- Pablo

Leave a Comment

Please sign in to add a comment. Not a member? Join today