//__________________________________________________________________________________ void mpiBgmLoop (int rank, int size) { long senderID = 0; _String * resStr = nil; ReportWarning (_String ("MPI Node:") & (long)rank & " is ready for MPIBgmCacheNodeScores tasks"); // receive serialized Bgm _String* theMessage = MPIRecvString (-1, senderID); while (theMessage->sLength) { _ExecutionList exL (*theMessage); _PMathObj res = exL.Execute(); // should send this process into CacheNodeScores() resStr = res ? (_String*)res->toStr() : new _String ("0"); ReportWarning (_String ("MPI Node: ") & (long)rank & " executed HBL with result:\n" & resStr); if (bgmNamesList.lLength < 1) { _String errMsg ("Malformed HBL. No valid BGM has been defined.\n"); FlagError (errMsg); break; } } DeleteObject (theMessage); }
//__________________________________________________________________________________ void mpiNormalLoop (int rank, int size, _String & baseDir) { long senderID = 0; ReportWarning ("[MPI] Entered mpiNormalLoop"); _String* theMessage = MPIRecvString (-1,senderID), // listen for messages from any node * resStr = nil; while (theMessage->sLength) { setParameter (mpiNodeID, (_Parameter)rank); setParameter (mpiNodeCount, (_Parameter)size); //ReportWarning (*theMessage); DeleteObject (resStr); resStr = nil; if (theMessage->startswith (mpiLoopSwitchToOptimize) ) { hyphyMPIOptimizerMode = theMessage->Cut(mpiLoopSwitchToOptimize.sLength,-1).toNum(); ReportWarning (_String("[MPI] Switched to mpiOptimizer loop with mode ") & hyphyMPIOptimizerMode); MPISendString (mpiLoopSwitchToOptimize,senderID); mpiOptimizerLoop (rank,size); ReportWarning ("[MPI] Returned from mpiOptimizer loop"); hyphyMPIOptimizerMode = _hyphyLFMPIModeNone; pathNames && & baseDir; } else if ( theMessage->Equal (&mpiLoopSwitchToBGM) ) { ReportWarning ("[MPI] Received signal to switch to mpiBgmLoop"); MPISendString (mpiLoopSwitchToBGM, senderID); // feedback to source to confirm receipt of message mpiBgmLoop (rank, size); ReportWarning ("[MPI] Returned from mpiBgmLoop"); } else { if (theMessage->beginswith ("#NEXUS")) { _String msgCopy (*theMessage); ReportWarning ("[MPI] Received a function to optimize"); ReadDataSetFile (nil,true,theMessage); ReportWarning ("[MPI] Done with the optimization"); _Variable* lfName = FetchVar(LocateVarByName(MPI_NEXUS_FILE_RETURN)); if (lfName) { resStr = (_String*)(lfName->Compute()->toStr()); } else { _FString *lfID = (_FString*)FetchObjectFromVariableByType (&lf2SendBack, STRING); if (!lfID) { FlagError (_String("[MPI] Malformed MPI likelihood function optimization request - did not specify the LF name to return in variable ") & lf2SendBack & ".\n\n\n" ); break; } long f = likeFuncNamesList.Find (lfID->theString); if (f<0) { FlagError ("[MPI] Malformed MPI likelihood function optimization request - LF name to return did not refer to a well-defined likelihood function.\n\n\n"); break; } _Parameter pv; checkParameter (shortMPIReturn, pv ,0); resStr = (_String*)checkPointer(new _String (1024L,true)); ((_LikelihoodFunction*)likeFuncList (f))->SerializeLF(*resStr,pv>0.5?_hyphyLFSerializeModeShortMPI:_hyphyLFSerializeModeLongMPI); resStr->Finalize(); } } else { _ExecutionList exL (*theMessage); _PMathObj res = exL.Execute(); resStr = res?(_String*)res->toStr():new _String ("0"); } checkPointer (resStr); MPISendString(*resStr,senderID); _Parameter keepState = 0.0; checkParameter (preserveSlaveNodeState, keepState, 0.0); if (keepState < 0.5) { PurgeAll (true); pathNames && & baseDir; } } DeleteObject (theMessage); theMessage = MPIRecvString (-1,senderID); } /*MPISendString(empty,senderID);*/ DeleteObject (resStr); DeleteObject (theMessage); }
//__________________________________________________________________________________ void mpiNormalLoop (int rank, int size, _String & baseDir) { long senderID = 0; ReportWarning ("Entered mpiNormalLoop"); _String* theMessage = MPIRecvString (-1,senderID), // listen for messages from any node * resStr = nil, _bgmSwitch ("_BGM_SWITCH_"), css("_CONTEXT_SWITCH_MPIPARTITIONS_"); while (theMessage->sLength) { setParameter (mpiNodeID, (_Parameter)rank); setParameter (mpiNodeCount, (_Parameter)size); //ReportWarning (*theMessage); DeleteObject (resStr); resStr = nil; if (theMessage->Equal (&css) ) { mpiPartitionOptimizer = true; ReportWarning ("Switched to mpiOptimizer loop"); MPISendString(css,senderID); mpiOptimizerLoop (rank,size); ReportWarning ("Returned from mpiOptimizer loop"); mpiPartitionOptimizer = false; pathNames && & baseDir; } else if ( theMessage->Equal (&_bgmSwitch) ) { ReportWarning ("Received signal to switch to mpiBgmLoop"); MPISendString (_bgmSwitch, senderID); // feedback to source to confirm receipt of message mpiBgmLoop (rank, size); ReportWarning ("Returned from mpiBgmLoop"); } else { if (theMessage->beginswith ("#NEXUS")) { _String msgCopy (*theMessage); ReportWarning ("Received a function to optimize"); ReadDataSetFile (nil,true,theMessage); ReportWarning ("Done with the optimization"); _Variable* lfName = FetchVar(LocateVarByName(MPI_NEXUS_FILE_RETURN)); if (lfName) { resStr = (_String*)(lfName->Compute()->toStr()); } else { long f = LocateVarByName (lf2SendBack); if (f>=0) lfName = FetchVar(f); if (!(lfName&&(lfName->ObjectClass()==STRING))) { _String errMsg ("Malformed MPI likelihood function optimization request - missing LF name to return.\n\n\n"); errMsg = errMsg & msgCopy; FlagError (errMsg); break; } f = likeFuncNamesList.Find (((_FString*)lfName->Compute())->theString); if (f<0) { _String errMsg ("Malformed MPI likelihood function optimization request - invalid LF name to return.\n\n\n"); errMsg = errMsg & msgCopy; FlagError (errMsg); break; } _Parameter pv; checkParameter (shortMPIReturn, pv ,0); resStr = new _String (1024L,true); checkPointer (resStr); ((_LikelihoodFunction*)likeFuncList (f))->SerializeLF(*resStr,pv>0.5?5:2); resStr->Finalize(); } } else { _ExecutionList exL (*theMessage); /*printf ("Received:\n %s\n", ((_String*)exL.toStr())->sData);*/ _PMathObj res = exL.Execute(); resStr = res?(_String*)res->toStr():new _String ("0"); } checkPointer (resStr); DeleteObject (theMessage); MPISendString(*resStr,senderID); _Parameter keepState = 0.0; checkParameter (preserveSlaveNodeState, keepState, 0.0); if (keepState < 0.5) { PurgeAll (true); pathNames && & baseDir; } } theMessage = MPIRecvString (-1,senderID); } /*MPISendString(empty,senderID);*/ DeleteObject (resStr); DeleteObject (theMessage); }