/** This implements an algorithm for finding the max element of an array
 *  in parallel. It has a defect, because there is data race.
 */
#include<civlc.cvh>

$input int BLOCK_SIZE;
$input int NPROCS;

int getMax(int* array, int length){
  int myMax = 0;
  $proc procs[NPROCS];

  void max_worker(int id){
    for(int i = 0; i < BLOCK_SIZE; i++){
      int currentIndex = BLOCK_SIZE * id + i;

      if(currentIndex >= length)
        break;
      if(myMax < array[currentIndex])
        myMax = array[currentIndex];
    }
  }
  for(int i = 0; i < NPROCS; i++){
    procs[i] = $spawn max_worker(i);
  }
  for(int i = 0; i < NPROCS; i++){
    $wait(procs[i]);
  }
  return myMax;
}
