1 | #include <actor.hfa> |
---|
2 | #include <fstream.hfa> |
---|
3 | #include <stdlib.hfa> |
---|
4 | #include <string.h> |
---|
5 | #include <stdio.h> |
---|
6 | |
---|
7 | unsigned int xr = 500, xc = 500, yc = 500, Processors = 1; // default values |
---|
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 | ((message &) 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 ( unsigned int i = 0; i < yc; i += 1 ) { // multiply X_row by Y_col and sum products |
---|
28 | msg.Z[i] = 0; |
---|
29 | for ( unsigned int j = 0; j < xc; j += 1 ) { |
---|
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 = atoi( argv[4] ); |
---|
41 | if ( Processors < 1 ) goto Usage; |
---|
42 | } // if |
---|
43 | case 4: |
---|
44 | if ( strcmp( argv[3], "d" ) != 0 ) { // default ? |
---|
45 | xr = atoi( argv[3] ); |
---|
46 | if ( xr < 1 ) goto Usage; |
---|
47 | } // if |
---|
48 | case 3: |
---|
49 | if ( strcmp( argv[2], "d" ) != 0 ) { // default ? |
---|
50 | xc = atoi( argv[2] ); |
---|
51 | if ( xc < 1 ) goto Usage; |
---|
52 | } // if |
---|
53 | case 2: |
---|
54 | if ( strcmp( argv[1], "d" ) != 0 ) { // default ? |
---|
55 | yc = atoi( argv[1] ); |
---|
56 | if ( yc < 1 ) goto Usage; |
---|
57 | } // if |
---|
58 | case 1: // use defaults |
---|
59 | break; |
---|
60 | default: |
---|
61 | Usage: |
---|
62 | sout | "Usage: " | argv[0] |
---|
63 | | " [ yc (> 0) | 'd' (default " | yc |
---|
64 | | ") ] [ xc (> 0) | 'd' (default " | xc |
---|
65 | | ") ] [ xr (> 0) | 'd' (default " | xr |
---|
66 | | ") ] [ processors (> 0) | 'd' (default " | Processors |
---|
67 | | ") ]" ; |
---|
68 | exit( EXIT_FAILURE ); |
---|
69 | } // switch |
---|
70 | |
---|
71 | unsigned int r, c; |
---|
72 | int * Z[xr], * X[xr], * Y[xc]; |
---|
73 | |
---|
74 | for ( r = 0; r < xr; r += 1 ) { // create/initialize X matrix |
---|
75 | X[r] = aalloc( xc ); |
---|
76 | for ( c = 0; c < xc; c += 1 ) { |
---|
77 | X[r][c] = r * c % 37; // for timing |
---|
78 | } // for |
---|
79 | } // for |
---|
80 | for ( r = 0; r < xc; r += 1 ) { // create/initialize Y matrix |
---|
81 | Y[r] = aalloc( yc ); |
---|
82 | for ( c = 0; c < yc; c += 1 ) { |
---|
83 | Y[r][c] = r * c % 37; // for timing |
---|
84 | } // for |
---|
85 | } // for |
---|
86 | for ( r = 0; r < xr; r += 1 ) { // create Z matrix |
---|
87 | Z[r] = aalloc( yc ); |
---|
88 | } // for |
---|
89 | |
---|
90 | executor e{ Processors, Processors, Processors == 1 ? 1 : Processors * 16, true }; |
---|
91 | |
---|
92 | printf("starting\n"); |
---|
93 | |
---|
94 | start_actor_system( e ); |
---|
95 | |
---|
96 | printf("started\n"); |
---|
97 | |
---|
98 | derived_msg messages[xr]; |
---|
99 | |
---|
100 | derived_actor actors[xr]; |
---|
101 | |
---|
102 | for ( unsigned int r = 0; r < xr; r += 1 ) { |
---|
103 | messages[r]{ Z[r], X[r], Y }; |
---|
104 | } // for |
---|
105 | |
---|
106 | for ( unsigned int r = 0; r < xr; r += 1 ) { |
---|
107 | actors[r] << messages[r]; |
---|
108 | } // for |
---|
109 | |
---|
110 | printf("stopping\n"); |
---|
111 | |
---|
112 | stop_actor_system(); |
---|
113 | |
---|
114 | printf("stopped\n"); |
---|
115 | |
---|
116 | for ( r = 0; r < xr; r += 1 ) { // deallocate X and Z matrices |
---|
117 | free( X[r] ); |
---|
118 | free( Z[r] ); |
---|
119 | } // for |
---|
120 | for ( r = 0; r < xc; r += 1 ) { // deallocate Y matrix |
---|
121 | free( Y[r] ); |
---|
122 | } // for |
---|
123 | |
---|
124 | return 0; |
---|
125 | } |
---|