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: #undef  __FUNCT__
  9: /*@ 
 10:   PartitionCreate - This function creates an empty mesh partition.

 12:   Collective on Mesh

 14:   Input Parameter:
 15: . mesh - The mesh to be partitioned

 17:   Output Parameter:
 18: . part - The partition

 20:   Level: beginner

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

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

 53:   *part = p;
 54:   return(0);
 55: }

 57: #undef  __FUNCT__
 59: /*@ 
 60:   PartitionSerialize - This function stores or recreates a partition using a viewer for a binary file.

 62:   Collective on MPI_Comm

 64:   Input Parameters:
 65: + mesh   - The mesh that is partitioned
 66: . viewer - The viewer context
 67: - store  - This flag is PETSC_TRUE is data is being written, otherwise it will be read

 69:   Output Parameter:
 70: . part   - The partition

 72:   Level: beginner

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


 90:   PetscTypeCompare((PetscObject) viewer, PETSC_VIEWER_BINARY, &match);
 91:   if (match == PETSC_FALSE) SETERRQ(PETSC_ERR_ARG_WRONG, "Must be binary viewer");
 92:   PetscViewerBinaryGetDescriptor(viewer, &fd);
 93:   PetscObjectGetComm((PetscObject) mesh, &comm);

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

130:   return(0);
131: }