Actual source code: ex3.c

  1: static const char help[] = "Test freeing of MPI types in PetscSF\n\n";

  3: #include <petscvec.h>
  4: #include <petscsf.h>

  6: int main(int argc, char **argv)
  7: {
  8:   PetscSF      sf;
  9:   Vec          A, Aout;
 10:   PetscScalar *bufA;
 11:   PetscScalar *bufAout;
 12:   PetscMPIInt  rank, size;
 13:   PetscInt     nroots, nleaves;
 14:   PetscInt    *ilocal;
 15:   PetscSFNode *iremote;
 16:   PetscBool    test_dupped_type;
 17:   MPI_Datatype contig;

 19:   PetscFunctionBeginUser;
 20:   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
 21:   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
 22:   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));

 24:   PetscCheck(size == 1, PETSC_COMM_WORLD, PETSC_ERR_WRONG_MPI_SIZE, "Only coded for one MPI process");

 26:   PetscOptionsBegin(PETSC_COMM_WORLD, "", "PetscSF type freeing options", "none");
 27:   test_dupped_type = PETSC_FALSE;
 28:   PetscCall(PetscOptionsBool("-test_dupped_type", "Test dupped input type", "", test_dupped_type, &test_dupped_type, NULL));
 29:   PetscOptionsEnd();

 31:   PetscCall(PetscSFCreate(PETSC_COMM_WORLD, &sf));
 32:   PetscCall(PetscSFSetFromOptions(sf));

 34:   nleaves = 1;
 35:   nroots  = 1;
 36:   PetscCall(PetscMalloc1(nleaves, &ilocal));

 38:   for (PetscInt i = 0; i < nleaves; i++) ilocal[i] = i;

 40:   PetscCall(PetscMalloc1(nleaves, &iremote));
 41:   iremote[0].rank  = 0;
 42:   iremote[0].index = 0;
 43:   PetscCall(PetscSFSetGraph(sf, nroots, nleaves, ilocal, PETSC_OWN_POINTER, iremote, PETSC_OWN_POINTER));
 44:   PetscCall(PetscSFSetUp(sf));
 45:   PetscCall(PetscSFView(sf, PETSC_VIEWER_STDOUT_WORLD));
 46:   PetscCall(VecCreate(PETSC_COMM_WORLD, &A));
 47:   PetscCall(VecSetSizes(A, 4, PETSC_DETERMINE));
 48:   PetscCall(VecSetFromOptions(A));
 49:   PetscCall(VecSetUp(A));

 51:   PetscCall(VecDuplicate(A, &Aout));
 52:   PetscCall(VecGetArray(A, &bufA));
 53:   for (PetscInt i = 0; i < 4; i++) bufA[i] = (PetscScalar)i;
 54:   PetscCall(VecRestoreArray(A, &bufA));

 56:   PetscCall(VecGetArrayRead(A, (const PetscScalar **)&bufA));
 57:   PetscCall(VecGetArray(Aout, &bufAout));

 59:   PetscCallMPI(MPI_Type_contiguous(4, MPIU_SCALAR, &contig));
 60:   PetscCallMPI(MPI_Type_commit(&contig));

 62:   if (test_dupped_type) {
 63:     MPI_Datatype tmp;
 64:     PetscCallMPI(MPI_Type_dup(contig, &tmp));
 65:     PetscCallMPI(MPI_Type_free(&contig));
 66:     contig = tmp;
 67:   }
 68:   PetscCall(PetscSFRegisterPersistent(sf, contig, bufA, bufAout));
 69:   for (PetscInt i = 0; i < 10000; i++) {
 70:     PetscCall(PetscSFBcastBegin(sf, contig, bufA, bufAout, MPI_REPLACE));
 71:     PetscCall(PetscSFBcastEnd(sf, contig, bufA, bufAout, MPI_REPLACE));
 72:   }
 73:   PetscCall(PetscSFDeregisterPersistent(sf, contig, bufA, bufAout));
 74:   PetscCall(VecRestoreArrayRead(A, (const PetscScalar **)&bufA));
 75:   PetscCall(VecRestoreArray(Aout, &bufAout));

 77:   PetscCall(VecView(Aout, PETSC_VIEWER_STDOUT_WORLD));
 78:   PetscCall(VecDestroy(&A));
 79:   PetscCall(VecDestroy(&Aout));
 80:   PetscCall(PetscSFDestroy(&sf));
 81:   PetscCallMPI(MPI_Type_free(&contig));
 82:   PetscCall(PetscFinalize());
 83:   return 0;
 84: }

 86: /*TEST

 88:    test:
 89:       suffix: basic
 90:       args: -sf_type basic

 92:    test:
 93:       suffix: basic_dupped
 94:       args: -test_dupped_type -sf_type basic

 96:    test:
 97:       suffix: window
 98:       filter: grep -v "type" | grep -v "sort"
 99:       args: -sf_type window -sf_window_sync {{fence active lock}} -sf_window_flavor {{create allocate dynamic}}
100:       requires: defined(PETSC_HAVE_MPI_ONE_SIDED) defined(PETSC_HAVE_MPI_FEATURE_DYNAMIC_WINDOW)

102:    test:
103:       suffix: window_dupped
104:       filter: grep -v "type" | grep -v "sort"
105:       args: -test_dupped_type -sf_type window -sf_window_sync {{fence active lock}} -sf_window_flavor {{create allocate dynamic}}
106:       requires: defined(PETSC_HAVE_MPI_ONE_SIDED) defined(PETSC_HAVE_MPI_FEATURE_DYNAMIC_WINDOW)

108:    test:
109:       suffix: window_shared
110:       output_file: output/ex3_window.out
111:       filter: grep -v "type" | grep -v "sort"
112:       args: -sf_type window -sf_window_sync {{fence active lock}} -sf_window_flavor shared
113:       requires: defined(PETSC_HAVE_MPI_PROCESS_SHARED_MEMORY) defined(PETSC_HAVE_MPI_ONE_SIDED) !defined(PETSC_HAVE_I_MPI)

115:    test:
116:       suffix: window_dupped_shared
117:       output_file: output/ex3_window_dupped.out
118:       filter: grep -v "type" | grep -v "sort"
119:       args: -test_dupped_type -sf_type window -sf_window_sync {{fence active lock}} -sf_window_flavor shared
120:       requires: defined(PETSC_HAVE_MPI_PROCESS_SHARED_MEMORY) defined(PETSC_HAVE_MPI_ONE_SIDED) !defined(PETSC_HAVE_I_MPI)

122: TEST*/