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 { |
---|
10 | inline actor; |
---|
11 | }; |
---|
12 | void ?{}( derived_actor & this ) { ((actor &)this){}; } |
---|
13 | |
---|
14 | struct derived_msg { |
---|
15 | inline message; |
---|
16 | int * Z; |
---|
17 | int * X; |
---|
18 | int ** Y; |
---|
19 | }; |
---|
20 | |
---|
21 | void ?{}( derived_msg & this ) {} |
---|
22 | void ?{}( derived_msg & this, int * Z, int * X, int ** Y ) { |
---|
23 | ((message &) this){ Finished }; |
---|
24 | this.Z = Z; |
---|
25 | this.X = X; |
---|
26 | this.Y = Y; |
---|
27 | } |
---|
28 | |
---|
29 | Allocation receive( derived_actor & receiver, derived_msg & msg ) { |
---|
30 | for ( unsigned int i = 0; i < yc; i += 1 ) { // multiply X_row by Y_col and sum products |
---|
31 | msg.Z[i] = 0; |
---|
32 | for ( unsigned int j = 0; j < xc; j += 1 ) { |
---|
33 | msg.Z[i] += msg.X[j] * msg.Y[j][i]; |
---|
34 | } // for |
---|
35 | } // for |
---|
36 | return Finished; |
---|
37 | } |
---|
38 | |
---|
39 | int main( int argc, char * argv[] ) { |
---|
40 | switch ( argc ) { |
---|
41 | case 5: |
---|
42 | if ( strcmp( argv[4], "d" ) != 0 ) { // default ? |
---|
43 | Processors = atoi( argv[4] ); |
---|
44 | if ( Processors < 1 ) goto Usage; |
---|
45 | } // if |
---|
46 | case 4: |
---|
47 | if ( strcmp( argv[3], "d" ) != 0 ) { // default ? |
---|
48 | xr = atoi( argv[3] ); |
---|
49 | if ( xr < 1 ) goto Usage; |
---|
50 | } // if |
---|
51 | case 3: |
---|
52 | if ( strcmp( argv[2], "d" ) != 0 ) { // default ? |
---|
53 | xc = atoi( argv[2] ); |
---|
54 | if ( xc < 1 ) goto Usage; |
---|
55 | } // if |
---|
56 | case 2: |
---|
57 | if ( strcmp( argv[1], "d" ) != 0 ) { // default ? |
---|
58 | yc = atoi( argv[1] ); |
---|
59 | if ( yc < 1 ) goto Usage; |
---|
60 | } // if |
---|
61 | case 1: // use defaults |
---|
62 | break; |
---|
63 | default: |
---|
64 | Usage: |
---|
65 | sout | "Usage: " | argv[0] |
---|
66 | | " [ yc (> 0) | 'd' (default " | yc |
---|
67 | | ") ] [ xc (> 0) | 'd' (default " | xc |
---|
68 | | ") ] [ xr (> 0) | 'd' (default " | xr |
---|
69 | | ") ] [ processors (> 0) | 'd' (default " | Processors |
---|
70 | | ") ]" ; |
---|
71 | exit( EXIT_FAILURE ); |
---|
72 | } // switch |
---|
73 | |
---|
74 | unsigned int r, c; |
---|
75 | int * Z[xr], * X[xr], * Y[xc]; |
---|
76 | |
---|
77 | for ( r = 0; r < xr; r += 1 ) { // create/initialize X matrix |
---|
78 | X[r] = aalloc( xc ); |
---|
79 | for ( c = 0; c < xc; c += 1 ) { |
---|
80 | X[r][c] = r * c % 37; // for timing |
---|
81 | } // for |
---|
82 | } // for |
---|
83 | for ( r = 0; r < xc; r += 1 ) { // create/initialize Y matrix |
---|
84 | Y[r] = aalloc( yc ); |
---|
85 | for ( c = 0; c < yc; c += 1 ) { |
---|
86 | Y[r][c] = r * c % 37; // for timing |
---|
87 | } // for |
---|
88 | } // for |
---|
89 | for ( r = 0; r < xr; r += 1 ) { // create Z matrix |
---|
90 | Z[r] = aalloc( yc ); |
---|
91 | } // for |
---|
92 | |
---|
93 | executor e{ Processors, Processors, Processors == 1 ? 1 : Processors * 16, true }; |
---|
94 | |
---|
95 | printf("starting\n"); |
---|
96 | |
---|
97 | start_actor_system( e ); |
---|
98 | |
---|
99 | printf("started\n"); |
---|
100 | |
---|
101 | derived_msg messages[xr]; |
---|
102 | |
---|
103 | derived_actor actors[xr]; |
---|
104 | |
---|
105 | for ( unsigned int r = 0; r < xr; r += 1 ) { |
---|
106 | messages[r]{ Z[r], X[r], Y }; |
---|
107 | } // for |
---|
108 | |
---|
109 | for ( unsigned int r = 0; r < xr; r += 1 ) { |
---|
110 | actors[r] | messages[r]; |
---|
111 | } // for |
---|
112 | |
---|
113 | printf("stopping\n"); |
---|
114 | |
---|
115 | stop_actor_system(); |
---|
116 | |
---|
117 | printf("stopped\n"); |
---|
118 | |
---|
119 | for ( r = 0; r < xr; r += 1 ) { // deallocate X and Z matrices |
---|
120 | free( X[r] ); |
---|
121 | free( Z[r] ); |
---|
122 | } // for |
---|
123 | for ( r = 0; r < xc; r += 1 ) { // deallocate Y matrix |
---|
124 | free( Y[r] ); |
---|
125 | } // for |
---|
126 | |
---|
127 | return 0; |
---|
128 | } |
---|