/* deleteObj: * Remove obj from g. * obj may belong to a subgraph of g, so we first must map * obj to its version in g. * If g is null, remove object from root graph. * If obj is a (sub)graph, close it. The g parameter is unused. * Return 0 on success, non-zero on failure. */ int deleteObj(Agraph_t * g, Agobj_t * obj) { gdata *data; if (AGTYPE(obj) == AGRAPH) { g = (Agraph_t *) obj; if (g != agroot(g)) return agclose(g); data = gData(g); if (data->lock & 1) { error(ERROR_WARNING, "Cannot delete locked graph %s", agnameof(g)); data->lock |= 2; return -1; } else return agclose(g); } /* node or edge */ if (!g) g = agroot(agraphof(obj)); if (obj) return agdelete(g, obj); else return -1; }
/* lockGraph: * Set lock so that graph g will not be deleted. * g must be a root graph. * If v > 0, set lock * If v = 0, unset lock and delete graph is necessary. * If v < 0, no op * Always return previous lock state. * Return -1 on error. */ int lockGraph(Agraph_t * g, int v) { gdata *data; int oldv; if (g != agroot(g)) { error(ERROR_WARNING, "Graph argument to lock() is not a root graph"); return -1; } data = gData(g); oldv = data->lock & 1; if (v > 0) data->lock |= 1; else if ((v == 0) && oldv) { if (data->lock & 2) agclose(g); else data->lock = 0; } return oldv; }
/* **** This test is designed to test the validity of the three add members of the PolyFit class **** Addition to the PolyFit object is tested with individual datum, gpstk::Vectors of data and **** std::vectors of data. **** These are tested against a least squares polynomial fit that was done by hand **** Please note isSingular, Solution, Degreem N and Solve were tested inderectly **** Please note, I don't know enough about Covariance to test it for the example by hand */ void xPolyFit :: addTest (void) { gpstk::PolyFit<double> AddSingle(2); gpstk::PolyFit<double> AddGVect(2); gpstk::PolyFit<double> AddSVect(2); double data[4] = {0.,2.,4.,-1.}; double time[4] = {3.,3.,4.,2.,}; gpstk::Vector<double> gData(4,0.); gData[0] = 0.; gData[1] = 2.; gData[2] = 4.; gData[3] = -1.; gpstk::Vector<double> gTime(4,0.); gTime[0] = 3.; gTime[1] = 3.; gTime[2] = 4.; gTime[3] = 2.; std::vector<double> vData(4,0.); vData[0] = 0.; vData[1] = 2.; vData[2] = 4.; vData[3] = -1.; std::vector<double> vTime(4,0.); vTime[0] = 3.; vTime[1] = 3.; vTime[2] = 4.; vTime[3] = 2.; //Done by hand gpstk::Vector<double> ExpSolution(2,0.); ExpSolution[0] = 152./59; ExpSolution[1] = 20./59; for (int i =0;i<4;i++) { AddSingle.Add(time[i],data[i]); } gpstk::Vector<double> SingleSolution = AddSingle.Solution(); CPPUNIT_ASSERT_DOUBLES_EQUAL(ExpSolution[0],SingleSolution[0],1e-6); CPPUNIT_ASSERT_DOUBLES_EQUAL(ExpSolution[1],SingleSolution[1],1e-6); CPPUNIT_ASSERT_EQUAL((unsigned) 4, AddSingle.N()); CPPUNIT_ASSERT_EQUAL((unsigned) 2, AddSingle.Degree()); CPPUNIT_ASSERT_EQUAL(false, AddSingle.isSingular()); //Add on an unweighted sample, N should increase but everything else should be the same AddSingle.Add(7.,20.,0); gpstk::Vector<double> SingleSolution2 = AddSingle.Solution(); CPPUNIT_ASSERT_DOUBLES_EQUAL(ExpSolution[0],SingleSolution2[0],1e-6); CPPUNIT_ASSERT_DOUBLES_EQUAL(ExpSolution[1],SingleSolution2[1],1e-6); CPPUNIT_ASSERT_EQUAL((unsigned) 5, AddSingle.N()); CPPUNIT_ASSERT_EQUAL((unsigned) 2, AddSingle.Degree()); CPPUNIT_ASSERT_EQUAL(false, AddSingle.isSingular()); AddGVect.Add(gTime,gData); gpstk::Vector<double> gVectSolution = AddGVect.Solution(); CPPUNIT_ASSERT_DOUBLES_EQUAL(ExpSolution[0],gVectSolution[0],1e-6); CPPUNIT_ASSERT_DOUBLES_EQUAL(ExpSolution[1],gVectSolution[1],1e-6); CPPUNIT_ASSERT_EQUAL((unsigned) 4, AddGVect.N()); CPPUNIT_ASSERT_EQUAL((unsigned) 2, AddGVect.Degree()); CPPUNIT_ASSERT_EQUAL(false, AddGVect.isSingular()); AddSVect.Add(vTime,vData); gpstk::Vector<double> sVectSolution = AddSVect.Solution(); CPPUNIT_ASSERT_DOUBLES_EQUAL(ExpSolution[0],sVectSolution[0],1e-6); CPPUNIT_ASSERT_DOUBLES_EQUAL(ExpSolution[1],sVectSolution[1],1e-6); CPPUNIT_ASSERT_EQUAL((unsigned) 4, AddSVect.N()); CPPUNIT_ASSERT_EQUAL((unsigned) 2, AddSVect.Degree()); CPPUNIT_ASSERT_EQUAL(false, AddSVect.isSingular()); }
void RepartitionFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>:: DeterminePartitionPlacement(const Matrix& A, GOVector& decomposition, GO numPartitions) const { RCP<const Map> rowMap = A.getRowMap(); RCP<const Teuchos::Comm<int> > comm = rowMap->getComm()->duplicate(); int numProcs = comm->getSize(); RCP<const Teuchos::MpiComm<int> > tmpic = rcp_dynamic_cast<const Teuchos::MpiComm<int> >(comm); TEUCHOS_TEST_FOR_EXCEPTION(tmpic == Teuchos::null, Exceptions::RuntimeError, "Cannot cast base Teuchos::Comm to Teuchos::MpiComm object."); RCP<const Teuchos::OpaqueWrapper<MPI_Comm> > rawMpiComm = tmpic->getRawMpiComm(); const Teuchos::ParameterList& pL = GetParameterList(); // maxLocal is a constant which determins the number of largest edges which are being exchanged // The idea is that we do not want to construct the full bipartite graph, but simply a subset of // it, which requires less communication. By selecting largest local edges we hope to achieve // similar results but at a lower cost. const int maxLocal = pL.get<int>("repartition: remap num values"); const int dataSize = 2*maxLocal; ArrayRCP<GO> decompEntries; if (decomposition.getLocalLength() > 0) decompEntries = decomposition.getDataNonConst(0); // Step 1: Sort local edges by weight // Each edge of a bipartite graph corresponds to a triplet (i, j, v) where // i: processor id that has some piece of part with part_id = j // j: part id // v: weight of the edge // We set edge weights to be the total number of nonzeros in rows on this processor which // correspond to this part_id. The idea is that when we redistribute matrix, this weight // is a good approximation of the amount of data to move. // We use two maps, original which maps a partition id of an edge to the corresponding weight, // and a reverse one, which is necessary to sort by edges. std::map<GO,GO> lEdges; for (LO i = 0; i < decompEntries.size(); i++) lEdges[decompEntries[i]] += A.getNumEntriesInLocalRow(i); // Reverse map, so that edges are sorted by weight. // This results in multimap, as we may have edges with the same weight std::multimap<GO,GO> revlEdges; for (typename std::map<GO,GO>::const_iterator it = lEdges.begin(); it != lEdges.end(); it++) revlEdges.insert(std::make_pair(it->second, it->first)); // Both lData and gData are arrays of data which we communicate. The data is stored // in pairs, so that data[2*i+0] is the part index, and data[2*i+1] is the corresponding edge weight. // We do not store processor id in data, as we can compute that by looking on the offset in the gData. Array<GO> lData(dataSize, -1), gData(numProcs * dataSize); int numEdges = 0; for (typename std::multimap<GO,GO>::reverse_iterator rit = revlEdges.rbegin(); rit != revlEdges.rend() && numEdges < maxLocal; rit++) { lData[2*numEdges+0] = rit->second; // part id lData[2*numEdges+1] = rit->first; // edge weight numEdges++; } // Step 2: Gather most edges // Each processors contributes maxLocal edges by providing maxLocal pairs <part id, weight>, which is of size dataSize MPI_Datatype MpiType = MpiTypeTraits<GO>::getType(); MPI_Allgather(static_cast<void*>(lData.getRawPtr()), dataSize, MpiType, static_cast<void*>(gData.getRawPtr()), dataSize, MpiType, *rawMpiComm); // Step 3: Construct mapping // Construct the set of triplets std::vector<Triplet<int,int> > gEdges(numProcs * maxLocal); size_t k = 0; for (LO i = 0; i < gData.size(); i += 2) { GO part = gData[i+0]; GO weight = gData[i+1]; if (part != -1) { // skip nonexistent edges gEdges[k].i = i/dataSize; // determine the processor by its offset (since every processor sends the same amount) gEdges[k].j = part; gEdges[k].v = weight; k++; } } gEdges.resize(k); // Sort edges by weight // NOTE: compareTriplets is actually a reverse sort, so the edges weight is in decreasing order std::sort(gEdges.begin(), gEdges.end(), compareTriplets<int,int>); // Do matching std::map<int,int> match; std::vector<char> matchedRanks(numProcs, 0), matchedParts(numProcs, 0); int numMatched = 0; for (typename std::vector<Triplet<int,int> >::const_iterator it = gEdges.begin(); it != gEdges.end(); it++) { GO rank = it->i; GO part = it->j; if (matchedRanks[rank] == 0 && matchedParts[part] == 0) { matchedRanks[rank] = 1; matchedParts[part] = 1; match[part] = rank; numMatched++; } } GetOStream(Statistics0) << "Number of unassigned paritions before cleanup stage: " << (numPartitions - numMatched) << " / " << numPartitions << std::endl; // Step 4: Assign unassigned partitions // We do that through random matching for remaining partitions. Not all part numbers are valid, but valid parts are a subset of [0, numProcs). // The reason it is done this way is that we don't need any extra communication, as we don't need to know which parts are valid. for (int part = 0, matcher = 0; part < numProcs; part++) if (match.count(part) == 0) { // Find first non-matched rank while (matchedRanks[matcher]) matcher++; match[part] = matcher++; } // Step 5: Permute entries in the decomposition vector for (LO i = 0; i < decompEntries.size(); i++) decompEntries[i] = match[decompEntries[i]]; }
// Returns a reference to a gnssSatTypeValue object after differencing the // data type values given in the diffTypes field with respect to reference // satellite data. // // @param gData Data object holding the data. // satTypeValueMap& NablaOp::Process(satTypeValueMap& gData) throw(ProcessingException) { try { double maxElevation(0.0); // If configured to do so, let's look for reference satellite if (lookReferenceSat) { // Loop through all satellites in reference station data set, // looking for reference satellite satTypeValueMap::iterator it; for (it = gData.begin(); it != gData.end(); ++it) { // The satellite with the highest elevation will usually be // the reference satellite if ( gData((*it).first)(TypeID::elevation) > maxElevation ) { refSat = (*it).first; maxElevation = gData((*it).first)(TypeID::elevation); } } } // End of 'if (lookReferenceSat)' // We will use reference satellite data as reference data satTypeValueMap refData(gData.extractSatID(refSat)); // We must remove reference satellite data from data set gData.removeSatID(refSat); SatIDSet satRejectedSet; // Loop through all the satellites in station data set satTypeValueMap::iterator it; for (it = gData.begin(); it != gData.end(); ++it) { // We must compute the difference for all types in // 'diffTypes' set TypeIDSet::const_iterator itType; for(itType = diffTypes.begin(); itType != diffTypes.end(); ++itType) { double value1(0.0); double value2(0.0); try { // Let's try to compute the difference value1 = gData((*it).first)(*itType); value2 = refData(refSat)(*itType); // Get difference into data structure gData((*it).first)((*itType)) = value1 - value2; } catch(...) { // If some value is missing, then schedule this satellite // for removal satRejectedSet.insert( (*it).first ); continue; } } // End of 'for(itType = diffTypes.begin(); ...' } // End of 'for (it = gData.begin(); it != gData.end(); ++it)' // Remove satellites with missing data gData.removeSatID(satRejectedSet); return gData; } catch(Exception& u) { // Throw an exception if something unexpected happens ProcessingException e( getClassName() + ":" + u.what() ); GPSTK_THROW(e); } } // End of method 'NablaOp::Process()'
/* Returns a reference to a satTypeValueMap object after differencing * data type values given in 'diffTypes' field with respect to * reference station data in 'refData' field. * * @param gData Data object holding the data. */ satTypeValueMap& DeltaOp::Process(satTypeValueMap& gData) throw(ProcessingException) { try { SatIDSet satRejectedSet; // Loop through all the satellites in the station data set satTypeValueMap::iterator it; for (it = gData.begin(); it != gData.end(); ++it) { // Let's find if the same satellite is present in refData satTypeValueMap::const_iterator itref; itref = refData.find((*it).first); // If we found the satellite, let's proceed with the differences if (itref != refData.end()) { // We must compute the difference for all the types in // 'diffTypes' set TypeIDSet::const_iterator itType; for( itType = diffTypes.begin(); itType != diffTypes.end(); ++itType ) { double value1(0.0); double value2(0.0); try { // Let's try to compute the difference value1 = gData((*it).first)(*itType); value2 = refData((*it).first)(*itType); // Get difference into data structure gData((*it).first)((*itType)) = value1 - value2; } catch(...) { // If some value is missing, then schedule this // satellite for removal satRejectedSet.insert( (*it).first ); // Skip this value if problems arise continue; } } // End of 'for( itType = diffTypes.begin(); ...' // update CSFlag if(updateCSFlag) { double CSValue1 = gData[it->first][TypeID::CSL1] +refData[it->first][TypeID::CSL1]; double CSValue2 = gData[it->first][TypeID::CSL2] +refData[it->first][TypeID::CSL2]; gData[it->first][TypeID::CSL1] = (CSValue1 > 0.0) ? 1.0 : 0.0; gData[it->first][TypeID::CSL2] = (CSValue2 > 0.0) ? 1.0 : 0.0; } // End of 'if(updateCSFlag)' } else { // If we didn't find the same satellite in both sets, mark // it for deletion satRejectedSet.insert( (*it).first ); continue; } // End of 'if (itref != refData.end())' } // End of 'for (it = gData.begin(); it != gData.end(); ++it)' // If ordered so, delete the missing satellites if (deleteMissingSats) { gData.removeSatID(satRejectedSet); } return gData; } catch(Exception& u) { // Throw an exception if something unexpected happens ProcessingException e( getClassName() + ":" + u.what() ); GPSTK_THROW(e); } } // End of method 'DeltaOp::Process()'
/* Returns a reference to a satTypeValueMap object after differencing * data type values given in 'diffTypes' field with respect to * reference station data in 'refData' field. * * @param gData Data object holding the data. */ satTypeValueMap& DoubleOp::Process(satTypeValueMap& gData) throw(ProcessingException) { try { // First, we get difference data between two stations sdStations.Process(gData); // Second, we should check if the elevation of the ref satellite // is useable, if not, pick up a new ref satellite with the highest // elevation bool lookHigestElevation = true; if(refSatID.isValid()) { satTypeValueMap::iterator it = gData.find(refSatID); if(it!=gData.end()) { double elev = gData(it->first)(TypeID::elevation); if(elev > refSatMinElev) lookHigestElevation = false; } } if(lookHigestElevation) { double maxElevation(0.0); // Loop through all satellites in reference station data set, // looking for reference satellite satTypeValueMap::iterator it; for (it = gData.begin(); it != gData.end(); ++it) { // The satellite with the highest elevation will usually be // the reference satellite if ( gData((*it).first)(TypeID::elevation) > maxElevation ) { refSatID = (*it).first; maxElevation = gData((*it).first)(TypeID::elevation); } } // end for } // End 'if(lookHigestElevation)' // At last, We get the final DD data sdSatellites.setRefSat(refSatID); sdSatellites.Process(gData); return gData; } catch(Exception& u) { // Throw an exception if something unexpected happens ProcessingException e( getClassName() + ":" + StringUtils::asString( getIndex() ) + ":" + u.what() ); GPSTK_THROW(e); } } // End of method 'DoubleOp::Process()'