charmxi: Add EP index lookup function via entry method function pointer
[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     // Useless temporary variables just to feed the CkIndex functions
17     avg avger;
18     CkReduction::reducerType foo;
19     CkCallback bar;
20
21     CkIndex_libArray::idx_doSomething< cntType > (
22         static_cast<void (libArray::*) (const cntType&, const CkReduction::reducerType&, const CkCallback&)>(NULL));
23     CkIndex_libArray::doSomething(avger, foo, bar);
24     CkReductionTarget(pgm, acceptResults<avg>);
25     CkReductionTarget(pgm, acceptResults< cntType >);
26 };
27
28
29 // reducer type definitions
30 CkReduction::reducerType countReducer;
31 CkReduction::reducerType avgReducer;
32
33 // Register reducer functions
34 void register_reducers()
35 {
36     countReducer = CkReduction::addReducer(cntType::reduce_count);
37     avgReducer   = CkReduction::addReducer(avg::reduce_avg);
38 }
39
40
41 // Test driver
42 class pgm : public CBase_pgm
43 {
44     public:
45         pgm(CkArgMsg *m): nElements(2 * CkNumPes()), nDatumsPerChare(1000), nDone(0)
46         {
47             CkPrintf("[main] Creating a library chare array with %d chares and %d datums per chare\n",
48                     nElements, nDatumsPerChare);
49             // Create the library chare array
50             arrProxy = CProxy_libArray::ckNew(nDatumsPerChare, nElements);
51             thisProxy.startTest();
52             delete m;
53         }
54         
55         void startTest() {
56             // Run the tests
57             // Setup a redn cb and start the parallel sum computation of sum and avg
58             CkCallback avgCB(CkReductionTarget(pgm, acceptResults<avg>), thisProxy);
59             arrProxy.doSomething(avg(), avgReducer, avgCB);
60
61             // Setup a redn cb and start the parallel count of num elements less than given threshold
62             CkCallback cntCB(CkReductionTarget(pgm, acceptResults< cntType >), thisProxy);
63             arrProxy.doSomething( cntType(0.5), countReducer, cntCB );
64         }
65
66         template <typename T>
67         void acceptResults(T op) {
68             std::ostringstream out;
69             out << "[main] Applied operation to all data in library chare array in parallel:\n"
70                 << op
71                 << "\n";
72             CkPrintf("%s", out.str().c_str());
73             if (++nDone == 2)
74                 CkExit();
75         }
76
77     private:
78         CProxy_libArray arrProxy;
79         int nElements, nDone, nDatumsPerChare;
80 };
81
82 #define CK_TEMPLATES_ONLY
83 #include "client.def.h"
84 #undef CK_TEMPLATES_ONLY
85 #include "client.def.h"
86