Add hello load balancing example with GreedyRefineLB
[charm.git] / examples / charm++ / load_balancing / hello / hello.C
blob435692f362b829ceba29246773f7b534d002d85b
1 #include "hello.decl.h"
3 /* readonly */ CProxy_Main mainProxy;
4 /* readonly */ CProxy_Hello helloProxy;
6 class Main : public CBase_Main {
7   public:
8     Main(CkArgMsg* m) {
9       delete m;
11       // 2 chares per PE
12       int n = 2 * CkNumPes();
14       helloProxy = CProxy_Hello::ckNew(n);
15       helloProxy.work();
16     }
18     void done() {
19       CkExit();
20     }
23 class Hello : public CBase_Hello {
24   int pe;
26   public:
27     Hello() {
28       usesAtSync = true;
29       pe = CkMyPe();
30       CkPrintf("Hello, I'm chare %d on PE %d\n", thisIndex, pe);
31     }
33     Hello(CkMigrateMessage* m) { }
35     void pup(PUP::er &p) {
36       p|pe;
37     }
39     void work() {
40       // For chares on latter half of the PEs, introduce artificial load
41       // so that they can be migrated to the lower half
42       bool heavy = (CkMyPe() >= (CkNumPes() / 2));
43       double start_time = CkWallTimer();
44       if (heavy) {
45         // Busy wait for one second
46         while (CkWallTimer() - start_time < 1) { }
47       }
49       // Informs the runtime system that the chare is ready to migrate
50       AtSync();
51     }
53     void ResumeFromSync() {
54       if (CkMyPe() != pe) {
55         CkPrintf("I'm chare %d, I moved to PE %d from PE %d\n", thisIndex, CkMyPe(), pe);
56       }
57       else {
58         CkPrintf("I'm chare %d, I'm staying on PE %d\n", thisIndex, pe);
59       }
61       CkCallback cb(CkReductionTarget(Main, done), mainProxy);
62       contribute(cb);
63     }
66 #include "hello.def.h"