00001
00002
00003
00004
00005
00006
00007 #include "util.h"
00008 #include "cpaimd.h"
00009 #include "groups.h"
00010 #include "fftCacheSlab.h"
00011 #include "StructFactorCache.h"
00012 #include "../../include/debug_flags.h"
00013 #include "../../src_piny_physics_v1.0/include/class_defs/CP_OPERATIONS/class_cpnonlocal.h"
00014 extern CProxy_CP_State_ParticlePlane particlePlaneProxy;
00015 extern Config config;
00016
00017
00018
00019
00020 int StructFactCache::existStructFact(int planeIndex) {
00021 for(int i=0; i<planeCountList.length(); i++){
00022 PlaneCount thisCount = planeCountList[i];
00023 if(thisCount.plane == planeIndex) {
00024 return i;
00025 }
00026 }
00027 #ifdef _CP_DEBUG_SF_CACHE_
00028 CkPrintf("[%d] lacks plane %d\n",CkMyPe(),planeIndex);
00029 #endif
00030
00031 return -1;
00032 }
00033
00034
00035
00036
00037
00038 int StructFactCache::existStructFactGrp(int planeIndex, int atmGrp) {
00039 for(int i=0; i<planeCountList.length(); i++){
00040 PlaneCount thisCount = planeCountList[i];
00041 if(thisCount.plane == planeIndex) {
00042 if(structFactorAtmGrps[i][atmGrp]==1)
00043 {
00044 #ifdef _CP_DEBUG_SF_CACHE_
00045 CkPrintf("[%d] has plane %d atom %d\n",CkMyPe(),planeIndex,atmGrp);
00046 #endif
00047 return i;
00048 }
00049 }
00050 }
00051 #ifdef _CP_DEBUG_SF_CACHE_
00052 CkPrintf("[%d] lacks plane %d atom %d\n",CkMyPe(),planeIndex,atmGrp);
00053 #endif
00054 return -1;
00055 }
00056
00057
00058
00059
00060
00061
00062 int StructFactCache::incCountStructFact(int planeIndex) {
00063
00064 for(int i=0; i<planeCountList.length(); i++){
00065 PlaneCount* thisCount = &(planeCountList[i]);
00066 if(thisCount->plane == planeIndex) {
00067 thisCount->count ++;
00068 return thisCount->count;
00069 }
00070 }
00071
00072 planeCountList.insert(planeCountList.length(), PlaneCount(planeIndex, 1, 1));
00073 return 1;
00074 }
00075
00076
00077
00078
00079
00080
00081 int StructFactCache::decCountStructFact(int planeIndex) {
00082 for(int i=0; i<planeCountList.length(); i++){
00083 PlaneCount* thisCount = &(planeCountList[i]);
00084 if(thisCount->plane == planeIndex) {
00085 thisCount->count--;
00086 if(thisCount->count < 0) {
00087 thisCount->count = 0;
00088 CkPrintf("Over Decrementing counters!!!\n");
00089 }
00090 return thisCount->count;
00091 }
00092 }
00093 CkPrintf("Trying to Decrement non-existing counters!!!\n");
00094 return -1;
00095 }
00096
00097
00098
00099
00100
00101
00102 void StructFactCache::printCountStructFact() {
00103 CkPrintf("/////////////////////////////////////\n");
00104 for(int i=0; i<planeCountList.length(); i++){
00105 PlaneCount thisCount = planeCountList[i];
00106 CkPrintf(" plane -- %d, count -- %d\n", thisCount.plane, thisCount.count);
00107 }
00108 CkPrintf("/////////////////////////////////////\n");
00109 }
00110
00111
00112
00113
00114
00115
00116 void StructFactCache::setZero(int planeIndex) {
00117 int i = existStructFact(planeIndex);
00118 if(i>=0)
00119 {
00120 #ifdef _CP_DEBUG_SF_CACHE_
00121 CkPrintf("[%d] zeros plane %d\n",CkMyPe(),planeIndex);
00122 #endif
00123 planeCountList[i].updated = 0;
00124 for(int j=0;j<numSfGrps;j++)
00125 structFactorAtmGrps[i][j]=0;
00126 }
00127 }
00128
00129
00130
00131
00132
00133
00134
00135 int StructFactCache::getStructFact(int planeIndex, int atmGrpIndex, complex** sf,
00136 complex** sf_x, complex** sf_y, complex** sf_z) {
00137 int i = existStructFact(planeIndex);
00138 if(i < 0) return -1;
00139 int arroffset=structFactorSize[i]*atmGrpIndex*natm_nl_grp_max;
00140 *sf = structFactorList[i]+arroffset;
00141 *sf_x = structFactorfxList[i]+arroffset;
00142 *sf_y = structFactorfyList[i]+arroffset;
00143 *sf_z = structFactorfzList[i]+arroffset;
00144 return i;
00145 }
00146
00147
00148
00149
00150
00151 void StructFactCache::getStructFactIdx(int sfindex, int atmGrpIndex, complex** sf,
00152 complex** sf_x, complex** sf_y, complex** sf_z) {
00153 int arroffset=structFactorSize[sfindex]*atmGrpIndex*natm_nl_grp_max;
00154 *sf = structFactorList[sfindex]+arroffset;
00155 *sf_x = structFactorfxList[sfindex]+arroffset;
00156 *sf_y = structFactorfyList[sfindex]+arroffset;
00157 *sf_z = structFactorfzList[sfindex]+arroffset;
00158 }
00159
00160
00161
00162 void StructFactCache::acceptStructFact(StructFactorMsg *msg)
00163 {
00164
00165
00166 int atmIndex=msg->atmGrpIndex;
00167 int planeIndex=msg->planeIndex;
00168 int arroffset=msg->gsSize*atmIndex*natm_nl_grp_max;
00169 int sfindex=existStructFact(planeIndex);
00170 #ifdef _CP_DEBUG_SF_CACHE_
00171 CkPrintf("[%d] received SF for plane %d atom %d\n",CkMyPe(),planeIndex, atmIndex);
00172 #endif
00173
00174 if(sfindex<0)
00175 {
00176
00177 incCountStructFact(planeIndex);
00178 int structSize=msg->gsSize*numSfGrps*natm_nl_grp_max;
00179 complex *structFactor = (complex *)fftw_malloc(structSize*sizeof(complex));
00180 complex *structFactor_fx = (complex *)fftw_malloc(structSize*sizeof(complex));
00181 complex *structFactor_fy = (complex *)fftw_malloc(structSize*sizeof(complex));
00182 complex *structFactor_fz = (complex *)fftw_malloc(structSize*sizeof(complex));
00183 int *atmGrpArrive = new int[numSfGrps];
00184 bzero(atmGrpArrive, numSfGrps*sizeof(int));
00185 structFactorAtmGrps.insert(structFactorAtmGrps.length(),atmGrpArrive);
00186 structFactorList.insert(structFactorList.length(),structFactor);
00187 structFactorfxList.insert(structFactorfxList.length(), structFactor_fx);
00188 structFactorfyList.insert(structFactorfyList.length(), structFactor_fy);
00189 structFactorfzList.insert(structFactorfzList.length(), structFactor_fz);
00190 structFactorSize.insert(structFactorSize.length(), msg->gsSize);
00191
00192 sfindex=existStructFact(planeIndex);
00193 #ifdef _CP_DEBUG_SF_CACHE_
00194 CkPrintf("[%d] created SF entry %d for plane %d atom %d\n",CkMyPe(),sfindex, planeIndex, atmIndex);
00195 #endif
00196 }
00197 CkAssert(sfindex>=0);
00198 planeCountList[sfindex].updated = 1;
00199 structFactorAtmGrps[sfindex][atmIndex] = 1;
00200 CmiMemcpy(structFactorList[sfindex]+arroffset,msg->structFactor,msg->datalen*sizeof(complex));
00201 CmiMemcpy(structFactorfxList[sfindex]+arroffset,msg->structFactor_fx,msg->datalen*sizeof(complex));
00202 CmiMemcpy(structFactorfyList[sfindex]+arroffset,msg->structFactor_fy,msg->datalen*sizeof(complex));
00203 CmiMemcpy(structFactorfzList[sfindex]+arroffset,msg->structFactor_fz,msg->datalen*sizeof(complex));
00204
00205
00206 int ppregindex;
00207 if((ppregindex=existsPP(planeIndex,atmIndex))>=0)
00208 {
00209 PlaneAtom regPPs=ppList[ppregindex];
00210 CkArrayIndex2D idx2d;
00211 for(int i=0;i<regPPs.particles.length();i++)
00212 {
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222 idx2d=regPPs.particles[i];
00223
00224 PPDummyMsg *pmsg = new (8*sizeof(int)) PPDummyMsg;
00225 pmsg->atmGrp=atmIndex;
00226 pmsg->sfindex=sfindex;
00227 CkSetQueueing(pmsg, CK_QUEUEING_IFIFO);
00228 *(int*)CkPriorityPtr(pmsg) = config.sfpriority+atmIndex+numSfGrps;
00229 particlePlaneProxy(idx2d.index[0], idx2d.index[1]).computeZ(pmsg);
00230
00231 }
00232 }
00233 else
00234 {
00235 char out[100];
00236 snprintf(out, 100, "[%d] accepted SF for %d %d without any registered particles",CkMyPe(), planeIndex, atmIndex);
00237 CkAbort(out);
00238 }
00239 delete msg;
00240 }
00241
00242
00243
00244
00245
00246
00247 int StructFactCache::registerPP(int state, int plane, int atom) {
00248
00249 PlaneAtom pa(plane,atom);
00250 CkArrayIndex2D idx2d(state,plane);
00251 for(int i=0; i<ppList.length(); i++){
00252 if(ppList[i]==pa)
00253 {
00254 ppList[i].particles.push_back(idx2d);
00255 #ifdef _CP_DEBUG_SF_CACHE_
00256 CkPrintf("[%d] registers PP[%d,%d] atom %d at %d\n",CkMyPe(),state,plane,atom, ppList[i].particles.length()-1);
00257 #endif
00258 return i;
00259 }
00260 }
00261
00262 ppList.push_back(pa);
00263 ppList[ppList.length()-1].particles.push_back(idx2d);
00264 #ifdef _CP_DEBUG_SF_CACHE_
00265 CkPrintf("SFC [%d] registers PP[%d,%d] atom %d at %d\n",CkMyPe(),state,plane,atom,ppList[ppList.length()-1].particles.length()-1);
00266 #endif
00267 return ppList.length()-1;
00268 }
00269
00270
00271 int StructFactCache::existsPP(int plane, int atom) {
00272
00273 PlaneAtom pa(plane,atom);
00274 for(int i=0; i<ppList.length(); i++)
00275 if(ppList[i]==pa)
00276 return i;
00277 return -1;
00278 }
00279
00280
00281
00282
00283 void StructFactCache::removeAll() {
00284
00285 for(int i=structFactorList.length()-1; i>=0; i--){
00286 fftw_free(structFactorList[i]);
00287 fftw_free(structFactorfxList[i]);
00288 fftw_free(structFactorfyList[i]);
00289 fftw_free(structFactorfzList[i]);
00290 delete []structFactorAtmGrps[i];
00291 }
00292
00293 structFactorList.removeAll();
00294 structFactorfxList.removeAll();
00295 structFactorfyList.removeAll();
00296 structFactorfzList.removeAll();
00297 structFactorSize.removeAll();
00298 gSpaceSlabs.removeAll();
00299 }
00300
00301
00302