int orderLoad ( Order * const ordeptr, FILE * const stream) { PASTIX_INT versval; /* Version number */ PASTIX_INT cblknbr; PASTIX_INT cblknum; PASTIX_INT vertnbr; PASTIX_INT vertnum; PASTIX_INT vertnnd; PASTIX_INT * permtax; PASTIX_INT * peritax; int i; if ((intLoad (stream, &versval) + intLoad (stream, &cblknbr) + intLoad (stream, &vertnbr) != 3) || (versval != 0) || (cblknbr > vertnbr)) { errorPrint ("orderLoad: bad input (1)"); return (1); } if (((ordeptr->rangtab = (PASTIX_INT *) memAlloc ((cblknbr + 1) * sizeof (PASTIX_INT))) == NULL) || ((ordeptr->permtab = (PASTIX_INT *) memAlloc (vertnbr * sizeof (PASTIX_INT))) == NULL) || ((ordeptr->peritab = (PASTIX_INT *) memAlloc (vertnbr * sizeof (PASTIX_INT))) == NULL)) { errorPrint ("orderLoad: out of memory"); orderExit (ordeptr); orderInit (ordeptr); return (1); } ordeptr->cblknbr = cblknbr; for (cblknum = 0, i = 1; (i == 1) && (cblknum <= cblknbr); cblknum ++) /* Read column-block data */ i = intLoad (stream, &ordeptr->rangtab[cblknum]); for (vertnum = 0; (i == 1) && (vertnum < vertnbr); vertnum ++) /* Read direct permutation */ i = intLoad (stream, &ordeptr->permtab[vertnum]); if (i != 1) { errorPrint ("orderLoad: bad input (2)"); orderExit (ordeptr); orderInit (ordeptr); return (1); } permtax = ordeptr->permtab - ordeptr->rangtab[0]; peritax = ordeptr->peritab - ordeptr->rangtab[0]; for (vertnum = ordeptr->rangtab[0], vertnnd = vertnum + vertnbr; /* Build inverse permutation */ vertnum < vertnnd; vertnum ++) peritax[permtax[vertnum]] = vertnum; return (0); }
int readMMHeader( SCOTCH_Num * vertnbr, SCOTCH_Num * edgenbr, FILE * const stream) { int c; SCOTCH_Num vertnum0, vertnum1; int symmetric = 0; char firstline[1024]; int pos = 0; memSet(firstline, '\0', 1024); fgets(firstline, 16, stream); if (strcmp(firstline, "%%MatrixMarket ")) return (-1); while (((c = fgetc(stream)) != '\n') && (c != EOF)) { firstline[pos++] = toupper(c); if (pos == 1024) return (-1); } firstline[pos] = '\0'; if (strstr(firstline, "SYMMETRIC")) symmetric = 1; while ((c = fgetc(stream)) == '%') { if (skipLine(stream) != 0) return (-1); /* End of file reached */ } ungetc (c, stream); if (intLoad (stream, &vertnum0) != 1) { /* Read row number */ return (-1); } if (intLoad (stream, &vertnum1) != 1) { /* Read col number */ return (-1); } if (vertnum0 != vertnum1) { /* Non square matrix */ return (-1); } *vertnbr = vertnum1; if (intLoad (stream, edgenbr) != 1) { /* Read edge number */ return (-1); } *edgenbr -= *vertnbr; /* No loop in graph */ return (symmetric); }
int main ( int argc, char * argv[]) { SCOTCH_Strat bipastrat; /* Bipartitioning strategy */ SCOTCH_Arch archdat; /* Target (terminal) architecture */ SCOTCH_Graph grafdat; /* Source graph to turn into architecture */ SCOTCH_Num vertnbr; /* Number of vertices in graph */ SCOTCH_Num * vlbltab; /* Pointer to vertex label array, if present */ SCOTCH_Num listnbr; /* Size of list array */ SCOTCH_Num * listtab; /* Pointer to list array */ C_VertSort * sorttab; /* Vertex label sort area */ SCOTCH_Num baseval; int flagval; /* Process flags */ int i; errorProg ("amk_grf"); if ((argc >= 2) && (argv[1][0] == '?')) { /* If need for help */ usagePrint (stdout, C_usageList); return (0); } flagval = C_FLAGNONE; SCOTCH_stratInit (&bipastrat); fileBlockInit (C_fileTab, C_FILENBR); /* Set default stream pointers */ for (i = 1; i < argc; i ++) { /* Loop for all option codes */ if ((argv[i][0] != '-') || (argv[i][1] == '\0') || (argv[i][1] == '.')) { /* If found a file name */ if (C_fileNum < C_FILEARGNBR) /* File name has been given */ fileBlockName (C_fileTab, C_fileNum ++) = argv[i]; else { errorPrint ("main: too many file names given"); return (1); } } else { /* If found an option name */ switch (argv[i][1]) { case 'B' : /* Bipartitioning strategy */ case 'b' : SCOTCH_stratExit (&bipastrat); SCOTCH_stratInit (&bipastrat); if ((SCOTCH_stratGraphBipart (&bipastrat, &argv[i][2])) != 0) { errorPrint ("main: invalid bipartitioning strategy"); return (1); } break; case 'H' : /* Give the usage message */ case 'h' : usagePrint (stdout, C_usageList); return (0); case 'L' : /* Input vertex list */ case 'l' : flagval |= C_FLAGVRTINP; if (argv[i][2] != '\0') C_filenamevrtinp = &argv[i][2]; break; case 'V' : fprintf (stderr, "amk_grf, version " SCOTCH_VERSION_STRING "\n"); fprintf (stderr, "Copyright 2004,2007,2008,2010-2012,2014 IPB, Universite de Bordeaux, INRIA & CNRS, France\n"); fprintf (stderr, "This software is libre/free software under CeCILL-C -- see the user's manual for more information\n"); return (0); default : errorPrint ("main: unprocessed option '%s'", argv[i]); return (1); } } } fileBlockOpen (C_fileTab, C_FILENBR); /* Open all files */ SCOTCH_graphInit (&grafdat); /* Create graph structure */ SCOTCH_graphLoad (&grafdat, C_filepntrgrfinp, -1, 0); /* Load source graph */ SCOTCH_graphData (&grafdat, &baseval, &vertnbr, NULL, NULL, NULL, /* Get graph data */ &vlbltab, NULL, NULL, NULL); listnbr = 0; /* Initialize vertex list */ listtab = NULL; if (flagval & C_FLAGVRTINP) { /* If list of vertices provided */ SCOTCH_Num listnum; if ((intLoad (C_filepntrvrtinp, &listnbr) != 1) || /* Read list size */ (listnbr < 0) || (listnbr > vertnbr)) { errorPrint ("main: bad list input (1)"); return (1); } if ((listtab = (SCOTCH_Num *) memAlloc (listnbr * sizeof (SCOTCH_Num) + 1)) == NULL) { errorPrint ("main: out of memory (1)"); return (1); } for (listnum = 0; listnum < listnbr; listnum ++) { /* Read list data */ if (intLoad (C_filepntrvrtinp, &listtab[listnum]) != 1) { errorPrint ("main: bad list input (2)"); return (1); } } intSort1asc1 (listtab, listnbr); for (listnum = 0; listnum < listnbr - 1; listnum ++) { /* Search for duplicates */ if (listtab[listnum] == listtab[listnum + 1]) { errorPrint ("main: duplicate list labels"); memFree (listtab); return (1); } } if (vlbltab != NULL) { /* If graph has vertex labels */ SCOTCH_Num vertnum; if ((sorttab = (C_VertSort *) memAlloc (vertnbr * sizeof (C_VertSort))) == NULL) { errorPrint ("main: out of memory (2)"); memFree (listtab); return (1); } for (vertnum = 0; vertnum < vertnbr; vertnum ++) { /* Initialize sort area */ sorttab[vertnum].vlblnum = vlbltab[vertnum]; sorttab[vertnum].vertnum = vertnum; } intSort2asc1 (sorttab, vertnbr); /* Sort by ascending labels */ for (listnum = 0, vertnum = 0; listnum < listnbr; listnum ++) { /* For all labels in list */ while ((vertnum < vertnbr) && (sorttab[vertnum].vlblnum < listtab[listnum])) vertnum ++; /* Search vertex graph with corresponding label */ if ((vertnum >= vertnbr) || /* If label not found */ (sorttab[vertnum].vlblnum > listtab[listnum])) { errorPrint ("main: list label '" SCOTCH_NUMSTRING "' not in graph", (SCOTCH_Num) listtab[listnum]); memFree (sorttab); memFree (listtab); return (1); } listtab[listnum] = sorttab[vertnum ++].vertnum; /* Replace label by number */ } memFree (sorttab); /* Free sort area */ } } SCOTCH_archInit (&archdat); /* Initialize target architecture */ SCOTCH_archBuild (&archdat, &grafdat, listnbr, listtab, &bipastrat); /* Compute architecture */ SCOTCH_archSave (&archdat, C_filepntrtgtout); /* Write target architecture */ fileBlockClose (C_fileTab, C_FILENBR); /* Always close explicitely to end potential (un)compression tasks */ SCOTCH_graphExit (&grafdat); /* Free target graph */ SCOTCH_archExit (&archdat); /* Free target architecture */ SCOTCH_stratExit (&bipastrat); /* Free strategy string */ if (listtab != NULL) /* If vertex list provided */ memFree (listtab); /* Free it */ #ifdef COMMON_PTHREAD pthread_exit ((void *) 0); /* Allow potential (un)compression tasks to complete */ #endif /* COMMON_PTHREAD */ return (0); }
static int C_graphScat ( FILE * const stream, SCOTCH_Num procnbr, char * const nameptr) { SCOTCH_Num versval; SCOTCH_Num propval; char proptab[4]; int flagtab[3]; SCOTCH_Num baseval; SCOTCH_Num vertglbnbr; SCOTCH_Num edgeglbnbr; SCOTCH_Num procnum; if (intLoad (stream, &versval) != 1) { /* Read version number */ errorPrint ("C_graphScat: bad input (1)"); return (1); } if (versval != 0) { /* If version not zero */ errorPrint ("C_graphScat: only centralized graphs supported"); return (1); } if ((intLoad (stream, &vertglbnbr) != 1) || /* Read rest of header */ (intLoad (stream, &edgeglbnbr) != 1) || (intLoad (stream, &baseval) != 1) || (intLoad (stream, &propval) != 1) || (propval < 0) || (propval > 111)) { errorPrint ("C_graphScat: bad input (2)"); return (1); } sprintf (proptab, "%3.3d", (int) propval); /* Compute file properties */ flagtab[0] = proptab[0] - '0'; /* Vertex labels flag */ flagtab[1] = proptab[1] - '0'; /* Edge weights flag */ flagtab[2] = proptab[2] - '0'; /* Vertex loads flag */ for (procnum = 0; procnum < procnbr; procnum ++) { char * nametmp; FILE * ostream; SCOTCH_Num vertlocnbr; SCOTCH_Num vertlocnum; SCOTCH_Num edgelocnbr; nametmp = nameptr; if ((fileNameDistExpand (&nametmp, procnbr, procnum, -1) != 0) || ((ostream = fopen (nametmp, "w+")) == NULL)) { errorPrint ("C_graphScat: cannot open file"); return (1); } memFree (nametmp); /* Expanded name no longer needed */ vertlocnbr = DATASIZE (vertglbnbr, procnbr, procnum); if (fprintf (ostream, "2\n" SCOTCH_NUMSTRING "\t" SCOTCH_NUMSTRING "\n" SCOTCH_NUMSTRING "\t" SCOTCH_NUMSTRING "\n" SCOTCH_NUMSTRING "\t%015d\n" SCOTCH_NUMSTRING "\t%3s\n", /* Write file header */ (SCOTCH_Num) procnbr, (SCOTCH_Num) procnum, (SCOTCH_Num) vertglbnbr, (SCOTCH_Num) edgeglbnbr, (SCOTCH_Num) vertlocnbr, 0, /* Number of edges not yet known */ (SCOTCH_Num) baseval, proptab) == EOF) { errorPrint ("C_graphScat: bad output (1)"); return (1); } for (vertlocnum = edgelocnbr = 0; vertlocnum < vertlocnbr; vertlocnum ++) { SCOTCH_Num degrval; if (flagtab[0] != 0) { /* If must read label */ SCOTCH_Num vlblval; /* Value where to read vertex label */ if (intLoad (stream, &vlblval) != 1) { /* Read label data */ errorPrint ("C_graphScat: bad input (3)"); return (1); } intSave (ostream, vlblval); putc ('\t', ostream); } if (flagtab[2] != 0) { /* If must read vertex load */ SCOTCH_Num veloval; /* Value where to read vertex load */ if (intLoad (stream, &veloval) != 1) { /* Read vertex load data */ errorPrint ("C_graphScat: bad input (4)"); return (1); } intSave (ostream, veloval); putc ('\t', ostream); } if (intLoad (stream, °rval) != 1) { /* Read vertex degree */ errorPrint ("C_graphScat: bad input (5)"); return (1); } intSave (ostream, degrval); edgelocnbr += degrval; for ( ; degrval > 0; degrval --) { SCOTCH_Num edgeval; /* Value where to read edge end */ if (flagtab[1] != 0) { /* If must read edge load */ SCOTCH_Num edloval; /* Value where to read edge load */ if (intLoad (stream, &edloval) != 1) { /* Read edge load data */ errorPrint ("C_graphScat: bad input (6)"); return (1); } putc ('\t', ostream); intSave (ostream, edloval); } if (intLoad (stream, &edgeval) != 1) { /* Read edge data */ errorPrint ("C_graphScat: bad input (7)"); return (1); } putc ('\t', ostream); intSave (ostream, edgeval); } putc ('\n', ostream); } rewind (ostream); if (fprintf (ostream, "2\n" SCOTCH_NUMSTRING "\t" SCOTCH_NUMSTRING "\n" SCOTCH_NUMSTRING "\t" SCOTCH_NUMSTRING "\n" SCOTCH_NUMSTRING "\t%015lld\n" SCOTCH_NUMSTRING "\t%3s\n", /* Write file header */ (SCOTCH_Num) procnbr, (SCOTCH_Num) procnum, (SCOTCH_Num) vertglbnbr, (SCOTCH_Num) edgeglbnbr, (SCOTCH_Num) vertlocnbr, (long long) edgelocnbr, /* Now we know the exact number of edges */ (SCOTCH_Num) baseval, proptab) == EOF) { errorPrint ("C_graphScat: bad output (2)"); return (1); } fclose (ostream); } return (0); }