Actual source code: ex152.c

  1: static const char help[] = "Test ParMETIS handling of negative weights.\n\n";

  3: /* Test contributed by John Fettig */

  5: /*
  6:   Implements two tests for a bug reported in ParMETIS. These tests are not expected to pass without the
  7:   patches in the PETSc distribution of ParMETIS. See parmetis.py

  9:  The bug was reported upstream, but has received no action so far.

 11:  http://glaros.dtc.umn.edu/gkhome/node/837
 12: */

 14: #include <petscsys.h>
 15: #include <petsc/private/matparmetisimpl.h>
 16: #include <parmetis.h>

 18: int main(int argc, char *argv[])
 19: {
 20:   PetscBool   flg;
 21:   PetscMPIInt rank, size;
 22:   idx_t       ni, isize, *vtxdist, *xadj, *adjncy, *vwgt, *part;
 23:   idx_t       wgtflag = 0, numflag = 0, ncon = 1, ndims = 3, edgecut = 0;
 24:   idx_t       options[5];
 25:   PetscReal  *xyz;
 26:   real_t     *sxyz, *tpwgts, ubvec[1];
 27:   MPI_Comm    comm;
 28:   FILE       *fp;
 29:   char        fname[PETSC_MAX_PATH_LEN], prefix[PETSC_MAX_PATH_LEN] = "";
 30:   size_t      red;

 32:   PetscFunctionBeginUser;
 33:   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
 34: #if PetscDefined(USE_64BIT_INDICES)
 35:   PetscCall(PetscPrintf(PETSC_COMM_WORLD, "This example only works with 32-bit indices\n"));
 36:   PetscCall(PetscFinalize());
 37:   return 0;
 38: #endif
 39:   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
 40:   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));

 42:   PetscOptionsBegin(PETSC_COMM_WORLD, NULL, "ParMETIS test options", "");
 43:   PetscCall(PetscOptionsString("-prefix", "Path and prefix of test file", "", prefix, prefix, sizeof(prefix), &flg));
 44:   PetscCheck(flg, PETSC_COMM_WORLD, PETSC_ERR_USER, "Must specify -prefix");
 45:   PetscOptionsEnd();

 47:   PetscCall(PetscMalloc1(size + 1, &vtxdist));

 49:   PetscCall(PetscSNPrintf(fname, sizeof(fname), "%s.%d.graph", prefix, rank));

 51:   PetscCall(PetscFOpen(PETSC_COMM_SELF, fname, "r", &fp));

 53:   red = fread(vtxdist, sizeof(idx_t), size + 1, fp);
 54:   PetscCheck(red == (size_t)(size + 1), PETSC_COMM_SELF, PETSC_ERR_SYS, "Unable to read from data file");

 56:   ni = vtxdist[rank + 1] - vtxdist[rank];

 58:   PetscCall(PetscMalloc1(ni + 1, &xadj));

 60:   red = fread(xadj, sizeof(idx_t), ni + 1, fp);
 61:   PetscCheck(red == (size_t)(ni + 1), PETSC_COMM_SELF, PETSC_ERR_SYS, "Unable to read from data file");

 63:   PetscCall(PetscMalloc1(xadj[ni], &adjncy));

 65:   for (PetscInt i = 0; i < ni; i++) {
 66:     red = fread(&adjncy[xadj[i]], sizeof(idx_t), xadj[i + 1] - xadj[i], fp);
 67:     PetscCheck(red == (size_t)(xadj[i + 1] - xadj[i]), PETSC_COMM_SELF, PETSC_ERR_SYS, "Unable to read from data file");
 68:   }

 70:   PetscCall(PetscFClose(PETSC_COMM_SELF, fp));

 72:   PetscCall(PetscSNPrintf(fname, sizeof(fname), "%s.%d.graph.xyz", prefix, rank));
 73:   PetscCall(PetscFOpen(PETSC_COMM_SELF, fname, "r", &fp));

 75:   PetscCall(PetscMalloc3(ni * ndims, &xyz, ni, &part, size, &tpwgts));
 76:   PetscCall(PetscMalloc1(ni * ndims, &sxyz));

 78:   red = fread(xyz, sizeof(PetscReal), ndims * ni, fp);
 79:   PetscCheck(red == (size_t)(ndims * ni), PETSC_COMM_SELF, PETSC_ERR_SYS, "Unable to read from data file");
 80:   for (PetscInt i = 0; i < ni * ndims; i++) sxyz[i] = (real_t)xyz[i];

 82:   PetscCall(PetscFClose(PETSC_COMM_SELF, fp));

 84:   vwgt = NULL;

 86:   for (PetscInt i = 0; i < size; i++) tpwgts[i] = (real_t)(1. / size);
 87:   isize = size;

 89:   ubvec[0]   = (real_t)1.05;
 90:   options[0] = 0;
 91:   options[1] = 2;
 92:   options[2] = 15;
 93:   options[3] = 0;
 94:   options[4] = 0;

 96:   PetscCallMPI(MPI_Comm_dup(MPI_COMM_WORLD, &comm));
 97:   PetscCallParMETIS(ParMETIS_V3_PartGeomKway, vtxdist, xadj, adjncy, vwgt, NULL, &wgtflag, &numflag, &ndims, sxyz, &ncon, &isize, tpwgts, ubvec, options, &edgecut, part, &comm);
 98:   PetscCallMPI(MPI_Comm_free(&comm));

100:   PetscCall(PetscFree(vtxdist));
101:   PetscCall(PetscFree(xadj));
102:   PetscCall(PetscFree(adjncy));
103:   PetscCall(PetscFree3(xyz, part, tpwgts));
104:   PetscCall(PetscFree(sxyz));
105:   PetscCall(PetscFinalize());
106:   return 0;
107: }

109: /*TEST

111:    build:
112:       requires: parmetis

114:    test:
115:       nsize: 2
116:       requires: parmetis datafilespath !complex double !defined(PETSC_USE_64BIT_INDICES)
117:       args: -prefix ${DATAFILESPATH}/parmetis-test/testnp2
118:       output_file: output/empty.out

120:    test:
121:       suffix: 2
122:       nsize: 4
123:       requires: parmetis datafilespath !complex double !defined(PETSC_USE_64BIT_INDICES)
124:       args: -prefix ${DATAFILESPATH}/parmetis-test/testnp4
125:       output_file: output/empty.out

127: TEST*/