source: src/tests/matrixSum.c @ b8a17e2

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since b8a17e2 was b8a17e2, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

add concurrency test to concurrently add rows of matrix

  • Property mode set to 100644
File size: 1.1 KB
Line 
1#include <fstream>
2#include <kernel>
3#include <thread>
4
5thread Adder {
6    int * row, cols, * subtotal;                        // communication
7};
8
9void ?{}( Adder & adder, int row[], int cols, int & subtotal ) {
10    adder.row = row;
11    adder.cols = cols;
12    adder.subtotal = &subtotal;
13}
14
15void main( Adder & adder ) {
16    *adder.subtotal = 0;
17    for ( int c = 0; c < adder.cols; c += 1 ) {
18        *adder.subtotal += adder.row[c];
19    } // for
20}
21
22int main() {
23    const int rows = 10, cols = 1000;
24    int matrix[rows][cols], subtotals[rows], total = 0;
25    processor p;                                        // extra kernel thread
26
27    for ( int r = 0; r < rows; r += 1 ) {
28        for ( int c = 0; c < cols; c += 1 ) {
29            matrix[r][c] = 1;
30        } // for
31    } // for
32    Adder * adders[rows];
33    for ( int r = 0; r < rows; r += 1 ) {               // start threads to sum rows
34        adders[r] = &(*malloc()){ matrix[r], cols, subtotals[r] };
35//      adders[r] = new( matrix[r], cols, &subtotals[r] );
36    } // for
37    for ( int r = 0; r < rows; r += 1 ) {               // wait for threads to finish
38        delete( adders[r] );
39        total += subtotals[r];                          // total subtotals
40    } // for
41    sout | total | endl;
42}
Note: See TracBrowser for help on using the repository browser.