xref: /petsc/src/mat/tests/ex152.c (revision 58d68138c660dfb4e9f5b03334792cd4f2ffd7cc)
1 static const char help[] = "Test ParMETIS handling of negative weights.\n\n";
2 
3 /* Test contributed by John Fettig */
4 
5 /*
6  * This file 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
8  *
9  *
10  * The bug was reported upstream, but has received no action so far.
11  *
12  * http://glaros.dtc.umn.edu/gkhome/node/837
13  *
14  */
15 
16 #include <petscsys.h>
17 #include <parmetis.h>
18 
19 #define PetscCallPARMETIS(...) \
20   do { \
21     int metis_ierr = __VA_ARGS__; \
22     PetscCheck(metis_ierr != METIS_ERROR_INPUT, PETSC_COMM_SELF, PETSC_ERR_LIB, "ParMETIS error due to wrong inputs and/or options"); \
23     PetscCheck(metis_ierr != METIS_ERROR_MEMORY, PETSC_COMM_SELF, PETSC_ERR_LIB, "ParMETIS error due to insufficient memory"); \
24     PetscCheck(metis_ierr != METIS_ERROR, PETSC_COMM_SELF, PETSC_ERR_LIB, "ParMETIS general error"); \
25   } while (0)
26 
27 int main(int argc, char *argv[]) {
28   PetscBool   flg;
29   PetscMPIInt rank, size;
30   idx_t       ni, isize, *vtxdist, *xadj, *adjncy, *vwgt, *part;
31   idx_t       wgtflag = 0, numflag = 0, ncon = 1, ndims = 3, edgecut = 0;
32   idx_t       options[5];
33   PetscReal  *xyz;
34   real_t     *sxyz, *tpwgts, ubvec[1];
35   MPI_Comm    comm;
36   FILE       *fp;
37   char        fname[PETSC_MAX_PATH_LEN], prefix[PETSC_MAX_PATH_LEN] = "";
38   size_t      red;
39 
40   PetscFunctionBeginUser;
41   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
42 #if defined(PETSC_USE_64BIT_INDICES)
43   PetscCall(PetscPrintf(PETSC_COMM_WORLD, "This example only works with 32 bit indices\n"));
44   PetscCall(PetscFinalize());
45   return 0;
46 #endif
47   MPI_Comm_rank(PETSC_COMM_WORLD, &rank);
48   MPI_Comm_size(PETSC_COMM_WORLD, &size);
49 
50   PetscOptionsBegin(PETSC_COMM_WORLD, NULL, "Parmetis test options", "");
51   PetscCall(PetscOptionsString("-prefix", "Path and prefix of test file", "", prefix, prefix, sizeof(prefix), &flg));
52   PetscCheck(flg, PETSC_COMM_WORLD, PETSC_ERR_USER, "Must specify -prefix");
53   PetscOptionsEnd();
54 
55   PetscCall(PetscMalloc1(size + 1, &vtxdist));
56 
57   PetscCall(PetscSNPrintf(fname, sizeof(fname), "%s.%d.graph", prefix, rank));
58 
59   PetscCall(PetscFOpen(PETSC_COMM_SELF, fname, "r", &fp));
60 
61   red = fread(vtxdist, sizeof(idx_t), size + 1, fp);
62   PetscCheck(red == (size_t)(size + 1), PETSC_COMM_SELF, PETSC_ERR_SYS, "Unable to read from data file");
63 
64   ni = vtxdist[rank + 1] - vtxdist[rank];
65 
66   PetscCall(PetscMalloc1(ni + 1, &xadj));
67 
68   red = fread(xadj, sizeof(idx_t), ni + 1, fp);
69   PetscCheck(red == (size_t)(ni + 1), PETSC_COMM_SELF, PETSC_ERR_SYS, "Unable to read from data file");
70 
71   PetscCall(PetscMalloc1(xadj[ni], &adjncy));
72 
73   for (PetscInt i = 0; i < ni; i++) {
74     red = fread(&adjncy[xadj[i]], sizeof(idx_t), xadj[i + 1] - xadj[i], fp);
75     PetscCheck(red == (size_t)(xadj[i + 1] - xadj[i]), PETSC_COMM_SELF, PETSC_ERR_SYS, "Unable to read from data file");
76   }
77 
78   PetscCall(PetscFClose(PETSC_COMM_SELF, fp));
79 
80   PetscCall(PetscSNPrintf(fname, sizeof(fname), "%s.%d.graph.xyz", prefix, rank));
81   PetscCall(PetscFOpen(PETSC_COMM_SELF, fname, "r", &fp));
82 
83   PetscCall(PetscMalloc3(ni * ndims, &xyz, ni, &part, size, &tpwgts));
84   PetscCall(PetscMalloc1(ni * ndims, &sxyz));
85 
86   red = fread(xyz, sizeof(PetscReal), ndims * ni, fp);
87   PetscCheck(red == (size_t)(ndims * ni), PETSC_COMM_SELF, PETSC_ERR_SYS, "Unable to read from data file");
88   for (PetscInt i = 0; i < ni * ndims; i++) sxyz[i] = (size_t)xyz[i];
89 
90   PetscCall(PetscFClose(PETSC_COMM_SELF, fp));
91 
92   vwgt = NULL;
93 
94   for (PetscInt i = 0; i < size; i++) tpwgts[i] = 1. / size;
95   isize = size;
96 
97   ubvec[0]   = 1.05;
98   options[0] = 0;
99   options[1] = 2;
100   options[2] = 15;
101   options[3] = 0;
102   options[4] = 0;
103 
104   PetscCallMPI(MPI_Comm_dup(MPI_COMM_WORLD, &comm));
105   PetscCallPARMETIS(ParMETIS_V3_PartGeomKway(vtxdist, xadj, adjncy, vwgt, NULL, &wgtflag, &numflag, &ndims, sxyz, &ncon, &isize, tpwgts, ubvec, options, &edgecut, part, &comm));
106   PetscCallMPI(MPI_Comm_free(&comm));
107 
108   PetscCall(PetscFree(vtxdist));
109   PetscCall(PetscFree(xadj));
110   PetscCall(PetscFree(adjncy));
111   PetscCall(PetscFree3(xyz, part, tpwgts));
112   PetscCall(PetscFree(sxyz));
113   PetscCall(PetscFinalize());
114   return 0;
115 }
116 
117 /*TEST
118 
119    build:
120       requires: parmetis
121 
122    test:
123       nsize: 2
124       requires: parmetis datafilespath !complex double !defined(PETSC_USE_64BIT_INDICES)
125       args: -prefix ${DATAFILESPATH}/parmetis-test/testnp2
126 
127    test:
128       suffix: 2
129       nsize: 4
130       requires: parmetis datafilespath !complex double !defined(PETSC_USE_64BIT_INDICES)
131       args: -prefix ${DATAFILESPATH}/parmetis-test/testnp4
132 
133 TEST*/
134