Hey there,
I have a created a very simple program that does not make any sense in particular, but made me wounder where the observed overhead does come from:
#include <stdio.h> #include <string.h> #include <cilk/cilk.h> #include <chrono> int num = 45; long fib(int n) { std::chrono::time_point<std::chrono::high_resolution_clock> t1; if( n == num ) { t1 = std::chrono::high_resolution_clock::now(); fprintf(stdout, "STARTED FIB\n"); } if( n < 2 ) return n; long result = fib(n-1) + fib(n-2); if( n == num ) { auto t2 = std::chrono::high_resolution_clock::now(); auto int_ms = std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1); fprintf(stdout, "FINISHED FIB in %ldms.\n", int_ms.count()); } return result; } int main(int argc, char** argv ) { cilk_spawn fib( num ); cilk_sync; // fib(num); return 0; }
Of course the spawn does not at all improve the program's performance, but I did it, because I wanted to measure the overhead of using spawns and syncs.
I run this program twice one time with the given main function and one time without using cilk_spawn and cilk_sync at all (using line 34 instead of 32 and 33). The output was as follows:
without spawn:
STARTED FIB
FINISHED FIB in 11836ms.
with spawn:
STARTED FIB
FINISHED FIB in 18320ms.
So why is the pure execution of the function that I didn't touched at all slowed down by this amount? Of course there is a overhead by creating threads and spawning and waiting for the spawn to be done, but my measurment takes place within the executed function and that function is not modified at all by Cilk. There are several new helper function created around this function call, but the function stays as it is. So why is the same serial function execution suddenly 7 seconds slower than before? The overhead introduced by Cilk should not be measured by my function, should it?
I am very grateful for any possible explanation on this topic.