Re: later and output

2017-02-25 Thread Joh-Tob Schäg
I am glad i could be of some help.
To be honest Linsday you do a good job for the community and a better job
than i do to be honest.
Maybe somebody can spend some time and spin this in to a draft for the
future less terse picolisp documentation.
I am currently busy with learning for pre-exames.

2017-02-25 21:58 GMT+01:00 Lindsay John Lawrence <
lawrence.lindsayj...@gmail.com>:

>
> My earlier function 'primeFactorz' had a bug! returning the wrong results
> for actual prime numbers :(
>
> : (primeFactorz 17)
> -> (3 5)   # WRONG!
>
> I have to be more careful with integer arithmetic. The corrected function
> is:
>
> (de primeFactorz (N)
>(use Result
>   (recur (N)
>  (let (Root (inc (sqrt N))  X 2)
> (when (gt0 (% N X))
>(setq X 3)
>(while
>   (and
>  (gt0 (% N X))
>  (<= (inc 'X 2) Root) ) ) )
> (setq X (if (<= X Root) X N)
>   Result (cons X Result))
> (unless (= X N) (recurse (/ N X))) ) )
>   Result ) )
>
> : (primeFactorz 17)
> -> (17)
>
> /Lindsay
>
> Addum: I have to say I am  impressed with how straightforward it is to
> parallelize code in picolisp!
>
> I found my bug while playing with Joh-Tob's Check function and the
> discussion on this thread really helped me to understand how to use 'later'.
>
> : (bench (CheckPrimeFactors 1 CheckP1))
> 1.287 sec
> -> NIL
> : (bench (CheckPrimeFactors 1 CheckP2))
> 1.310 sec
> -> NIL
> : (bench (CheckPrimeFactors 1 CheckP1=P2))
> 1.340 sec
> -> NIL
> : (bench (CheckPrimeFactors 32 CheckOdd))
> (2 3 T 5 T 7 8 T)
> (T 11 12 13 T T T 17)
> (18 19 20 T T 23 T T)
> (T 27 28 29 30 31 32 T)
> 0.006 sec
> -> (T 27 28 29 30 31 32 T)
> : (bench (CheckPrimeFactors 32 CheckEven))
> (T T 4 T 6 T T 9)
> (10 T T T 14 15 16 T)
> (T T T 21 22 T 24 25)
> (26 T T T T T T 33)
> 0.006 sec
> -> (26 T T T T T T 33)
>
>
> Definitions: Assuming 'prime-factors' 'primeFactorz':
>
> : CheckP1
> -> ((N) (ifn (= N (apply '* (prime-factors N))) N T))
> : CheckP2
> -> ((N) (ifn (= N (apply '* (primeFactorz N))) N T))
> : CheckP1=P2
> -> ((N) (ifn (= (sort (prime-factors N)) (sort (primeFactorz N))) N T))
> : CheckOdd
> -> ((N) (if (bit? 1 (length (prime-factors N))) N T))
> : CheckEven
> -> ((N) (ifn (bit? 1 (length (prime-factors N))) N T))
>
>
> (de CheckPrimeFactors (Limit Checkit)
>(let Lst (need 8)
>   (for (N 2 (>= Limit N))
>  (map
> '((L)
>(set L NIL)
>(later L (Checkit N))
>(inc 'N) )
> Lst )
>  (wait NIL (full Lst))
>  (ifn (fully flg? Lst) (println Lst)) ) ) )
>
>
>
>


Re: later and output

2017-02-25 Thread Lindsay John Lawrence
My earlier function 'primeFactorz' had a bug! returning the wrong results
for actual prime numbers :(

: (primeFactorz 17)
-> (3 5)   # WRONG!

I have to be more careful with integer arithmetic. The corrected function
is:

(de primeFactorz (N)
   (use Result
  (recur (N)
 (let (Root (inc (sqrt N))  X 2)
(when (gt0 (% N X))
   (setq X 3)
   (while
  (and
 (gt0 (% N X))
 (<= (inc 'X 2) Root) ) ) )
(setq X (if (<= X Root) X N)
  Result (cons X Result))
(unless (= X N) (recurse (/ N X))) ) )
  Result ) )

: (primeFactorz 17)
-> (17)

/Lindsay

Addum: I have to say I am  impressed with how straightforward it is to
parallelize code in picolisp!

I found my bug while playing with Joh-Tob's Check function and the
discussion on this thread really helped me to understand how to use 'later'.

: (bench (CheckPrimeFactors 1 CheckP1))
1.287 sec
-> NIL
: (bench (CheckPrimeFactors 1 CheckP2))
1.310 sec
-> NIL
: (bench (CheckPrimeFactors 1 CheckP1=P2))
1.340 sec
-> NIL
: (bench (CheckPrimeFactors 32 CheckOdd))
(2 3 T 5 T 7 8 T)
(T 11 12 13 T T T 17)
(18 19 20 T T 23 T T)
(T 27 28 29 30 31 32 T)
0.006 sec
-> (T 27 28 29 30 31 32 T)
: (bench (CheckPrimeFactors 32 CheckEven))
(T T 4 T 6 T T 9)
(10 T T T 14 15 16 T)
(T T T 21 22 T 24 25)
(26 T T T T T T 33)
0.006 sec
-> (26 T T T T T T 33)


Definitions: Assuming 'prime-factors' 'primeFactorz':

: CheckP1
-> ((N) (ifn (= N (apply '* (prime-factors N))) N T))
: CheckP2
-> ((N) (ifn (= N (apply '* (primeFactorz N))) N T))
: CheckP1=P2
-> ((N) (ifn (= (sort (prime-factors N)) (sort (primeFactorz N))) N T))
: CheckOdd
-> ((N) (if (bit? 1 (length (prime-factors N))) N T))
: CheckEven
-> ((N) (ifn (bit? 1 (length (prime-factors N))) N T))


(de CheckPrimeFactors (Limit Checkit)
   (let Lst (need 8)
  (for (N 2 (>= Limit N))
 (map
'((L)
   (set L NIL)
   (later L (Checkit N))
   (inc 'N) )
Lst )
 (wait NIL (full Lst))
 (ifn (fully flg? Lst) (println Lst)) ) ) )


Re: later and output

2017-02-25 Thread Alexander Burger
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 (> 1000 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  (>= 1000 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  (>= 1000 N))
 (map
'((L)
   (set L NIL)
   (later L (check N))
   (inc 'N) )
Lst )
 (wait NIL (full Lst))
 (println Lst) ) )

♪♫ Alex
-- 
UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe


later and output

2017-02-25 Thread Joh-Tob Schäg
I tried to parallelize the following code:
'(let N 2
   (while (> 1000 N )
  (check N)
  (inc 'N)))
where (check N) is defined as '(ifn (= N (apply '* (prime-factors N)))
(print N))

'later has the following form  '(later
place_where_to_save_the_return_result . program to execute)
However i am not interested in storing the result and (later 'NIL prog)
does not work for obvious reasons.
Furthermore (later (new) (print 8)) does not work either.
Using files as in (out "+file" (later (new) (print 8))) does not work
either.

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
process was created or in a file which was opened by it (without reopening
the file before each in print in the child process)?