using Cilk reducers in c

using Cilk reducers in c

newport_j's picture

I have a lot of legacy code that is in c that I would like to parallelize. However, reducers are for c++ code. There is some paper titled:

"Using an Intel Cilk Plus Reducer in C"

but it is only 1 page long.

There is no example code posted,example codewill be posted "when ready". At what version of Intel Cilk canI use reducers in c? Also, where can I find some examples ofcode?.

Newport_j

8 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.
Pablo Halpern (Intel)'s picture

Intel Cilk++ does not support C, it supports only C++. Intel Cilk Plus DOES support C. In general, we are advising people, when at all possible, to migrate from Cilk++ to Intel Cilk Plus. You can learn about it at http://cilk.com. Intel Cilk Plus is a fully supported product undergoing continued development; Intel Cilk++ is no longer being updated.

If you do migrate to Intel Cilk Plus, you will find that the support for reducers in C is limited. C does not have templates, so we use a combination of macros and manual programming to create reducers. I'm not sure what one-page description you are refering to, but I would not be surprised if it is currently the only public documentation available on the topic. As I said, the product is still evolving and C reducers are a relatively new and immature feature.

Alternatively, in either of the two products, you can consider converting your code from C to C++. The main area where C++ is not a proper superset of C is in the automatic conversion of void* to a non-void pointer type. It is tedious, but not difficult, to add casts for all such conversions, making the code both C and C++ compliant. Then you can use C++ reducers, which are easier and more expressive than C reducers.

Here is an example of using a reducer in C (using Intel Cilk Plus):

/* fib.cpp                  -*-C++-*-                           */

#include 
#include 
#include 
#include 

/* Declare a reducer called "result" holding a int with initial value 0. */
CILK_C_REDUCER_OPADD(result, int, 0);

void fib(int n)
{
    if (n < 2)
    {
        REDUCER_VIEW(result) += n;
        return;
    }

    cilk_spawn fib(n - 2);
    fib(n - 1);
}

int main(int argc, char *argv[])
{
    int n = 10;
    if (argc > 1)
        n = atoi(argv[1]);

    fib(n);
    printf("result = %dn", result.value);

    return 0;
}

/* End fib.cpp */

-Pablo

newport_j's picture

http://software.intel.com/en-us/articles/using-an-intel-cilk-plus-reducer-in-c/

The above link should direct you to the location that discusses Cilk reducers in c. Be forewarned - it is only 1 page, but it is all we got.

Newport_j

Pablo Halpern (Intel)'s picture
Quoting newport_j

http://software.intel.com/en-us/articles/using-an-intel-cilk-plus-reducer-in-c/

The above link should direct you to the location that discusses Cilk reducers in c. Be forewarned - it is only 1 page, but it is all we got.

Newport_j

Yup. The bad news is that it's terse. The good news is that it's accurate.

-Pablo

newport_j's picture

The above commerical versions of Cilk++ by Intel is a little confusing. It seems that you can buy items a la carte or buy them in a complete suite. I use Ubuntu Linux and Ihope that the Intel Cilk++ version for Linux is compataible. What products must I purchase (actually have my boss purchase) that handle reducers in cas well as c++?

Also, a simple questions about using reducers; how much do they add to the overhead of the processor running the program?

Newport_j

Pablo Halpern (Intel)'s picture

Any of the C++ compiler or compiler suites for Linux should meet your needs for functionality that is equivalentto Cilk++ or better.

The question about overhead is difficult to answer. It depends on what you are comparing against and on the nature of your code. A reducer lookup (i.e., any operatation thatproduces a reducer view) is a function call. However, under many circumstances the compiler can optimize away redundant lookups, especially within loops.(A reducer's view changes only at a spawn or sync, so using a reducer multiple times in a serial stretch of code is cheap after the optimizer gets done with it.) Also, a reducer lookup is typically cheaper than acquiring a mutex lock, so compared to other forms of preventing data races, they can be cheaper (sometimes much cheaper). However, using a reducer is usually more expensive than rewriting your code to completely eliminate potential races, i.e., bh passing local variables around by reference.

I hope this helps,
-Pablo

Brandon Hewitt (Intel)'s picture

Intel Cilk Plus, which supports C, is currently only available in Intel Parallel Composer 2011 which only supports Windows*. However, we will have a product available in the near future that supports Cilk Plus on Linux* with the Intel C++ Compiler. I'll update this thread when that's available.

Brandon Hewitt Technical Consulting Engineer Tools Knowledge Base: "http://software.intel.com/en-us/articles/tools" Software Product Support info: "http://www.intel.com/software/support"
Brandon Hewitt (Intel)'s picture

Fyi, that Linux support is now available. Seehttp://software.intel.com/en-us/forums/showthread.php?t=78546&o=a&s=lrfor more info.

Brandon Hewitt Technical Consulting Engineer Tools Knowledge Base: "http://software.intel.com/en-us/articles/tools" Software Product Support info: "http://www.intel.com/software/support"

Login to leave a comment.