1
# ----------------------------------------------------------------------------
3
# - afnix example : chapter 6 : example 09 -
4
# ----------------------------------------------------------------------------
5
# - This program is free software; you can redistribute it and/or modify -
6
# - it provided that this copyright notice is kept intact. -
8
# - This program is distributed in the hope that it will be useful, but -
9
# - without any warranty; without even the implied warranty of -
10
# - merchantability or fitness for a particular purpose. In not event shall -
11
# - the copyright holder be liable for any direct, indirect, incidental or -
12
# - special damages arising in any way out of the use of this software. -
13
# ----------------------------------------------------------------------------
14
# - copyright (c) 1999-2011 amaury darsch -
15
# ----------------------------------------------------------------------------
17
# initialize the shared library
18
interp:library "afnix-sys"
19
interp:library "afnix-mth"
21
# this expression initializes a matrix with random numbers
22
# the matrix is a square one with its size as an argument
23
const init-matrix (n) {
27
trans v (m:add (Vector))
30
v:add (afnix:mth:get-random-integer)
36
# this expression multiply one line with one column
37
const mult-line-column (u v) {
38
assert (u:length) (v:length)
40
for (x y) (u v) (result:+= (* x y))
44
# this expression multiply two vectors assuming one is a line and one is
45
# a column coming from the matrix
46
const mult-matrix (mx my) {
48
assert true (vector-p lv)
50
assert true (vector-p cv)
51
trans thr (tset:request)
52
assert true (thread-p thr)
53
launch thr (mult-line-column lv cv)
58
# check for some arguments
59
if (== 0 (interp:argv:length)) {
60
errorln "usage: axi EXP0609 size [pool size]"
64
# get the matrix size and pool size
65
const n (Integer (interp:argv:get 0))
66
const psiz (if (== 2 (interp:argv:length)) (Integer (interp:argv:get 1)) 10)
68
# create the shared thread set
69
trans tset (Thrset psiz true)
71
# perform the matrix multiplication
72
mult-matrix (init-matrix n) (init-matrix n)