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