Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Parallel Computation

by Anonymous Monk
on Nov 16, 2010 at 16:16 UTC ( #871770=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have a toy problem but with the intention of learning. I like to add two 5-digit numbers. First I like to split both numbers into digits and then have different programs/threads/processes add them and provide the carry etc.. and get the final answer. How I can do this ? I like to go further with multiplication of 5-digit numbers etc..

Thanks.

Comment on Parallel Computation
Re: Parallel Computation
by zentara (Archbishop) on Nov 16, 2010 at 16:38 UTC
    I like to split both numbers into digits and then have different programs/threads/processes add them and provide the carry etc.. and get the final answer.

    So you mean you want to write a computer program to add 5 digit numbers, the way we did it manually before calculators and computers?

    Why? The whole purpose of computers is to avoid having to do that! :-)

    P.S. If you are serious, learn to split numbers into digits, then learn to add them via modulus. See grep integer and fractional part from a division (modulus ?)


    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
      Why? The whole purpose of computers is to avoid having to do that! :-)

      Yeah, but the whole point of computer science class is to learn how the computers do it. The point of asking for help with your homework being learning how to cheat your way through life. Can't seem to learn that early enough these days.

        The point of asking for help with your homework being learning how to cheat your way through life.

        The best lesson you'll learn is that you can't cheat your way through life.

        You might make it through college that way, but then reality will bite.

        And if you haven't learnt the lesson by then, you soon will, but it'll be too late.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
        Hi, I am the original poster. This is not the homework, and only for learning purpose. I am not in school either from many years.
Re: Parallel Computation
by bobr (Monk) on Nov 16, 2010 at 18:33 UTC
    Do you referring to tasks like Sort N numbers using NxN processors? We did those task in Parallel algorithm class way back on college, using PRAM assembler.

    I am not sure perl is really best environment for such play, since its threads are quite heavy-weight and have quite a cost for shared data (which are essential here). But for experiments you can build number of threads (like for each digit) and wait until less-significant digit thread provide its carry.

    EDIT: You can also use library like Coro for lighter cooperative threads.

    -- Roman

      No, I am just talking about the simple exercise that I like to do for myself in Perl. I can implement even via fork() if possible. What I am interested is in the mechanism rather than cost.
Re: Parallel Computation
by BrowserUk (Pope) on Nov 16, 2010 at 19:12 UTC

    As an exercise in understanding how computers do math it makes no sense, because computers do math in binary. You'd be better looking at how half adders work.

    And it makes no sense to parallelise this, it is inherently serial. You cannot add or multiply the second two digits until you have the results from the first two.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Parallel Computation
by Anonymous Monk on Nov 16, 2010 at 22:35 UTC

      I'd like to see a demonstration of that?

        Here is a skeleton of what you want.

        #!/usr/bin/perl use Parallel::ForkManager; my $max_threads = 5; my $fork_mgr = new Parallel::ForkManager($max_threads); $fork_mgr->run_on_finish ( sub { my($child_pid, $exit_code, $child_id, $exit_signal, $core_dump +) = @_; # Code to store the results from each thread go here. } ); foreach my $item (@big_list) { $fork_mgr->start($url_ent) and next URL # Code in this block will run in parallel my $result = do_stuff($item); # Store the final result. The value you pass to finish will be # received by the sub you defined in run_on_finish $fork_mgr->finish($result); } $fork_mgr->wait_all_children(); # Now all child threads have finished, # your results should be available.

        A few extra tips:

        If you are running this code in the perl debugger, then you might want to debug the child threads. If so, run your program in a unix/linux xterm window. The debugger will create new xterm windows for each child thread, so you can separately step through the parent and the children

        Conversely, if you don't want to step through the children, and your screen is filling up with windows from child threads, you can use the debug option: o inhibit_exit=0 To suppress the display of windows for child threads that finish without hitting a breakpoint.</c>

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://871770]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2014-10-21 05:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (96 votes), past polls