[gambit-list] threading

Leif Bruder leifbruder at gmail.com
Sat Mar 11 05:08:43 EST 2017


Here's an Erlang version, with lots of grains of salt added: AFAIK Erlang
doesn't provide bigint expt and sqrt, so they had to be added to the Erlang
src. And Erlang isn't exactly known for numerical performance. With that in
mind, here goes:

-module(fib).
-export([main/1]).
-mode(compile).

expt(_, 0) -> 1;
expt(A, 1) -> A;
expt(A, B) -> A * expt(A, B-1).

isqrt(Num, M, N) when abs(M - N) =< 1 ->
    if  N * N =< Num -> N;
        true         -> N - 1
    end;

isqrt(Num, _, N) ->
    isqrt(Num, N, (N + Num div N) div 2).

integer_sqrt(Value) when Value >= 0 -> isqrt(Value, 1, (1 + Value) div 2).

short_delay(Value) -> integer_sqrt(Value).

range(I, J) -> lists:seq(I, J-1).

tfib(0, GV) -> short_delay(GV), 1;
tfib(1, GV) -> short_delay(GV), 1;
tfib(N, GV) ->
    short_delay(GV),
    Pid = self(),
    spawn(fun() -> Pid ! tfib(N-2, GV) end),
    Y = tfib(N-1, GV),
    receive
        Result -> Result + Y
    end.

goloop(0) -> ok;
goloop(ThreadsRemaining) ->
    receive
        _ -> goloop(ThreadsRemaining - 1)
    end.

go(N, Repeat, Granularity) ->
    Pid = self(),
    GV = expt(11, Granularity),
    Threads = [spawn(fun() -> Pid ! tfib(N, GV) end) || _ <- range(0,
Repeat)],
    goloop(length(Threads)).

main(_) ->
    go(15, 100,    0),
    go(15,  20, 1001).


$ time escript fib.erl

real    1m22.728s
user    7m54.460s
sys    0m0.396s


Ran on a six-core machine, Erlang used up all cores up to nearly 100%.

Leif

2017-03-11 1:45 GMT+01:00 Marc Feeley <feeley at iro.umontreal.ca>:

> I considered Ruby… but then learned that MRI only implements green threads…
>
> Thanks for the suggestions for other languages… Perl actually supports
> threads? Who would have thought!
>
> Can I ask for your help in translating the benchmark program?
>
> The actual computation performed by the “short-delay” procedure doesn’t
> matter, but it should take about 50 microseconds (in order to isolate the
> performance related to the implementation of threads).
>
> Marc
>
> ;;; Compute fib using threads.
>
> (declare (standard-bindings) (fixnum) (not safe) (block))
>
> (define big 0)
>
> (define (granularity-set! g)
>   (set! big (expt 11 g)))
>
> (define (short-delay)
>   (integer-sqrt big))
>
> (define (tfib n)
>   (short-delay)
>   (if (< n 2)
>       1
>       (let* ((x (thread-start! (make-thread (lambda () (tfib (- n 2))))))
>              (y (tfib (- n 1))))
>           (+ (thread-join! x) y))))
>
> (define (range i j)
>   (let loop ((j (- j 1)) (lst '()))
>     (if (< j i)
>         lst
>         (loop (- j 1) (cons j lst)))))
>
> (define (go n repeat)
>   (let ((threads
>          (map (lambda (i) (make-thread (lambda () (tfib n))))
>               (range 0 repeat))))
>     (for-each thread-start! threads)
>     (map thread-join! threads)))
>
> (granularity-set! 0)
>
> (go 15 100)
>
> (granularity-set! 1001) ;; granularity of about 50us per thread
>
> (go 15 20)
>
>
> > On Mar 10, 2017, at 7:30 PM, C K Kashyap <ckkashyap at gmail.com> wrote:
> >
> > Super!!!
> > Perl 5 may be a good candidate as well.
> > Regards,
> > Kashyap
> >
> > On Fri, Mar 10, 2017 at 4:28 PM, Bradley Lucier <lucier at math.purdue.edu>
> wrote:
> > Would a multi-threaded http server test what you want to test?
> >
> > Brad
> > _______________________________________________
> > Gambit-list mailing list
> > Gambit-list at iro.umontreal.ca
> > https://webmail.iro.umontreal.ca/mailman/listinfo/gambit-list
> >
>
> _______________________________________________
> Gambit-list mailing list
> Gambit-list at iro.umontreal.ca
> https://webmail.iro.umontreal.ca/mailman/listinfo/gambit-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.iro.umontreal.ca/pipermail/gambit-list/attachments/20170311/befb35f1/attachment.htm>


More information about the Gambit-list mailing list