ParallelDomainProblem.cc 42.4 KB
Newer Older
Thomas Witkowski's avatar
Thomas Witkowski committed
1001
1002
1003
1004
1005

      break;
    case 2:
      if (el->getFirstChild()) {
	addAllEdgeDOFs(el->getFirstChild(), 0, dofs);
Thomas Witkowski's avatar
Thomas Witkowski committed
1006
	addAllEdgeDOFs(el->getSecondChild(), 1, dofs);
Thomas Witkowski's avatar
Thomas Witkowski committed
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
      } else {
	addThisEdge = true;
      } 

      break;
    default:
      ERROR_EXIT("Should never happen!\n");
    }

    if (addThisEdge) {
Thomas Witkowski's avatar
Thomas Witkowski committed
1017
      ElementDofIterator elDofIter(feSpace, true);
Thomas Witkowski's avatar
Thomas Witkowski committed
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
      elDofIter.reset(el);
      do {
	if (elDofIter.getCurrentPos() == 1 &&
	    elDofIter.getCurrentElementPos() == ithEdge) {
	  dofs.push_back(elDofIter.getDofPtr());
	}
      } while(elDofIter.next());      
    }
  }


  void ParallelDomainBase::createDOFMemberInfo(DofToPartitions& partitionDOFs,
					       DofContainer& rankDOFs,
					       DofToRank& boundaryDOFs)
1032
  {
1033
    // === Determine to each dof the set of partitions the dof belongs to. ===
1034

Thomas Witkowski's avatar
Thomas Witkowski committed
1035
    ElementDofIterator elDofIt(feSpace);
1036

1037
1038
1039
1040
    TraverseStack stack;
    ElInfo *elInfo = stack.traverseFirst(mesh, -1, Mesh::CALL_LEAF_EL);
    while (elInfo) {
      Element *element = elInfo->getElement();
1041
1042
1043
1044
1045
1046
      elDofIt.reset(element);
      do {
	// Determine to each dof the partition(s) it corresponds to.
	partitionDOFs[elDofIt.getDofPtr()].insert(partitionVec[element->getIndex()]);
      } while(elDofIt.next());

1047
1048
1049
      elInfo = stack.traverseNext(elInfo);
    }

1050

1051
    // === Determine the set of ranks dofs and the dofs ownership at the boundary. ===
1052

1053
    // iterate over all DOFs
Thomas Witkowski's avatar
Thomas Witkowski committed
1054
    for (DofToPartitions::iterator it = partitionDOFs.begin();
1055
1056
	 it != partitionDOFs.end();
	 ++it) {
1057
1058

      // iterate over all partition the current DOF is part of.
1059
1060
1061
      for (std::set<int>::iterator itpart1 = it->second.begin();
	   itpart1 != it->second.end();
	   ++itpart1) {
1062

1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
	if (*itpart1 == mpiRank) {
	  if (it->second.size() == 1) {
	    rankDOFs.push_back(it->first);
	  } else {	    
	    // This dof is at the ranks boundary. It is owned by the rank only if
	    // the rank number is the highest of all ranks containing this dof.

	    bool insert = true;
	    int highestRank = mpiRank;
	    for (std::set<int>::iterator itpart2 = it->second.begin();
		 itpart2 != it->second.end();
		 ++itpart2) {
	      if (*itpart2 > mpiRank)
		insert = false;

	      if (*itpart2 > highestRank)
		highestRank = *itpart2;
	    }

	    if (insert)
	      rankDOFs.push_back(it->first);

	    boundaryDOFs[it->first] = highestRank;
	  }

	  break;
	}
1090

1091
1092
      }
    }
Thomas Witkowski's avatar
Thomas Witkowski committed
1093
1094

    sort(rankDOFs.begin(), rankDOFs.end(), cmpDofsByValue);
1095
1096
1097
  }


