Test entry method templates: register EPs and reducers in initnode, not initproc...
[charm.git] / tests / charm++ / method_templates / pgm.C
1 #include "client.decl.h"
2 #include "mylib.h"
3 #define CK_TEMPLATES_ONLY
4 #include "mylib.def.h"
5 #undef CK_TEMPLATES_ONLY
6
7 #include "utils.h"
8 #include <sstream>
9 #include <functional>
10 #include <iterator>
11
12 typedef count< std::less<libdtype> > cntType;
13 // Temporary initproc to register the instantiated EPs
14 void register_instantiations()
15 {
16     CkReductionTarget(pgm, acceptResults<avg>);
17     CkReductionTarget(pgm, acceptResults< cntType >);
18 };
19
20
21 // reducer type definitions
22 CkReduction::reducerType countReducer;
23 CkReduction::reducerType avgReducer;
24
25 // Register reducer functions
26 void register_reducers()
27 {
28     countReducer = CkReduction::addReducer(cntType::reduce_count);
29     avgReducer   = CkReduction::addReducer(avg::reduce_avg);
30 }
31
32
33 // Test driver
34 class pgm : public CBase_pgm
35 {
36     public:
37         pgm(CkArgMsg *m): nElements(2 * CkNumPes()), nDatumsPerChare(1000), nDone(0)
38         {
39             CkPrintf("[main] Creating a library chare array with %d chares and %d datums per chare\n",
40                     nElements, nDatumsPerChare);
41             // Create the library chare array
42             arrProxy = CProxy_libArray::ckNew(nDatumsPerChare, nElements);
43             thisProxy.startTest();
44             delete m;
45         }
46         
47         void startTest() {
48             // Run the tests
49             // Setup a redn cb and start the parallel sum computation of sum and avg
50             CkCallback avgCB(CkReductionTarget(pgm, acceptResults<avg>), thisProxy);
51             arrProxy.doSomething(avg(), avgReducer, avgCB);
52
53             // Setup a redn cb and start the parallel count of num elements less than given threshold
54             CkCallback cntCB(CkReductionTarget(pgm, acceptResults< cntType >), thisProxy);
55             arrProxy.doSomething( cntType(0.5), countReducer, cntCB );
56         }
57
58         template <typename T>
59         void acceptResults(T op) {
60             std::ostringstream out;
61             out << "[main] Applied operation to all data in library chare array in parallel:\n"
62                 << op
63                 << "\n";
64             CkPrintf("%s", out.str().c_str());
65             if (++nDone == 2)
66                 CkExit();
67         }
68
69     private:
70         CProxy_libArray arrProxy;
71         int nElements, nDone, nDatumsPerChare;
72 };
73
74 #define CK_TEMPLATES_ONLY
75 #include "client.def.h"
76 #undef CK_TEMPLATES_ONLY
77 #include "client.def.h"
78