Skip to content

Instantly share code, notes, and snippets.

@ksato9700
Created December 29, 2011 20:31
Show Gist options
  • Save ksato9700/1536077 to your computer and use it in GitHub Desktop.
Save ksato9700/1536077 to your computer and use it in GitHub Desktop.
Yet another systolic array simulator implementation in Erlang
-module(systolic_simulator3).
-export([run/0, pipe/3, terminator/0, cell/2, cell/4, input/3]).
input(AB, Next, [])->
Next ! {AB, stop};
input(AB, Next, Seq)->
[Head | Rest] = Seq,
Next ! {AB, Head},
input(AB, Next, Rest).
pipe(AB, Cell, Next)->
receive
{AB, Val}->
Cell ! {AB, Val},
Next ! {AB, Val},
pipe(AB, Cell, Next)
end.
terminator ()->
receive
{AB, Val}->
[AB, Val]
end.
cell(Name, C, AB, ABV)->
receive
{AB, stop}->
io:format("Result: ~p: ~p~n", [Name, C]);
{AB, Val}->
cell(Name, Val* ABV + C)
end.
cell(Name, C)->
receive
{a, Val}->
cell(Name, C, b, Val);
{b, Val}->
cell(Name, C, a, Val)
end.
generate_cell(Name, Next)->
Cell = spawn(?MODULE, cell, [Name, 0]),
PipeA = spawn(?MODULE, pipe, [a, Cell, element(1,Next)]),
PipeB = spawn(?MODULE, pipe, [b, Cell, element(2,Next)]),
{PipeA, PipeB}.
run()->
TERM = spawn(?MODULE, terminator, []),
{C33A,C33B} = generate_cell("C33", {TERM, TERM}),
{C32A,C32B} = generate_cell("C32", {C33A, TERM}),
{C23A,C23B} = generate_cell("C23", {TERM, C33B}),
{C22A,C22B} = generate_cell("C22", {C23A, C32B}),
{C31A,C31B} = generate_cell("C31", {C32A, TERM}),
{C13A,C13B} = generate_cell("C13", {TERM, C23B}),
{C21A,C21B} = generate_cell("C21", {C22A, C31B}),
{C12A,C12B} = generate_cell("C12", {C13A, C22B}),
{C11A,C11B} = generate_cell("C11", {C12A, C21B}),
spawn(?MODULE, input, [a, C11A, [3,2,3]]),
spawn(?MODULE, input, [a, C21A, [4,5,2]]),
spawn(?MODULE, input, [a, C31A, [2,3,5]]),
spawn(?MODULE, input, [b, C11B, [3,4,2]]),
spawn(?MODULE, input, [b, C12B, [2,5,3]]),
spawn(?MODULE, input, [b, C13B, [3,2,5]]).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment