source: tests/concurrency/examples/matrixSum.cfa

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

clean up command-line handling and I/O

  • Property mode set to 100644
File size: 1.5 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2017 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// matrixSum.cfa --
8//
9// Author           : Peter A. Buhr
10// Created On       : Mon Oct  9 08:29:28 2017
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Fri Sep  8 19:05:34 2023
13// Update Count     : 19
14//
15
16#include <fstream.hfa>
17#include <thread.hfa>
18
19thread Adder {
20        int * row, cols, & subtotal;                                            // communication
21};
22
23void ?{}( Adder & adder, int row[], int cols, int & subtotal ) {
24        adder.[ row, cols ] = [ row, cols ];                            // expression disallowed in multi-member access
25        &adder.subtotal = &subtotal;
26}
27
28void main( Adder & adder ) with( adder ) {                              // thread starts here
29        subtotal = 0;
30        for ( c; cols ) {
31                subtotal += row[c];
32        } // for
33}
34
35int main() {
36        const int rows = 10, cols = 1000;
37        int matrix[rows][cols], subtotals[rows], total = 0;
38        processor p;                                                                            // add kernel thread
39
40        for ( r; rows ) {                                                                       // initialize
41                for ( c; cols ) {
42                        matrix[r][c] = 1;
43                } // for
44        } // for
45
46        Adder * adders[rows];
47        for ( r; rows ) {                                                                       // start threads to sum rows
48                adders[r] = &(*malloc()){ matrix[r], cols, subtotals[r] };
49                // adders[r] = new( matrix[r], cols, subtotals[r] );
50        } // for
51
52        for ( r; rows ) {                                                                       // wait for threads to finish
53                delete( adders[r] );
54                total += subtotals[r];                                                  // total subtotals
55        } // for
56        sout | total;
57}
58
59// Local Variables: //
60// compile-command: "cfa matrixSum.cfa" //
61// End: //
Note: See TracBrowser for help on using the repository browser.