views:

141

answers:

5

I am writing multi process fibonacci number calculator , I have a file that keeps track of the fibonacci numbers , first process open the file and write first fibonacci numbers (0 and 1 ), then do fork and its child process read the last two numbers add them up and write the next into file and close the file and fork again this process continue like that with forking and child adding up numbers and writing calculated number into file, using fork inside the for not a good solution neither recursive call,is there any suggesstion for problem ??

Here is the link of the problem we are talking about multi-process part of the problem which is part 2

http://cse.yeditepe.edu.tr/~sbaydere/fall2010/cse331/files/assignments/F10A1.pdf

+2  A: 

Assuming you're calculating them in a "simple" way (i.e. without using a cunning formula), I don't think it's a good candidate for parallel processing at all.

It's easy to come up with an O(n) solution, but each result depends on the previous one, so it's inherently tricky to parallelize. I can't see any benefit in your current parallel approach, as after each process has finished its own job and forked a child to get the next number, it's basically done... so you might as well do the work of the forked child in the existing process.

Jon Skeet
There are about 100 of them representable in 64bits, so there's no reason to use parallel calculations.
ruslik
I know it is not a good solution for this problem , but it is important to show that multi thread part is better than this solution, problem also have multi-threaded written part.It is just comparison
Burak Dede
@Burak an algorithm has parts that can be parallelized and parts that cannot. This one cannot be.
ruslik
@Ruslik , I know it is not a good candidate for multiprocessing but it is a homework so i have no choice other than implementing it but i agree with you
Burak Dede
@Burak: Picking an example which is doesn't parallelize nicely in the first place is a really bad idea if you're trying to compare multiple threads vs multiple processes. It's like asking whether apples or oranges are better for building houses with.
Jon Skeet
@Burak then it's OK if you have performance issues. You've proved that for this particular algorithm the serial version is the best one.
ruslik
yes serial version is way better than multiprocess one , i just wonder if any other solution different than using fork inside for to do it.
Burak Dede
@Jon Skeet, apples, hands down! :)
Amigable Clark Kant
+2  A: 

Fibonacci number calculation is a really strange idea to go multiprocess. Indeed, to calculate a number, you do need to know the previous two. Multiple processes cannot calculate other numbers but the next one, and only the next one. Multiple processes will all calculate the next Fibonacci number. Anyway, you'll double check.

Didier Trosset
+1  A: 

You might want to look at this article:

http://cgi.cse.unsw.edu.au/~dons/blog/2007/11/29

There are more ideas here:

http://www.haskell.org/haskellwiki/The_Fibonacci_sequence

stusmith
A: 

Or this paper. Seems very relevant, but then I am no mathematician.

Amigable Clark Kant
A: 

probably this isn't solving your problem, but here is a trivial way to calculate the fibonacci numbers of a given range

int fibo(int n) { return (n<=2)?n:(fibo(n-1))+(fibo(n-2)); }

aautsch
i am not using recursive algorithm in this manner and algorithm is not the issue here performance is ,i know the fibonacci algorithm
Burak Dede