c++ - general tbb issue for calculating fibonacci numbers -
I came across a TBB template as an example of work-based programming for calculating Fibonacci numbers in c ++ I am But when I run it I get the value of 1717986912 which can not happen. Production should be 3. What am I doing wrong?
Class Fibatas: Public work {public: const long n; Long long * const amount; Execute Execute :: Virtual Function Work Override: (if n & lt; 0) {return 0; } And {long X, Y; FibTask & amp; A = * new (allocate_child ()) FIbtask (N-1, and X); FibTask & amp; B = * new (allocate_child ()) Fibatac (N -2, & y); // Set one for "Wait for another for two children". Set_ref_count (3); // Start B. Moving eggs (B); // Start a race and wait for all children (A and B). Spawn_and_wait_for_all (a); // sum * sum = x + y; } Return tap; } Long parallelfib (long n) {long amount; FibTask & amp; A = * new (task :: allocated_root ()) FIbtask (N, and Yoga); Job: spawn_root_and_wait (a); Refund Amount; }}; Long main (int argc, char ** argv) {Fibcash * obj = new fibatasc (3,0); Long B = obj-> Parallelbb (3); Std :: cout & lt; & Lt; B; Return 0; }
This cutoff has been messed up here. It should be at least 2 eg .:
if (n & lt; 2) {* sum = n; Return tap; } The original example also uses serialfib as shown here
Inadequate method to calculate Fibonacci numbers using disabled blocking style techniques. Serialfib Will be even more disabled without calling ()
WARNING: Please note that the purpose of this example is to display only this special level level TBB API and this is a special way of using it. It does not intend to reuse it, unless you really do not know why you are doing it.
Int Fib (int nin) {if (n & lt; cfuf), the modern high level API {// 2 minimum return is Fiberial (N); } And {int x, y; tbb :: parallel_invoke ([& amp;] {x = fib (n-1);}, [& amp;] {y = fib (n -2);}); Return x + y; }}
Comments
Post a Comment