examples: add example program demonstrating sync entry methods
[charm.git] / examples / charm++ / AllReduce / AllReduce.h
1 //! main char object.  Creates array, handles command line, catches reduction.
2 #include <ctime>
3
4 class main : public CBase_main
5 {
6 private:
7   CProxy_AllReduce arr;
8         double startTime;
9 public:
10
11   main(CkArgMsg* m);
12
13   void done()
14   {
15           static int checkIn=0;
16           checkIn++;
17           if (checkIn==units) {
18                   CkPrintf("All done in %lf seconds\n",(CkWallTimer()-startTime));
19                   CkExit();  
20           }
21   }
22
23 };
24
25
26 //! AllReduce. A small example which does nothing useful, but provides an extremely simple, yet complete, use of a normal reduction case.
27 /** The reduction performs a summation i+k1 i+k2 for i=0 to units
28  */
29 class AllReduce : public CBase_AllReduce
30 {
31  private:
32         CProxy_main mainProxy;
33         double* myData;
34  public:
35
36         AllReduce(CProxy_main ma)   { mainProxy=ma;  }
37
38   AllReduce(CkMigrateMessage *m) {};
39
40         void init()
41         {
42                                 myData = new double[allredSize/(sizeof(double))];
43                                                 for (int i=0; i<allredSize/sizeof(double); i++) {
44                                                                                 myData[i] = (double)i;
45                                                                                                 }
46         }
47
48   //! add our index to the global floats and contribute the result.
49   void dowork ()
50     {
51         CkCallback cb(CkIndex_AllReduce::report(NULL),  thisProxy);
52       contribute(allredSize,myData,CkReduction::sum_double,cb); 
53     }
54   //! catches the completed reduction results.
55   void report(CkReductionMsg *msg)
56   {
57           int reducedArrSize=msg->getSize()/sizeof(double);
58           double *output=(double *)msg->getData();
59           CkPrintf("done %d\n", thisIndex);
60           for(int i=0;i<reducedArrSize;i++) {
61                   if (output[i]!=i*units) {
62                           CkPrintf("result error!\n");
63                   }
64           }
65           delete msg;
66           mainProxy.done();
67   }
68
69 };
70