ParallelDomainBase.cc 75.3 KB
Newer Older
2001
    SerUtil::serialize(out, initialPartitionMesh);
2002
2003
    SerUtil::serialize(out, partitionVec);
    SerUtil::serialize(out, oldPartitionVec);
2004
    SerUtil::serialize(out, nRankDofs);
2005
2006
2007
2008
    
    myIntBoundary.serialize(out);
    otherIntBoundary.serialize(out);

2009
2010
    serialize(out, sendDofs);
    serialize(out, recvDofs);
2011
2012
2013
2014
2015

    SerUtil::serialize(out, mapLocalGlobalDOFs);
    SerUtil::serialize(out, mapLocalToDofIndex);
    SerUtil::serialize(out, isRankDof);

2016
    serialize(out, vertexDof);
2017

2018
2019
2020
    SerUtil::serialize(out, rstart);
    SerUtil::serialize(out, nRankRows);
    SerUtil::serialize(out, nOverallRows);
2021
2022
2023
2024
2025
  }


  void ParallelDomainBase::deserialize(std::istream &in)
  {
2026
    SerUtil::deserialize(in, elemWeights);
2027
    SerUtil::deserialize(in, initialPartitionMesh);
2028
2029
    SerUtil::deserialize(in, partitionVec);
    SerUtil::deserialize(in, oldPartitionVec);
2030
    SerUtil::deserialize(in, nRankDofs);    
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059

    // Create two maps: one from from element indices to the corresponding element 
    // pointers, and one map from Dof indices to the corresponding dof pointers.
    std::map<int, Element*> elIndexMap;
    std::map<int, const DegreeOfFreedom*> dofMap;
    ElementDofIterator elDofIter(feSpace);
    TraverseStack stack;
    ElInfo *elInfo = stack.traverseFirst(mesh, -1, Mesh::CALL_LEAF_EL);
    while (elInfo) {
      Element *el = elInfo->getElement();
      elIndexMap[el->getIndex()] = el;
      elInfo = stack.traverseNext(elInfo);

      elDofIter.reset(el);
      do {
	dofMap[elDofIter.getDof()] = elDofIter.getDofPtr();
      } while(elDofIter.next());      
    }

    myIntBoundary.deserialize(in, elIndexMap);
    otherIntBoundary.deserialize(in, elIndexMap);

    deserialize(in, sendDofs, dofMap);
    deserialize(in, recvDofs, dofMap);

    SerUtil::deserialize(in, mapLocalGlobalDOFs);
    SerUtil::deserialize(in, mapLocalToDofIndex);
    SerUtil::deserialize(in, isRankDof);

2060
    deserialize(in, vertexDof, dofMap);
2061

2062
2063
2064
2065
2066
    SerUtil::deserialize(in, rstart);
    SerUtil::deserialize(in, nRankRows);
    SerUtil::deserialize(in, nOverallRows);

    deserialized = true;
2067
2068
2069
  }


