ParallelDomainBase.cc 75 KB
Newer Older
2001
2002
2003
2004
2005

      SerUtil::deserialize(in, dof);
      SerUtil::deserialize(in, dofSet);

      data[dof] = dofSet;
2006
    }    
2007
2008
  }
  
2009
  void ParallelDomainBase::dbgCreateElementMap(ElementIdxToDofs &elMap)
Thomas Witkowski's avatar
Thomas Witkowski committed
2010
  {
2011
    FUNCNAME("ParallelDomainBase::dbgCreateElementMap()");
Thomas Witkowski's avatar
Thomas Witkowski committed
2012

2013
    int dim = mesh->getDim();
Thomas Witkowski's avatar
Thomas Witkowski committed
2014
2015
2016
2017
2018
2019
    elMap.clear();
       
    TraverseStack stack;
    ElInfo *elInfo = stack.traverseFirst(mesh, -1, Mesh::CALL_LEAF_EL);
    while (elInfo) {
      Element *el = elInfo->getElement();
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
      switch (dim) {
      case 2:
	orderDofs(el->getDOF(0), el->getDOF(1), el->getDOF(2), elMap[el->getIndex()]);
	break;
      case 3:
	orderDofs(el->getDOF(0), el->getDOF(1), el->getDOF(2), el->getDOF(3), elMap[el->getIndex()]);
	break;
      default:
	ERROR_EXIT("What is this?\n");
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
2030
2031
2032
2033
      elInfo = stack.traverseNext(elInfo);
    }
  }

2034
  void ParallelDomainBase::dbgTestElementMap(ElementIdxToDofs &elMap)
Thomas Witkowski's avatar
Thomas Witkowski committed
2035
  {
2036
    FUNCNAME("ParallelDomainbase::dbgTestElementMap()");
Thomas Witkowski's avatar
Thomas Witkowski committed
2037

2038
2039
2040
    int dim = mesh->getDim();
    int nVertex = Global::getGeo(VERTEX, dim);
    DofContainer vec(nVertex);
Thomas Witkowski's avatar
Thomas Witkowski committed
2041
2042
2043
2044
2045

    TraverseStack stack;
    ElInfo *elInfo = stack.traverseFirst(mesh, -1, Mesh::CALL_LEAF_EL);
    while (elInfo) {
      Element *el = elInfo->getElement();
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058

      switch (dim) {
      case 2:
	orderDofs(el->getDOF(0), el->getDOF(1), el->getDOF(2), vec);
	break;
      case 3:
	orderDofs(el->getDOF(0), el->getDOF(1), el->getDOF(2), el->getDOF(3), vec);
	break;
      default:
	ERROR_EXIT("What is this?\n");
      }

      for (int i = 0; i < nVertex; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
2059
2060
2061
2062
2063
	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: ";
2064
	  for (int j = 0; j < nVertex; j++)
Thomas Witkowski's avatar
Thomas Witkowski committed
2065
2066
2067
2068
	    std::cout << elMap[el->getIndex()][j] << " = " 
		      << *(elMap[el->getIndex()][j]) << "  ";
	  std::cout << std::endl;
	  std::cout << "[DBG " << mpiRank << "]: New numeration is:  ";
2069
	  for (int j = 0; j < nVertex; j++)
Thomas Witkowski's avatar
Thomas Witkowski committed
2070
2071
2072
2073
2074
2075
2076
2077
2078
	    std::cout << vec[j] << " = "  << *(vec[j]) << "  ";
	  std::cout << std::endl;
	  ERROR_EXIT("WRONG NEW DOF NUMERATION!\n");
	}
      }
      elInfo = stack.traverseNext(elInfo);
    }
  }

2079
  void ParallelDomainBase::dbgTestInteriorBoundary()
Thomas Witkowski's avatar
Thomas Witkowski committed
2080
  {
2081
    FUNCNAME("ParallelDomainBase::dbgTestInteriorBoundary()");
Thomas Witkowski's avatar
Thomas Witkowski committed
2082
2083
2084
2085
2086
2087
2088
2089
2090

    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) {
2091

Thomas Witkowski's avatar
Thomas Witkowski committed
2092
2093
      int nSendInt = rankIt->second.size();
      int* buffer = new int[nSendInt];
2094
      for (int i = 0; i < nSendInt; i++)
2095
	buffer[i] = (rankIt->second)[i].rankObj.elIndex;
2096
      
Thomas Witkowski's avatar
Thomas Witkowski committed
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
      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];
2128
	int elIndex2 = otherIntBoundary.boundary[rankIt->first][i].neighObj.elIndex;
Thomas Witkowski's avatar
Thomas Witkowski committed
2129
2130
2131
2132
2133
2134
2135
2136
2137

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

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

2138
  void ParallelDomainBase::dbgTestCommonDofs(bool printCoords)
