~ubuntu-branches/ubuntu/wily/afnix/wily

« back to all changes in this revision

Viewing changes to exp/ref/EXP0609.als

  • Committer: Bazaar Package Importer
  • Author(s): Anibal Monsalve Salazar
  • Date: 2011-03-16 21:31:18 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20110316213118-gk4k3ez3e5d2huna
Tags: 2.0.0-1
* QA upload.
* New upstream release
* Debian source format is 3.0 (quilt)
* Fix debhelper-but-no-misc-depends
* Fix ancient-standards-version
* Fix package-contains-linda-override
* debhelper compatibility is 7
* Fix dh-clean-k-is-deprecated

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# ----------------------------------------------------------------------------
 
2
# - EXP0609.als                                                              -
 
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.                   -
 
7
# -                                                                          -
 
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
# ----------------------------------------------------------------------------
 
16
 
 
17
# initialize the shared library
 
18
interp:library "afnix-sys"
 
19
interp:library "afnix-mth"
 
20
 
 
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) {
 
24
  trans i (Integer 0)
 
25
  const m (Vector)
 
26
  do {
 
27
    trans v (m:add (Vector))
 
28
    trans j (Integer)
 
29
    do {
 
30
      v:add (afnix:mth:get-random-integer)
 
31
    } (< (j:++) n)
 
32
  } (< (i:++) n)
 
33
  eval m
 
34
}
 
35
 
 
36
# this expression multiply one line with one column
 
37
const mult-line-column (u v) {
 
38
  assert (u:length) (v:length)
 
39
  trans result 0
 
40
  for (x y) (u v) (result:+= (* x y))
 
41
  eval result
 
42
}
 
43
 
 
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) {
 
47
  for (lv) (mx) {
 
48
    assert true (vector-p lv)
 
49
    for (cv) (my) {
 
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)
 
54
    }
 
55
  }
 
56
}
 
57
 
 
58
# check for some arguments
 
59
if (== 0 (interp:argv:length)) {
 
60
  errorln "usage: axi EXP0609 size [pool size]"
 
61
  afnix:sys:exit 1
 
62
}
 
63
 
 
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)
 
67
 
 
68
# create the shared thread set
 
69
trans tset (Thrset psiz true)
 
70
 
 
71
# perform the matrix multiplication
 
72
mult-matrix (init-matrix n) (init-matrix n)
 
73
 
 
74