source: tests/concurrency/actors/matrixMultiply.cfa

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

formatting

  • Property mode set to 100644
File size: 2.6 KB
RevLine 
[418882a]1#include <actor.hfa>
2#include <fstream.hfa>
3#include <stdlib.hfa>
4#include <string.h>
5#include <stdio.h>
6
[b244fa8]7ssize_t xr = 500, xc = 500, yc = 500, Processors = 1; // default values, must be signed
[418882a]8
[4933f18]9struct derived_actor { inline actor; };
[418882a]10
11struct derived_msg {
[7edf912]12        inline message;
13        int * Z;
[418882a]14        int * X;
[7edf912]15        int ** Y;
[418882a]16};
17
18void ?{}( derived_msg & this ) {}
19void ?{}( derived_msg & this, int * Z, int * X, int ** Y ) {
[7edf912]20        set_allocation( this, Nodelete );
21        this.Z = Z;
22        this.X = X;
23        this.Y = Y;
[418882a]24}
25
[c880a7b]26allocation receive( derived_actor & receiver, derived_msg & msg ) {
[b244fa8]27        for ( i; yc ) {                                                                         // multiply X_row by Y_col and sum products
[7edf912]28                msg.Z[i] = 0;
[b244fa8]29                for ( j; xc ) {
[7edf912]30                        msg.Z[i] += msg.X[j] * msg.Y[j][i];
31                } // for
32        } // for
33        return Finished;
[418882a]34}
35
36int main( int argc, char * argv[] ) {
[7edf912]37        switch ( argc ) {
[418882a]38          case 5:
39                if ( strcmp( argv[4], "d" ) != 0 ) {                    // default ?
[50be8af]40                        Processors = ato( argv[4] );
41                        if ( Processors < 1 ) fallthru default;
[418882a]42                } // if
43          case 4:
44                if ( strcmp( argv[3], "d" ) != 0 ) {                    // default ?
[50be8af]45                        xr = ato( argv[3] );
46                        if ( xr < 1 ) fallthru default;
[418882a]47                } // if
48          case 3:
49                if ( strcmp( argv[2], "d" ) != 0 ) {                    // default ?
[50be8af]50                        xc = ato( argv[2] );
51                        if ( xc < 1 ) fallthru default;
[418882a]52                } // if
53          case 2:
54                if ( strcmp( argv[1], "d" ) != 0 ) {                    // default ?
[50be8af]55                        yc = ato( argv[1] );
56                        if ( yc < 1 ) fallthru default;
[418882a]57                } // if
58          case 1:                                                                                       // use defaults
59                break;
60          default:
[50be8af]61                exit | "Usage: " | argv[0]
[418882a]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
[b244fa8]71        for ( r; xr ) {                                                                         // create/initialize X matrix
[418882a]72                X[r] = aalloc( xc );
[b244fa8]73                for ( c; xc ) {
[418882a]74                        X[r][c] = r * c % 37;                                           // for timing
75                } // for
76        } // for
[b244fa8]77        for ( r; xc ) {                                                                         // create/initialize Y matrix
[418882a]78                Y[r] = aalloc( yc );
[b244fa8]79                for ( c; yc ) {
[418882a]80                        Y[r][c] = r * c % 37;                                           // for timing
81                } // for
82        } // for
[b244fa8]83        for ( r; xr ) {                                                                         // create Z matrix
[418882a]84                Z[r] = aalloc( yc );
85        } // for
86
[7edf912]87        executor e{ Processors, Processors, Processors == 1 ? 1 : Processors * 16, true };
[418882a]88
[7edf912]89        sout | "starting";
[418882a]90
[7edf912]91        start_actor_system( e );
[418882a]92
[7edf912]93        sout | "started";
[418882a]94
[7edf912]95        derived_msg messages[xr];
[418882a]96
[7edf912]97        derived_actor actors[xr];
[418882a]98
[b244fa8]99        for ( r; xr ) {
[418882a]100                messages[r]{ Z[r], X[r], Y };
101        } // for
102
[b244fa8]103        for ( r; xr ) {
[77fd9fe2]104                actors[r] | messages[r];
[418882a]105        } // for
106
[7edf912]107        sout | "stopping";
[418882a]108
[7edf912]109        stop_actor_system();
[418882a]110
[7edf912]111        sout | "stopped";
[418882a]112
[b244fa8]113        for ( r; xr ) {                                                                         // deallocate X and Z matrices
[418882a]114                free( X[r] );
[7edf912]115                free( Z[r] );
[418882a]116        } // for
[b244fa8]117        for ( r; xc ) {                                                                         // deallocate Y matrix
[7edf912]118                free( Y[r] );
[418882a]119        } // for
[50be8af]120}
Note: See TracBrowser for help on using the repository browser.