Quantcast
Channel: Intel® Cilk™ Plus
Viewing all articles
Browse latest Browse all 77

Q about fibonacci example

$
0
0
//From the examples website: https://www.cilkplus.org/download#block-views-code-samples-block-1
int fib(int n)
{
    if (n < 2)
        return n;
    int x = cilk_spawn fib(n-1);
    int y = fib(n-2);
    cilk_sync;
    return x + y;
}

This leads to much recalculation of old fib values. A better implementation would store the value, and return it if it has already been computed. However, that also requires critical sections or locks to serialize access to "has_already_been_computed" variable. Does cilk have a mechanism for that?

I'm looking to implement a general DAG code, where every node evaluates its predecessors. Each node should be evaluated only once, after which it becomes possible to retrieve its value. Right now it seems to me that cilk lacks the mechanism for that. 

What feature am I missing?

Victor.


Viewing all articles
Browse latest Browse all 77

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>