| [73abe95] | 1 | //
|
|---|
| [d6655bd] | 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.
|
|---|
| [73abe95] | 6 | //
|
|---|
| [dc8511c] | 7 | // matrixSum.cfa --
|
|---|
| [73abe95] | 8 | //
|
|---|
| [d6655bd] | 9 | // Author : Peter A. Buhr
|
|---|
| 10 | // Created On : Mon Oct 9 08:29:28 2017
|
|---|
| 11 | // Last Modified By : Peter A. Buhr
|
|---|
| [50be8af5] | 12 | // Last Modified On : Fri Sep 8 19:05:34 2023
|
|---|
| 13 | // Update Count : 19
|
|---|
| [73abe95] | 14 | //
|
|---|
| [d6655bd] | 15 |
|
|---|
| [73abe95] | 16 | #include <fstream.hfa>
|
|---|
| 17 | #include <thread.hfa>
|
|---|
| [b8a17e2] | 18 |
|
|---|
| 19 | thread Adder {
|
|---|
| [c107f4ec] | 20 | int * row, cols, & subtotal; // communication
|
|---|
| [b8a17e2] | 21 | };
|
|---|
| 22 |
|
|---|
| 23 | void ?{}( Adder & adder, int row[], int cols, int & subtotal ) {
|
|---|
| [c107f4ec] | 24 | adder.[ row, cols ] = [ row, cols ]; // expression disallowed in multi-member access
|
|---|
| 25 | &adder.subtotal = &subtotal;
|
|---|
| [b8a17e2] | 26 | }
|
|---|
| 27 |
|
|---|
| [c107f4ec] | 28 | void main( Adder & adder ) with( adder ) { // thread starts here
|
|---|
| 29 | subtotal = 0;
|
|---|
| [8f34661] | 30 | for ( c; cols ) {
|
|---|
| [c107f4ec] | 31 | subtotal += row[c];
|
|---|
| 32 | } // for
|
|---|
| [b8a17e2] | 33 | }
|
|---|
| 34 |
|
|---|
| 35 | int main() {
|
|---|
| [50be8af5] | 36 | const int rows = 10, cols = 1000;
|
|---|
| [c107f4ec] | 37 | int matrix[rows][cols], subtotals[rows], total = 0;
|
|---|
| 38 | processor p; // add kernel thread
|
|---|
| [b8a17e2] | 39 |
|
|---|
| [50be8af5] | 40 | for ( r; rows ) { // initialize
|
|---|
| [8f34661] | 41 | for ( c; cols ) {
|
|---|
| [9f865d1] | 42 | matrix[r][c] = 1;
|
|---|
| 43 | } // for
|
|---|
| [c107f4ec] | 44 | } // for
|
|---|
| [50be8af5] | 45 |
|
|---|
| [c107f4ec] | 46 | Adder * adders[rows];
|
|---|
| [8f34661] | 47 | for ( r; rows ) { // start threads to sum rows
|
|---|
| [9f865d1] | 48 | adders[r] = &(*malloc()){ matrix[r], cols, subtotals[r] };
|
|---|
| [50be8af5] | 49 | // adders[r] = new( matrix[r], cols, subtotals[r] );
|
|---|
| [c107f4ec] | 50 | } // for
|
|---|
| [50be8af5] | 51 |
|
|---|
| [8f34661] | 52 | for ( r; rows ) { // wait for threads to finish
|
|---|
| [9f865d1] | 53 | delete( adders[r] );
|
|---|
| 54 | total += subtotals[r]; // total subtotals
|
|---|
| [c107f4ec] | 55 | } // for
|
|---|
| [200fcb3] | 56 | sout | total;
|
|---|
| [b8a17e2] | 57 | }
|
|---|
| [d6655bd] | 58 |
|
|---|
| 59 | // Local Variables: //
|
|---|
| [dc8511c] | 60 | // compile-command: "cfa matrixSum.cfa" //
|
|---|
| [d6655bd] | 61 | // End: //
|
|---|