source: tests/concurrency/actors/matrixMultiply.cfa

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

formatting

  • Property mode set to 100644
File size: 2.6 KB
Line 
1#include <actor.hfa>
2#include <fstream.hfa>
3#include <stdlib.hfa>
4#include <string.h>
5#include <stdio.h>
6
7ssize_t 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 ( i; yc ) {                                                                         // multiply X_row by Y_col and sum products
28                msg.Z[i] = 0;
29                for ( j; xc ) {
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        int * Z[xr], * X[xr], * Y[xc];
70
71        for ( r; xr ) {                                                                         // create/initialize X matrix
72                X[r] = aalloc( xc );
73                for ( c; xc ) {
74                        X[r][c] = r * c % 37;                                           // for timing
75                } // for
76        } // for
77        for ( r; xc ) {                                                                         // create/initialize Y matrix
78                Y[r] = aalloc( yc );
79                for ( c; yc ) {
80                        Y[r][c] = r * c % 37;                                           // for timing
81                } // for
82        } // for
83        for ( r; xr ) {                                                                         // create Z matrix
84                Z[r] = aalloc( yc );
85        } // for
86
87        executor e{ Processors, Processors, Processors == 1 ? 1 : Processors * 16, true };
88
89        sout | "starting";
90
91        start_actor_system( e );
92
93        sout | "started";
94
95        derived_msg messages[xr];
96
97        derived_actor actors[xr];
98
99        for ( r; xr ) {
100                messages[r]{ Z[r], X[r], Y };
101        } // for
102
103        for ( r; xr ) {
104                actors[r] | messages[r];
105        } // for
106
107        sout | "stopping";
108
109        stop_actor_system();
110
111        sout | "stopped";
112
113        for ( r; xr ) {                                                                         // deallocate X and Z matrices
114                free( X[r] );
115                free( Z[r] );
116        } // for
117        for ( r; xc ) {                                                                         // deallocate Y matrix
118                free( Y[r] );
119        } // for
120}
Note: See TracBrowser for help on using the repository browser.