Skip to content

Instantly share code, notes, and snippets.

@ksato9700
Created December 29, 2011 09:20
Show Gist options
  • Save ksato9700/1533163 to your computer and use it in GitHub Desktop.
Save ksato9700/1533163 to your computer and use it in GitHub Desktop.
A sample program to simulate systolic array compuatation
-module(systolic_simulator2).
-export([run/0, cell/6, input/3, terminator/0]).
input(AB, Next, [])->
Next ! {AB, stop};
input(AB, Next, Seq)->
[Head | Rest] = Seq,
Next ! {AB, Head},
input(AB, Next, Rest).
cell(Name, a, b, C, NextA, NextB)->
receive
{a,Val}->
NextA ! {a, Val},
cell(Name, Val, b, C, NextA, NextB);
{b,Val}->
NextB ! {b, Val},
cell(Name, a, Val, C, NextA, NextB)
end;
cell(Name, A, b, C, NextA, NextB)->
receive
{b,Val}->
NextB ! {b, Val},
cell(Name, A, Val, C, NextA, NextB)
end;
cell(Name, a, B, C, NextA, NextB) ->
receive
{a,Val}->
NextA ! {a, Val},
cell(Name, Val, B, C, NextA, NextB)
end;
cell(Name, stop, stop, C, NextA, NextB) ->
io:format("~p: Result: ~p~n", [Name, C]),
[NextA, NextB];
cell(Name, A, B, C, NextA, NextB)->
cell(Name, a, b, A*B+C, NextA, NextB).
terminator ()->
receive
{AB, Val}->
[AB, Val]
end.
run()->
TER = spawn(?MODULE, terminator, []),
C33 = spawn(?MODULE, cell, ["C33", a, b, 0, TER, TER]),
C32 = spawn(?MODULE, cell, ["C32", a, b, 0, C33, TER]),
C23 = spawn(?MODULE, cell, ["C23", a, b, 0, TER, C33]),
C22 = spawn(?MODULE, cell, ["C22", a, b, 0, C23, C32]),
C31 = spawn(?MODULE, cell, ["C31", a, b, 0, C32, TER]),
C13 = spawn(?MODULE, cell, ["C13", a, b, 0, TER, C23]),
C21 = spawn(?MODULE, cell, ["C21", a, b, 0, C22, C31]),
C12 = spawn(?MODULE, cell, ["C12", a, b, 0, C13, C22]),
C11 = spawn(?MODULE, cell, ["C11", a, b, 0, C12, C21]),
spawn(?MODULE, input, [a, C11, [3,2,3]]),
spawn(?MODULE, input, [a, C21, [4,5,2]]),
spawn(?MODULE, input, [a, C31, [2,3,5]]),
spawn(?MODULE, input, [b, C11, [3,4,2]]),
spawn(?MODULE, input, [b, C12, [2,5,3]]),
spawn(?MODULE, input, [b, C13, [3,2,5]]).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment