Actual source code: partcreate.c

  1: #ifdef PETSC_RCS_HEADER
  2: static char vcid[] = "$Id: partcreate.c,v 1.4 2000/01/10 03:54:25 knepley Exp $";
  3: #endif

 5:  #include src/mesh/meshimpl.h

  7: /*@ 
  8:   PartitionCreate - This function creates an empty mesh partition.

 10:   Collective on Mesh

 12:   Input Parameter:
 13: . mesh - The mesh to be partitioned

 15:   Output Parameter:
 16: . part - The partition

 18:   Level: beginner

 20: .keywords: partition, mesh
 21: .seealso: PartitionSetUp(), PartitionDestroy(), GridCreate()
 22: @*/
 23: int PartitionCreate(Mesh mesh, Partition *part)
 24: {
 25:   MPI_Comm  comm;
 26:   Partition p;
 27:   int       ierr;

 30:   PetscObjectGetComm((PetscObject) mesh, &comm);
 32:   PetscHeaderCreate(p, _Partition, struct _PartitionOps, PARTITION_COOKIE, -1, "Partition", comm, PartitionDestroy, PartitionView);
 33:   PetscLogObjectCreate(p);
 34:   PetscLogObjectMemory(p, sizeof(struct _Partition));
 35:   PetscMemzero(p->ops, sizeof(struct _PartitionOps));
 36:   p->bops->publish        = PETSC_NULL /* PartitionPublish_Petsc */;
 37:   p->type_name            = PETSC_NULL;
 38:   p->mesh                 = mesh;
 39:   p->data                 = PETSC_NULL;
 40:   p->isElementPartitioned = PETSC_FALSE;
 41:   p->ordering             = PETSC_NULL;
 42:   p->numLocElements       = 0;
 43:   p->numElements          = 0;
 44:   p->numOverlapElements   = 0;
 45:   p->firstElement         = PETSC_NULL;
 46:   p->ghostElements        = PETSC_NULL;
 47:   p->ghostElementProcs    = PETSC_NULL;
 48:   MPI_Comm_size(comm, &p->numProcs);
 49:   MPI_Comm_size(comm, &p->rank);

 51:   *part = p;
 52:   return(0);
 53: }

 55: /*@ 
 56:   PartitionSerialize - This function stores or recreates a partition using a viewer for a binary file.

 58:   Collective on MPI_Comm

 60:   Input Parameters:
 61: + mesh   - The mesh that is partitioned
 62: . viewer - The viewer context
 63: - store  - This flag is PETSC_TRUE is data is being written, otherwise it will be read

 65:   Output Parameter:
 66: . part   - The partition

 68:   Level: beginner

 70: .keywords: partition, serialize
 71: .seealso: MeshSerialize(), GridSerialize()
 72: @*/
 73: int PartitionSerialize(Mesh mesh, Partition *part, PetscViewer viewer, PetscTruth store)
 74: {
 75:   MPI_Comm   comm;
 76:   int      (*serialize)(Mesh, Partition *, PetscViewer, PetscTruth);
 77:   int        fd, len;
 78:   char      *name;
 79:   PetscTruth match;
 80:   int        ierr;


 86:   PetscTypeCompare((PetscObject) viewer, PETSC_VIEWER_BINARY, &match);
 87:   if (match == PETSC_FALSE) SETERRQ(PETSC_ERR_ARG_WRONG, "Must be binary viewer");
 88:   PetscViewerBinaryGetDescriptor(viewer, &fd);
 89:   PetscObjectGetComm((PetscObject) mesh, &comm);

 91:   if (store) {
 93:     PetscStrlen((*part)->class_name, &len);
 94:     PetscBinaryWrite(fd, &len,                     1,   PETSC_INT,  0);
 95:     PetscBinaryWrite(fd,  (*part)->class_name,     len, PETSC_CHAR, 0);
 96:     PetscStrlen((*part)->serialize_name, &len);
 97:     PetscBinaryWrite(fd, &len,                     1,   PETSC_INT,  0);
 98:     PetscBinaryWrite(fd,  (*part)->serialize_name, len, PETSC_CHAR, 0);
 99:     PetscFListFind(comm, PartitionSerializeList, (*part)->serialize_name, (void (**)(void)) &serialize);
100:     if (!serialize) SETERRQ(PETSC_ERR_ARG_WRONG, "Type cannot be serialized");
101:     (*serialize)(mesh, part, viewer, store);
102:   } else {
103:     PetscBinaryRead(fd, &len,    1,   PETSC_INT);
104:     PetscMalloc((len+1) * sizeof(char), &name);
105:     name[len] = 0;
106:     PetscBinaryRead(fd,  name,   len, PETSC_CHAR);
107:     PetscStrcmp(name, "Partition", &match);
108:     PetscFree(name);
109:     if (match == PETSC_FALSE) SETERRQ(PETSC_ERR_ARG_WRONG, "Non-partition object");
110:     /* Dispatch to the correct routine */
111:     if (!PartitionSerializeRegisterAllCalled) {
112:       PartitionSerializeRegisterAll(PETSC_NULL);
113:     }
114:     if (!PartitionSerializeList) SETERRQ(PETSC_ERR_ARG_CORRUPT, "Could not find table of methods");
115:     PetscBinaryRead(fd, &len,    1,   PETSC_INT);
116:     PetscMalloc((len+1) * sizeof(char), &name);
117:     name[len] = 0;
118:     PetscBinaryRead(fd,  name,   len, PETSC_CHAR);
119:     PetscFListFind(comm, PartitionSerializeList, name, (void (**)(void)) &serialize);
120:     if (!serialize) SETERRQ(PETSC_ERR_ARG_WRONG, "Type cannot be serialized");
121:     (*serialize)(mesh, part, viewer, store);
122:     PetscStrfree((*part)->serialize_name);
123:     (*part)->serialize_name = name;
124:   }

126:   return(0);
127: }