On Sat, Feb 25, 2017 at 11:24:15AM +0100, Joh-Tob Schäg wrote:
> I tried to parallelize the following code:
> '(let N 2
> (while (> 10000000 N )
> (check N)
> (inc 'N)))
> where (check N) is defined as '(ifn (= N (apply '* (prime-factors N)))
> (print N))
OK, good!
> 'later has the following form '(later
> place_where_to_save_the_return_result . program to execute)
Correct.
> However i am not interested in storing the result and (later 'NIL prog)
> does not work for obvious reasons.
Right. It does not work well because too many processes are started too quickly.
> Furthermore (later (new) (print 8)) does not work either.
> Using files as in (out "+file" (later (new) (print 8))) does not work
> either.
This is a different problem. Because 'later' is a frontend to 'pipe' using PLIO,
you better not 'print' something to the current output channel.
This would work:
: (later (new) (* 3 4))
-> $377236506200
: (val @)
-> 12
or
: (later (cons) (* 3 4))
-> (NIL)
: (car @)
-> 12
> Is there a way to run '(check N) on multiple cores at the same time and get
> the results 'print-ed in to the standard output of the instance where the
As you cannot print to stdout (which is reserved for the pipe), you could print
to a file or simply to the console *inside* the child process:
: (later (cons) (out "/dev/tty" (println 8)))
-> (NIL)
: 8
(car @)
-> 8
Remains the problem of excessive process creation. I would handle this by
creating only eg. 8 processes at one time:
(let Lst (need 8)
(for (N 2 (>= 10000000 N))
(map
'((L)
(set L NIL)
(later L (out "/dev/tty" (println (check N))))
(inc 'N) )
Lst )
(wait NIL (full Lst)) ) )
Or, instead of printing in the child, you could also just print the *results* in
the main process:
(let Lst (need 8)
(for (N 2 (>= 10000000 N))
(map
'((L)
(set L NIL)
(later L (check N))
(inc 'N) )
Lst )
(wait NIL (full Lst))
(println Lst) ) )
♪♫ Alex
--
UNSUBSCRIBE: mailto:[email protected]?subject=Unsubscribe