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().
10: Collective on MPI_Comm
12: Input Parameter:
13: . comm - The communicator for the vector object
15: Output Parameter:
16: . vec - The vector object
18: Level: beginner
20: .keywords: vector, create
21: .seealso: VecSetType(), VecSetSizes(), VecCreateMPIWithArray(), VecCreateMPI(), VecDuplicate(),
22: VecDuplicateVecs(), VecCreateGhost(), VecCreateSeq(), VecPlaceArray()
23: @*/
24: int VecCreate(MPI_Comm comm, Vec *vec)
25: {
26: Vec v;
31: *vec = PETSC_NULL;
32: #ifndef PETSC_USE_DYNAMIC_LIBRARIES
33: VecInitializePackage(PETSC_NULL);
34: #endif
36: PetscHeaderCreate(v, _p_Vec, struct _VecOps, VEC_COOKIE, -1, "Vec", comm, VecDestroy, VecView);
37: PetscLogObjectCreate(v);
38: PetscLogObjectMemory(v, sizeof(struct _p_Vec));
39: PetscMemzero(v->ops, sizeof(struct _VecOps));
40: v->bops->publish = PETSC_NULL /* VecPublish_Petsc */;
41: v->type_name = PETSC_NULL;
42: v->serialize_name = PETSC_NULL;
44: v->map = PETSC_NULL;
45: v->data = PETSC_NULL;
46: v->n = -1;
47: v->N = -1;
48: v->bs = -1;
49: v->mapping = PETSC_NULL;
50: v->bmapping = PETSC_NULL;
51: v->array_gotten = PETSC_FALSE;
52: v->petscnative = PETSC_FALSE;
53: v->esivec = PETSC_NULL;
55: *vec = v;
56: return(0);
57: }
59: /*@
60: VecSerialize - This function stores or recreates a vector using a viewer for a binary file.
62: Collective on MPI_Comm
64: Input Parameters:
65: + comm - The communicator for the vector object
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: . v - The vector
72: Level: beginner
74: .keywords: vector, serialize
75: .seealso: GridSerialize()
76: @*/
77: int VecSerialize(MPI_Comm comm, Vec *v, PetscViewer viewer, PetscTruth store)
78: {
79: int (*serialize)(MPI_Comm, Vec *, PetscViewer, PetscTruth);
80: int fd, len;
81: char *name;
82: PetscTruth match;
83: int ierr;
89: PetscTypeCompare((PetscObject) viewer, PETSC_VIEWER_BINARY, &match);
90: if (match == PETSC_FALSE) SETERRQ(PETSC_ERR_ARG_WRONG, "Must be binary viewer");
91: PetscViewerBinaryGetDescriptor(viewer, &fd);
93: if (VecSerializeRegisterAllCalled == PETSC_FALSE) {
94: VecSerializeRegisterAll(PETSC_NULL);
95: }
96: if (VecSerializeList == PETSC_NULL) SETERRQ(PETSC_ERR_ARG_CORRUPT, "Could not find table of methods");
98: if (store) {
100: PetscStrlen((*v)->class_name, &len);
101: PetscBinaryWrite(fd, &len, 1, PETSC_INT, 0);
102: PetscBinaryWrite(fd, (*v)->class_name, len, PETSC_CHAR, 0);
103: PetscStrlen((*v)->serialize_name, &len);
104: PetscBinaryWrite(fd, &len, 1, PETSC_INT, 0);
105: PetscBinaryWrite(fd, (*v)->serialize_name, len, PETSC_CHAR, 0);
106: PetscFListFind(comm, VecSerializeList, (*v)->serialize_name, (void (**)(void)) &serialize);
107: if (!serialize) SETERRQ(PETSC_ERR_ARG_WRONG, "Type cannot be serialized");
108: (*serialize)(comm, v, viewer, store);
109: } else {
110: PetscBinaryRead(fd, &len, 1, PETSC_INT);
111: PetscMalloc((len+1) * sizeof(char), &name);
112: name[len] = 0;
113: PetscBinaryRead(fd, name, len, PETSC_CHAR);
114: PetscStrcmp(name, "Vec", &match);
115: PetscFree(name);
116: if (match == PETSC_FALSE) SETERRQ(PETSC_ERR_ARG_WRONG, "Non-vector object");
117: /* Dispatch to the correct routine */
118: PetscBinaryRead(fd, &len, 1, PETSC_INT);
119: PetscMalloc((len+1) * sizeof(char), &name);
120: name[len] = 0;
121: PetscBinaryRead(fd, name, len, PETSC_CHAR);
122: PetscFListFind(comm, VecSerializeList, name, (void (**)(void)) &serialize);
123: if (!serialize) SETERRQ(PETSC_ERR_ARG_WRONG, "Type cannot be serialized");
124: (*serialize)(comm, v, viewer, store);
125: PetscStrfree((*v)->serialize_name);
126: (*v)->serialize_name = name;
127: }
128:
129: return(0);
130: }