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

« back to all changes in this revision

Viewing changes to exp/ref/0607.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
 
# - 0607.als                                                                -
3
 
# - afnix example : chapter 6 : example 07                                  -
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 no 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-2007 amaury darsch                                   -
15
 
# ---------------------------------------------------------------------------
16
 
 
17
 
# initialize the shared library
18
 
interp:library "afnix-sys"
19
 
 
20
 
# shared vector of threads descriptors
21
 
const thr-group (Vector)
22
 
 
23
 
# this procedure waits until all threads in the group are finished
24
 
const wait-all nil (for (thr) (thr-group) (thr:wait))
25
 
 
26
 
# this procedure initialize a matrix with random numbers
27
 
# the matrix is a square one with its size as an argument
28
 
const init-matrix (n) {
29
 
  trans i (Integer 0)
30
 
  const m (Vector)
31
 
  do {
32
 
    trans v (m:append (Vector))
33
 
    trans j (Integer)
34
 
    do {
35
 
      v:append (afnix:sys:get-random)
36
 
    } (< (j:++) n)
37
 
  } (< (i:++) n)
38
 
  eval m
39
 
}
40
 
 
41
 
# this procedure multiply one line with one column
42
 
const mult-line-column (u v) {
43
 
  assert (u:length) (v:length)
44
 
  trans result 0
45
 
  for (x y) (u v) (result:+= (* x y))
46
 
  eval result
47
 
}
48
 
 
49
 
# this procedure multiply two vectors assuming one is a line and one is
50
 
# a column coming from the matrix
51
 
const mult-matrix (mx my) {
52
 
  for (lv) (mx) {
53
 
    assert true (vector-p lv)
54
 
    for (cv) (my) {
55
 
      assert true (vector-p cv)
56
 
      thr-group:append (launch (mult-line-column lv cv))
57
 
    }
58
 
  }
59
 
}
60
 
 
61
 
# check for some arguments
62
 
if (== 0 (interp:argv:length)) {
63
 
  errorln "usage: axi 0807.als size"
64
 
  afnix:sys:exit 1
65
 
}
66
 
 
67
 
# get the integer and multiply
68
 
const n (Integer (interp:argv:get 0))
69
 
mult-matrix (init-matrix n) (init-matrix n)
70
 
 
71
 
# wait for all threads to complete
72
 
wait-all
73
 
 
74
 
# make sure we have the right number
75
 
assert (* n n) (thr-group:length)
76