examples/charm++: Always use CkWallTimer, not CmiWallTimer (tested)
[charm.git] / examples / charm++ / cell / md / pairCompute.C
1 #include "pairCompute.h"
2 #include "patch.h"
3 #include "main.h"
4
5
6 PairCompute::PairCompute() {
7   particleX[0] = particleX[1] = NULL;
8   particleY[0] = particleY[1] = NULL;
9   particleZ[0] = particleZ[1] = NULL;
10   particleQ[0] = particleQ[1] = NULL;
11   forceX[0] = forceX[1] = NULL;
12   forceY[0] = forceY[1] = NULL;
13   forceZ[0] = forceZ[1] = NULL;
14   numParticles = -1;
15 }
16
17
18 PairCompute::PairCompute(CkMigrateMessage* msg) {
19   CkAbort("PairCompute::PairCompute(CkMigrateMessage* msg) not implemented yet");
20 }
21
22
23 PairCompute::~PairCompute() {
24
25   #if USE_PROXY_PATCHES == 0
26     if (particleX[0] != NULL) { CmiFreeAligned(particleX[0]); particleX[0] = NULL; }
27     if (particleX[1] != NULL) { CmiFreeAligned(particleX[1]); particleX[1] = NULL; }
28     if (particleY[0] != NULL) { CmiFreeAligned(particleY[0]); particleY[0] = NULL; }
29     if (particleY[1] != NULL) { CmiFreeAligned(particleY[1]); particleY[1] = NULL; }
30     if (particleZ[0] != NULL) { CmiFreeAligned(particleZ[0]); particleZ[0] = NULL; }
31     if (particleZ[1] != NULL) { CmiFreeAligned(particleZ[1]); particleZ[1] = NULL; }
32     if (particleQ[0] != NULL) { CmiFreeAligned(particleQ[0]); particleQ[0] = NULL; }
33     if (particleQ[1] != NULL) { CmiFreeAligned(particleQ[1]); particleQ[1] = NULL; }
34   #endif
35   if (forceX[0] != NULL) { CmiFreeAligned(forceX[0]); forceX[0] = NULL; }
36   if (forceX[1] != NULL) { CmiFreeAligned(forceX[1]); forceX[1] = NULL; }
37   if (forceY[0] != NULL) { CmiFreeAligned(forceY[0]); forceY[0] = NULL; }
38   if (forceY[1] != NULL) { CmiFreeAligned(forceY[1]); forceY[1] = NULL; }
39   if (forceZ[0] != NULL) { CmiFreeAligned(forceZ[0]); forceZ[0] = NULL; }
40   if (forceZ[1] != NULL) { CmiFreeAligned(forceZ[1]); forceZ[1] = NULL; }
41   numParticles = -1;
42 }
43
44
45 void PairCompute::init(int numParticlesPerPatch) {
46
47   // Initialize the arrays
48   numParticles = numParticlesPerPatch;
49   #if USE_PROXY_PATCHES == 0
50     particleX[0] = (MD_FLOAT*)(CmiMallocAligned(numParticles * sizeof(MD_FLOAT), 128));
51     particleX[1] = (MD_FLOAT*)(CmiMallocAligned(numParticles * sizeof(MD_FLOAT), 128));
52     particleY[0] = (MD_FLOAT*)(CmiMallocAligned(numParticles * sizeof(MD_FLOAT), 128));
53     particleY[1] = (MD_FLOAT*)(CmiMallocAligned(numParticles * sizeof(MD_FLOAT), 128));
54     particleZ[0] = (MD_FLOAT*)(CmiMallocAligned(numParticles * sizeof(MD_FLOAT), 128));
55     particleZ[1] = (MD_FLOAT*)(CmiMallocAligned(numParticles * sizeof(MD_FLOAT), 128));
56     particleQ[0] = (MD_FLOAT*)(CmiMallocAligned(numParticles * sizeof(MD_FLOAT), 128));
57     particleQ[1] = (MD_FLOAT*)(CmiMallocAligned(numParticles * sizeof(MD_FLOAT), 128));
58   #endif
59   forceX[0] = (MD_FLOAT*)(CmiMallocAligned(numParticles * sizeof(MD_FLOAT), 128));
60   forceX[1] = (MD_FLOAT*)(CmiMallocAligned(numParticles * sizeof(MD_FLOAT), 128));
61   forceY[0] = (MD_FLOAT*)(CmiMallocAligned(numParticles * sizeof(MD_FLOAT), 128));
62   forceY[1] = (MD_FLOAT*)(CmiMallocAligned(numParticles * sizeof(MD_FLOAT), 128));
63   forceZ[0] = (MD_FLOAT*)(CmiMallocAligned(numParticles * sizeof(MD_FLOAT), 128));
64   forceZ[1] = (MD_FLOAT*)(CmiMallocAligned(numParticles * sizeof(MD_FLOAT), 128));
65   patchDataCount = 0;
66
67   // Check in with the main chare
68   mainProxy.initCheckIn();
69 }
70
71
72 void PairCompute::patchData(int numParticles, MD_FLOAT* particleX, MD_FLOAT* particleY, MD_FLOAT* particleZ, MD_FLOAT* particleQ, int fromPatch, CProxy_ProxyPatch proxyPatchProxy) {
73   #if USE_PROXY_PATCHES != 0
74     this->proxyPatchProxy[fromPatch] = proxyPatchProxy;
75   #endif
76   patchData(numParticles, particleX, particleY, particleZ, particleQ, fromPatch);
77 }
78
79 void PairCompute::patchData(int numParticles, MD_FLOAT* particleX, MD_FLOAT* particleY, MD_FLOAT* particleZ, MD_FLOAT* particleQ, int fromPatch) {
80
81   // Copy the data from the parameters
82   #if USE_PROXY_PATCHES != 0
83     this->particleX[fromPatch] = particleX;
84     this->particleY[fromPatch] = particleY;
85     this->particleZ[fromPatch] = particleZ;
86     this->particleQ[fromPatch] = particleQ;
87   #else
88     memcpy(this->particleX[fromPatch], particleX, numParticles * sizeof(MD_FLOAT));
89     memcpy(this->particleY[fromPatch], particleY, numParticles * sizeof(MD_FLOAT));
90     memcpy(this->particleZ[fromPatch], particleZ, numParticles * sizeof(MD_FLOAT));
91     memcpy(this->particleQ[fromPatch], particleQ, numParticles * sizeof(MD_FLOAT));
92   #endif
93
94   // Increment the patch count and initiate the calculation of both patches have
95   //   sent their data to this compute
96   patchDataCount++;
97   if (patchDataCount >= 2) {
98
99     // DMK - DEBUG - Call doCalc function locally instead of sending a message
100     #if CMK_CELL != 0 && 0
101       CkIndex_PairCompute::_accelCall_spe_doCalc_void(this->numParticles,
102                                                       this->thisIndex.x, this->thisIndex.y,
103                                                       this->particleX[0], this->particleX[1],
104                                                       this->particleY[0], this->particleY[1],
105                                                       this->particleZ[0], this->particleZ[1],
106                                                       this->particleQ[0], this->particleQ[1],
107                                                       this->forceX[0], this->forceX[0],
108                                                       this->forceY[0], this->forceY[0],
109                                                       this->forceZ[0], this->forceZ[0],
110                                                       this->localFlopCount,
111                                                       this
112                                                      );
113     #else
114       thisProxy(thisIndex.x, thisIndex.y).doCalc();  // Send message to self to do calculation
115     #endif
116
117     patchDataCount = 0;
118   }
119 }
120
121
122 void PairCompute::doCalc_callback() {
123
124   // DMK - DEBUG
125   #if ENABLE_USER_EVENTS != 0
126     double __start_time__ = CkWallTimer();
127   #endif
128
129   // DMK - DEBUG
130   #if COUNT_FLOPS != 0
131     globalFlopCount += localFlopCount;
132   #endif
133
134   #if USE_PROXY_PATCHES != 0
135
136     (proxyPatchProxy[0].ckLocalBranch())->forceCheckIn(numParticles, forceX[0], forceY[0], forceZ[0]);
137     (proxyPatchProxy[1].ckLocalBranch())->forceCheckIn(numParticles, forceX[1], forceY[1], forceZ[1]);
138
139   #else
140
141     // DMK - DEBUG
142     NetworkProgress;
143
144     // Calculate the index of patch 0 and send force data back to it
145     int p0Index = thisIndex.x;
146     int p0IndexX = PATCH_I_TO_X(p0Index);
147     int p0IndexY = PATCH_I_TO_Y(p0Index);
148     int p0IndexZ = PATCH_I_TO_Z(p0Index);
149     patchArrayProxy(p0IndexX, p0IndexY, p0IndexZ).forceCheckIn(numParticles, forceX[0], forceY[0], forceZ[0]);
150
151     // DMK - DEBUG
152     NetworkProgress;
153
154     // Calculate the index of patch 1 and send force data back to it
155     int p1Index = thisIndex.y;
156     int p1IndexX = PATCH_I_TO_X(p1Index);
157     int p1IndexY = PATCH_I_TO_Y(p1Index);
158     int p1IndexZ = PATCH_I_TO_Z(p1Index);
159     patchArrayProxy(p1IndexX, p1IndexY, p1IndexZ).forceCheckIn(numParticles, forceX[1], forceY[1], forceZ[1]);
160
161     // DMK - DEBUG
162     NetworkProgress;
163
164   #endif
165
166   // DMK - DEBUG
167   #if ENABLE_USER_EVENTS != 0
168     double __end_time__ = CkWallTimer();
169     traceUserBracketEvent(PROJ_USER_EVENT_PAIRCOMPUTE_DOCALC_CALLBACK, __start_time__, __end_time__);
170   #endif
171 }
172
173
174 #include "pairCompute.def.h"