1 | #include <actor.hfa> |
---|
2 | #include <fstream.hfa> |
---|
3 | #include <stdlib.hfa> |
---|
4 | #include <string.h> |
---|
5 | #include <stdio.h> |
---|
6 | |
---|
7 | ssize_t xr = 500, xc = 500, yc = 500, Processors = 1; // default values, must be signed |
---|
8 | |
---|
9 | struct derived_actor { inline actor; }; |
---|
10 | |
---|
11 | struct derived_msg { |
---|
12 | inline message; |
---|
13 | int * Z; |
---|
14 | int * X; |
---|
15 | int ** Y; |
---|
16 | }; |
---|
17 | |
---|
18 | void ?{}( derived_msg & this ) {} |
---|
19 | void ?{}( derived_msg & this, int * Z, int * X, int ** Y ) { |
---|
20 | set_allocation( this, Nodelete ); |
---|
21 | this.Z = Z; |
---|
22 | this.X = X; |
---|
23 | this.Y = Y; |
---|
24 | } |
---|
25 | |
---|
26 | allocation receive( derived_actor & receiver, derived_msg & msg ) { |
---|
27 | for ( i; yc ) { // multiply X_row by Y_col and sum products |
---|
28 | msg.Z[i] = 0; |
---|
29 | for ( j; xc ) { |
---|
30 | msg.Z[i] += msg.X[j] * msg.Y[j][i]; |
---|
31 | } // for |
---|
32 | } // for |
---|
33 | return Finished; |
---|
34 | } |
---|
35 | |
---|
36 | int main( int argc, char * argv[] ) { |
---|
37 | switch ( argc ) { |
---|
38 | case 5: |
---|
39 | if ( strcmp( argv[4], "d" ) != 0 ) { // default ? |
---|
40 | Processors = ato( argv[4] ); |
---|
41 | if ( Processors < 1 ) fallthru default; |
---|
42 | } // if |
---|
43 | case 4: |
---|
44 | if ( strcmp( argv[3], "d" ) != 0 ) { // default ? |
---|
45 | xr = ato( argv[3] ); |
---|
46 | if ( xr < 1 ) fallthru default; |
---|
47 | } // if |
---|
48 | case 3: |
---|
49 | if ( strcmp( argv[2], "d" ) != 0 ) { // default ? |
---|
50 | xc = ato( argv[2] ); |
---|
51 | if ( xc < 1 ) fallthru default; |
---|
52 | } // if |
---|
53 | case 2: |
---|
54 | if ( strcmp( argv[1], "d" ) != 0 ) { // default ? |
---|
55 | yc = ato( argv[1] ); |
---|
56 | if ( yc < 1 ) fallthru default; |
---|
57 | } // if |
---|
58 | case 1: // use defaults |
---|
59 | break; |
---|
60 | default: |
---|
61 | exit | "Usage: " | argv[0] |
---|
62 | | " [ yc (> 0) | 'd' (default " | yc |
---|
63 | | ") ] [ xc (> 0) | 'd' (default " | xc |
---|
64 | | ") ] [ xr (> 0) | 'd' (default " | xr |
---|
65 | | ") ] [ processors (> 0) | 'd' (default " | Processors |
---|
66 | | ") ]" ; |
---|
67 | } // switch |
---|
68 | |
---|
69 | int * Z[xr], * X[xr], * Y[xc]; |
---|
70 | |
---|
71 | for ( r; xr ) { // create/initialize X matrix |
---|
72 | X[r] = aalloc( xc ); |
---|
73 | for ( c; xc ) { |
---|
74 | X[r][c] = r * c % 37; // for timing |
---|
75 | } // for |
---|
76 | } // for |
---|
77 | for ( r; xc ) { // create/initialize Y matrix |
---|
78 | Y[r] = aalloc( yc ); |
---|
79 | for ( c; yc ) { |
---|
80 | Y[r][c] = r * c % 37; // for timing |
---|
81 | } // for |
---|
82 | } // for |
---|
83 | for ( r; xr ) { // create Z matrix |
---|
84 | Z[r] = aalloc( yc ); |
---|
85 | } // for |
---|
86 | |
---|
87 | executor e{ Processors, Processors, Processors == 1 ? 1 : Processors * 16, true }; |
---|
88 | |
---|
89 | sout | "starting"; |
---|
90 | actor_start( e ); |
---|
91 | sout | "started"; |
---|
92 | derived_msg messages[xr]; |
---|
93 | derived_actor actors[xr]; |
---|
94 | |
---|
95 | for ( r; xr ) { |
---|
96 | messages[r]{ Z[r], X[r], Y }; |
---|
97 | } // for |
---|
98 | for ( r; xr ) { |
---|
99 | actors[r] | messages[r]; |
---|
100 | } // for |
---|
101 | |
---|
102 | sout | "stopping"; |
---|
103 | actor_stop(); |
---|
104 | sout | "stopped"; |
---|
105 | |
---|
106 | for ( r; xr ) { // deallocate X and Z matrices |
---|
107 | free( X[r] ); |
---|
108 | free( Z[r] ); |
---|
109 | } // for |
---|
110 | for ( r; xc ) { // deallocate Y matrix |
---|
111 | free( Y[r] ); |
---|
112 | } // for |
---|
113 | } |
---|