Sunday, January 25, 2009

SEAForth Matrix Multiplication Example update 1

It's been about 1 year since I posted my initial SEAForth matrix multiplication example. A lot has happened since then including changes to the VentureForth IDE/simulator as well as the commercial release of actual SEAForth chips. That's the good news. The bad news (for me at least) was that the changes were such that it broke my old code. That wouldn't have been so bad if there had been "this has been deprecated" section in the docs, but then I suppose that would have made the transition too easy. ;) Seriously though, some of the changes actually make the code much cleaner. Also I've made the code much more modular thanks to the judicious use of include files.

First I'll go through the changes that I've noticed in the new system and then an overview the new matrix multiplication code.

Change 1 : Node specification

In the original VentureForth you specified the node you were compiling to this way:


12 node !


Now the compiling node is specified this way:


12 {node
...
.../ some VentureForth code
...
node}


It's a lot clearer where compiling to a node begins and ends. This can also lead to a clean modular code pattern that will be discussed later.

Change 2 : Stream Specification

Section 2.3.1 of the VentureForth manual explains streams. Basically it's a way for code and data to be initially distributed to the correct nodes. If you are just learning the SEAForth system as I am you probably aren't ready to create your own custom stream nodepath. But you do need to make sure all of the nodes are initialized. The following code will do that.

19stream

reset

Change 3 : Literal Specification

The original VentureForth compiler would compile literals automatically. Now a "#" is required after every literal. Note that port selection constants are really literals and require a "#" after them also. So:


'r--- b!
7 for @p+ !b unext


becomes:


'r--- # b!
7 # for @p+ !b unext

Toward More Modular Programs

I saw the following code pattern in the "blinktest" sample project:


\ Main program.vf
.
.
19 {node 0 org here =p include module.vf node}
.
.


So I've followed a similar pattern for my code, breaking what was a single monolithic module into 6 smaller (and in my opinion easier to read) modules.

Main module : matrixmult.vf

( $Id: matrixmult.vf,v 1.0 2006-11-2 $ )
\ Matrix multiplication example
\ Parrallel multiplication of a 2 row matrix by a 2 column matrix
\ 1) Data initially is in node 12
\ 2) Row1 is passed to node 18
\ 3) Row2 is passed to node 13
\ 4) Column1 is passed to nodes 18 and 13 simultaneously
\ 5) Multiplications R1*C1 and R2*C1 done simultaneously
\ 6) Column2 is passed to nodes 18 and 13 simultaneously
\ 7) Multiplications R1*C2 and R2*C2 done simultaneously
\ 8) Results R1*C1, R1*C2, R2*C1, R2*C2 are returned to node 12

v.VF +include" c7Dr03/romconfig.f"

12 {node include node12.vf node}
13 {node include node13.vf node}
18 {node include node18.vf node}

19stream

reset

cr


Node 12 code : node12.vf
Note: This is the taskmaster node. It passes the data to nodes 13 and 18 to be processed and the collates the results.



\ ******* node 12 ***************************************
\ Pass row1 to node 13, row2 to node 18
\ and columns 1 and 2 to nodes 13 and 18 simultaneously

0 org here =p

\ Stream R1 to node 13
'r--- # b! \ point reg b to right node. (I/O node 13)
7 # for @p+ !b unext
1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ,

\ Stream R2 to node 18
| '-d-- # b! . . \ point reg b to down node. (I/O node 18)
7 # for @p+ !b . unext
9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 ,

\ Stream C1 to nodes 13 and 18 simultaneously
'rd-- # b! . . \ point reg b to right and down nodes. (I/O nodes 13 and 18)
0 # !b . . \ dummy write for synch purposes
15 # for @p+ !b . unext
17 , 18 , 19 , 20 , 21 , 22 , 23 , 24 ,
25 , 26 , 27 , 28 , 29 , 30 , 31 , 32 ,

59 # a! . . \ point A register to address 59
'r--- # b! . . \ point reg b to right node. (I/O node 13)
@b !a+ . . \ Get result R1*C1
@b !a+ . . \ Get result R1*C2
'-d-- # b! . . \ point reg b to down node. (I/O node 18)
@b !a+ . . \ Get result R2*c1
@b !a+ . . \ Get result R2*c2


