source: tests/concurrent/actors/matrix.cfa@ 82b96953

ADT ast-experimental stuck-waitfor-destruct
Last change on this file since 82b96953 was 418882af, checked in by caparsons <caparson@…>, 3 years ago

added two actor tests. More to come

  • Property mode set to 100644
File size: 3.7 KB
Line 
1#include <actor.hfa>
2#include <fstream.hfa>
3#include <stdlib.hfa>
4#include <string.h>
5#include <stdio.h>
6
7//////////////////////////////////////////////////////////
8// sample to show what code I need to generate per receive routine:
9//////////////////////////////////////////////////////////
10
11unsigned int xr = 2048, xc = 2048, yc = 2048, Processors = 1; // default values
12
13struct derived_actor {
14 inline actor;
15};
16void ?{}( derived_actor & this ) { ((actor &)this){}; }
17
18struct derived_msg {
19 inline message;
20 int * Z;
21 int * X;
22 int ** Y;
23};
24
25void ?{}( derived_msg & this ) {}
26void ?{}( derived_msg & this, int * Z, int * X, int ** Y ) {
27 ((message &) this){ Finished };
28 this.Z = Z;
29 this.X = X;
30 this.Y = Y;
31}
32
33Allocation receive( derived_actor & receiver, derived_msg & msg ) {
34 for ( unsigned int i = 0; i < yc; i += 1 ) { // multiply X_row by Y_col and sum products
35 msg.Z[i] = 0;
36 for ( unsigned int j = 0; j < xc; j += 1 ) {
37 msg.Z[i] += msg.X[j] * msg.Y[j][i];
38 } // for
39 } // for
40 return Finished;
41}
42
43// // for each receive in the compiler we will need to generate the following routine:
44// static inline derived_actor & ?|?( derived_actor & receiver, derived_msg & msg ) { // add two structures
45// request * new_req = alloc();
46// Allocation (*my_work_fn)( derived_actor &, derived_msg & ) = receive;
47// __receive_fn fn = (__receive_fn)my_work_fn;
48// (*new_req){ &receiver, &msg, fn };
49// send( receiver, *new_req );
50// return receiver;
51// }
52
53int main( int argc, char * argv[] ) {
54 switch ( argc ) {
55 case 5:
56 if ( strcmp( argv[4], "d" ) != 0 ) { // default ?
57 Processors = atoi( argv[4] );
58 if ( Processors < 1 ) goto Usage;
59 } // if
60 case 4:
61 if ( strcmp( argv[3], "d" ) != 0 ) { // default ?
62 xr = atoi( argv[3] );
63 if ( xr < 1 ) goto Usage;
64 } // if
65 case 3:
66 if ( strcmp( argv[2], "d" ) != 0 ) { // default ?
67 xc = atoi( argv[2] );
68 if ( xc < 1 ) goto Usage;
69 } // if
70 case 2:
71 if ( strcmp( argv[1], "d" ) != 0 ) { // default ?
72 yc = atoi( argv[1] );
73 if ( yc < 1 ) goto Usage;
74 } // if
75 case 1: // use defaults
76 break;
77 default:
78 Usage:
79 sout | "Usage: " | argv[0]
80 | " [ yc (> 0) | 'd' (default " | yc
81 | ") ] [ xc (> 0) | 'd' (default " | xc
82 | ") ] [ xr (> 0) | 'd' (default " | xr
83 | ") ] [ processors (> 0) | 'd' (default " | Processors
84 | ") ]" ;
85 exit( EXIT_FAILURE );
86 } // switch
87
88 unsigned int r, c;
89 int * Z[xr], * X[xr], * Y[xc];
90
91 for ( r = 0; r < xr; r += 1 ) { // create/initialize X matrix
92 X[r] = aalloc( xc );
93 for ( c = 0; c < xc; c += 1 ) {
94 X[r][c] = r * c % 37; // for timing
95 } // for
96 } // for
97 for ( r = 0; r < xc; r += 1 ) { // create/initialize Y matrix
98 Y[r] = aalloc( yc );
99 for ( c = 0; c < yc; c += 1 ) {
100 Y[r][c] = r * c % 37; // for timing
101 } // for
102 } // for
103 for ( r = 0; r < xr; r += 1 ) { // create Z matrix
104 Z[r] = aalloc( yc );
105 } // for
106
107 // processor procs[Processors - 1];
108
109 executor e{ Processors, Processors, Processors == 1 ? 1 : Processors * 16, true };
110
111 printf("starting\n");
112
113 start_actor_system( e );
114
115 printf("started\n");
116
117 derived_msg messages[xr];
118
119 derived_actor actors[xr];
120
121 for ( unsigned int r = 0; r < xr; r += 1 ) {
122 messages[r]{ Z[r], X[r], Y };
123 } // for
124
125 for ( unsigned int r = 0; r < xr; r += 1 ) {
126 actors[r] | messages[r];
127 } // for
128
129 printf("stopping\n");
130
131 stop_actor_system();
132
133 printf("stopped\n");
134
135 for ( r = 0; r < xr; r += 1 ) { // deallocate X and Z matrices
136 free( X[r] );
137 free( Z[r] );
138 } // for
139 for ( r = 0; r < xc; r += 1 ) { // deallocate Y matrix
140 free( Y[r] );
141 } // for
142
143 return 0;
144}
Note: See TracBrowser for help on using the repository browser.