Contents of sampleReduction.html

//sample program to illustrate writing custom reductions
//implements one to take the minimum (same as built-in operator "min")

use Random;  //to get random numbers

class MyMin : ReduceScanOp {
  //custom reduction operator; inherits from operator base class
  //object of MyMin class is a "tally", holding state of computation
  //finds min element (equiv. to built-in "min")

  type eltType;				//type of elements

  var soFar : eltType = max(eltType);
  //attribute storing the minimum value so far
  //attribute initialize replaces constructor (role of "init")
  //this initialization creates minimum of empty array (i.e. largest value)

  proc accumulate(val : eltType) {
    //adds val to the tally; new value should be min{old value, val}
    if(val < soFar) { soFar = val; }
  }
  
  proc combine(other : MyMin) {
    //update this tally with the other one
    if(other.soFar < soFar) { soFar = other.soFar; }
  }

  proc generate() {
    //"reduce-gen"; returns value that corresponds to the tally
    return soFar;
  }
} 

//code to run this reduction
//first, we create and print an array of random real numbers
var array : [0..9] real;
fillRandom(array);
writeln("Array: ", array);

//now perform the reduction and print the result
var result = MyMin reduce array;
writeln("Min value: ", result);