Actual source code: ex53.c

petsc-3.13.6 2020-09-29
Report Typos and Errors
  1: static char help[] = "Test resource recycling and MPI_Comm and keyval creation in mpi or mpiuni\n";

  3:  #include <petscsys.h>

  5: #define  CHKMPIERR(ierr)  do {if (ierr) MPI_Abort(MPI_COMM_WORLD, ierr);} while(0)

  7: int main(int argc,char **argv)
  8: {
 10:   PetscInt       i;
 11:   PetscMPIInt    key1,key2,attr1=100,attr2=200,*attr,flag;
 12:   MPI_Comm       newcomm;

 14:   MPI_Init(&argc,&argv);CHKMPIERR(ierr);

 16:   /* Repeated keyval or comm create/free should not blow up MPI */
 17:   for (i=0; i<500; i++) {
 18:     MPI_Comm_create_keyval(MPI_COMM_NULL_COPY_FN,MPI_COMM_NULL_DELETE_FN,&key1,NULL);CHKMPIERR(ierr);
 19:     MPI_Comm_free_keyval(&key1);CHKMPIERR(ierr);
 20:     MPI_Comm_dup(MPI_COMM_WORLD,&newcomm);CHKMPIERR(ierr);
 21:     MPI_Comm_free(&newcomm);CHKMPIERR(ierr);
 22:   }

 24:   /* The following keyval/attr code exposes a bug in old mpiuni code, where it had wrong newcomm returned in MPI_Comm_dup. */
 25:   MPI_Comm_create_keyval(MPI_COMM_NULL_COPY_FN,MPI_COMM_NULL_DELETE_FN,&key1,NULL);CHKMPIERR(ierr);
 26:   MPI_Comm_create_keyval(MPI_COMM_NULL_COPY_FN,MPI_COMM_NULL_DELETE_FN,&key2,NULL);CHKMPIERR(ierr);
 27:   MPI_Comm_dup(MPI_COMM_WORLD,&newcomm);CHKMPIERR(ierr);
 28:   if (MPI_COMM_WORLD == newcomm) printf("Error: wrong newcomm returned by MPI_Comm_dup()\n");

 30:   MPI_Comm_set_attr(MPI_COMM_WORLD,key1,&attr1);CHKMPIERR(ierr);
 31:   MPI_Comm_set_attr(newcomm,key2,&attr2);CHKMPIERR(ierr);
 32:   MPI_Comm_get_attr(newcomm,key1,&attr,&flag);CHKMPIERR(ierr);
 33:   if (flag) printf("Error: newcomm should not have attribute for keyval %d\n", (int)key1);
 34:   MPI_Comm_get_attr(MPI_COMM_WORLD,key1,&attr,&flag);CHKMPIERR(ierr);
 35:   if (*attr != attr1) printf("Error: expected attribute %d, but got %d\n", (int)attr1, (int)*attr);
 36:   MPI_Comm_get_attr(newcomm,key2,&attr,&flag);CHKMPIERR(ierr);
 37:   if (*attr != attr2) printf("Error: expected attribute %d, but got %d\n", (int)attr2, (int)*attr);

 39:   MPI_Comm_delete_attr(MPI_COMM_WORLD,key1);CHKMPIERR(ierr);
 40:   MPI_Comm_delete_attr(newcomm,key2);CHKMPIERR(ierr);
 41:   MPI_Comm_free_keyval(&key1);CHKMPIERR(ierr);
 42:   MPI_Comm_free_keyval(&key2);CHKMPIERR(ierr);
 43:   MPI_Comm_free(&newcomm);CHKMPIERR(ierr);

 45:   /* Init/Finalize PETSc multiple times when MPI is initialized */
 46:   for (i=0; i<500; i++) {
 47:     PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
 48:     PetscFinalize();if (ierr) return ierr;
 49:   }

 51:   MPI_Finalize();
 52:   return ierr;
 53: }

 55: /*TEST
 56:    # Elemental in debug mode has bugs that it can not be repeatedly init/finalize'd for more than 300 times
 57:    testset:
 58:     output_file: output/ex53_1.out
 59:     test:
 60:       suffix: 1
 61:       requires: !elemental

 63:     test:
 64:       suffix: 2
 65:       requires: elemental !define(PETSC_USE_DEBUG)
 66: TEST*/