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: #undef __FUNCT__
9: /*@C
10: VecCreate - Creates an empty vector object. The type can then be set with VecSetType(),
11: or VecSetFromOptions().
13: If you never call VecSetType() or VecSetFromOptions() it will generate an
14: error when you try to use the vector.
16: Collective on MPI_Comm
18: Input Parameter:
19: . comm - The communicator for the vector object
21: Output Parameter:
22: . vec - The vector object
24: Level: beginner
26: .keywords: vector, create
27: .seealso: VecSetType(), VecSetSizes(), VecCreateMPIWithArray(), VecCreateMPI(), VecDuplicate(),
28: VecDuplicateVecs(), VecCreateGhost(), VecCreateSeq(), VecPlaceArray()
29: @*/
30: int VecCreate(MPI_Comm comm, Vec *vec)
31: {
32: Vec v;
37: *vec = PETSC_NULL;
38: #ifndef PETSC_USE_DYNAMIC_LIBRARIES
39: VecInitializePackage(PETSC_NULL);
40: #endif
42: PetscHeaderCreate(v, _p_Vec, struct _VecOps, VEC_COOKIE, -1, "Vec", comm, VecDestroy, VecView);
43: PetscLogObjectCreate(v);
44: PetscLogObjectMemory(v, sizeof(struct _p_Vec));
45: PetscMemzero(v->ops, sizeof(struct _VecOps));
46: v->bops->publish = PETSC_NULL /* VecPublish_Petsc */;
47: v->type_name = PETSC_NULL;
48: v->serialize_name = PETSC_NULL;
50: v->map = PETSC_NULL;
51: v->data = PETSC_NULL;
52: v->n = -1;
53: v->N = -1;
54: v->bs = -1;
55: v->mapping = PETSC_NULL;
56: v->bmapping = PETSC_NULL;
57: v->array_gotten = PETSC_FALSE;
58: v->petscnative = PETSC_FALSE;
59: v->esivec = PETSC_NULL;
61: *vec = v;
62: return(0);
63: }
65: #undef __FUNCT__
67: /*@
68: VecSerialize - This function stores or recreates a vector using a viewer for a binary file.
70: Collective on MPI_Comm
72: Input Parameters:
73: + comm - The communicator for the vector object
74: . viewer - The viewer context
75: - store - This flag is PETSC_TRUE is data is being written, otherwise it will be read
77: Output Parameter:
78: . v - The vector
80: Level: beginner
82: .keywords: vector, serialize
83: .seealso: GridSerialize()
84: @*/
85: int VecSerialize(MPI_Comm comm, Vec *v, PetscViewer viewer, PetscTruth store)
86: {
87: int (*serialize)(MPI_Comm, Vec *, PetscViewer, PetscTruth);
88: int fd, len;
89: char *name;
90: PetscTruth match;
91: int ierr;
97: PetscTypeCompare((PetscObject) viewer, PETSC_VIEWER_BINARY, &match);
98: if (match == PETSC_FALSE) SETERRQ(PETSC_ERR_ARG_WRONG, "Must be binary viewer");
99: PetscViewerBinaryGetDescriptor(viewer, &fd);
101: if (VecSerializeRegisterAllCalled == PETSC_FALSE) {
102: VecSerializeRegisterAll(PETSC_NULL);
103: }
104: if (VecSerializeList == PETSC_NULL) SETERRQ(PETSC_ERR_ARG_CORRUPT, "Could not find table of methods");
106: if (store) {
108: PetscStrlen((*v)->class_name, &len);
109: PetscBinaryWrite(fd, &len, 1, PETSC_INT, 0);
110: PetscBinaryWrite(fd, (*v)->class_name, len, PETSC_CHAR, 0);
111: PetscStrlen((*v)->serialize_name, &len);
112: PetscBinaryWrite(fd, &len, 1, PETSC_INT, 0);
113: PetscBinaryWrite(fd, (*v)->serialize_name, len, PETSC_CHAR, 0);
114: PetscFListFind(comm, VecSerializeList, (*v)->serialize_name, (void (**)(void)) &serialize);
115: if (!serialize) SETERRQ(PETSC_ERR_ARG_WRONG, "Type cannot be serialized");
116: (*serialize)(comm, v, viewer, store);
117: } else {
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: PetscStrcmp(name, "Vec", &match);
123: PetscFree(name);
124: if (match == PETSC_FALSE) SETERRQ(PETSC_ERR_ARG_WRONG, "Non-vector object");
125: /* Dispatch to the correct routine */
126: PetscBinaryRead(fd, &len, 1, PETSC_INT);
127: PetscMalloc((len+1) * sizeof(char), &name);
128: name[len] = 0;
129: PetscBinaryRead(fd, name, len, PETSC_CHAR);
130: PetscFListFind(comm, VecSerializeList, name, (void (**)(void)) &serialize);
131: if (!serialize) SETERRQ(PETSC_ERR_ARG_WRONG, "Type cannot be serialized");
132: (*serialize)(comm, v, viewer, store);
133: PetscStrfree((*v)->serialize_name);
134: (*v)->serialize_name = name;
135: }
136:
137: return(0);
138: }