1 | #include <actor.hfa> |
---|
2 | #include <fstream.hfa> |
---|
3 | #include <stdlib.hfa> |
---|
4 | #include <string.h> |
---|
5 | #include <stdio.h> |
---|
6 | #include "bench.hfa" |
---|
7 | |
---|
8 | unsigned int xr = 500, xc = 500, yc = 500, Processors = 1; // default values |
---|
9 | |
---|
10 | struct derived_actor { inline actor; }; |
---|
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 * 32, true }; |
---|
91 | |
---|
92 | uint64_t start_time = bench_time(); |
---|
93 | |
---|
94 | start_actor_system( e ); |
---|
95 | |
---|
96 | derived_msg messages[xr]; |
---|
97 | |
---|
98 | derived_actor actors[xr]; |
---|
99 | |
---|
100 | for ( unsigned int r = 0; r < xr; r += 1 ) { |
---|
101 | messages[r]{ Z[r], X[r], Y }; |
---|
102 | } // for |
---|
103 | |
---|
104 | for ( unsigned int r = 0; r < xr; r += 1 ) { |
---|
105 | actors[r] << messages[r]; |
---|
106 | } // for |
---|
107 | |
---|
108 | stop_actor_system(); |
---|
109 | |
---|
110 | uint64_t end_time = bench_time(); |
---|
111 | |
---|
112 | printf("%.2f\n", ((double)(end_time - start_time))*((double)1e-9) ); |
---|
113 | |
---|
114 | for ( r = 0; r < xr; r += 1 ) { // deallocate X and Z matrices |
---|
115 | free( X[r] ); |
---|
116 | free( Z[r] ); |
---|
117 | } // for |
---|
118 | for ( r = 0; r < xc; r += 1 ) { // deallocate Y matrix |
---|
119 | free( Y[r] ); |
---|
120 | } // for |
---|
121 | |
---|
122 | return 0; |
---|
123 | } |
---|