Active4 months ago
I need to parallel (with openmp) the Fibonacci sequence from this sequential code to calculate the 105-th term of the sequence, but I have been stuck for 3 weeks without any good idea, someone have any idea or tip of a good way to do it?
Here is the sequential code in C:
chqrlieC program to print the Fibonacci series using recursion function. Online C functions programs and examples with solutions, explanation and output for computer science and information technology students pursuing BE, BTech, MCA, MTech, MCS, MSc, BCA, BSc. Find step by step code solutions to sample programming questions with syntax and structure for lab practicals and assignments.
69.3k99 gold badges6060 silver badges116116 bronze badges
JESUSJESUS
2 Answers
Instead of trying to parallelise the bignum addition, which is tricky, you can take try and compute multiple terms in parallel:
Note also that you should compute blocks of digits at a time: 8 or 9 base-10 digits can be computed using 32-bit array elements.
Here is a modified version with multiple improvements:
- it computes blocks of 8 digits at a time
- it can take command line arguments
- it uses much less memory
- it can handle much larger values
- it is much more efficient (20x)
You should be able to parallelise it easily.
chqrliechqrlie69.3k99 gold badges6060 silver badges116116 bronze badges
As you already know, F(100000) is an astronomically huge number. And to compute that value, you have to sum two other really huge numbers F(99999) and F(99998).
Here's my hint:
You've got two numbers that are many (thousands) of digits long and N processors. You can split the addition up across multiple threads. For example:
To compute F(169), you have to add both of those numbers above. But let's treat it as 4 separate additions of 9 digits each.
So now we've got 4 summations. Two of which,
C
and D
, have a carry operation. So we just need to adjust the result of the left of each by +1So your algorithm to compute Fib(100000) is something like the following. Where
BigNumber
is a struct that is a representation of your digits. You are using an array of characters, which is also acceptable.Your
selbieselbieParallelAdd
will break the numbers passed via pointer, f1 and f0, into N groups of K digits each, where N is the number of processors you have available or want to use. Then each processor computes the addition of each - using the code you already have. After those N operations have completed, scan the result set to see which additions contained a result that was K+1 digits long, and then adjust with +1 logic as described above. The combine into a single string and shove back into the pointer address referenced by pF2
.60.8k1111 gold badges6969 silver badges133133 bronze badges