1. Vijay Mahadevan
  2. petsc

Commits

Vijay Mahadevan  committed 2494be9

Fix few causes for memory leaks -- caught via -malloc_test option

  • Participants
  • Parent commits 63cd93b
  • Branches master

Comments (0)

Files changed (2)

File src/dm/impls/moab/dmmbmat.cxx

View file
  • Ignore whitespace
   DM_Moab         *dmmoab=(DM_Moab*)dm->data;
   PetscInt        *nnz=0,*onz=0;
   char            *tmp=0;
+  Mat             A;
   MatType         mtype;
 
   PetscFunctionBegin;
   ierr = DMMoab_Compute_NNZ_From_Connectivity(dm,&innz,nnz,&ionz,onz,(tmp?PETSC_TRUE:PETSC_FALSE));CHKERRQ(ierr);
 
   /* create the Matrix and set its type as specified by user */
-  ierr = MatCreate(dmmoab->pcomm->comm(), J);CHKERRQ(ierr);
-  ierr = MatSetSizes(*J, dmmoab->nloc*dmmoab->numFields, dmmoab->nloc*dmmoab->numFields, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
-  ierr = MatSetBlockSize(*J, dmmoab->bs);CHKERRQ(ierr);
-  ierr = MatSetType(*J, mtype);CHKERRQ(ierr);
-  ierr = MatSetFromOptions(*J);CHKERRQ(ierr);
+  ierr = MatCreate(dmmoab->pcomm->comm(), &A);CHKERRQ(ierr);
+  ierr = MatSetSizes(A, dmmoab->nloc*dmmoab->numFields, dmmoab->nloc*dmmoab->numFields, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
+  ierr = MatSetType(A, mtype);CHKERRQ(ierr);
+  ierr = MatSetBlockSize(A, dmmoab->bs);CHKERRQ(ierr);
+  ierr = MatSetDM(A, dm);CHKERRQ(ierr);  /* set DM reference */
+  ierr = MatSetFromOptions(A);CHKERRQ(ierr);
 
   if (!dmmoab->ltog_map) SETERRQ(dmmoab->pcomm->comm(), PETSC_ERR_ORDER, "Cannot create a DMMoab Mat without calling DMSetUp first.");
-  ierr = MatSetLocalToGlobalMapping(*J,dmmoab->ltog_map,dmmoab->ltog_map);CHKERRQ(ierr);
+  ierr = MatSetLocalToGlobalMapping(A,dmmoab->ltog_map,dmmoab->ltog_map);CHKERRQ(ierr);
 
   /* set preallocation based on different supported Mat types */
-  ierr = MatSeqAIJSetPreallocation(*J, innz, nnz);CHKERRQ(ierr);
-  ierr = MatMPIAIJSetPreallocation(*J, innz, nnz, ionz, onz);CHKERRQ(ierr);
-  ierr = MatSeqBAIJSetPreallocation(*J, dmmoab->bs, innz, nnz);CHKERRQ(ierr);
-  ierr = MatMPIBAIJSetPreallocation(*J, dmmoab->bs, innz, nnz, ionz, onz);CHKERRQ(ierr);
+  ierr = MatSeqAIJSetPreallocation(A, innz, nnz);CHKERRQ(ierr);
+  ierr = MatMPIAIJSetPreallocation(A, innz, nnz, ionz, onz);CHKERRQ(ierr);
+  ierr = MatSeqBAIJSetPreallocation(A, dmmoab->bs, innz, nnz);CHKERRQ(ierr);
+  ierr = MatMPIBAIJSetPreallocation(A, dmmoab->bs, innz, nnz, ionz, onz);CHKERRQ(ierr);
 
   /* clean up temporary memory */
   ierr = PetscFree(nnz);CHKERRQ(ierr);
   ierr = PetscFree(onz);CHKERRQ(ierr);
 
   /* set up internal matrix data-structures */
-  ierr = MatSetUp(*J);CHKERRQ(ierr);
-
-  /* set DM reference */
-  ierr = MatSetDM(*J, dm);CHKERRQ(ierr);
+  ierr = MatSetUp(A);CHKERRQ(ierr);
 
   /* set the correct NNZ pattern by setting matrix entries - make the matrix ready to use */
-  ierr = DMMoab_MatFillMatrixEntries_Private(dm,*J);CHKERRQ(ierr);
+  ierr = DMMoab_MatFillMatrixEntries_Private(dm,A);CHKERRQ(ierr);
+
+  *J = A;
   PetscFunctionReturn(0);
 }
 

File src/dm/impls/moab/examples/tests/ex2.cxx

View file
  • Ignore whitespace
   char          output_file[PETSC_MAX_PATH_LEN];   /* Output mesh file name */
   PetscBool     write_output;                        /* Write output mesh and data to file */
   PetscInt      nfields;         /* Number of fields */
-  char          *fieldnames[128]; /* Name of a defined field on the mesh */
+  char          *fieldnames[PETSC_MAX_PATH_LEN]; /* Name of a defined field on the mesh */
 } AppCtx;
 
 #undef __FUNCT__
 PetscErrorCode ProcessOptions(MPI_Comm comm, AppCtx *options)
 {
   PetscErrorCode ierr;
-  PetscBool      flg;
+  PetscBool      flg,is_user_fields;
 
   PetscFunctionBegin;
   options->debug             = PETSC_FALSE;
   options->input_file[0]     = '\0';
   ierr = PetscStrcpy(options->output_file,"ex2.h5m");CHKERRQ(ierr);
 
+  //ierr = PetscOptionsHasName("", "-fields", is_user_fields);
+
   ierr = PetscOptionsBegin(comm, "", "Meshing Problem Options", "DMMOAB");CHKERRQ(ierr);
   ierr = PetscOptionsBool("-debug", "Enable debug messages", "ex2.c", options->debug, &options->debug, NULL);CHKERRQ(ierr);
   ierr = PetscOptionsBool("-interlace", "Use interlaced arrangement for the field data", "ex2.c", options->interlace, &options->interlace, NULL);CHKERRQ(ierr);
 
 #undef __FUNCT__
 #define __FUNCT__ "CreateMesh"
-PetscErrorCode CreateMesh(MPI_Comm comm, AppCtx *user, DM *dm)
+PetscErrorCode CreateMesh(MPI_Comm comm, AppCtx *user)
 {
   PetscInt       i;
   size_t         len;
   ierr = PetscStrlen(user->input_file, &len);CHKERRQ(ierr);
   if (len) {
     if (user->debug) PetscPrintf(comm, "Loading mesh from file: %s and creating a DM object.\n",user->input_file);
-    ierr = DMMoabLoadFromFile(comm, user->dim, user->input_file, "", dm);CHKERRQ(ierr);
+    ierr = DMMoabLoadFromFile(comm, user->dim, user->input_file, "", &user->dm);CHKERRQ(ierr);
   }
   else {
     if (user->debug) {
       PetscPrintf(comm, "Creating a %D-dimensional structured %s mesh of %Dx%Dx%D in memory and creating a DM object.\n",user->dim,(user->simplex?"simplex":"regular"),user->nele,user->nele,user->nele);
     }
-    ierr = DMMoabCreateBoxMesh(comm, user->dim, user->simplex, NULL, user->nele, 1, dm);CHKERRQ(ierr);
+    ierr = DMMoabCreateBoxMesh(comm, user->dim, user->simplex, NULL, user->nele, 1, &user->dm);CHKERRQ(ierr);
   }
 
   if (user->debug) {
     PetscPrintf(comm, "Setting field names to DM: \n");
     for (i=0; i<user->nfields; i++)
-      PetscPrintf(comm, "\t Field{0} = %s.\n",user->fieldnames[i]);
+      PetscPrintf(comm, "\t Field{%D} = %s.\n",i,user->fieldnames[i]);
   }
-  ierr     = DMMoabSetFieldNames(*dm, user->nfields, (const char**)user->fieldnames);CHKERRQ(ierr);
-  ierr     = PetscObjectSetName((PetscObject) *dm, "Structured Mesh");CHKERRQ(ierr);
+  ierr     = DMMoabSetFieldNames(user->dm, user->nfields, (const char**)user->fieldnames);CHKERRQ(ierr);
+  ierr     = PetscObjectSetName((PetscObject)user->dm, "Structured Mesh");CHKERRQ(ierr);
   ierr     = PetscLogEventEnd(user->createMeshEvent,0,0,0,0);CHKERRQ(ierr);
-  user->dm = *dm;
   PetscFunctionReturn(0);
 }
 
   Vec            solution;
   Mat            system;
   MPI_Comm       comm;
+  PetscInt       i;
   PetscErrorCode ierr;
 
   ierr = PetscInitialize(&argc, &argv, NULL, help);CHKERRQ(ierr);
   comm = PETSC_COMM_WORLD;
   ierr = ProcessOptions(comm, &user);CHKERRQ(ierr);
-  ierr = CreateMesh(comm, &user, &user.dm);CHKERRQ(ierr);
+  ierr = CreateMesh(comm, &user);CHKERRQ(ierr);
 
   /* set block size */
   ierr = DMMoabSetBlockSize(user.dm, (user.interlace?user.nfields:1));CHKERRQ(ierr);
   ierr = DMSetUp(user.dm);CHKERRQ(ierr);
 
   if (user.debug) PetscPrintf(comm, "Creating a global vector defined on DM and setting random data.\n");
-  ierr = DMCreateGlobalVector(user.dm, &solution);CHKERRQ(ierr);
+  ierr = DMCreateGlobalVector(user.dm,&solution);CHKERRQ(ierr);
   ierr = PetscRandomCreate(comm,&rctx);CHKERRQ(ierr);
   ierr = PetscRandomSetType(rctx,PETSCRAND48);CHKERRQ(ierr);
   ierr = VecSetRandom(solution,rctx);CHKERRQ(ierr);
     ierr = DMMoabOutput(user.dm,(const char*)user.output_file,"");CHKERRQ(ierr);
   }
 
+  if (user.fieldnames) {
+    for(i=0; i<user.nfields; i++) {
+      ierr = PetscFree(user.fieldnames[i]);CHKERRQ(ierr);
+    }
+    //ierr = PetscFree(user.fieldnames);CHKERRQ(ierr);
+  }
   ierr = PetscRandomDestroy(&rctx);CHKERRQ(ierr);
   ierr = VecDestroy(&solution);CHKERRQ(ierr);
+  ierr = MatDestroy(&system);CHKERRQ(ierr);
   ierr = DMDestroy(&user.dm);CHKERRQ(ierr);
   ierr = PetscFinalize();
   return 0;
 }
+