Nodes 13 and 18 basically do the same thing. Get a row of data from node 12, then use that row to calculate a dot product of two columns of data.

Node 13 code : node13.vf

\ ******* node 13 *********************************************
\ Receive row1 then multiply it by columns 1 and 2
\ and return results.

0 org

include 8bitmult.vf

here =p
'r--- # b! . . \ point reg b to right node

include vectormult.vf


Node 18 code : node18.vf

\ ******* node 18 *********************************************
\ Receive row2 then multiply it by columns 1 and 2
\ and return results.

0 org

include 8bitmult.vf

here =p
'-d-- # b! . . \ point reg b to down node

include vectormult.vf


8 bit multiply : 8bitmult.vf

: 8*8 ( n1 n2 -- n1*n2 ) \ 16 bit output
push 2* 2* . \ left shift n1 8 times
2* 2* 2* .
2* 2* 2* .
pop +* +* +*
+* +* +* +*
+* push drop .
pop ;


Vector multiply : vectormult.vf

\ ******* vector multiply *******************************
\ Receive a row then multiply it by columns 1 and 2
\ and return results.

$20 # dup a! push \ point reg a to buffer - leave adr on return stack

7 # for @b !a+ unext

@b drop \ dummy read for synchronization purposes

pop dup a! push \ reset a to start of buffer
dup xor \ initilize TOS to 0
7 # for
@b @a+ 8*8
+ next

dup dup xor \ preserve result from vector mult - initialize TOS to 0

pop a!
7 # for
@b @a+ 8*8
+ next

!b !b \ Send results back to right node

Labels:

Monday, January 28, 2008

SEAForth matrix multiplication example

Disclaimer: I don't work for IntellaSys and I consider myself a novice in NOSC programming in general and the SEAForth architecture in particular. But, by using documentation publicly at the IntellaSys website I have figured out how to write working programs for it.



I'm going to diverge from ColorForth for this particular blog entry to talk about SEAForth. Hopefully the ColorForth version of the SEAForth compiler will be released soon, but for now the public SEAForth programming environment, VentureForth, is closer IMO to MachineForth. (No color tokens. You have to keep up with code slots etc.) Still VentureForth does have a strong "fun factor" when it comes to programming. Personally I think that when programming, rather than thinking about ALGOL like "algorithms" you should think about the hardware and how to directly map your problem across it. Programming comes later.

SEAForth Architecture

The SEAForth architecture is the latest hardware design from Forth creator Chuck Moore. Like previous designs it is a NOSC or "no operand stack computer". The history and documentation of Mr. Moore's early NOSC work is documented at Jeff Fox's Ultratechnology website. This means that all operations take their operands from the stack. SEAForth goes a step further and has multiple cores (24 in the first implementation) on one chip.


Here is a diagram of a SEAForth chip.



U

















U





18R19L20R21L22R23


D

D

D

D

D

D



12R13L14R15L16R17


U

U

U

U

U

U



06R07L08R09L10R23


D

D

D

D

D

D

L00R01L02R03L04R05


U





















The numbers 0..23 represent the individual cores. The letters U, D, L and R represent the ports. Mr. Fox explained that these ports can point to other cores on chip or to off chip devices. For example communication between ports 18 and 19 always use the "R" port regardless of whether its 18 sending the data or receiving it. Also the U port for cores 18 and 23 goes to the A/D and D/A pins in some configurations. Knowing how port communication works is essential for understanding the "crawler" application walkthrough that IntellaSys has provided with their development kit.

Sample Application

In trying to come up with a test program to explore SEAForth, I settled on an obvious parallel processing application that's easy to understand, matrix multiplication. First lets look at the mathematical definition of matrix multiplication. The product of two matrices A and B results in a new matrix C where each element of C is the sum of the products of the rows in A and the columns in B. Let's take the case of a 2 row matrix by a 2 column matrix.




Matrix A:




12345679
910111213141516


Matrix B:









1725
1826
1927
2028
2129
2230
2331
2432

If we label the matrix A rows R1 and R2 and the matrix B columns C1 and C2 then the resulting matrix is:




R1*C1R1*C2
R2*C1R2*C2
Mapping of problem to SEAForth chip

