1 #ifndef FILE_PARALLEL 2 #define FILE_PARALLEL 3 4 5 6 #ifdef VTRACE 7 #include "vt_user.h" 8 #else 9 #define VT_USER_START(n) 10 #define VT_USER_END(n) 11 #define VT_TRACER(n) 12 #endif 13 14 15 namespace netgen 16 { 17 18 #ifdef OLD 19 #ifdef PARALLEL 20 template <class T> MyGetMPIType()21 inline MPI_Datatype MyGetMPIType ( ) 22 { cerr << "ERROR in GetMPIType() -- no type found" << endl;return 0; } 23 template <> MyGetMPIType()24 inline MPI_Datatype MyGetMPIType<int> ( ) 25 { return MPI_INT; } 26 template <> MyGetMPIType()27 inline MPI_Datatype MyGetMPIType<double> ( ) 28 { return MPI_DOUBLE; } 29 template <> MyGetMPIType()30 inline MPI_Datatype MyGetMPIType<char> ( ) 31 { return MPI_CHAR; } 32 template<> MyGetMPIType()33 inline MPI_Datatype MyGetMPIType<size_t> ( ) 34 { return MPI_UINT64_T; } 35 #else 36 typedef int MPI_Datatype; 37 template <class T> inline MPI_Datatype MyGetMPIType ( ) { return 0; } 38 #endif 39 #endif 40 41 42 #ifdef PARALLEL 43 enum { MPI_TAG_CMD = 110 }; 44 enum { MPI_TAG_MESH = 210 }; 45 enum { MPI_TAG_VIS = 310 }; 46 47 48 [[deprecated("mympi_send int, use comm.Send instead")]] MyMPI_Send(int i,int dest,int tag,MPI_Comm comm)49 inline void MyMPI_Send (int i, int dest, int tag, MPI_Comm comm) 50 { 51 int hi = i; 52 MPI_Send( &hi, 1, MPI_INT, dest, tag, comm); 53 } 54 55 [[deprecated("mympi_revc int, use comm.Recv instead")]] MyMPI_Recv(int & i,int src,int tag,MPI_Comm comm)56 inline void MyMPI_Recv (int & i, int src, int tag, MPI_Comm comm) 57 { 58 MPI_Status status; 59 MPI_Recv( &i, 1, MPI_INT, src, tag, comm, &status); 60 } 61 62 [[deprecated("mympi_send string, use comm.Send instead")]] MyMPI_Send(const string & s,int dest,int tag,MPI_Comm comm)63 inline void MyMPI_Send (const string & s, int dest, int tag, MPI_Comm comm) 64 { 65 MPI_Send( const_cast<char*> (s.c_str()), s.length(), MPI_CHAR, dest, tag, comm); 66 } 67 68 [[deprecated("mympi_revc string, use comm.Recv instead")]] MyMPI_Recv(string & s,int src,int tag,MPI_Comm comm)69 inline void MyMPI_Recv (string & s, int src, int tag, MPI_Comm comm) 70 { 71 MPI_Status status; 72 int len; 73 MPI_Probe (src, tag, MPI_COMM_WORLD, &status); 74 MPI_Get_count (&status, MPI_CHAR, &len); 75 s.assign (len, ' '); 76 MPI_Recv( &s[0], len, MPI_CHAR, src, tag, comm, &status); 77 } 78 79 80 81 template <class T, int BASE> 82 [[deprecated("mympi_send ngflatarray, use comm.send instead")]] MyMPI_Send(NgFlatArray<T,BASE> s,int dest,int tag,MPI_Comm comm)83 inline void MyMPI_Send (NgFlatArray<T, BASE> s, int dest, int tag, MPI_Comm comm) 84 { 85 MPI_Send( &s.First(), s.Size(), GetMPIType<T>(), dest, tag, comm); 86 } 87 88 template <class T, int BASE> 89 [[deprecated("mympi_recv ngflatarray, use comm.Recv instead")]] MyMPI_Recv(NgFlatArray<T,BASE> s,int src,int tag,MPI_Comm comm)90 inline void MyMPI_Recv ( NgFlatArray<T, BASE> s, int src, int tag, MPI_Comm comm) 91 { 92 MPI_Status status; 93 MPI_Recv( &s.First(), s.Size(), GetMPIType<T>(), src, tag, comm, &status); 94 } 95 96 template <class T, int BASE> MyMPI_Recv(NgArray<T,BASE> & s,int src,int tag,MPI_Comm comm)97 inline void MyMPI_Recv ( NgArray <T, BASE> & s, int src, int tag, MPI_Comm comm) 98 { 99 MPI_Status status; 100 int len; 101 MPI_Probe (src, tag, comm, &status); 102 MPI_Get_count (&status, GetMPIType<T>(), &len); 103 104 s.SetSize (len); 105 MPI_Recv( &s.First(), len, GetMPIType<T>(), src, tag, comm, &status); 106 } 107 108 template <class T, int BASE> MyMPI_Recv(NgArray<T,BASE> & s,int tag,MPI_Comm comm)109 inline int MyMPI_Recv ( NgArray <T, BASE> & s, int tag, MPI_Comm comm) 110 { 111 MPI_Status status; 112 int len; 113 MPI_Probe (MPI_ANY_SOURCE, tag, comm, &status); 114 115 int src = status.MPI_SOURCE; 116 117 MPI_Get_count (&status, GetMPIType<T>(), &len); 118 119 s.SetSize (len); 120 MPI_Recv( &s.First(), len, GetMPIType<T>(), src, tag, comm, &status); 121 122 return src; 123 } 124 125 126 /* 127 template <class T, int BASE> 128 inline void MyMPI_ISend (NgFlatArray<T, BASE> s, int dest, int tag, MPI_Request & request) 129 { 130 MPI_Isend( &s.First(), s.Size(), MyGetMPIType<T>(), dest, tag, MPI_COMM_WORLD, & request); 131 } 132 133 134 template <class T, int BASE> 135 inline void MyMPI_IRecv (NgFlatArray<T, BASE> s, int dest, int tag, MPI_Request & request) 136 { 137 MPI_Irecv( &s.First(), s.Size(), MyGetMPIType<T>(), dest, tag, MPI_COMM_WORLD, & request); 138 } 139 */ 140 141 template <class T, int BASE> 142 [[deprecated("mympi_isend ngflatarray, use comm.send instead")]] MyMPI_ISend(NgFlatArray<T,BASE> s,int dest,int tag,MPI_Comm comm)143 inline MPI_Request MyMPI_ISend (NgFlatArray<T, BASE> s, int dest, int tag, MPI_Comm comm) 144 { 145 MPI_Request request; 146 MPI_Isend( &s.First(), s.Size(), GetMPIType<T>(), dest, tag, comm, &request); 147 return request; 148 } 149 150 template <class T, int BASE> 151 [[deprecated("mympi_irecv ngflatarray, use comm.recv instead")]] MyMPI_IRecv(NgFlatArray<T,BASE> s,int dest,int tag,MPI_Comm comm)152 inline MPI_Request MyMPI_IRecv (NgFlatArray<T, BASE> s, int dest, int tag, MPI_Comm comm) 153 { 154 MPI_Request request; 155 MPI_Irecv( &s.First(), s.Size(), GetMPIType<T>(), dest, tag, comm, &request); 156 return request; 157 } 158 159 /* 160 template <class T, int BASE> 161 inline void MyMPI_ISend (NgFlatArray<T, BASE> s, int dest, int tag) 162 { 163 MPI_Request request; 164 MPI_Isend( &s.First(), s.Size(), MyGetMPIType<T>(), dest, tag, MPI_COMM_WORLD, &request); 165 MPI_Request_free (&request); 166 } 167 168 169 template <class T, int BASE> 170 inline void MyMPI_IRecv (NgFlatArray<T, BASE> s, int dest, int tag) 171 { 172 MPI_Request request; 173 MPI_Irecv( &s.First(), s.Size(), MyGetMPIType<T>(), dest, tag, MPI_COMM_WORLD, &request); 174 MPI_Request_free (&request); 175 } 176 */ 177 178 179 180 /* 181 send a table entry to each of the processes in the group ... 182 receive-table entries will be set 183 */ 184 185 template <typename T> MyMPI_ExchangeTable(TABLE<T> & send_data,TABLE<T> & recv_data,int tag,const NgMPI_Comm & comm)186 inline void MyMPI_ExchangeTable (TABLE<T> & send_data, 187 TABLE<T> & recv_data, int tag, 188 const NgMPI_Comm & comm) 189 { 190 int rank = comm.Rank(); 191 int ntasks = comm.Size(); 192 193 Array<int> send_sizes(ntasks); 194 Array<int> recv_sizes(ntasks); 195 for (int i = 0; i < ntasks; i++) 196 send_sizes[i] = send_data[i].Size(); 197 198 comm.AllToAll (send_sizes, recv_sizes); 199 200 for (int i = 0; i < ntasks; i++) 201 recv_data.SetEntrySize (i, recv_sizes[i], sizeof(T)); 202 203 Array<MPI_Request> requests; 204 for (int dest = 0; dest < ntasks; dest++) 205 if (dest != rank && send_data[dest].Size()) 206 requests.Append (comm.ISend (FlatArray<T>(send_data[dest]), dest, tag)); 207 208 for (int dest = 0; dest < ntasks; dest++) 209 if (dest != rank && recv_data[dest].Size()) 210 requests.Append (comm.IRecv (FlatArray<T>(recv_data[dest]), dest, tag)); 211 212 MyMPI_WaitAll (requests); 213 } 214 215 216 extern void MyMPI_SendCmd (const char * cmd); 217 extern string MyMPI_RecvCmd (); 218 219 220 template <class T> MyMPI_Bcast(T & s,MPI_Comm comm)221 inline void MyMPI_Bcast (T & s, MPI_Comm comm) 222 { 223 MPI_Bcast (&s, 1, GetMPIType<T>(), 0, comm); 224 } 225 226 template <class T> MyMPI_Bcast(NgArray<T,0> & s,NgMPI_Comm comm)227 inline void MyMPI_Bcast (NgArray<T, 0> & s, NgMPI_Comm comm) 228 { 229 int size = s.Size(); 230 MyMPI_Bcast (size, comm); 231 // if (MyMPI_GetId(comm) != 0) s.SetSize (size); 232 if (comm.Rank() != 0) s.SetSize (size); 233 MPI_Bcast (&s[0], size, GetMPIType<T>(), 0, comm); 234 } 235 236 template <class T> MyMPI_Bcast(NgArray<T,0> & s,int root,MPI_Comm comm)237 inline void MyMPI_Bcast (NgArray<T, 0> & s, int root, MPI_Comm comm) 238 { 239 int id; 240 MPI_Comm_rank(comm, &id); 241 242 int size = s.Size(); 243 MPI_Bcast (&size, 1, MPI_INT, root, comm); 244 if (id != root) s.SetSize (size); 245 if ( !size ) return; 246 MPI_Bcast (&s[0], size, GetMPIType<T>(), root, comm); 247 } 248 249 template <class T, class T2> 250 [[deprecated("mympi_allgather deprecated, use comm.allgather")]] MyMPI_Allgather(const T & send,NgFlatArray<T2> recv,MPI_Comm comm)251 inline void MyMPI_Allgather (const T & send, NgFlatArray<T2> recv, MPI_Comm comm) 252 { 253 MPI_Allgather( const_cast<T*> (&send), 1, GetMPIType<T>(), &recv[0], 1, GetMPIType<T2>(), comm); 254 } 255 256 template <class T, class T2> 257 [[deprecated("mympi_alltoall deprecated, use comm.alltoall")]] MyMPI_Alltoall(NgFlatArray<T> send,NgFlatArray<T2> recv,MPI_Comm comm)258 inline void MyMPI_Alltoall (NgFlatArray<T> send, NgFlatArray<T2> recv, MPI_Comm comm) 259 { 260 MPI_Alltoall( &send[0], 1, GetMPIType<T>(), &recv[0], 1, GetMPIType<T2>(), comm); 261 } 262 263 264 #endif // PARALLEL 265 266 } 267 268 #endif 269