Thomas Witkowski's avatar
Thomas Witkowski committed
2139
  {
2140
    FUNCNAME("ParallelDomainBase::dbgTestCommonDofs()");
2141

2142
    // Maps to each neighbour rank an array of WorldVectors. This array contains the 
Thomas Witkowski's avatar
Thomas Witkowski committed
2143
    // coordinates of all dofs this rank shares on the interior boundary with the 
2144
2145
    // neighbour rank. A rank sends the coordinates to another rank, if it owns the
    // boundarys dof.
Thomas Witkowski's avatar
Thomas Witkowski committed
2146
    RankToCoords sendCoords;
2147
2148
2149
    // 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
2150
    RankToCoords recvCoords;
2151
2152
2153

    WorldVector<double> coords;
   
Thomas Witkowski's avatar
Thomas Witkowski committed
2154
    for (RankToDofContainer::iterator it = sendDofs.begin();
2155
2156
2157
2158
	 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);
2159
2160
2161
2162
2163
2164
2165
2166

	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");
	}
2167

2168
2169
2170
2171
	sendCoords[it->first].push_back(coords);
      }
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
2172
    for (RankToDofContainer::iterator it = recvDofs.begin();
2173
2174
2175
2176
	 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);
2177
2178
2179
2180
2181
2182
2183
2184
2185

	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");
	}

2186
2187
2188
2189
	recvCoords[it->first].push_back(coords);
      }
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
2190
2191
2192
2193
2194
2195
    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
2196
    for (RankToCoords::iterator it = sendCoords.begin(); it != sendCoords.end(); ++it)
Thomas Witkowski's avatar
Thomas Witkowski committed
2197
2198
      sendSize[it->first] = it->second.size();

Thomas Witkowski's avatar
Thomas Witkowski committed
2199
    for (RankToCoords::iterator it = recvCoords.begin(); it != recvCoords.end(); ++it)
Thomas Witkowski's avatar
Thomas Witkowski committed
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
      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
2232
    std::map<int, double*> sendCoordsBuffer, recvCoordsBuffer;
Thomas Witkowski's avatar
Thomas Witkowski committed
2233
2234
    requestCounter = 0;

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

2237
2238
    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
2239

2240
2241
2242
2243
      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
2244
      request[requestCounter++] = mpiComm.Isend(sendCoordsBuffer[it->first],
2245
						it->second.size() * dimOfWorld,
Thomas Witkowski's avatar
Thomas Witkowski committed
2246
						MPI_DOUBLE, it->first, 0);
2247
2248
    }

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

      request[requestCounter++] = mpiComm.Irecv(recvCoordsBuffer[it->first], 
2253
						it->second.size() * dimOfWorld,
Thomas Witkowski's avatar
Thomas Witkowski committed
2254
2255
2256
2257
2258
						MPI_DOUBLE, it->first, 0);
    }

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

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

2262
    double eps = 1e-13;
2263

Thomas Witkowski's avatar
Thomas Witkowski committed
2264
    for (RankToCoords::iterator it = recvCoords.begin(); it != recvCoords.end(); ++it) {
Thomas Witkowski's avatar
Thomas Witkowski committed
2265
      for (int i = 0; i < static_cast<int>(it->second.size()); i++) {
2266
2267
2268
	for (int j = 0; j < dimOfWorld; j++) {
	  bool c = fabs((it->second)[i][j] - 
			recvCoordsBuffer[it->first][i * dimOfWorld + j]) < eps;
2269
	  
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
	  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
2284

2285
2286
	  TEST_EXIT(c)("Wrong DOFs in rank %d!\n", mpiRank);
	}
Thomas Witkowski's avatar
Thomas Witkowski committed
2287
2288
2289
      } 

      delete [] recvCoordsBuffer[it->first];
Thomas Witkowski's avatar
Thomas Witkowski committed
2290
2291
2292
    }
  }

2293

2294
  void ParallelDomainBase::printMapLocalGlobal(int rank)
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
  {    
    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;
      }
    }
  }


2331
  void ParallelDomainBase::printMapPeriodic(int rank)
2332
  {
2333
2334
    FUNCNAME("ParallelDomainBase::printMapPeriodic()");

2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
    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;

2346
	DegreeOfFreedom localdof = -1;
2347
2348
2349
2350
2351
	for (DofMapping::iterator dofIt = mapLocalGlobalDOFs.begin();
	     dofIt != mapLocalGlobalDOFs.end(); ++dofIt)
	  if (dofIt->second == it->first)
	    localdof = dofIt->first;

2352
2353
	TEST_EXIT(localdof != -1)("There is something wrong!\n");

2354
2355
2356
2357
2358
	WorldVector<double> coords;
	mesh->getDofIndexCoords(localdof, feSpace, coords);
	coords.print();
      }
    }
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
  }

  
  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();
      }
2376

2377
2378
2379
2380
2381
2382
2383
2384
2385
      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();
      }      
    }
2386
2387
2388
  }


2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
  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);
  }

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