So this is how I mapped the problem to the SEAForth chip.
  • Data is initially in node 12
  • Row 1 is passed to node 18
  • Row 2 is passed to node 13
  • Column 1 is passed to nodes 18 and 13 simultaneously
  • Multiplications R1*C1 and R2*C1 are done simultaneously
  • Column 2 is passed to nodes 18 and 13 simultaneously
  • Multiplications R1*C2 and R2*C2 are done simultaneously
  • Results R1*C1, R1*C2 from node 18 are returned to node 12
  • Results R2*C1, R2*C2 from node 13 are returned to node 12
Data streaming

As you may have noticed, in this program node 12 is responsible for streaming data to other nodes and then collecting the results. For this test program the data is stored in node 12's core memory. For larger applications this could be streamed in from external memory. Consider the following code snippet.


\ Stream R1 to node 13
'r--- b! . . \ point reg b to right node. (I/O node 13)
7 for @p+ !b . unext
[ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , ]

This is a microloop. The first line configures the b register to communicate with the "right" node to node 12. The next line is a microloop. The entire loop fits in one SEAForth word of memory. The data (values 1...8) are stored in the words immediately following the loop. The instruction @p+ copies a word from the program counter to the stack and the increments the program counter. The instruction !b takes the word on the top of the stack and sends it to the node(s) pointed to by the b register. You can write to more than one node at once as shown by this code.


\ Stream C1 to nodes 13 and 18 simultaneously
'rd-- b! . . \ point reg b to right and down nodes. (I/O nodes 13 and 18)
0 !b . . \ dummy write for synch purposes
15 for @p+ !b . unext
[ 17 , 18 , 19 , 20 , 21 , 22 , 23 , 24 ,
25 , 26 , 27 , 28 , 29 , 30 , 31 , 32 , ]

The a register is used to write to internal core memory. The last part of the code for node 12 reads 4 results (2 from node 13 and 2 from node 18) and stores them sequentially starting at hex address 32.

$32 a! . . \ point A register to address 32
'r--- b! . . \ point reg b to right node. (I/O node 13)
@b !a+ . . \ Get result R1*C1
@b !a+ . . \ Get result R1*C2
'-d-- b! . . \ point reg b to down node. (I/O node 18)
@b !a+ . . \ Get result R2*c1
@b !a+ . . \ Get result R2*c2


Multiplication on SEAForth

The SEAForth chip does not have a direct multiplication instruction. Instead it has a multiply step. All you really need to know is that if you want to multiply two n-bit numbers n1 and n2, put both on the stack, shift 1 number left n times, then execute the multiply step n times. For 8 bit multiplication you can use the following code.

: 8*8 ( n1 n2 -- n1*n2 ) \ 16 bit output
push 2* 2* . \ left shift n1 8 times
2* 2* 2* .
2* 2* 2* .
pop +* +* +*
+* +* +* +*
+* push drop .
pop ;

This creates a subroutine 8*8 which is used to do 8 bit multiplies. That's all I need for this particular example. Here is the rest of the code for node 13. Node 18 uses the same code.


-d-- b! . . \ point reg b to down node (I/O node 12)
$20 dup a! push \ point reg a to buffer - leave adr on return stack

7 for @b !a+ . unext \ read row R1 from node 12 and store in buffer

@b drop . . \ dummy read for synchronization purposes

pop dup a! push \ reset a to start of buffer
dup xor \ initialize TOS to 0
7 for \ Loop to multiply elements from R1 and C1
@b @a+ 8*8 . \ Get next R1 element from buffer and next C1 element from node 12
+ next . .

dup dup xor . \ preserve result from vector multiply - initialize TOS to 0

pop a! . .
\ reset a to start of buffer
7 for \ Loop to multiply elements from R1 and C2
@b @a+ 8*8 . \ Get next R1 element from buffer and next C2 element from node 12
+ next . .

!b !b . . \ Send results back to node 12

Performance

First I need to point out that the goal isn't to show how fast I can do multiplication but rather how efficient the SEAForth architecture can be for exploiting parallelism. On the simulator I have it was able to complete a single 8 bit multiplication in 50 cycles. Considering that there are a total of 32 multiplications that 1,600 cycles for multiplication alone. Using 3 cores the operation is completed in 945 cycles which represents a 41% speed up. It should be intuitively obvious that even great speed ups can be achieved by mapping the problem over more cores. I have a couple of ideas on that to test but I'll save that for another day.

Labels: