// // Created by Brady Bodily on 3/28/20. // #include "MakePermutationMatrix.h" #include "mpi.h" #include #include std::vector MakePermutationMatrix::MakePermutation(std::vector source, int rank, int size, std::vector> costMatrix){ std::vector returnVector; std::vector localCostVector; std::sort(source.begin(), source.end()); std::vector myPerm = source; std::vector nextPerm = source; auto offset = (myPerm.size()+1)/size; //return vector should be the size of number of processors as they are going to only return their own smallest. if(rank) returnVector.resize(source.size(), 0); //the "nextPerm" is the permutation to stop at. std::rotate(nextPerm.begin(), nextPerm.begin()+(rank+1)*offset, nextPerm.end()); //rotate my perm. std::rotate(myPerm.begin(), myPerm.begin()+offset*rank,myPerm.end()); std::cout << "source.last(): " << source[8].GetName() << std::endl; std::cout << "source: "; for(int i = 0; i < source.size(); i++){ std::cout << source[i].GetName() << " "; } std::cout << std::endl << std::endl; std::cout <<"Rank: " << rank << " next perm: "; for(int i = 0; i < nextPerm.size(); i++){ std::cout << nextPerm[i].GetName() << " "; } std::cout << std::endl; do { double cost = 0; std::cout <<"Rank: " << rank << " myPerm: "; for(int i = 0; i < myPerm.size(); i++){ std::cout << myPerm[i].GetName() << " "; } std::cout << std::endl; //Add up the cost of the current permutation. for (int i = 1; i < myPerm.size(); i++) { cost += costMatrix[myPerm[i].GetName()][myPerm[i-1].GetName()]; } // std::cout << "cost: " << cost <(1,localCostVector[0]); // for(int i = 0; i < localCostVector.size(); i++) { // if(localCostVector[i] < lowest[0]) // lowest[0] = localCostVector[i]; // std::cout << lowest[0] << " "; // } std::cout<< "Rank: "<< rank<< "cost count: "<< localCostVector.size() << std::endl; std::cout << "Rank: "<< rank <<" Last Perm: "; for(int i = 0; i < myPerm.size(); i++){ std::cout << myPerm[i].GetName() << " "; } std::cout << std::endl; //MPI_Gather(&localCostVector,1, MPI_DOUBLE, &returnVector,1, MPI_DOUBLE,0, MPI_COMM_WORLD); return returnVector; } std::vector MakePermutationMatrix::GetLowestCost(std::vector source, int rank, int size, std::vector> costMatrix){ return MakePermutation(source, rank, size, costMatrix); }