BIE |
/home/weinberg/src/BIE/include/ModelEvaluationRequest.h00001 #ifndef ModelEvaluationRequest_h 00002 #define ModelEvaluationRequest_h 00003 00004 #include "EvaluationRequest.h" 00005 #include <BIEMutex.h> 00006 #include <cc++/thread.h> 00007 #include <vector> 00008 #include <BIEmpi.h> 00009 00010 using namespace ost; 00011 00012 #undef MODEL_EVALUATION_FINE_TRACE 00013 00014 namespace BIE { 00015 00021 class ModelEvaluationRequest : public EvaluationRequest { 00022 public: 00023 ModelEvaluationRequest(int tileId, double x, double y, int numEvalsAtPoint, 00024 Semaphore& finished, 00025 vector<double>& result 00026 ) 00027 { 00028 LogIt("Request construction"); 00029 // load up array that will be used as a buffer to MPI_send 00030 args[0] = tileId; 00031 args[1] = x; 00032 args[2] = y; 00033 00034 this->finished = &finished; // remember address of semaphore to clock when we're done 00035 this->result = &result; // remember address of vector to store result in. 00036 00037 returnSize = numEvalsAtPoint; // number of doubles in return value 00038 00039 // ++MPIMutex; 00040 // MPI_Comm_rank(MPI_COMM_WORLD, &myId); // save id for debugging 00041 //--MPIMutex; 00042 this->myId = myid; // GLOBAL from BIEmpi.cc 00043 } 00044 00045 virtual ~ModelEvaluationRequest(){} 00046 00047 virtual void Send(int destination, vector<MPI_Comm>*comms) 00048 { 00049 if (sent){ 00050 cerr << "ModelEvaluationRequest.Send: ERROR. Attempt to send message twice from a Model request.\n"; 00051 exit(1); 00052 } 00053 00054 // first do a send to to the server to get its attention. Rethink This. 00055 int command = 4; 00056 00057 LogIt("Model just before Mutex"); 00058 00059 sendMutexWaitTime.start(); 00060 ++MPIMutex; 00061 sendMutexWaitTime.stop(); 00062 00063 00064 LogIt("Model just before first send"); 00065 sendCmdTime.start(); 00066 MPI_Send(&command, 1, MPI_INT, destination, 11, MPI_COMM_WORLD); // send command id 00067 // MPI_Send(&command, 1, MPI_INT, 0, 11, (*comms)[destination]); // send command id 00068 sendCmdTime.stop(); 00069 00070 LogIt("Model between sends"); 00071 00072 // Next send the actual message 00073 sendEvalTime.start(); 00074 MPI_Send(args, sizeof(args)/sizeof(double), MPI_DOUBLE, destination, 22, MPI_COMM_WORLD); 00075 // MPI_Send(args, sizeof(args)/sizeof(double), MPI_DOUBLE, 0, 22, (*comms)[destination]); 00076 sendEvalTime.stop(); 00077 00078 LogIt("Model After 2nd send"); 00079 --MPIMutex; 00080 00081 sent = true; 00082 00083 #ifdef MODEL_EVALUATION_FINE_TRACE 00084 cout << "ModelEvaluationsRequest.Send: Sent model eval request from " << myId << " to " << destination << "\n"; 00085 #endif 00086 } 00087 00088 00089 // Receive and process a message for which we have a probeStatus from MPI. 00090 // The message is expected to be a reply to our request for evaluation. 00091 virtual void Recv(MPI_Status *probeStatus) 00092 { 00093 int tag = probeStatus->MPI_TAG; 00094 int sender = probeStatus->MPI_SOURCE; 00095 int messageReturnSize; 00096 00097 MPI_Status status; 00098 00099 #ifdef MODEL_EVALUATION_FINE_TRACE 00100 cout << "ModelEvaluationsRequest.Recv: message received from " << sender << " on " << myId << "\n"; 00101 #endif 00102 00103 if (tag !=23){ 00104 cerr << "ModelEvaluationRequest.Recv Tag mismatch in MPI probe. Expected 23 recieved " << tag << "\n"; 00105 cerr << "Process: "<< myId << " Sender = " << sender << "\n"; 00106 exit(1); 00107 } 00108 00109 if (received){ 00110 cout << "ModelEvaluationRequest.Recv ERROR. Attempt to receive two results from a request.\n"; 00111 exit(1); 00112 } 00113 00114 double *z = new double[returnSize]; 00115 00116 ++MPIMutex; 00117 MPI_Get_count(probeStatus, MPI_DOUBLE, &messageReturnSize); 00118 --MPIMutex; 00119 00120 if (messageReturnSize != returnSize){ 00121 cerr << "ModelEvaluationRequest.Recv: Error - returned value wrong size from modelEvaluation. Expecting " << 00122 returnSize << " got " << messageReturnSize << "\n"; 00123 00124 cerr << "Process: "<< myId << " Sender = " << sender << "\n"; 00125 00126 exit(1); 00127 } 00128 00129 00130 ++MPIMutex; 00131 MPI_Recv(z, returnSize, MPI_DOUBLE, sender, 23, MPI_COMM_WORLD, &status); //shouldn't block 00132 --MPIMutex; 00133 received = true; 00134 00135 00136 result->resize(returnSize); // return value 00137 00138 // copy buffer into vector 00139 for(int i=0; i<returnSize; i++) (*result)[i] = z[i]; 00140 00141 delete[] z; 00142 00143 00144 #ifdef MODEL_EVALUATION_FINE_TRACE 00145 cout << "ModelEvaluationsRequest.Recv: completion posted from " << sender << " on " << myId << "\n"; 00146 #endif 00147 00148 finished->post(); // notify the user's thread that requested the evaluation 00149 00150 } 00151 00152 double args[3]; 00153 vector<double> *result; 00154 int returnSize; 00155 00156 private: 00157 int myId; 00158 }; 00159 } 00160 #endif // ModelEvaluationRequest_h Send suggestions, questions, and feedback to WEINBERG at ASTRO dot UMASS dot EDU. Documentation generated at Fri Mar 26 00:35:10 2010 by
|