Example : Collect.C

Classes/Techniques Demonstrated :

Collect.C

This example demonstrates the HPCxx_Collective class. Specificly, it performs a concatenation of a group of arrays into one single large array so that the threads may perform the important action of summing up all of the values in the array. This example is similar to both the Reduct and the Barrier examples, except that each array starts out only with its own small array and inherits from the gather operation a pointer to the big array which is the concatenation of all of the arrays from all of the threads.

class MyThread : public HPCxx_Thread {
  HPCxx_Collective<int> &coll;
  int *infoAr, *newAr;
  int totalThreads, myId, count, myCollectKey;
  
public:
  MyThread(int n, int id, HPCxx_Collective<int> &_coll):
    totalThreads(n), myId(id), coll(_coll), HPCxx_Thread()
  {
      myCollectKey=coll.acquireKey();
  }
  
  void run() {
    int size=10;
    int total=0;
    infoAr=new int[10];
    
    srand48((int)infoAr);
    cout << "Thread " << myId << " generating array values..." << endl;
 
    for(count=0; count<10; count++) {
      infoAr[count]=(int)(1000*drand48());
    }
    
    cout << "Thread " << myId << " passing to collection..." << endl;

    newAr=coll.gather(myCollectKey, infoAr, size);

    for(count=0; count<totalThreads*10+1; count++) {
      if(myId==0) {
	cout << "newAr[" << count << "] = " << newAr[count] << endl;
      }
      total+=newAr[count];
    }
    
    cout << "Thread " << myId << "computed sum " << total << "  Size returned was " << size << endl;
  } 
};

int foo(int n, HPCxx_Barrier barrier, HPCxx_Collective<int> coll){
  MyThread *t[100];
  int total=0, count=0, collectKey0;
  int dummy=0;
  int size=1, *infoAr;
  
  for(int i = 0; i < n; i++){
    t[i] = new MyThread(n, i, coll);
    t[i]->start();
  }  

  cout << "DONE" << endl << flush;
  return 0;
}


int main(int argc, char **argv)
{
  HPCxx_Group *g;
  int n;

  hpcxx_init(argc, argv, g);

  cout << "INPUT" << endl << flush;
  cout << "Number of threads : " << endl << flush;
  cin >> n;
  g->setNumThreads(n);
  HPCxx_Barrier barrier(*g);
  HPCxx_Collective<int> coll(g);
  foo(n, barrier, coll);
  return hpcxx_exit(g);
}
      

hpc++@extreme.indiana.edu

Last modified: Thu Apr 22 02:11:12 EST 1999