From bf0532867d4b5d7e891a45609d496d213fb3ce84 Mon Sep 17 00:00:00 2001 From: StefanoPetrilli Date: Sat, 25 Jun 2022 08:48:49 -0500 Subject: [PATCH] Functions in different files --- ...mEdgesLinearSearchMesgBndlSmallMateCMP.cpp | 168 ++---------------- .../impl/aggregator/computeCandidateMate.cpp | 69 +++++++ amgprec/impl/aggregator/findOwnerOfGhost.cpp | 52 ++++++ amgprec/impl/aggregator/isAlreadyMatched.cpp | 42 +++++ samples/advanced/pdegen/runs/amg_pde3d.inp | 2 +- 5 files changed, 177 insertions(+), 156 deletions(-) create mode 100644 amgprec/impl/aggregator/computeCandidateMate.cpp create mode 100644 amgprec/impl/aggregator/findOwnerOfGhost.cpp create mode 100644 amgprec/impl/aggregator/isAlreadyMatched.cpp diff --git a/amgprec/impl/aggregator/algoDistEdgeApproxDomEdgesLinearSearchMesgBndlSmallMateCMP.cpp b/amgprec/impl/aggregator/algoDistEdgeApproxDomEdgesLinearSearchMesgBndlSmallMateCMP.cpp index f747f1fc..ea0e460f 100644 --- a/amgprec/impl/aggregator/algoDistEdgeApproxDomEdgesLinearSearchMesgBndlSmallMateCMP.cpp +++ b/amgprec/impl/aggregator/algoDistEdgeApproxDomEdgesLinearSearchMesgBndlSmallMateCMP.cpp @@ -1,6 +1,10 @@ #include "MatchBoxPC.h" #include #include +#include "isAlreadyMatched.cpp" +#include "findOwnerOfGhost.cpp" +#include "computeCandidateMate.cpp" + // *********************************************************************** // // MatchboxP: A C++ library for approximate weighted matching @@ -238,9 +242,9 @@ void dalgoDistEdgeApproxDomEdgesLinearSearchMesgBndlSmallMateCMP( //Initialize the locks //TODO this can be executed as task in parallel with other unparallelizable tasks //TODO destroy the locks -#pragma omp for schedule(static) - for(int i = 0; i < NLVer; i++) - omp_init_lock(&MateLock[i]); +//#pragma omp for schedule(static) +// for(int i = 0; i < NLVer; i++) +// omp_init_lock(&MateLock[i]); // TODO comments about the reduction #pragma omp for reduction(+ : numGhostEdges) @@ -752,12 +756,14 @@ void dalgoDistEdgeApproxDomEdgesLinearSearchMesgBndlSmallMateCMP( //TODO is it possible to lock without a critical region? //TODO there must be a more elegant and efficient way to do this + /* while(true) { if (omp_test_lock(&MateLock[v - StartIndex])) { if (omp_test_lock(&MateLock[w - StartIndex])) break; else omp_unset_lock(&MateLock[v - StartIndex]); } } + */ if ((w < StartIndex) || (w > EndIndex)) { //A ghost @@ -815,8 +821,8 @@ void dalgoDistEdgeApproxDomEdgesLinearSearchMesgBndlSmallMateCMP( } //End of if(CandidateMate(w) = v } //End of Else - omp_unset_lock(&MateLock[v - StartIndex]); - omp_unset_lock(&MateLock[w - StartIndex]); + //omp_unset_lock(&MateLock[v - StartIndex]); + //omp_unset_lock(&MateLock[w - StartIndex]); } //End of if(w >=0) else { @@ -859,7 +865,7 @@ void dalgoDistEdgeApproxDomEdgesLinearSearchMesgBndlSmallMateCMP( #pragma omp critical(innerProcessMatched) { - while(!omp_test_lock(&MateLock[u - StartIndex])); + //while(!omp_test_lock(&MateLock[u - StartIndex])); if (candidateMate[NLVer + Ghost2LocalMap[v]] == u) candidateMate[NLVer + Ghost2LocalMap[v]] = -1; @@ -887,7 +893,7 @@ void dalgoDistEdgeApproxDomEdgesLinearSearchMesgBndlSmallMateCMP( msgInd++; } //End of If( v != Mate[u] ) - omp_unset_lock(&MateLock[u - StartIndex]); + //omp_unset_lock(&MateLock[u - StartIndex]); } //End of critical region } //End of Else //A Ghost Vertex @@ -1637,154 +1643,6 @@ void dalgoDistEdgeApproxDomEdgesLinearSearchMesgBndlSmallMateCMP( //MPI_Barrier(comm); } //End of algoDistEdgeApproxDomEdgesLinearSearchMesgBndlSmallMate - -///Find the owner of a ghost node: -inline MilanInt findOwnerOfGhost(MilanLongInt vtxIndex, MilanLongInt *mVerDistance, - MilanInt myRank, MilanInt numProcs) { - //MilanLongInt Size = mVerDistance.size(); - MilanLongInt mStartInd = mVerDistance[myRank]; - MilanInt Start = 0; - MilanInt End = numProcs; - MilanInt Current = 0; - -#if 0 - if ( vtxIndex < mStartInd ) - End = myRank; - else - Start = myRank; -#endif - - while ( Start <= End ) { - Current = (End + Start)/2; - //CASE-1: - if ( mVerDistance[Current] == vtxIndex ) { - while ( mVerDistance[Current+1] == vtxIndex ) { - Current++; - if ( Current == numProcs ) - return (-1); - } - return (Current); - } - else { //CASE 2: - if ( mVerDistance[Current] > vtxIndex ) - End = Current - 1; - else //CASE 3: - Start = Current + 1; - } - } //End of While() - if ( Current == 0 ) - return (Current); - else { - if ( mVerDistance[Current] > vtxIndex ) - return (Current-1); - else - return (Current); - } //End of else - return (-1); //It should not reach here! -} //End of findOwnerOfGhost() - -/** - * Execute the research fr the Candidate Mate without controlling if the vertices are already matched. - * Returns the vertices with the highest weight - * @param adj1 - * @param adj2 - * @param verLocInd - * @param edgeLocWeight - * @return - */ -inline MilanLongInt firstComputeCandidateMate(MilanLongInt adj1, - MilanLongInt adj2, - MilanLongInt* verLocInd, - MilanReal* edgeLocWeight) -{ - MilanInt w = -1; - MilanReal heaviestEdgeWt = MilanRealMin; //Assign the smallest Value possible first LDBL_MIN - int finalK; - for (int k = adj1; k < adj2; k++) { - - if ((edgeLocWeight[k] > heaviestEdgeWt) || - ((edgeLocWeight[k] == heaviestEdgeWt) && (w < verLocInd[k]))) { - heaviestEdgeWt = edgeLocWeight[k]; - w = verLocInd[k]; - finalK = k; - } - } //End of for loop - return finalK; -} - -/** - * //TODO documentation - * @param k - * @param verLocInd - * @param StartIndex - * @param EndIndex - * @param GMate - * @param Mate - * @param Ghost2LocalMap - * @return - */ -inline bool isAlreadyMatched(MilanLongInt node, - MilanLongInt StartIndex, - MilanLongInt EndIndex, - vector &GMate, - MilanLongInt* Mate, - map &Ghost2LocalMap -) { - - bool result = false; -#pragma omp critical(Mate) - { - if ((node < StartIndex) || (node > EndIndex)) { //Is it a ghost vertex? - if (GMate[Ghost2LocalMap[node]] >= 0)// Already matched - result = true; - } else { //A local vertex - if (Mate[node - StartIndex] >= 0) // Already matched - result = true; - } - - } - - return result; -} - -/** - * //TODO documentation - * @param adj1 - * @param adj2 - * @param edgeLocWeight - * @param k - * @param verLocInd - * @param StartIndex - * @param EndIndex - * @param GMate - * @param Mate - * @param Ghost2LocalMap - * @return - */ -inline MilanLongInt computeCandidateMate(MilanLongInt adj1, - MilanLongInt adj2, - MilanReal* edgeLocWeight, - MilanLongInt k, - MilanLongInt* verLocInd, - MilanLongInt StartIndex, - MilanLongInt EndIndex, - vector & GMate, - MilanLongInt* Mate, - map & Ghost2LocalMap) -{ - MilanInt w = -1; - MilanReal heaviestEdgeWt = MilanRealMin; //Assign the smallest Value possible first LDBL_MIN - for (k = adj1; k < adj2; k++) { - if (isAlreadyMatched(verLocInd[k], StartIndex, EndIndex, GMate, Mate, Ghost2LocalMap)) continue; - - if ((edgeLocWeight[k] > heaviestEdgeWt) || - ((edgeLocWeight[k] == heaviestEdgeWt) && (w < verLocInd[k]))) { - heaviestEdgeWt = edgeLocWeight[k]; - w = verLocInd[k]; - } - } //End of for loop - return w; -} #endif #endif \ No newline at end of file diff --git a/amgprec/impl/aggregator/computeCandidateMate.cpp b/amgprec/impl/aggregator/computeCandidateMate.cpp new file mode 100644 index 00000000..92e3c92b --- /dev/null +++ b/amgprec/impl/aggregator/computeCandidateMate.cpp @@ -0,0 +1,69 @@ +#include "MatchBoxPC.h" + +/** + * Execute the research fr the Candidate Mate without controlling if the vertices are already matched. + * Returns the vertices with the highest weight + * @param adj1 + * @param adj2 + * @param verLocInd + * @param edgeLocWeight + * @return + */ +inline MilanLongInt firstComputeCandidateMate(MilanLongInt adj1, + MilanLongInt adj2, + MilanLongInt* verLocInd, + MilanReal* edgeLocWeight) +{ + MilanInt w = -1; + MilanReal heaviestEdgeWt = MilanRealMin; //Assign the smallest Value possible first LDBL_MIN + int finalK; + for (int k = adj1; k < adj2; k++) { + + if ((edgeLocWeight[k] > heaviestEdgeWt) || + ((edgeLocWeight[k] == heaviestEdgeWt) && (w < verLocInd[k]))) { + heaviestEdgeWt = edgeLocWeight[k]; + w = verLocInd[k]; + finalK = k; + } + } //End of for loop + return finalK; +} + +/** + * //TODO documentation + * @param adj1 + * @param adj2 + * @param edgeLocWeight + * @param k + * @param verLocInd + * @param StartIndex + * @param EndIndex + * @param GMate + * @param Mate + * @param Ghost2LocalMap + * @return + */ +inline MilanLongInt computeCandidateMate(MilanLongInt adj1, + MilanLongInt adj2, + MilanReal* edgeLocWeight, + MilanLongInt k, + MilanLongInt* verLocInd, + MilanLongInt StartIndex, + MilanLongInt EndIndex, + vector & GMate, + MilanLongInt* Mate, + map & Ghost2LocalMap) +{ + MilanInt w = -1; + MilanReal heaviestEdgeWt = MilanRealMin; //Assign the smallest Value possible first LDBL_MIN + for (k = adj1; k < adj2; k++) { + if (isAlreadyMatched(verLocInd[k], StartIndex, EndIndex, GMate, Mate, Ghost2LocalMap)) continue; + + if ((edgeLocWeight[k] > heaviestEdgeWt) || + ((edgeLocWeight[k] == heaviestEdgeWt) && (w < verLocInd[k]))) { + heaviestEdgeWt = edgeLocWeight[k]; + w = verLocInd[k]; + } + } //End of for loop + return w; +} \ No newline at end of file diff --git a/amgprec/impl/aggregator/findOwnerOfGhost.cpp b/amgprec/impl/aggregator/findOwnerOfGhost.cpp new file mode 100644 index 00000000..10850607 --- /dev/null +++ b/amgprec/impl/aggregator/findOwnerOfGhost.cpp @@ -0,0 +1,52 @@ +#include "MatchBoxPC.h" +#include +#include +#include +#include +#include "primitiveDataTypeDefinitions.h" +#include "dataStrStaticQueue.h" + +///Find the owner of a ghost node: +inline MilanInt findOwnerOfGhost(MilanLongInt vtxIndex, MilanLongInt *mVerDistance, + MilanInt myRank, MilanInt numProcs) { + //MilanLongInt Size = mVerDistance.size(); + MilanLongInt mStartInd = mVerDistance[myRank]; + MilanInt Start = 0; + MilanInt End = numProcs; + MilanInt Current = 0; + +#if 0 + if ( vtxIndex < mStartInd ) + End = myRank; + else + Start = myRank; +#endif + + while ( Start <= End ) { + Current = (End + Start)/2; + //CASE-1: + if ( mVerDistance[Current] == vtxIndex ) { + while ( mVerDistance[Current+1] == vtxIndex ) { + Current++; + if ( Current == numProcs ) + return (-1); + } + return (Current); + } + else { //CASE 2: + if ( mVerDistance[Current] > vtxIndex ) + End = Current - 1; + else //CASE 3: + Start = Current + 1; + } + } //End of While() + if ( Current == 0 ) + return (Current); + else { + if ( mVerDistance[Current] > vtxIndex ) + return (Current-1); + else + return (Current); + } //End of else + return (-1); //It should not reach here! +} //End of findOwnerOfGhost() diff --git a/amgprec/impl/aggregator/isAlreadyMatched.cpp b/amgprec/impl/aggregator/isAlreadyMatched.cpp new file mode 100644 index 00000000..5a9cf476 --- /dev/null +++ b/amgprec/impl/aggregator/isAlreadyMatched.cpp @@ -0,0 +1,42 @@ +#include "MatchBoxPC.h" +#include +#include +#include +#include +#include "primitiveDataTypeDefinitions.h" +#include "dataStrStaticQueue.h" + +/** + * //TODO documentation + * @param k + * @param verLocInd + * @param StartIndex + * @param EndIndex + * @param GMate + * @param Mate + * @param Ghost2LocalMap + * @return + */ +inline bool isAlreadyMatched(MilanLongInt node, + MilanLongInt StartIndex, + MilanLongInt EndIndex, + vector &GMate, + MilanLongInt* Mate, + map &Ghost2LocalMap +) { + + bool result = false; +#pragma omp critical(Mate) + { + if ((node < StartIndex) || (node > EndIndex)) { //Is it a ghost vertex? + if (GMate[Ghost2LocalMap[node]] >= 0)// Already matched + result = true; + } else { //A local vertex + if (Mate[node - StartIndex] >= 0) // Already matched + result = true; + } + + } + + return result; +} \ No newline at end of file diff --git a/samples/advanced/pdegen/runs/amg_pde3d.inp b/samples/advanced/pdegen/runs/amg_pde3d.inp index 904b6551..b6c448c3 100644 --- a/samples/advanced/pdegen/runs/amg_pde3d.inp +++ b/samples/advanced/pdegen/runs/amg_pde3d.inp @@ -1,6 +1,6 @@ %%%%%%%%%%% General arguments % Lines starting with % are ignored. CSR ! Storage format CSR COO JAD -020 ! IDIM; domain size. Linear system size is IDIM**3 +0020 ! IDIM; domain size. Linear system size is IDIM**3 CONST ! PDECOEFF: CONST, EXP, GAUSS Coefficients of the PDE BICGSTAB ! Iterative method: BiCGSTAB BiCGSTABL BiCG CG CGS FCG GCR RGMRES 2 ! ISTOPC