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: /*@C
  9:   PetscMapCreate - Creates an empty map object. The type can then be set with PetscMapSetType().

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

 16:   Output Parameter:
 17: . map  - The map object

 19:   Level: beginner

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

 31:   *map = PETSC_NULL;
 32: #ifndef PETSC_USE_DYNAMIC_LIBRARIES
 33:   VecInitializePackage(PETSC_NULL);
 34: #endif

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

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

 50:   *map = m;
 51:   return(0);
 52: }

 54: /*@ 
 55:   PetscMapSerialize - This function stores or recreates a map using a viewer for a binary file.

 57:   Collective on MPI_Comm

 59:   Input Parameters:
 60: + comm   - The communicator for the map object
 61: . viewer - The viewer context
 62: - store  - This flag is PETSC_TRUE is data is being written, otherwise it will be read

 64:   Output Parameter:
 65: . map    - The map

 67:   Level: beginner

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


 84:   PetscTypeCompare((PetscObject) viewer, PETSC_VIEWER_BINARY, &match);
 85:   if (match == PETSC_FALSE) SETERRQ(PETSC_ERR_ARG_WRONG, "Must be binary viewer");
 86:   PetscViewerBinaryGetDescriptor(viewer, &fd);

 88:   if (PetscMapSerializeRegisterAllCalled == PETSC_FALSE) {
 89:     PetscMapSerializeRegisterAll(PETSC_NULL);
 90:   }
 91:   if (PetscMapSerializeList == PETSC_NULL) SETERRQ(PETSC_ERR_ARG_CORRUPT, "Could not find table of methods");

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