Thomas Witkowski's avatar
Thomas Witkowski committed
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
  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();
      if (mpiRank == 0) std::cout << "EL = " << el->getIndex() << std::endl;
      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();
      if (mpiRank == 0) std::cout << "EL = " << el->getIndex() << std::endl;
      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
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
  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) {
      int nSendInt = rankIt->second.size();
      int* buffer = new int[nSendInt];
      for (int i = 0; i < nSendInt; i++)
	buffer[i] = (rankIt->second)[i].rankObject.el->getIndex();
      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];
	int elIndex2 = otherIntBoundary.boundary[rankIt->first][i].neighbourObject.el->getIndex();

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

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

  void ParallelDomainBase::DbgTestCommonDofs()
Thomas Witkowski's avatar
Thomas Witkowski committed
1208
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
1209
    FUNCNAME("ParallelDomainBase::DbgTestCommonDofs()");
1210

Thomas Witkowski's avatar
Thomas Witkowski committed
1211
1212
    // Maps to each neighbour rank an array of WorldVectors. This array contain the 
    // coordinates of all dofs this rank shares on the interior boundary with the 
1213
1214
    // neighbour rank. A rank sends the coordinates to another rank, if it owns the
    // boundarys dof.
Thomas Witkowski's avatar
Thomas Witkowski committed
1215
    RankToCoords sendCoords;
1216
1217
1218
    // 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
1219
    RankToCoords recvCoords;
1220
1221
1222

    WorldVector<double> coords;
   
Thomas Witkowski's avatar
Thomas Witkowski committed
1223
    for (RankToDofContainer::iterator it = sendDofs.begin();
1224
1225
1226
1227
1228
1229
1230
1231
1232
	 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);
	TEST_EXIT(b)("Cannot find DOF in mesh!\n");
	sendCoords[it->first].push_back(coords);
      }
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
1233
    for (RankToDofContainer::iterator it = recvDofs.begin();
1234
1235
1236
1237
1238
1239
1240
1241
1242
	 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);
	TEST_EXIT(b)("Cannot find DOF in mesh!\n");
	recvCoords[it->first].push_back(coords);
      }
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
1243
1244
1245
1246
1247
1248
    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
1249
    for (RankToCoords::iterator it = sendCoords.begin(); it != sendCoords.end(); ++it)
Thomas Witkowski's avatar
Thomas Witkowski committed
1250
1251
      sendSize[it->first] = it->second.size();

Thomas Witkowski's avatar
Thomas Witkowski committed
1252
    for (RankToCoords::iterator it = recvCoords.begin(); it != recvCoords.end(); ++it)
Thomas Witkowski's avatar
Thomas Witkowski committed
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
      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
1285
    std::map<int, double*> sendCoordsBuffer, recvCoordsBuffer;
Thomas Witkowski's avatar
Thomas Witkowski committed
1286
1287
    requestCounter = 0;

Thomas Witkowski's avatar
Thomas Witkowski committed
1288
    for (RankToCoords::iterator it = sendCoords.begin(); it != sendCoords.end(); ++it) {
Thomas Witkowski's avatar
Thomas Witkowski committed
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
      sendCoordsBuffer[it->first] = new double[it->second.size() * 2];

      for (int i = 0; i < static_cast<int>(it->second.size()); i++) {
	sendCoordsBuffer[it->first][i * 2] = (it->second)[i][0];
	sendCoordsBuffer[it->first][i * 2 + 1] = (it->second)[i][1];
      }

      request[requestCounter++] = mpiComm.Isend(sendCoordsBuffer[it->first],
						it->second.size() * 2,
						MPI_DOUBLE, it->first, 0);
1299
1300
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
1301
    for (RankToCoords::iterator it = recvCoords.begin(); it != recvCoords.end(); ++it) {
Thomas Witkowski's avatar
Thomas Witkowski committed
1302
1303
1304
1305
1306
1307
1308
1309
1310
      recvCoordsBuffer[it->first] = new double[it->second.size() * 2];

      request[requestCounter++] = mpiComm.Irecv(recvCoordsBuffer[it->first], 
						it->second.size() * 2,
						MPI_DOUBLE, it->first, 0);
    }

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

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

Thomas Witkowski's avatar
Thomas Witkowski committed
1314
    for (RankToCoords::iterator it = recvCoords.begin(); it != recvCoords.end(); ++it) {
Thomas Witkowski's avatar
Thomas Witkowski committed
1315
1316
      for (int i = 0; i < static_cast<int>(it->second.size()); i++) {

Thomas Witkowski's avatar
Thomas Witkowski committed
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
	std::cout << "[DBG] i = " << i << std::endl;

	std::cout.precision(4);
	std::cout << "[DBG]  "
		  << "Rank " << mpiRank << " from rank " << it->first
		  << " expect coords (" 
		  << (it->second)[i][0] << " , " <<  (it->second)[i][1] 
		  << ")  received coords ("
	          << recvCoordsBuffer[it->first][i * 2] << " , " 
		  << recvCoordsBuffer[it->first][i * 2 + 1] << ")" << std::endl;

	bool c0 = (it->second)[i][0] == recvCoordsBuffer[it->first][i * 2];
	bool c1 = (it->second)[i][1] == recvCoordsBuffer[it->first][i * 2 + 1];

	TEST_EXIT(c0 && c1)("Wrong DOFs!\n");
Thomas Witkowski's avatar
Thomas Witkowski committed
1332
1333
1334
      } 

      delete [] recvCoordsBuffer[it->first];
Thomas Witkowski's avatar
Thomas Witkowski committed
1335
1336
1337
1338
    }
  }