Thomas Witkowski's avatar
Thomas Witkowski committed
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
  void ParallelDomainBase::DbgCreateElementMap(ElementIdxToDofs &elMap)
  {
    FUNCNAME("ParallelDomainBase::DbgCreateElementMap()");

    TEST_EXIT(mesh->getDim() == 2)("Works only for 2d!\n");

    elMap.clear();
       
    TraverseStack stack;
    ElInfo *elInfo = stack.traverseFirst(mesh, -1, Mesh::CALL_LEAF_EL);
    while (elInfo) {
      Element *el = elInfo->getElement();
      orderDOFs(el->getDOF(0), el->getDOF(1), el->getDOF(2), elMap[el->getIndex()]);
      elInfo = stack.traverseNext(elInfo);
    }
  }

  void ParallelDomainBase::DbgTestElementMap(ElementIdxToDofs &elMap)
  {
    FUNCNAME("ParallelDomainbase::DbgTestElementMap()");

    TEST_EXIT(mesh->getDim() == 2)("Works only for 2d!\n");

    DofContainer vec(3);
    TraverseStack stack;
    ElInfo *elInfo = stack.traverseFirst(mesh, -1, Mesh::CALL_LEAF_EL);
    while (elInfo) {
      Element *el = elInfo->getElement();
      orderDOFs(el->getDOF(0), el->getDOF(1), el->getDOF(2), vec);
      for (int i = 0; i < 3; i++) {
	if (elMap[el->getIndex()][i] != vec[i]) {
	  std::cout << "[DBG " << mpiRank 
		    << "]: Wrong new dof numeration in element = " 
		    << el->getIndex() << std::endl;
	  std::cout << "[DBG " << mpiRank << "]: Old numeration was: ";
	  for (int j = 0; j < 3; j++)
	    std::cout << elMap[el->getIndex()][j] << " = " 
		      << *(elMap[el->getIndex()][j]) << "  ";
	  std::cout << std::endl;
	  std::cout << "[DBG " << mpiRank << "]: New numeration is:  ";
	  for (int j = 0; j < 3; j++)
	    std::cout << vec[j] << " = "  << *(vec[j]) << "  ";
	  std::cout << std::endl;
	  ERROR_EXIT("WRONG NEW DOF NUMERATION!\n");
	}
      }
      elInfo = stack.traverseNext(elInfo);
    }
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
  void ParallelDomainBase::DbgTestInteriorBoundary()
  {
    FUNCNAME("ParallelDomainBase::DbgTestInteriorBoundary()");

    std::vector<int*> sendBuffers, recvBuffers;

    MPI::Request request[myIntBoundary.boundary.size() + 
			 otherIntBoundary.boundary.size()];
    int requestCounter = 0;

    for (RankToBoundMap::iterator rankIt = myIntBoundary.boundary.begin();
	 rankIt != myIntBoundary.boundary.end(); ++rankIt) {
2132

Thomas Witkowski's avatar
Thomas Witkowski committed
2133
2134
      int nSendInt = rankIt->second.size();
      int* buffer = new int[nSendInt];
2135
      for (int i = 0; i < nSendInt; i++)
2136
	buffer[i] = (rankIt->second)[i].rankObject.elIndex;
2137
      
Thomas Witkowski's avatar
Thomas Witkowski committed
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
      sendBuffers.push_back(buffer);
      
      request[requestCounter++] =
	mpiComm.Isend(buffer, nSendInt, MPI_INT, rankIt->first, 0);
    }

    for (RankToBoundMap::iterator rankIt = otherIntBoundary.boundary.begin();
	 rankIt != otherIntBoundary.boundary.end(); ++rankIt) {
      int nRecvInt = rankIt->second.size();
      int *buffer = new int[nRecvInt];
      recvBuffers.push_back(buffer);

      request[requestCounter++] = 
	mpiComm.Irecv(buffer, nRecvInt, MPI_INT, rankIt->first, 0);
    }


    MPI::Request::Waitall(requestCounter, request);

    for (int i = 0; i < static_cast<int>(sendBuffers.size()); i++)
      delete [] sendBuffers[i];

    int bufCounter = 0;
    for (RankToBoundMap::iterator rankIt = otherIntBoundary.boundary.begin();
	 rankIt != otherIntBoundary.boundary.end(); ++rankIt) {

      TEST_EXIT(rankIt->second.size() == otherIntBoundary.boundary[rankIt->first].size())
	("Boundaries does not fit together!\n");      

      for (int i = 0; i < static_cast<int>(rankIt->second.size()); i++) {
	int elIndex1 = recvBuffers[bufCounter][i];
2169
	int elIndex2 = otherIntBoundary.boundary[rankIt->first][i].neighbourObject.elIndex;
Thomas Witkowski's avatar
Thomas Witkowski committed
2170
2171
2172
2173
2174
2175
2176
2177
2178

	TEST_EXIT(elIndex1 == elIndex2)("Wrong element index at interior boundary!\n");
      }

      delete [] recvBuffers[bufCounter++];
    }
  }
  

2179
  void ParallelDomainBase::DbgTestCommonDofs(bool printCoords)
Thomas Witkowski's avatar
Thomas Witkowski committed
2180
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
2181
    FUNCNAME("ParallelDomainBase::DbgTestCommonDofs()");
2182

2183
    // Maps to each neighbour rank an array of WorldVectors. This array contains the 
Thomas Witkowski's avatar
Thomas Witkowski committed
2184
    // coordinates of all dofs this rank shares on the interior boundary with the 
2185
2186
    // neighbour rank. A rank sends the coordinates to another rank, if it owns the
    // boundarys dof.
Thomas Witkowski's avatar
Thomas Witkowski committed
2187
    RankToCoords sendCoords;
2188
2189
2190
    // A rank receives all boundary dofs that are at its interior boundaries but are
    // not owned by the rank. This map stores for each rank the the coordinates of dofs
    // this rank expectes to receive from.
Thomas Witkowski's avatar
Thomas Witkowski committed
2191
    RankToCoords recvCoords;
2192
2193
2194

    WorldVector<double> coords;
   
Thomas Witkowski's avatar
Thomas Witkowski committed
2195
    for (RankToDofContainer::iterator it = sendDofs.begin();
2196
2197
2198
2199
	 it != sendDofs.end(); ++it) {
      for (std::vector<const DegreeOfFreedom*>::iterator dofIt = it->second.begin();
	   dofIt != it->second.end(); ++dofIt) {
	bool b = mesh->getDofIndexCoords(*dofIt, feSpace, coords);
2200
2201
2202
2203
2204
2205
2206
2207

	if (!b) {
	  std::cout << "ERROR (send): Cannot find DOF in mesh!" << std::endl;
	  std::cout << "  mpi rank = " << mpiRank 
		    << "  send to rank = " << it->first 
		    << "  dof = " << *dofIt << " = " << **dofIt << std::endl;
	  ERROR_EXIT("Should not happen!\n");
	}
2208

2209
2210
2211
2212
	sendCoords[it->first].push_back(coords);
      }
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
2213
    for (RankToDofContainer::iterator it = recvDofs.begin();
2214
2215
2216
2217
	 it != recvDofs.end(); ++it) {
      for (std::vector<const DegreeOfFreedom*>::iterator dofIt = it->second.begin();
	   dofIt != it->second.end(); ++dofIt) {
	bool b = mesh->getDofIndexCoords(*dofIt, feSpace, coords);
2218
2219
2220
2221
2222
2223
2224
2225
2226

	if (!b) {
	  std::cout << "ERROR (recv): Cannot find DOF in mesh!" << std::endl;
	  std::cout << "  mpi rank = " << mpiRank 
		    << "  send to rank = " << it->first 
		    << "  dof = " << *dofIt << " = " << **dofIt << std::endl;
	  ERROR_EXIT("Should not happen!\n");
	}

2227
2228
2229
2230
	recvCoords[it->first].push_back(coords);
      }
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
2231
2232
2233
2234
2235
2236
    std::vector<int> sendSize(mpiSize, 0);
    std::vector<int> recvSize(mpiSize, 0);
    std::vector<int> recvSizeBuffer(mpiSize, 0);
    MPI::Request request[(mpiSize - 1) * 2];
    int requestCounter = 0;

Thomas Witkowski's avatar
Thomas Witkowski committed
2237
    for (RankToCoords::iterator it = sendCoords.begin(); it != sendCoords.end(); ++it)
Thomas Witkowski's avatar
Thomas Witkowski committed
2238
2239
      sendSize[it->first] = it->second.size();

Thomas Witkowski's avatar
Thomas Witkowski committed
2240
    for (RankToCoords::iterator it = recvCoords.begin(); it != recvCoords.end(); ++it)
Thomas Witkowski's avatar
Thomas Witkowski committed
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
      recvSize[it->first] = it->second.size();

    for (int i = 0; i < mpiSize; i++) {
      if (i == mpiRank)
	continue;

      request[requestCounter++] = mpiComm.Isend(&(sendSize[i]), 1, MPI_INT, i, 0);
    }   

    for (int i = 0; i < mpiSize; i++) {
      if (i == mpiRank)
	continue;

      request[requestCounter++] = mpiComm.Irecv(&(recvSizeBuffer[i]), 1, MPI_INT, i, 0);
    }

    MPI::Request::Waitall(requestCounter, request);


    for (int i = 0; i < mpiSize; i++) {
      if (i == mpiRank)
	continue;

      if (recvSize[i] != recvSizeBuffer[i]) {
	std::cout << "Error: MPI rank " << mpiRank << " expectes to receive " 
		  << recvSize[i] << " DOFs from rank " << i << ". But this rank sends "
		  << recvSizeBuffer[i] << " DOFs!" << std::endl;

	ERROR_EXIT("Should not happen!\n");
      }
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
2273
    std::map<int, double*> sendCoordsBuffer, recvCoordsBuffer;
Thomas Witkowski's avatar
Thomas Witkowski committed
2274
2275
    requestCounter = 0;

2276
    int dimOfWorld = Global::getGeo(WORLD);
Thomas Witkowski's avatar
Thomas Witkowski committed
2277

2278
2279
    for (RankToCoords::iterator it = sendCoords.begin(); it != sendCoords.end(); ++it) {
      sendCoordsBuffer[it->first] = new double[it->second.size() * dimOfWorld];
Thomas Witkowski's avatar
Thomas Witkowski committed
2280

2281
2282
2283
2284
      for (int i = 0; i < static_cast<int>(it->second.size()); i++) 
	for (int j = 0; j < dimOfWorld; j++)
	  sendCoordsBuffer[it->first][i * dimOfWorld + j] = (it->second)[i][j];
      
Thomas Witkowski's avatar
Thomas Witkowski committed
2285
      request[requestCounter++] = mpiComm.Isend(sendCoordsBuffer[it->first],
2286
						it->second.size() * dimOfWorld,
Thomas Witkowski's avatar
Thomas Witkowski committed
2287
						MPI_DOUBLE, it->first, 0);
2288
2289
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
2290
    for (RankToCoords::iterator it = recvCoords.begin(); it != recvCoords.end(); ++it) {
2291
      recvCoordsBuffer[it->first] = new double[it->second.size() * dimOfWorld];
Thomas Witkowski's avatar
Thomas Witkowski committed
2292
2293

      request[requestCounter++] = mpiComm.Irecv(recvCoordsBuffer[it->first], 
2294
						it->second.size() * dimOfWorld,
Thomas Witkowski's avatar
Thomas Witkowski committed
2295
2296
2297
2298
2299
						MPI_DOUBLE, it->first, 0);
    }

    MPI::Request::Waitall(requestCounter, request);    

Thomas Witkowski's avatar
Thomas Witkowski committed
2300
    for (RankToCoords::iterator it = sendCoords.begin(); it != sendCoords.end(); ++it)
Thomas Witkowski's avatar
Thomas Witkowski committed
2301
2302
      delete [] sendCoordsBuffer[it->first];

2303
    double eps = 1e-13;
2304

Thomas Witkowski's avatar
Thomas Witkowski committed
2305
    for (RankToCoords::iterator it = recvCoords.begin(); it != recvCoords.end(); ++it) {
Thomas Witkowski's avatar
Thomas Witkowski committed
2306
      for (int i = 0; i < static_cast<int>(it->second.size()); i++) {
2307
2308
2309
	for (int j = 0; j < dimOfWorld; j++) {
	  bool c = fabs((it->second)[i][j] - 
			recvCoordsBuffer[it->first][i * dimOfWorld + j]) < eps;
2310
	  
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
	  if (printCoords && !c) {
	    std::cout << "[DBG] i = " << i << std::endl;
	    
	    std::cout.precision(5);
	    std::cout << "[DBG]  "
		      << "Rank " << mpiRank << " from rank " << it->first
		      << " expect coords (";
	    for (int k = 0; k < dimOfWorld; k++)
	      std::cout << (it->second)[i][k] << " , ";
	    std::cout << ")  received coords (";
	    for (int k = 0; k < dimOfWorld; k++)
	      std::cout << recvCoordsBuffer[it->first][i * dimOfWorld + k] << " , ";
	    std::cout << ")" << std::endl;
	  }
Thomas Witkowski's avatar
Thomas Witkowski committed
2325

2326
2327
	  TEST_EXIT(c)("Wrong DOFs in rank %d!\n", mpiRank);
	}
Thomas Witkowski's avatar
Thomas Witkowski committed
2328
2329
2330
      } 

      delete [] recvCoordsBuffer[it->first];
Thomas Witkowski's avatar
Thomas Witkowski committed
2331
2332
2333
    }
  }

2334

2335
  void ParallelDomainBase::printMapLocalGlobal(int rank)
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
  {    
    if (rank == -1 || mpiRank == rank) {
      std::cout << "====== DOF MAP LOCAL -> GLOBAL ====== " << std::endl;
      
      for (DofMapping::iterator it = mapLocalGlobalDOFs.begin();
	   it != mapLocalGlobalDOFs.end(); it++) {
	DegreeOfFreedom localdof = -1;
	if (mapLocalToDofIndex.count(it->first) > 0)
	  localdof = mapLocalToDofIndex[it->first];
	
	std::cout << "DOF " << it->first << " " 
		  << it->second << " " 
		  << localdof << std::endl;
	WorldVector<double> coords;
	mesh->getDofIndexCoords(it->first, feSpace, coords);
	coords.print();
	for (RankToDofContainer::iterator rankit = sendDofs.begin();
	     rankit != sendDofs.end(); ++rankit) {
	  for (DofContainer::iterator dofit = rankit->second.begin();
	       dofit != rankit->second.end(); ++dofit)
	    if (**dofit == it->first)
	      std::cout << "SEND DOF TO " << rankit->first << std::endl;	  
	}
	for (RankToDofContainer::iterator rankit = recvDofs.begin();
	     rankit != recvDofs.end(); ++rankit) {
	  for (DofContainer::iterator dofit = rankit->second.begin();
	       dofit != rankit->second.end(); ++dofit)
	    if (**dofit == it->first)
	      std::cout << "RECV DOF FROM " << rankit->first << std::endl;	  
	}
	std::cout << "------" << std::endl;
      }
    }
  }


2372
  void ParallelDomainBase::printMapPeriodic(int rank)
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
  {
    if (rank == -1 || mpiRank == rank) {
      std::cout << "====== DOF MAP PERIODIC ====== " << std::endl;

      for (PeriodicDofMap::iterator it = periodicDof.begin();
	   it != periodicDof.end(); ++it) {
	std::cout << "DOF MAP " << it->first << ": ";
	for (std::set<DegreeOfFreedom>::iterator dofit = it->second.begin();
	     dofit != it->second.end(); ++dofit)
	  std::cout << *dofit << " ";
	std::cout << std::endl;

	DegreeOfFreedom localdof;
	for (DofMapping::iterator dofIt = mapLocalGlobalDOFs.begin();
	     dofIt != mapLocalGlobalDOFs.end(); ++dofIt)
	  if (dofIt->second == it->first)
	    localdof = dofIt->first;

	WorldVector<double> coords;
	mesh->getDofIndexCoords(localdof, feSpace, coords);
	coords.print();
      }
    }
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
  }

  
  void ParallelDomainBase::printRankDofs(int rank, DofContainer& rankDofs,
					 DofContainer& rankAllDofs)
  {
    if (rank == -1 || mpiRank == rank) {
      std::cout << "====== RANK DOF INFORMATION ====== " << std::endl;

      std::cout << "  RANK OWNED DOFS: " << std::endl;
      for (DofContainer::iterator dofit = rankDofs.begin();
	   dofit != rankDofs.end(); ++dofit) {
	std::cout << "    " << **dofit << std::endl;
	WorldVector<double> coords;
	mesh->getDofIndexCoords(*dofit, feSpace, coords);
	coords.print();
      }
2413

2414
2415
2416
2417
2418
2419
2420
2421
2422
      std::cout << "  RANK ALL DOFS: " << std::endl;
      for (DofContainer::iterator dofit = rankAllDofs.begin();
	   dofit != rankAllDofs.end(); ++dofit) {
	std::cout << "    " << **dofit << std::endl;
	WorldVector<double> coords;
	mesh->getDofIndexCoords(*dofit, feSpace, coords);
	coords.print();
      }      
    }
2423
2424
2425
  }


2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
  void ParallelDomainBase::writePartitioningMesh(std::string filename)
  {
    FUNCNAME("ParallelDomainBase::writePartitioningMesh()");

    std::map<int, double> vec;    
    TraverseStack stack;
    ElInfo *elInfo = stack.traverseFirst(mesh, -1, 
					 Mesh::CALL_LEAF_EL | Mesh::FILL_COORDS);
    
    while (elInfo) {		  
      int index = elInfo->getElement()->getIndex();
      vec[index] = partitionVec[index];
      elInfo = stack.traverseNext(elInfo);
    }

    ElementFileWriter::writeFile(vec, feSpace, filename);
  }

2444
}
For faster browsing, not all history is shown. View entire blame