00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include "mpi.h"
00031
00032 #include "IPstream.H"
00033
00034
00035
00036
00037
00038
00040 Foam::DynamicList<MPI_Request> IPstream_outstandingRequests_;
00042
00043
00044
00045 Foam::IPstream::IPstream
00046 (
00047 const commsTypes commsType,
00048 const int fromProcNo,
00049 const label bufSize,
00050 streamFormat format,
00051 versionNumber version
00052 )
00053 :
00054 Pstream(commsType, bufSize),
00055 Istream(format, version),
00056 fromProcNo_(fromProcNo),
00057 messageSize_(0)
00058 {
00059 setOpened();
00060 setGood();
00061
00062 MPI_Status status;
00063
00064
00065
00066 if (!bufSize)
00067 {
00068 MPI_Probe(procID(fromProcNo_), msgType(), MPI_COMM_WORLD, &status);
00069 MPI_Get_count(&status, MPI_BYTE, &messageSize_);
00070
00071 buf_.setSize(messageSize_);
00072 }
00073
00074 messageSize_ = read(commsType, fromProcNo_, buf_.begin(), buf_.size());
00075
00076 if (!messageSize_)
00077 {
00078 FatalErrorIn
00079 (
00080 "IPstream::IPstream(const int fromProcNo, "
00081 "const label bufSize, streamFormat format, versionNumber version)"
00082 ) << "read failed"
00083 << Foam::abort(FatalError);
00084 }
00085 }
00086
00087
00088
00089
00090 Foam::label Foam::IPstream::read
00091 (
00092 const commsTypes commsType,
00093 const int fromProcNo,
00094 char* buf,
00095 const std::streamsize bufSize
00096 )
00097 {
00098 if (commsType == blocking || commsType == scheduled)
00099 {
00100 MPI_Status status;
00101
00102 if
00103 (
00104 MPI_Recv
00105 (
00106 buf,
00107 bufSize,
00108 MPI_PACKED,
00109 procID(fromProcNo),
00110 msgType(),
00111 MPI_COMM_WORLD,
00112 &status
00113 )
00114 )
00115 {
00116 FatalErrorIn
00117 (
00118 "IPstream::read"
00119 "(const int fromProcNo, char* buf, std::streamsize bufSize)"
00120 ) << "MPI_Recv cannot receive incomming message"
00121 << Foam::abort(FatalError);
00122
00123 return 0;
00124 }
00125
00126
00127
00128
00129 label messageSize;
00130 MPI_Get_count(&status, MPI_BYTE, &messageSize);
00131
00132 if (messageSize > bufSize)
00133 {
00134 FatalErrorIn
00135 (
00136 "IPstream::read"
00137 "(const int fromProcNo, char* buf, std::streamsize bufSize)"
00138 ) << "buffer (" << label(bufSize)
00139 << ") not large enough for incomming message ("
00140 << messageSize << ')'
00141 << Foam::abort(FatalError);
00142 }
00143
00144 return messageSize;
00145 }
00146 else if (commsType == nonBlocking)
00147 {
00148 MPI_Request request;
00149
00150 if
00151 (
00152 MPI_Irecv
00153 (
00154 buf,
00155 bufSize,
00156 MPI_PACKED,
00157 procID(fromProcNo),
00158 msgType(),
00159 MPI_COMM_WORLD,
00160 &request
00161 )
00162 )
00163 {
00164 FatalErrorIn
00165 (
00166 "IPstream::read"
00167 "(const int fromProcNo, char* buf, std::streamsize bufSize)"
00168 ) << "MPI_Recv cannot start non-blocking receive"
00169 << Foam::abort(FatalError);
00170
00171 return 0;
00172 }
00173
00174 IPstream_outstandingRequests_.append(request);
00175
00176 return 1;
00177 }
00178 else
00179 {
00180 FatalErrorIn
00181 (
00182 "IPstream::read"
00183 "(const int fromProcNo, char* buf, std::streamsize bufSize)"
00184 ) << "Unsupported communications type " << commsType
00185 << Foam::abort(FatalError);
00186
00187 return 0;
00188 }
00189 }
00190
00191
00192 void Foam::IPstream::waitRequests()
00193 {
00194 if (IPstream_outstandingRequests_.size())
00195 {
00196 if
00197 (
00198 MPI_Waitall
00199 (
00200 IPstream_outstandingRequests_.size(),
00201 IPstream_outstandingRequests_.begin(),
00202 MPI_STATUSES_IGNORE
00203 )
00204 )
00205 {
00206 FatalErrorIn
00207 (
00208 "IPstream::waitRequests()"
00209 ) << "MPI_Waitall returned with error" << endl;
00210 }
00211
00212 IPstream_outstandingRequests_.clear();
00213 }
00214 }
00215
00216
00217 bool Foam::IPstream::finishedRequest(const label i)
00218 {
00219 if (i >= IPstream_outstandingRequests_.size())
00220 {
00221 FatalErrorIn
00222 (
00223 "IPstream::finishedRequest(const label)"
00224 ) << "There are " << IPstream_outstandingRequests_.size()
00225 << " outstanding send requests and you are asking for i=" << i
00226 << nl
00227 << "Maybe you are mixing blocking/non-blocking comms?"
00228 << Foam::abort(FatalError);
00229 }
00230
00231 int flag;
00232 MPI_Test(&IPstream_outstandingRequests_[i], &flag, MPI_STATUS_IGNORE);
00233
00234 return flag != 0;
00235 }
00236
00237
00238
00239
00240