00001 #include "netmpi.h"
00002 #include<mpi.h>
00003 #include <stdarg.h>
00004 #include <iostream>
00005 bool NetMPI::m_loop = false;
00006 bool NetMPI::b1 = false;
00007 bool NetMPI::b2 = false;
00008 #ifdef DEBUG
00009 bool NetMPI::m_print = true;
00010 #else
00011 bool NetMPI::m_print = false;
00012 #endif
00013
00014 std::ostream* NetMPI::m_out = &(std::cout);
00015
00016 int NetMPI::m_rank = -1;
00017 int NetMPI::m_nP = -1;
00018
00019 int NetMPI::rank()
00020 {
00021 if (b1)
00022 return m_rank;
00023 else
00024 {
00025 b1=true;
00026 MPI_Comm_rank(MPI_COMM_WORLD,&m_rank);
00027 return m_rank;
00028 }
00029 }
00030
00031 int NetMPI::nProcess()
00032 {
00033 if (b2)
00034 return m_nP;
00035 else
00036 {
00037 b2=true;
00038 MPI_Comm_size(MPI_COMM_WORLD,&m_nP);
00039 return m_nP;
00040 }
00041 }
00042
00043 bool NetMPI::isLastProcess()
00044 {
00045 return rank()==(nProcess()-1);
00046 }
00047
00048 std::ostream& NetMPI::getLog()
00049 {
00050 return *m_out;
00051 }
00052
00053
00054
00055
00056 void NetMPI::exchangeDataRedBlack(VecIndex &vLSndMap,VecDouble &vLSnd,VecIndex &vLRcvMap,VecDouble &vLRcv,VecIndex &vRSndMap,VecDouble &vRSnd,VecIndex &vRRcvMap,VecDouble &vRRcv,VecDouble &dataSnd, VecDouble &dataRcv, int tag)
00057 {
00058 MPI_Request _req[4];
00059 MPI_Status _status[4];
00060 assert(vLSndMap.size() == vLSnd.size());
00061 assert(vRSndMap.size() == vRSnd.size());
00062 assert(vRRcvMap.size() == vRRcv.size());
00063 assert(vLRcvMap.size() == vLRcv.size());
00064 _req[0]=_req[1]=_req[2]=_req[3] = MPI_REQUEST_NULL;
00065
00066 int next = NetMPI::rank()+1;
00067 int prev = NetMPI::rank()-1;
00068 if (NetMPI::rank()%2 == 0)
00069 {
00070 for (unsigned i=0;i<vLSndMap.size();i++)
00071 vLSnd(i)=dataSnd(vLSndMap[i]);
00072 NetMPI::Isend(vLSnd,prev,tag,_req[0]);
00073
00074 for (unsigned i=0;i<vRSndMap.size();i++)
00075 vRSnd(i)=dataSnd(vRSndMap[i]);
00076 NetMPI::Isend(vRSnd,next,tag,_req[1]);
00077
00078 NetMPI::Irecv(vLRcv,prev,tag,_req[2]);
00079 NetMPI::Irecv(vRRcv,next,tag,_req[3]);
00080
00081 }
00082 else if (NetMPI::rank()%2 == 1)
00083 {
00084 NetMPI::Irecv(vRRcv,next,tag,_req[0]);
00085 NetMPI::Irecv(vLRcv,prev,tag,_req[1]);
00086
00087 for (unsigned i=0;i<vRSndMap.size();i++)
00088 vRSnd(i)=dataSnd(vRSndMap[i]);
00089 NetMPI::Isend(vRSnd,next,tag,_req[2]);
00090
00091
00092 for (unsigned i=0;i<vLSndMap.size();i++)
00093 vLSnd(i)=dataSnd(vLSndMap[i]);
00094 NetMPI::Isend(vLSnd,prev,tag,_req[3]);
00095
00096 }
00097 MPI_Waitall(4,_req,_status);
00098 for (unsigned i=0;i<vLRcv.size();i++)
00099 {
00100 dataRcv(vLRcvMap[i]) = vLRcv(i);
00101 }
00102 for (unsigned i=0;i<vRRcv.size();i++)
00103 {
00104 dataRcv(vRRcvMap[i]) = vRRcv(i);
00105 }
00106
00107
00108 }
00109
00110
00111 void NetMPI::exchangeDataRedBlack(VecIndex &vLSndMap,VecDouble &vLSnd,VecIndex &vLRcvMap,VecDouble &vLRcv,VecIndex &vRSndMap,VecDouble &vRSnd,VecIndex &vRRcvMap,VecDouble &vRRcv,VecDouble &data,int tag)
00112 {
00113 exchangeDataRedBlack(vLSndMap,vLSnd,vLRcvMap,vLRcv,vRSndMap,vRSnd,vRRcvMap,vRRcv,data,data,tag);
00114
00115 }
00116
00117 void NetMPI::exchangeDataRedBlack(VecIndex &vLSndMap,VecDouble &vLSnd,VecDouble &vLRcv,VecIndex &vRSndMap,VecDouble &vRSnd,VecDouble &vRRcv,VecDouble &data,int tag,VecReq &_req)
00118 {
00119 assert(_req.size() == 4);
00120 assert(vLSndMap.size() == vLSnd.size());
00121 assert(vRSndMap.size() == vRSnd.size());
00122
00123
00124
00125 int next = NetMPI::rank()+1;
00126 int prev = NetMPI::rank()-1;
00127 if (NetMPI::rank()%2 == 0)
00128 {
00129 for (unsigned i=0;i<vLSndMap.size();i++)
00130 vLSnd(i)=data(vLSndMap[i]);
00131 NetMPI::Isend(vLSnd,prev,tag,_req[0]);
00132
00133 for (unsigned i=0;i<vRSndMap.size();i++)
00134 vRSnd(i)=data(vRSndMap[i]);
00135 NetMPI::Isend(vRSnd,next,tag,_req[1]);
00136
00137 NetMPI::Irecv(vLRcv,prev,tag,_req[2]);
00138 NetMPI::Irecv(vRRcv,next,tag,_req[3]);
00139
00140 }
00141 else if (NetMPI::rank()%2 == 1)
00142 {
00143 NetMPI::Irecv(vRRcv,next,tag,_req[0]);
00144 NetMPI::Irecv(vLRcv,prev,tag,_req[1]);
00145
00146 for (unsigned i=0;i<vRSndMap.size();i++)
00147 vRSnd(i)=data(vRSndMap[i]);
00148 NetMPI::Isend(vRSnd,next,tag,_req[2]);
00149
00150
00151 for (unsigned i=0;i<vLSndMap.size();i++)
00152 vLSnd(i)=data(vLSndMap[i]);
00153 NetMPI::Isend(vLSnd,prev,tag,_req[3]);
00154
00155 }
00156
00157 }
00158
00159
00160 void NetMPI::trace(const char *format,...)
00161 {
00162 if (m_print)
00163 {
00164 char strOut[5000];
00165 va_list vl;
00166 va_start(vl,format);
00167 vsprintf(strOut,format,vl);
00168 va_end(vl);
00169 std::cout << rank() << ") " << strOut << std::endl;
00170
00171 }
00172 }
00173
00174 void NetMPI::debugPoint()
00175 {
00176 m_loop=true;
00177 while(m_loop)
00178 {
00179 ;
00180 }
00181
00182 }
00183
00184
00185 double NetMPI::getMinValue(double localValue)
00186 {
00187 double dd;
00188 MPI_Allreduce(&localValue,&dd,1,MPI_DOUBLE,MPI_MIN,MPI_COMM_WORLD);
00189 return dd;
00190 }
00191