- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
In my search application there are globally variables defined outside any function that I would like to use the cilk reducers on.
Specifically I have code like this:
#include "search.h" static int total_users = 0; static int total_matches = 0;
These total_x variables are incremented throughout the application on different functions.
I tried adding the following for total_users and received the following error:
cilk::reducer_opadd<static int> total_users;
cilk plus error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘:’ token
What am I doing wrong here?
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I think "static" wants to qualify the reducer itself, not the type argument for the reducer. What about:
static cilk::reducer_opadd<int> total_users;
Cheers,
Jim
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks Jim for your reply I still get an error after switching the order for declaration:
search.c:6:12: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘:’ token
static cilk::reducer_opadd<int> total_users;
^
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Since your file is a .c file, you probably need to use the C interface for reducers, instead of C++. Here is one previous forum post that gives one example. Perhaps that may help?
http://software.intel.com/en-us/forums/topic/265682
I thought there was a more complete example documented somewhere, but at the moment, I'm drawing a blank as to where that might be.
Cheers,
Jim
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Here's a simple example, mostly taken from the reducer documentation:
#include <stdio.h> #include <cilk/cilk.h> #include <cilk/reducer_opadd.h> static CILK_C_REDUCER_OPADD(r, double, 0); int main(int argc, char* argv[]) { double a[1000]; for(int j=0; j < 1000; j++) a= (double)j; CILK_C_REGISTER_REDUCER(r); cilk_for(int i = 0; i != 1000; ++i) { REDUCER_VIEW(r) += a; } CILK_C_UNREGISTER_REDUCER(r); printf("The sum of the elements of a is %f\n", REDUCER_VIEW(r)); return 0; }
Note that you can build the reducer documentation using doxygen. See cilk\ReadMe.html for instructions.
- Barry
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks Jim & Barry! What I did is I declared the variables as such:
static CILK_C_REDUCER_OPADD(total_users, int, 0); static CILK_C_REDUCER_OPADD(total_matches, int, 0);
Which worked.
And anywhere the variable was called I referenced like so:
if(total_matches.value >= scan_for_newest_members_page_size)
This seems to be working well.
My follow up question is on the role of the REDUCER_VIEW() function. If I understand correctly these are to be used in the program where serialization (or parallelism?) is happening?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
You really should build the documentation in include\cilk and read it. In particular, group___reducers.html for an overview of how reducers work, and page_reducers_in_c.html, for an overview of using reducers in C.
In brief, the first time a reducer is used in a strand (an instance of a parallel region), a "view" of that reducer is created with the identity value for the reducer's monoid. When two parallel regions merge, the two views are combined (reduced) to a single value. The Cilk runtime does this in a way to preserve the sequential ordering, assuming that your reducer is associative.
REDUCER_VIEW() accesses the view of the reducer in the current strand.
- Barry

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page