main
|
Last change
on this file was ea777aa, checked in by Alex Wilton <awilton@…>, 3 years ago |
|
Moved examples, include, build_default.properties, common.xml, and README out from dev.civl.com into the root of the repo.
git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@5704 fb995dde-84ed-4084-dfe6-e5aef3e2452c
|
-
Property mode
set to
100644
|
|
File size:
682 bytes
|
| 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.