Introducing Reductions

Reductions

By now, we've spent a lot of time in this tutorial talking about how to decompose problems into chares and distribute them across processors. That's great, but what good is splitting up your problem if you can't put it back together? When you need to combine values from an array of chares, you can turn to one of the most important parallel programming tools: a reduction.

Reductions turn data that is scattered across a chare array into a single value using a reduction operation, such as sum or maximum. Each of the chares in the array contribute some local data

"Hello World" Code

The "Hello" Chare Class

Header File (hello.h)
#ifndef __HELLO_H__
#define __HELLO_H__

class Hello : public CBase_Hello {

 public:

  /// Constructors ///
  Hello();
  Hello(CkMigrateMessage *msg);

  /// Entry Methods ///
  void sayHi(int from);
};

#endif //__HELLO_H__
Interface File (hello.ci)
module hello {

  array [1D] Hello {
    entry Hello();
    entry void sayHi(int);
  };

};
Header File (hello.C)
#include "hello.decl.h"

#include "hello.h"
#include "main.decl.h"


extern /* readonly */ CProxy_Main mainProxy;
extern /* readonly */ int numElements;


Hello::Hello() {
  // Nothing to do when the Hello chare object is created.
  // This is where member variables would be initialized
  // just like in a C++ class constructor.
}


// Constructor needed for chare object migration (ignore for now)
// NOTE: This constructor does not need to appear in the ".ci" file
Hello::Hello(CkMigrateMessage *msg) { }


void Hello ::sayHi(int from) {

  // Have this chare object say hello to the user.
  CkPrintf("\"Hello\" from Hello chare # %d on "
           "processor %d (told by %d).\n",
           thisIndex, CkMyPe(), from);

  // Tell the next chare object in this array of chare objects
  // to also say hello. If this is the last chare object in
  // the array of chare objects, then tell the main chare
  // object to exit the program.
  if (thisIndex < (numElements - 1))
    thisProxy[thisIndex + 1].sayHi(thisIndex);   else
    mainProxy.done(); }


#include "hello.def.h"