1339
1340
1341
1342
1343
1344
1345
1346
  ParallelDomainScal::ParallelDomainScal(const std::string& name,
					 ProblemScal *problem,
					 ProblemInstatScal *problemInstat)
    : ParallelDomainBase(name, 
			 problem, 
			 problemInstat, 
			 problem->getFESpace(),
			 problem->getRefinementManager()),
1347
      probScal(problem)
1348
  {
1349
    info = problem->getInfo();
1350
1351
  }

1352
  void ParallelDomainScal::initParallelization(AdaptInfo *adaptInfo)
1353
  {
1354
1355
1356
    FUNCNAME("ParallelDomainScal::initParallelization()");

    ParallelDomainBase::initParallelization(adaptInfo);
1357

Thomas Witkowski's avatar
Thomas Witkowski committed
1358
1359
1360
1361
1362
    DOFMatrix* m = probScal->getSystemMatrix();

    TEST_EXIT(m)("No DOF Matrix!\n");

    m->setIsRankDOF(isRankDOF);
1363
1364
  }

1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
  void ParallelDomainScal::solve()
  {
    FUNCNAME("ParallelDomainScal::solve()");

#ifdef _OPENMP
    double wtime = omp_get_wtime();
#endif
    clock_t first = clock();

    fillPetscMatrix(probScal->getSystemMatrix(), probScal->getRHS());      
    solvePetscMatrix(probScal->getSolution());

#ifdef _OPENMP
    INFO(info, 8)("solution of discrete system needed %.5f seconds system time / %.5f seconds wallclock time\n",
		   TIME_USED(first, clock()),
		   omp_get_wtime() - wtime);
#else
    INFO(info, 8)("solution of discrete system needed %.5f seconds\n",
		   TIME_USED(first, clock()));
#endif
  }

  Flag ParallelDomainScal::oneIteration(AdaptInfo *adaptInfo, Flag toDo)
1388
  {
1389
    FUNCNAME("ParallelDomainScal::oneIteration()");
1390

Thomas Witkowski's avatar
Thomas Witkowski committed
1391
1392
    Flag flag =  dynamic_cast<StandardProblemIteration*>(iterationIF)->
      buildAndAdapt(adaptInfo, toDo);
1393

1394
1395
    if (toDo.isSet(SOLVE))
      solve();
1396

1397
    if (toDo.isSet(SOLVE_RHS))
Thomas Witkowski's avatar
Thomas Witkowski committed
1398
      ERROR_EXIT("Not yet implemented!\n");
1399

Thomas Witkowski's avatar
Thomas Witkowski committed
1400
1401
    if (toDo.isSet(ESTIMATE))
      iterationIF->getProblem()->estimate(adaptInfo);
1402

1403
    return flag;
1404
  }
1405
1406

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