Parallel calculations

From Rosetta Code
Revision as of 17:21, 15 December 2010 by rosettacode>Abu (Created draft task, added PicoLisp)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Parallel calculations is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

Many programming languages allow you to specify computations to be run in parallel. While Concurrent computing is focused on concurrency, the purpose of this task is to distribute time-consuming calculations on as many CPUs as possible.

Assume we have a collection of numbers, and want to find the one with the largest minimal prime factor (that is, the one that contains relatively large factors). To speed up the search, the factorization should be done in parallel using separate threads or processes, to take advantage of multi-core CPUs.

Show how this can be formulated in your language. Parallelize the factorization of those numbers, then search the returned list of numbers and factors for the largest minimal factor, and return that number and its prime factors.

For the prime number decomposition you may use the solution of the Prime decomposition task.

PicoLisp

The 'later' function is used in PicoLisp to start parallel computations. The following solution calls 'later' on the 'factor' function from Prime decomposition#PicoLisp, and then 'wait's until all results are available: <lang PicoLisp>(let Lst

  (mapcan
     '((N)
        (later (cons)               # When done,
           (cons N (factor N)) ) )  # return the number and its factors
     (quote
        188573867500151328137405845301  # Process a collection of 12 numbers
        3326500147448018653351160281
        979950537738920439376739947
        2297143294659738998811251
        136725986940237175592672413
        3922278474227311428906119
        839038954347805828784081
        42834604813424961061749793
        2651919914968647665159621
        967022047408233232418982157
        2532817738450130259664889
        122811709478644363796375689 ) )
  (wait NIL (full Lst))  # Wait until all computations are done
  (maxi '((L) (apply min L)) Lst) )  # Result: Number in CAR, factors in CDR</lang>

Output:

-> (2532817738450130259664889 6531761 146889539 2639871491)