Actual source code: mapcreate.c

  1: #ifdef PETSC_RCS_HEADER
  2: static char vcid[] = "$Id: mapcreate.c,v 1.1 1999/06/21 02:03:50 knepley Exp $";
  3: #endif

 5:  #include src/vec/vecimpl.h


  8: #undef __FUNCT__  
 10: /*@C
 11:   PetscMapCreate - Creates an empty map object. The type can then be set with PetscMapSetType().

 13:   Collective on MPI_Comm
 14:  
 15:   Input Parameter:
 16: . comm - The MPI communicator for the map object 

 18:   Output Parameter:
 19: . map  - The map object

 21:   Level: beginner

 23: .keywords: PetscMap, create
 24: .seealso: PetscMapDestroy(), PetscMapGetLocalSize(), PetscMapGetSize(), PetscMapGetGlobalRange(), PetscMapGetLocalRange()
 25: @*/
 26: int PetscMapCreate(MPI_Comm comm, PetscMap *map)
 27: {
 28:   PetscMap m;
 29:   int      ierr;

 33:   *map = PETSC_NULL;
 34: #ifndef PETSC_USE_DYNAMIC_LIBRARIES
 35:   VecInitializePackage(PETSC_NULL);
 36: #endif

 38:   PetscHeaderCreate(m, _p_PetscMap, struct _PetscMapOps, MAP_COOKIE, -1, "PetscMap", comm, PetscMapDestroy, PETSC_NULL);
 39:   PetscLogObjectCreate(m);
 40:   PetscLogObjectMemory(m, sizeof(struct _p_PetscMap));
 41:   PetscMemzero(m->ops, sizeof(struct _PetscMapOps));
 42:   m->bops->publish  = PETSC_NULL /* PetscMapPublish_Petsc */;
 43:   m->type_name      = PETSC_NULL;
 44:   m->serialize_name = PETSC_NULL;

 46:   m->n      = -1;
 47:   m->N      = -1;
 48:   m->rstart = -1;
 49:   m->rend   = -1;
 50:   m->range  = PETSC_NULL;

 52:   *map = m;
 53:   return(0);
 54: }

 56: #undef __FUNCT__
 58: /*@ 
 59:   PetscMapSerialize - This function stores or recreates a map using a viewer for a binary file.

 61:   Collective on MPI_Comm

 63:   Input Parameters:
 64: + comm   - The communicator for the map object
 65: . viewer - The viewer context
 66: - store  - This flag is PETSC_TRUE is data is being written, otherwise it will be read

 68:   Output Parameter:
 69: . map    - The map

 71:   Level: beginner

 73: .keywords: maptor, serialize
 74: .seealso: GridSerialize()
 75: @*/
 76: int PetscMapSerialize(MPI_Comm comm, PetscMap *map, PetscViewer viewer, PetscTruth store)
 77: {
 78:   int      (*serialize)(MPI_Comm, PetscMap *, PetscViewer, PetscTruth);
 79:   int        fd, len;
 80:   char      *name;
 81:   PetscTruth match;
 82:   int        ierr;


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

 92:   if (PetscMapSerializeRegisterAllCalled == PETSC_FALSE) {
 93:     PetscMapSerializeRegisterAll(PETSC_NULL);
 94:   }
 95:   if (PetscMapSerializeList == PETSC_NULL) SETERRQ(PETSC_ERR_ARG_CORRUPT, "Could not find table of methods");

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