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