build: fix travis MPI/SMP build
[charm.git] / examples / charm++ / arraysection / check2d.C
blob27465a76bc657428e2be5e0dab48fb241ca91c02
1 #include <math.h>
2 #include "check2d.decl.h"
3 #include "ckmulticast.h"
5 /* readonly */ CProxy_Main mainProxy;
6 /* readonly */ CProxy_Check checkArray;
7 /* readonly */ int numchares_x;
8 /* readonly */ int numchares_y;
10 struct sectionBcastMsg : public CkMcastBaseMsg, public CMessage_sectionBcastMsg {
11         int k;
12         sectionBcastMsg(int _k) : k(_k) {}
13         void pup(PUP::er &p) {
14                 CMessage_sectionBcastMsg::pup(p);
15                 p|k;
16         }
19 class Main : public CBase_Main {
20         int sum;
22 public:
23         Main(CkArgMsg* msg) {
24                 if (msg->argc < 3) {
25                         ckout << "Usage: " << msg->argv[0] << " [number of x_chares] [number of y_chares]" << endl;
26                         CkExit(1);
27                 }
28                 numchares_x = atoi(msg->argv[1]);
29                 numchares_y = atoi(msg->argv[2]);
30                 ckout << "Numchares_x: " << numchares_x << " Numchares_y: " << numchares_y << endl;
31                 sum = 0;
32                 mainProxy = thisProxy;
33                 checkArray = CProxy_Check::ckNew(numchares_x, numchares_y);
34                 checkArray.createSection();
35         }
37         void done1It(int q, int output[]) {
38                 CkAssert(q == 2);
39                 int expected[2];
40                 int n_x = ((numchares_x - 1) % 2 == 0) ? (numchares_x - 1) : (numchares_x - 2);
41                 int n_y = ((numchares_y - 1) % 2 == 0) ? (numchares_y - 1) : (numchares_y - 2);
42                 int sumofIndices_x = n_x * (n_x + 2) / 4;  // even numbered chares are part of the section
43                 int sumofIndices_y = n_y * (n_y + 2) / 4;
44                 expected[0] = sumofIndices_x * ceil(numchares_y / 2.0);
45                 expected[1] = sumofIndices_y * ceil(numchares_x / 2.0);
46                 CkAssert((output[0] - expected[0]) == 0);
47                 CkAssert((output[1] - expected[1]) == 0);
48                 ckout << "Vector reduction: " << output[0] << " " << output[1] << endl;
49                 ckout << "Test passed successfully" << endl;
50                 CkExit();
51         }
54 class Check : public CBase_Check {
55         CProxySection_Check secProxy;
57 public:
58         Check() {}
60         void createSection() {
61                 if (thisIndex.x == 0 && thisIndex.y == 0) {
62                         // Use index range to create a section [lbound:ubound:stride]
63                         secProxy = CProxySection_Check::ckNew(checkArray.ckGetArrayID(), 0, numchares_x - 1, 2, 0, numchares_y - 1, 2);
64                         sectionBcastMsg *msg = new sectionBcastMsg(0);
65                         secProxy.recvMsg(msg);
66                 }
67         }
69         void recvMsg(sectionBcastMsg *msg) {
70                 ckout << "ArrayIndex: [" << thisIndex.x << ", " << thisIndex.y << "] - " << CkMyPe() << endl;
71                 int k = msg->k;
72                 std::vector<int> outVals(2);
73                 outVals[0] = k + thisIndex.x;
74                 outVals[1] = k + thisIndex.y;
75                 CkSectionInfo cookie;
76                 CkGetSectionInfo(cookie, msg);
77                 CkCallback cb(CkReductionTarget(Main, done1It), mainProxy);
78                 CProxySection_Check::contribute(outVals, CkReduction::sum_int, cookie, cb);
79                 delete msg;
80         }
83 #include "check2d.def.h"