12 #define OMPMINOPS 50000
86 int isfvwgts,
int isfvsizes)
89 size_t nfields, nvtxs, nedges, fmt,
ncon, lnlen;
92 int readsizes=0, readwgts=0, readvals=0, numbering=0;
106 }
while (line[0] ==
'%');
109 nfields = sscanf(line,
"%zu %zu %zu %zu", &nvtxs, &nedges, &fmt, &
ncon);
111 gk_errexit(
SIGERR,
"Header line must contain at least 2 integers (#vtxs and #edges).\n");
116 gk_errexit(
SIGERR,
"Cannot read this type of file format [fmt=%zu]!\n", fmt);
118 sprintf(fmtstr,
"%03zu", fmt%1000);
119 readsizes = (fmtstr[0] ==
'1');
120 readwgts = (fmtstr[1] ==
'1');
121 readvals = (fmtstr[2] ==
'1');
131 graph->nvtxs = nvtxs;
133 graph->xadj = gk_zmalloc(nvtxs+1,
"gk_graph_Read: xadj");
134 graph->adjncy = gk_i32malloc(nedges,
"gk_graph_Read: adjncy");
137 graph->fadjwgt = gk_fmalloc(nedges,
"gk_graph_Read: fadjwgt");
139 graph->iadjwgt = gk_i32malloc(nedges,
"gk_graph_Read: iadjwgt");
144 graph->fvsizes = gk_fmalloc(nvtxs,
"gk_graph_Read: fvsizes");
146 graph->ivsizes = gk_i32malloc(nvtxs,
"gk_graph_Read: ivsizes");
151 graph->fvwgts = gk_fmalloc(nvtxs*
ncon,
"gk_graph_Read: fvwgts");
153 graph->ivwgts = gk_i32malloc(nvtxs*
ncon,
"gk_graph_Read: ivwgts");
160 numbering = (numbering ? - 1 : 0);
161 for (
graph->xadj[0]=0, k=0,
i=0;
i<nvtxs;
i++) {
164 gk_errexit(
SIGERR,
"Pregraphure end of input file: file while reading row %d\n",
i);
165 }
while (line[0] ==
'%');
179 gk_errexit(
SIGERR,
"The line for vertex %zd does not have size information\n",
i+1);
180 if (
graph->fvsizes[
i] < 0)
186 gk_errexit(
SIGERR,
"The line for vertex %zd does not have size information\n",
i+1);
187 if (
graph->ivsizes[
i] < 0)
204 "for the %d constraints.\n",
i+1,
ncon);
206 gk_errexit(
SIGERR,
"The weight vertex %zd and constraint %zd must be >= 0\n",
i+1,
l);
212 "for the %d constraints.\n",
i+1,
ncon);
214 gk_errexit(
SIGERR,
"The weight vertex %zd and constraint %zd must be >= 0\n",
i+1,
l);
228 if ((
graph->adjncy[k] = ival + numbering) < 0)
239 gk_errexit(
SIGERR,
"Value could not be found for edge! Vertex:%zd, NNZ:%zd\n",
i, k);
241 graph->fadjwgt[k] = fval;
246 gk_errexit(
SIGERR,
"Value could not be found for edge! Vertex:%zd, NNZ:%zd\n",
i, k);
248 graph->iadjwgt[k] = ival;
258 gk_errexit(
SIGERR,
"gk_graph_Read: Something wrong with the number of edges in "
259 "the input file. nedges=%zd, Actualnedges=%zd.\n", nedges, k);
280 int hasvwgts, hasvsizes, hasewgts;
292 hasewgts = (
graph->iadjwgt ||
graph->fadjwgt);
294 hasvsizes = (
graph->ivsizes ||
graph->fvsizes);
298 if (hasvwgts || hasvsizes || hasewgts)
299 fprintf(fpout,
" %d%d%d", hasvsizes, hasvwgts, hasewgts);
300 fprintf(fpout,
"\n");
306 fprintf(fpout,
" %d",
graph->ivsizes[
i]);
308 fprintf(fpout,
" %f",
graph->fvsizes[
i]);
313 fprintf(fpout,
" %d",
graph->ivwgts[
i]);
315 fprintf(fpout,
" %f",
graph->fvwgts[
i]);
319 fprintf(fpout,
" %d",
graph->adjncy[
j]+1);
322 fprintf(fpout,
" %d",
graph->iadjwgt[
j]);
324 fprintf(fpout,
" %f",
graph->fadjwgt[
j]);
327 fprintf(fpout,
"\n");
351 gk_zmalloc(
graph->nvtxs+1,
"gk_graph_Dup: xadj"));
354 gk_i32malloc(
graph->nvtxs,
"gk_graph_Dup: ivwgts"));
357 gk_i32malloc(
graph->nvtxs,
"gk_graph_Dup: ivsizes"));
360 gk_i32malloc(
graph->nvtxs,
"gk_graph_Dup: ivlabels"));
363 gk_fmalloc(
graph->nvtxs,
"gk_graph_Dup: fvwgts"));
366 gk_fmalloc(
graph->nvtxs,
"gk_graph_Dup: fvsizes"));
371 gk_i32malloc(
graph->xadj[
graph->nvtxs],
"gk_graph_Dup: adjncy"));
374 gk_i32malloc(
graph->xadj[
graph->nvtxs],
"gk_graph_Dup: iadjwgt"));
377 gk_fmalloc(
graph->xadj[
graph->nvtxs],
"gk_graph_Dup: fadjwgt"));
396 if (vstart+nvtxs >
graph->nvtxs)
401 ngraph->
nvtxs = nvtxs;
405 ngraph->
xadj = gk_zcopy(nvtxs+1,
graph->xadj+vstart,
406 gk_zmalloc(nvtxs+1,
"gk_graph_ExtractSubgraph: xadj"));
407 for (
i=nvtxs;
i>=0;
i--)
412 ngraph->
ivwgts = gk_i32copy(nvtxs,
graph->ivwgts+vstart,
413 gk_i32malloc(nvtxs,
"gk_graph_ExtractSubgraph: ivwgts"));
415 ngraph->
ivsizes = gk_i32copy(nvtxs,
graph->ivsizes+vstart,
416 gk_i32malloc(nvtxs,
"gk_graph_ExtractSubgraph: ivsizes"));
418 ngraph->
vlabels = gk_i32copy(nvtxs,
graph->vlabels+vstart,
419 gk_i32malloc(nvtxs,
"gk_graph_ExtractSubgraph: vlabels"));
422 ngraph->
fvwgts = gk_fcopy(nvtxs,
graph->fvwgts+vstart,
423 gk_fmalloc(nvtxs,
"gk_graph_ExtractSubgraph: fvwgts"));
425 ngraph->
fvsizes = gk_fcopy(nvtxs,
graph->fvsizes+vstart,
426 gk_fmalloc(nvtxs,
"gk_graph_ExtractSubgraph: fvsizes"));
433 gk_i32malloc(
graph->xadj[vstart+nvtxs]-
graph->xadj[vstart],
434 "gk_graph_ExtractSubgraph: adjncy"));
438 gk_i32malloc(
graph->xadj[vstart+nvtxs]-
graph->xadj[vstart],
439 "gk_graph_ExtractSubgraph: iadjwgt"));
443 gk_fmalloc(
graph->xadj[vstart+nvtxs]-
graph->xadj[vstart],
444 "gk_graph_ExtractSubgraph: fadjwgt"));
463 int i, k, u,
v, nvtxs;
464 int freeperm=0, freeiperm=0;
479 ngraph->
xadj = gk_zmalloc(nvtxs+1,
"gk_graph_Reorder: xadj");
482 ngraph->
ivwgts = gk_i32malloc(nvtxs,
"gk_graph_Reorder: ivwgts");
485 ngraph->
ivsizes = gk_i32malloc(nvtxs,
"gk_graph_Reorder: ivsizes");
488 ngraph->
vlabels = gk_i32malloc(nvtxs,
"gk_graph_Reorder: ivlabels");
491 ngraph->
fvwgts = gk_fmalloc(nvtxs,
"gk_graph_Reorder: fvwgts");
494 ngraph->
fvsizes = gk_fmalloc(nvtxs,
"gk_graph_Reorder: fvsizes");
498 ngraph->
adjncy = gk_i32malloc(
graph->xadj[nvtxs],
"gk_graph_Reorder: adjncy");
501 ngraph->
iadjwgt = gk_i32malloc(
graph->xadj[nvtxs],
"gk_graph_Reorder: iadjwgt");
504 ngraph->
fadjwgt = gk_fmalloc(
graph->xadj[nvtxs],
"gk_graph_Reorder: fadjwgt");
510 perm = gk_i32malloc(nvtxs,
"gk_graph_Reorder: perm");
511 for (
i=0;
i<nvtxs;
i++)
516 iperm = gk_i32malloc(nvtxs,
"gk_graph_Reorder: iperm");
517 for (
i=0;
i<nvtxs;
i++)
522 ngraph->
xadj[0] = jj = 0;
523 for (
v=0;
v<nvtxs;
v++) {
543 ngraph->
xadj[
v+1] = jj;
577 int32_t mustfree_ccsr=0, mustfree_where=0;
579 nvtxs =
graph->nvtxs;
585 cptr = gk_i32malloc(nvtxs+1,
"gk_graph_FindComponents: cptr");
586 cind = gk_i32malloc(nvtxs,
"gk_graph_FindComponents: cind");
592 todo = gk_i32incset(nvtxs, 0, gk_i32malloc(nvtxs,
"gk_graph_FindComponents: todo"));
597 pos = gk_i32incset(nvtxs, 0, gk_i32malloc(nvtxs,
"gk_graph_FindComponents: pos"));
608 cptr[++ncmps] =
first;
624 j = todo[k] = todo[--ntodo];
635 cptr[++ncmps] =
first;
672 if (
graph->nvtxs <= 0)
675 nvtxs =
graph->nvtxs;
680 pos = gk_i32incset(nvtxs, 0, gk_i32malloc(nvtxs,
"gk_graph_ComputeBFSOrdering: pos"));
686 cot = gk_i32incset(nvtxs, 0, gk_i32malloc(nvtxs,
"gk_graph_ComputeBFSOrdering: cot"));
695 while (
first < nvtxs) {
722 if (r_perm !=
NULL) {
724 for (
i=0;
i<nvtxs;
i++)
731 if (r_iperm !=
NULL) {
770 if (
graph->nvtxs <= 0)
773 nvtxs =
graph->nvtxs;
778 degrees = gk_i32smalloc(nvtxs, 0,
"gk_graph_ComputeBestFOrdering: degrees");
781 minIDs = gk_i32smalloc(nvtxs, nvtxs+1,
"gk_graph_ComputeBestFOrdering: minIDs");
784 open = gk_i32malloc(nvtxs,
"gk_graph_ComputeBestFOrdering: open");
789 perm = gk_i32smalloc(nvtxs, -1,
"gk_graph_ComputeBestFOrdering: perm");
792 queue = gk_i32pqCreate(nvtxs);
793 for (
i=0;
i<nvtxs;
i++)
794 gk_i32pqInsert(queue,
i, 0);
795 gk_i32pqUpdate(queue,
v, 1);
800 for (
i=0;
i<nvtxs;
i++) {
801 if ((
v = gk_i32pqGetTop(queue)) == -1)
812 minIDs[u] = (
i < minIDs[u] ?
i : minIDs[u]);
816 gk_i32pqUpdate(queue, u, 1);
819 gk_i32pqUpdate(queue, u, degrees[u]);
822 for (k=0, jj=
xadj[u]; jj<
xadj[u+1]; jj++) {
826 gk_i32pqUpdate(queue, u, k);
830 for (k=0, jj=
xadj[u]; jj<
xadj[u+1]; jj++) {
834 gk_i32pqUpdate(queue, u, k);
845 if (r_perm !=
NULL) {
850 if (r_iperm !=
NULL) {
852 for (
i=0;
i<nvtxs;
i++)
862 gk_i32pqDestroy(queue);
891 int i, k, u, nvtxs, nopen, ntodo;
895 if (
graph->nvtxs <= 0)
898 nvtxs =
graph->nvtxs;
903 degrees = gk_i32smalloc(nvtxs, 0,
"gk_graph_ComputeBestFOrdering: degrees");
906 wdegrees = gk_i32smalloc(nvtxs, 0,
"gk_graph_ComputeBestFOrdering: wdegrees");
909 sod = gk_i32smalloc(nvtxs, 0,
"gk_graph_ComputeBestFOrdering: sod");
912 level = gk_i32smalloc(nvtxs, 0,
"gk_graph_ComputeBestFOrdering: level");
918 ot = gk_i32incset(nvtxs, 0, gk_i32malloc(nvtxs,
"gk_graph_FindComponents: ot"));
921 pos = gk_i32incset(nvtxs, 0, gk_i32malloc(nvtxs,
"gk_graph_FindComponents: pos"));
924 perm = gk_i32smalloc(nvtxs, -1,
"gk_graph_ComputeBestFOrdering: perm");
927 queue = gk_i32pqCreate(nvtxs);
928 gk_i32pqInsert(queue,
v, 1);
937 for (
i=0;
i<nvtxs;
i++) {
939 gk_i32pqInsert(queue, ot[0], 1);
943 if ((
v = gk_i32pqGetTop(queue)) == -1)
953 gk_errexit(
SIGERR,
"The position of v is not in open list. pos[%d]=%d is >=%d.\n",
v,
pos[
v], nopen);
956 ot[
pos[
v]] = ot[nopen-1];
959 ot[nopen-1] = ot[ntodo-1];
960 pos[ot[ntodo-1]] = nopen-1;
970 if (degrees[u] == 0) {
971 ot[
pos[u]] = ot[nopen];
978 gk_i32pqInsert(queue, u, 0);
988 gk_i32pqUpdate(queue, u, 1000*(
i+1)+degrees[u]);
992 gk_i32pqUpdate(queue, u, degrees[u]);
997 gk_i32pqUpdate(queue, u, wdegrees[u]);
1006 gk_i32pqUpdate(queue, u, -(1000*
level[u] - degrees[u]));
1010 gk_i32pqUpdate(queue, u, (
i+1)*degrees[u]);
1020 for (
j=0;
j<nopen;
j++) {
1023 gk_errexit(
SIGERR,
"For i=%d, the open list contains a closed vertex: ot[%zd]=%d, perm[%d]=%d.\n",
i,
j, u, u,
perm[u]);
1024 sod[u] += degrees[u];
1025 if (
i<1000 ||
i%25==0)
1026 gk_i32pqUpdate(queue, u, sod[u]);
1041 if (r_perm !=
NULL) {
1046 if (r_iperm !=
NULL) {
1048 for (
i=0;
i<nvtxs;
i++)
1058 gk_i32pqDestroy(queue);
1090 if (
graph->nvtxs <= 0)
1093 nvtxs =
graph->nvtxs;
1097 inqueue = gk_i32smalloc(nvtxs, 0,
"gk_graph_SingleSourceShortestPaths: inqueue");
1107 queue = gk_i32pqCreate(nvtxs);
1108 gk_i32pqInsert(queue,
v, 0);
1111 sps = gk_i32smalloc(nvtxs, -1,
"gk_graph_SingleSourceShortestPaths: sps");
1115 while ((
v = gk_i32pqGetTop(queue)) != -1) {
1121 if (inqueue[u] == 2)
1124 if (sps[u] < 0 || sps[
v]+
adjwgt[
i] < sps[u]) {
1128 gk_i32pqUpdate(queue, u, -sps[u]);
1130 gk_i32pqInsert(queue, u, -sps[u]);
1137 *r_sps = (
void *)sps;
1139 gk_i32pqDestroy(queue);
1148 queue = gk_fpqCreate(nvtxs);
1149 gk_fpqInsert(queue,
v, 0);
1152 sps = gk_fsmalloc(nvtxs, -1,
"gk_graph_SingleSourceShortestPaths: sps");
1156 while ((
v = gk_fpqGetTop(queue)) != -1) {
1162 if (inqueue[u] == 2)
1165 if (sps[u] < 0 || sps[
v]+
adjwgt[
i] < sps[u]) {
1169 gk_fpqUpdate(queue, u, -sps[u]);
1171 gk_fpqInsert(queue, u, -sps[u]);
1178 *r_sps = (
void *)sps;
1180 gk_fpqDestroy(queue);
1209 ptr =
graph->rowptr;
1211 val =
graph->rowval;
1216 gk_errexit(
SIGERR,
"Column-based view of the graphrix does not exists.\n");
1219 ptr =
graph->colptr;
1221 val =
graph->colval;
1229 #pragma omp parallel if (n > 100)
1239 cand = gk_ikvmalloc(
nn,
"gk_graph_SortIndices: cand");
1240 tval = gk_fmalloc(
nn,
"gk_graph_SortIndices: tval");
1242 #pragma omp for schedule(static)
1243 for (
i=0;
i<
n;
i++) {
1244 for (k=0,
j=ptr[
i];
j<ptr[
i+1];
j++) {
1247 cand[
j-ptr[
i]].val =
j-ptr[
i];
1248 cand[
j-ptr[
i]].key =
ind[
j];
1249 tval[
j-ptr[
i]] = val[
j];
1253 for (
j=ptr[
i];
j<ptr[
i+1];
j++) {
1254 ind[
j] = cand[
j-ptr[
i]].key;
1255 val[
j] = tval[cand[
j-ptr[
i]].val];
1281 ngraph->nrows = nrows;
1282 ngraph->ncols =
graph->ncols;
1284 for (nnz=0,
i=0;
i<nrows;
i++)
1285 nnz +=
graph->rowptr[rind[
i]+1]-
graph->rowptr[rind[
i]];
1287 ngraph->rowptr = gk_zmalloc(ngraph->nrows+1,
"gk_graph_ExtractPartition: rowptr");
1288 ngraph->rowind = gk_imalloc(nnz,
"gk_graph_ExtractPartition: rowind");
1289 ngraph->rowval = gk_fmalloc(nnz,
"gk_graph_ExtractPartition: rowval");
1291 ngraph->rowptr[0] = 0;
1292 for (nnz=0,
j=0, ii=0; ii<nrows; ii++) {
1297 ngraph->rowptr[++
j] = nnz;
1321 ngraph->ncols =
graph->ncols;
1323 for (nnz=0,
i=0;
i<
graph->nrows;
i++) {
1324 if (
part[
i] == pid) {
1330 ngraph->rowptr = gk_zmalloc(ngraph->nrows+1,
"gk_graph_ExtractPartition: rowptr");
1331 ngraph->rowind = gk_imalloc(nnz,
"gk_graph_ExtractPartition: rowind");
1332 ngraph->rowval = gk_fmalloc(nnz,
"gk_graph_ExtractPartition: rowval");
1334 ngraph->rowptr[0] = 0;
1335 for (nnz=0,
j=0,
i=0;
i<
graph->nrows;
i++) {
1336 if (
part[
i] == pid) {
1340 ngraph->rowptr[++
j] = nnz;
1369 nrows =
graph->nrows;
1370 rowptr =
graph->rowptr;
1371 rowind =
graph->rowind;
1372 rowval =
graph->rowval;
1374 ncolors = gk_imax(rowptr[nrows], color)+1;
1377 for (
i=0;
i<ncolors;
i++) {
1379 sgraphs[
i]->nrows =
graph->nrows;
1380 sgraphs[
i]->ncols =
graph->ncols;
1381 sgraphs[
i]->rowptr = gk_zsmalloc(nrows+1, 0,
"gk_graph_Split: sgraphs[i]->rowptr");
1384 for (
i=0;
i<nrows;
i++) {
1385 for (
j=rowptr[
i];
j<rowptr[
i+1];
j++)
1386 sgraphs[color[
j]]->rowptr[
i]++;
1388 for (
i=0;
i<ncolors;
i++)
1391 for (
i=0;
i<ncolors;
i++) {
1392 sgraphs[
i]->rowind = gk_imalloc(sgraphs[
i]->rowptr[nrows],
"gk_graph_Split: sgraphs[i]->rowind");
1393 sgraphs[
i]->rowval = gk_fmalloc(sgraphs[
i]->rowptr[nrows],
"gk_graph_Split: sgraphs[i]->rowval");
1396 for (
i=0;
i<nrows;
i++) {
1397 for (
j=rowptr[
i];
j<rowptr[
i+1];
j++) {
1398 sgraphs[color[
j]]->rowind[sgraphs[color[
j]]->rowptr[
i]] = rowind[
j];
1399 sgraphs[color[
j]]->rowval[sgraphs[color[
j]]->rowptr[
i]] = rowval[
j];
1400 sgraphs[color[
j]]->rowptr[
i]++;
1404 for (
i=0;
i<ncolors;
i++)
1433 int *rowind, *nrowind, *collen;
1434 float *rowval, *nrowval;
1439 nrows = ngraph->nrows =
graph->nrows;
1440 ncols = ngraph->ncols =
graph->ncols;
1442 rowptr =
graph->rowptr;
1443 rowind =
graph->rowind;
1444 rowval =
graph->rowval;
1446 nrowptr = ngraph->rowptr = gk_zmalloc(nrows+1,
"gk_graph_Prune: nrowptr");
1447 nrowind = ngraph->rowind = gk_imalloc(rowptr[nrows],
"gk_graph_Prune: nrowind");
1448 nrowval = ngraph->rowval = gk_fmalloc(rowptr[nrows],
"gk_graph_Prune: nrowval");
1453 collen = gk_ismalloc(ncols, 0,
"gk_graph_Prune: collen");
1455 for (
i=0;
i<nrows;
i++) {
1456 for (
j=rowptr[
i];
j<rowptr[
i+1];
j++) {
1458 collen[rowind[
j]]++;
1461 for (
i=0;
i<ncols;
i++)
1462 collen[
i] = (collen[
i] >= minf && collen[
i] <= maxf ? 1 : 0);
1465 for (nnz=0,
i=0;
i<nrows;
i++) {
1466 for (
j=rowptr[
i];
j<rowptr[
i+1];
j++) {
1467 if (collen[rowind[
j]]) {
1468 nrowind[nnz] = rowind[
j];
1469 nrowval[nnz] = rowval[
j];
1480 for (nnz=0,
i=0;
i<nrows;
i++) {
1481 if (rowptr[
i+1]-rowptr[
i] >= minf && rowptr[
i+1]-rowptr[
i] <= maxf) {
1482 for (
j=rowptr[
i];
j<rowptr[
i+1];
j++, nnz++) {
1483 nrowind[nnz] = rowind[
j];
1484 nrowval[nnz] = rowval[
j];
1520 ptr =
graph->rowptr;
1521 val =
graph->rowval;
1523 #pragma omp parallel if (ptr[n] > OMPMINOPS)
1525 #pragma omp for private(j,sum) schedule(static)
1526 for (
i=0;
i<
n;
i++) {
1527 for (sum=0.0,
j=ptr[
i];
j<ptr[
i+1];
j++){
1529 sum += val[
j]*val[
j];
1538 for (
j=ptr[
i];
j<ptr[
i+1];
j++)
1548 ptr =
graph->colptr;
1549 val =
graph->colval;
1551 #pragma omp parallel if (ptr[n] > OMPMINOPS)
1553 #pragma omp for private(j,sum) schedule(static)
1554 for (
i=0;
i<
n;
i++) {
1555 for (sum=0.0,
j=ptr[
i];
j<ptr[
i+1];
j++)
1557 sum += val[
j]*val[
j];
1565 for (
j=ptr[
i];
j<ptr[
i+1];
j++)