Question on reducers

Question on reducers

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?



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

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;



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;


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?

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.


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[j] = (double)j;

    cilk_for(int i = 0; i != 1000; ++i) {
        REDUCER_VIEW(r) += a[i];
    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

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?









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

Login to leave a comment.