Plan 9 from Bell Labs’s /usr/web/sources/contrib/fgb/root/sys/src/cmd/4th/examples/bench/matrix.4th

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


.( Loading Matrix Multiplication benchmark...) cr
\ NOTE: This version needs 0.5MB data space

\ A classical benchmark of an O(n**3) algorithm; Matrix Multiplication

\ Part of the programs gathered by John Hennessy for the MIPS
\ RISC project at Stanford. Translated to forth by  Marty Fraeman,
\ Johns Hopkins University/Applied Physics Laboratory.

include lib/comus.4th

200 constant row-size
row-size cells constant row-byte-size

row-size row-size [*] constant mat-size
mat-size cells constant mat-byte-size

mat-byte-size array ima
mat-byte-size array imb
mat-byte-size array imr

: initiate-matrix ( m[row-size][row-size] -- )
  mat-byte-size bounds do
    random dup 120 / 120 * - 60 - i !
  cell +loop
;

: innerproduct ( a[row][*] b[*][column] -- int)
  0 row-size 0 do
    >r over @ over @ * r> + >r
    swap cell+ swap row-byte-size +
    r>
  loop
  >r 2drop r>
;

: main  ( -- )
  ima initiate-matrix
  imb initiate-matrix 
  imr ima mat-byte-size bounds do
    imb row-byte-size bounds do
      j i innerproduct over ! cell+ 
    cell +loop
  row-size cells +loop
  drop
;

main

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.