// // Created by Brady Bodily on 3/28/20. // #include "MakePermutationMatrix.h" #include "mpi.h" #include #include #include long double MakePermutationMatrix::MakePermutation(std::vector source, int rank, int size, std::vector> costMatrix){ std::vector returnVector; std::vector localCostVector; int vectorOffset = source.size()/size; std::sort(source.begin(), source.end()); std::vector myPerm = source; std::vector nextPerm = source; auto lowest = LDBL_MAX; //return vector should be the size of number of processors as they are going to only return their own smallest. returnVector.resize(source.size(), 0); //the "nextPerm" is the permutation to stop at. if(rank != size-1) std::rotate(nextPerm.begin(), nextPerm.begin() + ((rank*vectorOffset)+vectorOffset), nextPerm.end()); std::partial_sort(nextPerm.begin()+1, nextPerm.end(), nextPerm.end()); //rotate my perm. if(rank!=0) std::rotate(myPerm.begin(), myPerm.begin()+(((rank-1)*vectorOffset)+vectorOffset),myPerm.end()); std::partial_sort(myPerm.begin()+1, myPerm.end(), myPerm.end()); if(rank == size-1){ nextPerm = source; } std::cout << "Rank: " << rank << " I'm here" << std::endl; std::cout << "Rank: " << rank << " Lowest: " << lowest << std::endl; do { double cost = 0; //Add up the cost of the current permutation. // std::cout <<"Rank: " << rank << " Current perm: "; //std::cout << "Rank: "<< rank; for (int i = 0; i < myPerm.size(); i++) { //std::cout << " " << myPerm[i].GetName(); cost += costMatrix[myPerm[i].GetName()][myPerm[i-1].GetName()]; } std::cout << "COST: " << cost < source, int rank, int size, std::vector> costMatrix){ std::vector l(size,0); auto lowest = MakePermutation(source, rank, size, costMatrix); MPI_Gather(&lowest,1,MPI_LONG_DOUBLE,l.data(),1,MPI_LONG_DOUBLE,0,MPI_COMM_WORLD); std::sort(l.begin(), l.end()); return l[l.size()-1]; }