1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
|
#include "CombBLAS/CombBLAS.h"
#include <mpi.h>
#include <sys/time.h>
#include <iostream>
#include <functional>
#include <algorithm>
#include <vector>
#include <string>
#include <sstream>
#include "BPMaximalMatching.h"
#ifdef THREADED
#ifndef _OPENMP
#define _OPENMP
#endif
#include <omp.h>
int cblas_splits = 1;
#endif
using namespace std;
using namespace combblas;
bool prune, mvInvertMate, randMM, moreSplit;
int init;
bool randMaximal;
bool fewexp;
template <typename PARMAT>
void Symmetricize(PARMAT & A)
{
// boolean addition is practically a "logical or"
// therefore this doesn't destruct any links
PARMAT AT = A;
AT.Transpose();
A += AT;
}
struct VertexType
{
public:
VertexType(int64_t p=-1, int64_t r=-1, int16_t pr=0){parent=p; root = r; prob = pr;};
friend bool operator<(const VertexType & vtx1, const VertexType & vtx2 )
{
if(vtx1.prob==vtx2.prob) return vtx1.parent<vtx2.parent;
else return vtx1.prob<vtx2.prob;
};
friend bool operator==(const VertexType & vtx1, const VertexType & vtx2 ){return vtx1.parent==vtx2.parent;};
friend ostream& operator<<(ostream& os, const VertexType & vertex ){os << "(" << vertex.parent << "," << vertex.root << ")"; return os;};
//private:
int64_t parent;
int64_t root;
int16_t prob; // probability of selecting an edge
};
typedef SpParMat < int64_t, bool, SpDCCols<int64_t,bool> > PSpMat_Bool;
/*
Remove isolated vertices and purmute
*/
void removeIsolated(PSpMat_Bool & A)
{
int nprocs, myrank;
MPI_Comm comm = A.getcommgrid()->GetWorld();
MPI_Comm_size(comm,&nprocs);
MPI_Comm_rank(comm,&myrank);
FullyDistVec<int64_t, int64_t> * ColSums = new FullyDistVec<int64_t, int64_t>(A.getcommgrid());
FullyDistVec<int64_t, int64_t> * RowSums = new FullyDistVec<int64_t, int64_t>(A.getcommgrid());
FullyDistVec<int64_t, int64_t> nonisoRowV; // id's of non-isolated (connected) Row vertices
FullyDistVec<int64_t, int64_t> nonisoColV; // id's of non-isolated (connected) Col vertices
FullyDistVec<int64_t, int64_t> nonisov; // id's of non-isolated (connected) vertices
A.Reduce(*ColSums, Column, plus<int64_t>(), static_cast<int64_t>(0));
A.Reduce(*RowSums, Row, plus<int64_t>(), static_cast<int64_t>(0));
// this steps for general graph
/*
ColSums->EWiseApply(*RowSums, plus<int64_t>()); not needed for bipartite graph
nonisov = ColSums->FindInds(bind2nd(greater<int64_t>(), 0));
nonisov.RandPerm(); // so that A(v,v) is load-balanced (both memory and time wise)
A.operator()(nonisov, nonisov, true); // in-place permute to save memory
*/
// this steps for bipartite graph
nonisoColV = ColSums->FindInds(bind2nd(greater<int64_t>(), 0));
nonisoRowV = RowSums->FindInds(bind2nd(greater<int64_t>(), 0));
delete ColSums;
delete RowSums;
{
nonisoColV.RandPerm();
nonisoRowV.RandPerm();
}
int64_t nrows1=A.getnrow(), ncols1=A.getncol(), nnz1 = A.getnnz();
double avgDeg1 = (double) nnz1/(nrows1+ncols1);
A.operator()(nonisoRowV, nonisoColV, true);
int64_t nrows2=A.getnrow(), ncols2=A.getncol(), nnz2 = A.getnnz();
double avgDeg2 = (double) nnz2/(nrows2+ncols2);
if(myrank == 0)
{
cout << "ncol nrows nedges deg \n";
cout << nrows1 << " " << ncols1 << " " << nnz1 << " " << avgDeg1 << " \n";
cout << nrows2 << " " << ncols2 << " " << nnz2 << " " << avgDeg2 << " \n";
}
MPI_Barrier(comm);
}
void ShowUsage()
{
int myrank;
MPI_Comm_rank(MPI_COMM_WORLD,&myrank);
if(myrank == 0)
{
cout << "\n-------------- usage --------------\n";
cout << "Usage (random matrix): ./maximal <er|g500|ssca> <Scale> <EDGEFACTOR> <algo><rand><moreSplit>\n";
cout << "Usage (input matrix): ./maximal <input> <matrix> <algo><rand><moreSplit>\n\n";
cout << " \n-------------- meaning of arguments ----------\n";
cout << "** er: Erdos-Renyi, g500: Graph500 benchmark, ssca: SSCA benchmark\n";
cout << "** scale: matrix dimention is 2^scale\n";
cout << "** edgefactor: average degree of vertices\n";
cout << "** algo : maximal matching algorithm used to initialize\n ";
cout << " greedy: greedy init , ks: Karp-Sipser, dmd: dynamic mindegree\n";
cout << " default: dynamic mindegree\n";
cout << "** (optional) rand: random parent selection in greedy/Karp-Sipser\n" ;
cout << "** (optional) moreSplit: more splitting of Matrix.\n" ;
cout << "(order of optional arguments does not matter)\n";
cout << " \n-------------- examples ----------\n";
cout << "Example: mpirun -np 4 ./maximal g500 18 16 ks rand" << endl;
cout << "Example: mpirun -np 4 ./maximal input cage12.mtx dmd\n" << endl;
}
}
void GetOptions(char* argv[], int argc)
{
string allArg="";
for(int i=0; i<argc; i++)
{
allArg += string(argv[i]);
}
if(allArg.find("moreSplit")!=string::npos)
moreSplit = true;
if(allArg.find("randMaximal")!=string::npos)
randMaximal = true;
if(allArg.find("greedy")!=string::npos)
init = GREEDY;
else if(allArg.find("ks")!=string::npos)
init = KARP_SIPSER;
else if(allArg.find("dmd")!=string::npos)
init = DMD;
else
init = DMD;
}
void showCurOptions()
{
ostringstream tinfo;
tinfo.str("");
tinfo << "\n---------------------------------\n";
tinfo << " Maximal matching algorithm options: ";
if(init == KARP_SIPSER) tinfo << " Karp-Sipser, ";
if(init == DMD) tinfo << " dynamic mindegree, ";
if(init == GREEDY) tinfo << " greedy, ";
if(randMaximal) tinfo << " random parent selection in greedy/Karp-Sipser, ";
if(moreSplit) tinfo << " moreSplit ";
tinfo << "\n---------------------------------\n\n";
SpParHelper::Print(tinfo.str());
}
void experiment( PSpMat_Bool & A, PSpMat_Bool & AT, FullyDistVec<int64_t, int64_t> degCol)
{
FullyDistVec<int64_t, int64_t> mateRow2Col ( A.getcommgrid(), A.getnrow(), (int64_t) -1);
FullyDistVec<int64_t, int64_t> mateCol2Row ( A.getcommgrid(), A.getncol(), (int64_t) -1);
// best option
init = DMD; randMaximal = false;
//showCurOptions();
MaximalMatching(A, AT, mateRow2Col, mateCol2Row, degCol, init, randMaximal);
mateRow2Col.Apply([](int64_t val){return (int64_t) -1;});
mateCol2Row.Apply([](int64_t val){return (int64_t) -1;});
// best option + KS
init = KARP_SIPSER; randMaximal = true;
//showCurOptions();
MaximalMatching(A, AT, mateRow2Col, mateCol2Row, degCol, init, randMaximal);
mateRow2Col.Apply([](int64_t val){return (int64_t) -1;});
mateCol2Row.Apply([](int64_t val){return (int64_t) -1;});
// best option + Greedy
init = GREEDY; randMaximal = true;
//showCurOptions();
MaximalMatching(A, AT, mateRow2Col, mateCol2Row, degCol, init, randMaximal);
mateRow2Col.Apply([](int64_t val){return (int64_t) -1;});
mateCol2Row.Apply([](int64_t val){return (int64_t) -1;});
// best option + KS
init = KARP_SIPSER; randMaximal = false;
//showCurOptions();
MaximalMatching(A, AT, mateRow2Col, mateCol2Row, degCol, init, randMaximal);
mateRow2Col.Apply([](int64_t val){return (int64_t) -1;});
mateCol2Row.Apply([](int64_t val){return (int64_t) -1;});
// best option + Greedy
init = GREEDY; randMaximal = false;
//showCurOptions();
MaximalMatching(A, AT, mateRow2Col, mateCol2Row, degCol, init, randMaximal);
mateRow2Col.Apply([](int64_t val){return (int64_t) -1;});
mateCol2Row.Apply([](int64_t val){return (int64_t) -1;});
}
int main(int argc, char* argv[])
{
// ------------ initialize MPI ---------------
int provided;
MPI_Init_thread(&argc, &argv, MPI_THREAD_SERIALIZED, &provided);
if (provided < MPI_THREAD_SERIALIZED)
{
printf("ERROR: The MPI library does not have MPI_THREAD_SERIALIZED support\n");
MPI_Abort(MPI_COMM_WORLD, 1);
}
int nprocs, myrank;
MPI_Comm_size(MPI_COMM_WORLD,&nprocs);
MPI_Comm_rank(MPI_COMM_WORLD,&myrank);
if(argc < 3)
{
ShowUsage();
MPI_Finalize();
return -1;
}
init = DMD;
randMaximal = false;
moreSplit = false;
// ------------ Process input arguments and build matrix ---------------
{
PSpMat_Bool * ABool;
ostringstream tinfo;
double t01, t02;
if(string(argv[1]) == string("input")) // input option
{
ABool = new PSpMat_Bool();
string filename(argv[2]);
tinfo.str("");
tinfo << "**** Reading input matrix: " << filename << " ******* " << endl;
SpParHelper::Print(tinfo.str());
t01 = MPI_Wtime();
ABool->ParallelReadMM(filename, true, maximum<double>());
t02 = MPI_Wtime();
ABool->PrintInfo();
tinfo.str("");
tinfo << "Reader took " << t02-t01 << " seconds" << endl;
SpParHelper::Print(tinfo.str());
GetOptions(argv+3, argc-3);
}
else if(argc < 4)
{
ShowUsage();
MPI_Finalize();
return -1;
}
else
{
unsigned scale = (unsigned) atoi(argv[2]);
unsigned EDGEFACTOR = (unsigned) atoi(argv[3]);
double initiator[4];
if(string(argv[1]) == string("er"))
{
initiator[0] = .25;
initiator[1] = .25;
initiator[2] = .25;
initiator[3] = .25;
}
else if(string(argv[1]) == string("g500"))
{
initiator[0] = .57;
initiator[1] = .19;
initiator[2] = .19;
initiator[3] = .05;
}
else if(string(argv[1]) == string("ssca"))
{
initiator[0] = .6;
initiator[1] = .4/3;
initiator[2] = .4/3;
initiator[3] = .4/3;
}
else
{
if(myrank == 0)
printf("The input type - %s - is not recognized.\n", argv[2]);
MPI_Abort(MPI_COMM_WORLD, 1);
}
SpParHelper::Print("Generating input matrix....\n");
t01 = MPI_Wtime();
DistEdgeList<int64_t> * DEL = new DistEdgeList<int64_t>();
DEL->GenGraph500Data(initiator, scale, EDGEFACTOR, true, true);
ABool = new PSpMat_Bool(*DEL, false);
delete DEL;
t02 = MPI_Wtime();
ABool->PrintInfo();
tinfo.str("");
tinfo << "Generator took " << t02-t01 << " seconds" << endl;
SpParHelper::Print(tinfo.str());
//Symmetricize(*ABool);
//removeIsolated(*ABool);
//SpParHelper::Print("Generated matrix symmetricized....\n");
ABool->PrintInfo();
GetOptions(argv+4, argc-4);
}
// randomly permute for load balance
SpParHelper::Print("Performing random permuation of matrix.\n");
FullyDistVec<int64_t, int64_t> prow(ABool->getcommgrid());
FullyDistVec<int64_t, int64_t> pcol(ABool->getcommgrid());
prow.iota(ABool->getnrow(), 0);
pcol.iota(ABool->getncol(), 0);
prow.RandPerm();
pcol.RandPerm();
(*ABool)(prow, pcol, true);
SpParHelper::Print("Performed random permuation of matrix.\n");
PSpMat_Bool A = *ABool;
PSpMat_Bool AT = A;
if(ABool->getnrow() > ABool->getncol())
AT.Transpose();
else
A.Transpose();
// Reduce is not multithreaded, so I am doing it here
FullyDistVec<int64_t, int64_t> degCol(A.getcommgrid());
A.Reduce(degCol, Column, plus<int64_t>(), static_cast<int64_t>(0));
int nthreads;
#ifdef _OPENMP
#pragma omp parallel
{
int splitPerThread = 1;
if(moreSplit) splitPerThread = 4;
nthreads = omp_get_num_threads();
cblas_splits = nthreads*splitPerThread;
}
tinfo.str("");
tinfo << "Threading activated with " << nthreads << " threads, and matrix split into "<< cblas_splits << " parts" << endl;
SpParHelper::Print(tinfo.str());
A.ActivateThreading(cblas_splits); // note: crash on empty matrix
AT.ActivateThreading(cblas_splits);
#endif
SpParHelper::Print(" #####################################################\n");
SpParHelper::Print(" ################## Run 1 ############################\n");
SpParHelper::Print(" #####################################################\n");
experiment(A, AT, degCol);
SpParHelper::Print(" #####################################################\n");
SpParHelper::Print(" ################## Run 2 ############################\n");
SpParHelper::Print(" #####################################################\n");
experiment(A, AT, degCol);
SpParHelper::Print(" #####################################################\n");
SpParHelper::Print(" ################## Run 3 ############################\n");
SpParHelper::Print(" #####################################################\n");
experiment(A, AT, degCol);
}
MPI_Finalize();
return 0;
}
|