Actual source code: ex25.c
1: static const char help[] = "Test PetscSF with derived data types created with MPI large count\n\n";
3: #include <petscsf.h>
5: int main(int argc, char **argv)
6: {
7: PetscSF sf;
8: PetscInt i, nroots, nleaves;
9: const PetscInt m = 4, n = 64;
10: PetscSFNode *iremote = NULL;
11: PetscMPIInt rank, size;
12: int *rootdata = NULL, *leafdata = NULL;
13: MPI_Datatype newtype;
15: PetscFunctionBeginUser;
16: PetscCall(PetscInitialize(&argc, &argv, NULL, help));
17: PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
18: PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
19: PetscCheck(size == 2, PETSC_COMM_WORLD, PETSC_ERR_WRONG_MPI_SIZE, "The test can only run with two MPI ranks");
21: PetscCall(PetscSFCreate(PETSC_COMM_WORLD, &sf));
22: PetscCall(PetscSFSetFromOptions(sf));
24: if (rank == 0) {
25: nroots = n;
26: nleaves = 0;
27: } else {
28: nroots = 0;
29: nleaves = n;
30: PetscCall(PetscMalloc1(nleaves, &iremote));
31: for (i = 0; i < nleaves; i++) {
32: iremote[i].rank = 0;
33: iremote[i].index = i;
34: }
35: }
36: PetscCall(PetscSFSetGraph(sf, nroots, nleaves, NULL, PETSC_OWN_POINTER, iremote, PETSC_OWN_POINTER));
38: PetscCall(PetscCalloc2(nroots * m, &rootdata, nleaves * m, &leafdata)); // allocate fat nodes to apply a derived data type of m MPI_INTs
40: if (rank == 0) rootdata[nroots * m - 1] = 123; // set the last integer in rootdata and then check on leaves
42: #if PetscDefined(HAVE_MPI_LARGE_COUNT)
43: PetscCallMPI(MPI_Type_contiguous_c(m, MPI_INT, &newtype));
44: #else
45: PetscCallMPI(MPI_Type_contiguous(m, MPI_INT, &newtype));
46: #endif
48: PetscCallMPI(MPI_Type_commit(&newtype));
50: PetscCall(PetscSFBcastBegin(sf, newtype, rootdata, leafdata, MPI_REPLACE)); // bcast rootdata to leafdata
51: PetscCall(PetscSFBcastEnd(sf, newtype, rootdata, leafdata, MPI_REPLACE));
53: if (rank == 1) PetscCheck(leafdata[nleaves * m - 1] == 123, PETSC_COMM_SELF, PETSC_ERR_PLIB, "SF: wrong results");
55: PetscCallMPI(MPI_Type_free(&newtype));
56: PetscCall(PetscFree2(rootdata, leafdata));
57: PetscCall(PetscSFDestroy(&sf));
58: PetscCall(PetscFinalize());
59: return 0;
60: }
62: /**TEST
63: test:
64: nsize: 2
65: output_file: output/empty.out
66: args: -sf_type {{basic neighbor}}
68: TEST**/