Actual source code: veccreate.c

  1: #ifdef PETSC_RCS_HEADER
  2: static char vcid[] = "$Id: vecserialize.c,v 1.10 2000/01/10 03:18:14 knepley Exp $";
  3: #endif

 5:  #include src/vec/vecimpl.h

  7: /*@C
  8:   VecCreate - Creates an empty vector object. The type can then be set with VecSetType(),
  9:   or VecSetFromOptions().

 11:    If you never  call VecSetType() or VecSetFromOptions() it will generate an 
 12:    error when you try to use the vector.

 14:   Collective on MPI_Comm

 16:   Input Parameter:
 17: . comm - The communicator for the vector object

 19:   Output Parameter:
 20: . vec  - The vector object

 22:   Level: beginner

 24: .keywords: vector, create
 25: .seealso: VecSetType(), VecSetSizes(), VecCreateMPIWithArray(), VecCreateMPI(), VecDuplicate(),
 26:           VecDuplicateVecs(), VecCreateGhost(), VecCreateSeq(), VecPlaceArray()
 27: @*/
 28: int VecCreate(MPI_Comm comm, Vec *vec)
 29: {
 30:   Vec v;

 35:   *vec = PETSC_NULL;
 36: #ifndef PETSC_USE_DYNAMIC_LIBRARIES
 37:   VecInitializePackage(PETSC_NULL);
 38: #endif

 40:   PetscHeaderCreate(v, _p_Vec, struct _VecOps, VEC_COOKIE, -1, "Vec", comm, VecDestroy, VecView);
 41:   PetscLogObjectCreate(v);
 42:   PetscLogObjectMemory(v, sizeof(struct _p_Vec));
 43:   PetscMemzero(v->ops, sizeof(struct _VecOps));
 44:   v->bops->publish  = PETSC_NULL /* VecPublish_Petsc */;
 45:   v->type_name      = PETSC_NULL;
 46:   v->serialize_name = PETSC_NULL;

 48:   v->map          = PETSC_NULL;
 49:   v->data         = PETSC_NULL;
 50:   v->n            = -1;
 51:   v->N            = -1;
 52:   v->bs           = -1;
 53:   v->mapping      = PETSC_NULL;
 54:   v->bmapping     = PETSC_NULL;
 55:   v->array_gotten = PETSC_FALSE;
 56:   v->petscnative  = PETSC_FALSE;
 57:   v->esivec       = PETSC_NULL;

 59:   *vec = v;
 60:   return(0);
 61: }

 63: /*@ 
 64:   VecSerialize - This function stores or recreates a vector using a viewer for a binary file.

 66:   Collective on MPI_Comm

 68:   Input Parameters:
 69: + comm   - The communicator for the vector object
 70: . viewer - The viewer context
 71: - store  - This flag is PETSC_TRUE is data is being written, otherwise it will be read

 73:   Output Parameter:
 74: . v      - The vector

 76:   Level: beginner

 78: .keywords: vector, serialize
 79: .seealso: GridSerialize()
 80: @*/
 81: int VecSerialize(MPI_Comm comm, Vec *v, PetscViewer viewer, PetscTruth store)
 82: {
 83:   int      (*serialize)(MPI_Comm, Vec *, PetscViewer, PetscTruth);
 84:   int        fd, len;
 85:   char      *name;
 86:   PetscTruth match;
 87:   int        ierr;


 93:   PetscTypeCompare((PetscObject) viewer, PETSC_VIEWER_BINARY, &match);
 94:   if (match == PETSC_FALSE) SETERRQ(PETSC_ERR_ARG_WRONG, "Must be binary viewer");
 95:   PetscViewerBinaryGetDescriptor(viewer, &fd);

 97:   if (VecSerializeRegisterAllCalled == PETSC_FALSE) {
 98:     VecSerializeRegisterAll(PETSC_NULL);
 99:   }
100:   if (VecSerializeList == PETSC_NULL) SETERRQ(PETSC_ERR_ARG_CORRUPT, "Could not find table of methods");

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