Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prolog version! #58

Open
ghost opened this issue Feb 17, 2016 · 2 comments
Open

Prolog version! #58

ghost opened this issue Feb 17, 2016 · 2 comments

Comments

@ghost
Copy link

ghost commented Feb 17, 2016

Out of interest I have included my own implementation in Prolog. This is written in my own Prolog ( https://github.com/infradig/yxtrang ) which implements Erlang-style processes and message-passing. It is quite slow as I haven't done too much optimisation in this area (in fact I just got it working well-enough to run the test at all).

:-module(skynet).
:-export([start/2]).

start(Size,Div) :-
        spawn(skynet(0,Size,Div)),
        recv(Tot),
        writeln('###=> ',Tot).

skynet(Num,1,Div) :- send(Num).

skynet(Num,Size,Div) :-
    NewSize is Size div Div,
    between(1,Div,Idx),
        NewNum is Num+((Idx-1)*NewSize),
        spawn(skynet(NewNum,NewSize,Div)),
        fail.

skynet(Num,Size,Div) :- process_sum(0,Div).

process_sum(Tot,0) :- send(parent,Tot).

process_sum(Tot,Idx) :-
    recv(N),
    NewTot is Tot+N,
    NewIdx is Idx-1,
    process_sum(NewTot,NewIdx).

It would be trivially easy to distribute the skynet processes over multiple nodes, as send/recv work transparently over networks as well.

@ghost
Copy link
Author

ghost commented Feb 17, 2016

$ ./yxt samples/skynet '--goal=skynet:start(1000000,10)'
Yxtrang v0.1alpha, 64-bits, Feb 17 2016
Consult default => 'samples/skynet'
###=> 499999500000
(13.814 s) yes

Can get it down to around 7 seconds by specifying a thread-pool size of 1 (--tpool=1), in effect forcing it to serialize. Prevents cache-thrashing I suppose.

@ghost
Copy link
Author

ghost commented May 18, 2016

Came back to it recently. Just over 3 seconds now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

0 participants