main
test-branch
| Line | |
|---|
| 1 | /** This implements an algorithm for finding the max element of an array
|
|---|
| 2 | * in parallel. It has a defect, because there is data race.
|
|---|
| 3 | */
|
|---|
| 4 | #include<civlc.cvh>
|
|---|
| 5 |
|
|---|
| 6 | $input int BLOCK_SIZE;
|
|---|
| 7 | $input int NPROCS;
|
|---|
| 8 |
|
|---|
| 9 | int getMax(int* array, int length){
|
|---|
| 10 | int myMax = 0;
|
|---|
| 11 | $proc procs[NPROCS];
|
|---|
| 12 |
|
|---|
| 13 | void max_worker(int id){
|
|---|
| 14 | for(int i = 0; i < BLOCK_SIZE; i++){
|
|---|
| 15 | int currentIndex = BLOCK_SIZE * id + i;
|
|---|
| 16 |
|
|---|
| 17 | if(currentIndex >= length)
|
|---|
| 18 | break;
|
|---|
| 19 | if(myMax < array[currentIndex])
|
|---|
| 20 | myMax = array[currentIndex];
|
|---|
| 21 | }
|
|---|
| 22 | }
|
|---|
| 23 | for(int i = 0; i < NPROCS; i++){
|
|---|
| 24 | procs[i] = $spawn max_worker(i);
|
|---|
| 25 | }
|
|---|
| 26 | for(int i = 0; i < NPROCS; i++){
|
|---|
| 27 | $wait(procs[i]);
|
|---|
| 28 | }
|
|---|
| 29 | return myMax;
|
|---|
| 30 | }
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.