source: tests/concurrency/actors/matrixMultiply.cfa @ e4c3819

Last change on this file since e4c3819 was e4c3819, checked in by Peter A. Buhr <pabuhr@…>, 9 months ago

refactor cofor.hfa into cofor.cfa, adjust Makefile.am to handle cofor.cfa

  • Property mode set to 100644
File size: 2.8 KB
Line 
1#include <actor.hfa>
2#include <fstream.hfa>
3#include <stdlib.hfa>
4#include <string.h>
5#include <stdio.h>
6
7int xr = 500, xc = 500, yc = 500, Processors = 1; // default values, must be signed
8
9struct derived_actor { inline actor; };
10
11struct derived_msg {
12        inline message;
13        int * Z;
14        int * X;
15        int ** Y;
16};
17
18void ?{}( derived_msg & this ) {}
19void ?{}( 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
26allocation 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
36int 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        unsigned int r, c;
70        int * Z[xr], * X[xr], * Y[xc];
71
72        for ( r = 0; r < xr; r += 1 ) {                                         // create/initialize X matrix
73                X[r] = aalloc( xc );
74                for ( c = 0; c < xc; c += 1 ) {
75                        X[r][c] = r * c % 37;                                           // for timing
76                } // for
77        } // for
78        for ( r = 0; r < xc; r += 1 ) {                                         // create/initialize Y matrix
79                Y[r] = aalloc( yc );
80                for ( c = 0; c < yc; c += 1 ) {
81                        Y[r][c] = r * c % 37;                                           // for timing
82                } // for
83        } // for
84        for ( r = 0; r < xr; r += 1 ) {                                         // create Z matrix
85                Z[r] = aalloc( yc );
86        } // for
87
88        executor e{ Processors, Processors, Processors == 1 ? 1 : Processors * 16, true };
89
90        sout | "starting";
91
92        start_actor_system( e );
93
94        sout | "started";
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        sout | "stopping";
109
110        stop_actor_system();
111
112        sout | "stopped";
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}
Note: See TracBrowser for help on using the repository browser.