source: issm/trunk-jpl/src/c/objects/ExternalResults/PetscVecExternalResult.cpp@ 12011

Last change on this file since 12011 was 12011, checked in by Eric.Larour, 13 years ago

Preliminary commit of new issm version, where serial code is starting to be stripped away. Will not run before some major debugging is done

File size: 6.6 KB
Line 
1/*!\file PetscVecExternalResult.c
2 * \brief: implementation of the PetscVecExternalResult object
3 */
4
5/*header files: */
6/*{{{1*/
7#ifdef HAVE_CONFIG_H
8 #include <config.h>
9#else
10#error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
11#endif
12
13#include <stdio.h>
14#include <string.h>
15#include "../objects.h"
16#include "../../EnumDefinitions/EnumDefinitions.h"
17#include "../../shared/shared.h"
18#include "../../Container/Container.h"
19#include "../../include/include.h"
20/*}}}*/
21
22/*PetscVecExternalResult constructors and destructor*/
23/*FUNCTION PetscVecExternalResult::PetscVecExternalResult(){{{1*/
24PetscVecExternalResult::PetscVecExternalResult(){
25 return;
26}
27/*}}}*/
28/*FUNCTION PetscVecExternalResult::PetscVecExternalResult(int enum_type,IssmPetscVec value){{{1*/
29PetscVecExternalResult::PetscVecExternalResult(int in_id, int in_enum_type,Vector* in_value,int in_step, double in_time){
30
31 id=in_id;
32 enum_type=in_enum_type;
33
34 value=NULL;
35
36 if(in_value){
37 value=in_value->Duplicate();
38 in_value->Copy(value);
39 }
40 else value=NULL;
41
42 step=in_step;
43 time=in_time;
44}
45/*}}}*/
46/*FUNCTION PetscVecExternalResult::~PetscVecExternalResult(){{{1*/
47PetscVecExternalResult::~PetscVecExternalResult(){
48 VecFree(&value);
49}
50/*}}}*/
51
52/*Object virtual functions definitions:*/
53/*FUNCTION PetscVecExternalResult::Echo {{{1*/
54void PetscVecExternalResult::Echo(void){
55
56 printf("PetscVecExternalResult:\n");
57 printf(" enum: %i (%s)\n",this->enum_type,EnumToStringx(this->enum_type));
58
59}
60/*}}}*/
61/*FUNCTION PetscVecExternalResult::DeepEcho{{{1*/
62void PetscVecExternalResult::DeepEcho(void){
63
64 int i;
65 printf("PetscVecExternalResult:\n");
66 printf(" id: %i\n",this->id);
67 printf(" enum: %i (%s)\n",this->enum_type,EnumToStringx(this->enum_type));
68 printf(" step: %i\n",this->step);
69 printf(" time: %g\n",this->time);
70 VecView(value,PETSC_VIEWER_STDOUT_WORLD);
71}
72/*}}}*/
73/*FUNCTION PetscVecExternalResult::Id{{{1*/
74int PetscVecExternalResult::Id(void){ return -1; }
75/*}}}*/
76/*FUNCTION PetscVecExternalResult::MyRank{{{1*/
77int PetscVecExternalResult::MyRank(void){
78 extern int my_rank;
79 return my_rank;
80}
81/*}}}*/
82#ifdef _SERIAL_
83/*FUNCTION PetscVecExternalResult::Marshall{{{1*/
84void PetscVecExternalResult::Marshall(char** pmarshalled_dataset){
85
86 char* marshalled_dataset=NULL;
87 int enum_value=0;
88 int M;
89 double* serial_value=NULL;
90
91 /*recover marshalled_dataset: */
92 marshalled_dataset=*pmarshalled_dataset;
93
94 /*get enum value of PetscVecExternalResult: */
95 enum_value=PetscVecExternalResultEnum;
96
97 /*marshall enum: */
98 memcpy(marshalled_dataset,&enum_value,sizeof(enum_value));marshalled_dataset+=sizeof(enum_value);
99
100 /*marshall PetscVecExternalResult data: */
101 memcpy(marshalled_dataset,&id,sizeof(id));marshalled_dataset+=sizeof(id);
102 memcpy(marshalled_dataset,&enum_type,sizeof(enum_type));marshalled_dataset+=sizeof(enum_type);
103 memcpy(marshalled_dataset,&step,sizeof(step));marshalled_dataset+=sizeof(step);
104 memcpy(marshalled_dataset,&time,sizeof(time));marshalled_dataset+=sizeof(time);
105
106 if(value){
107 VecGetSize(value,&M);
108 VecToMPISerial(&serial_value,value);
109 memcpy(marshalled_dataset,&M,sizeof(M));marshalled_dataset+=sizeof(M);
110 memcpy(marshalled_dataset,serial_value,M*sizeof(double));marshalled_dataset+=(M*sizeof(double));
111 }
112 else{
113 M=0;
114 memcpy(marshalled_dataset,&M,sizeof(M));marshalled_dataset+=sizeof(M);
115 }
116 /*Free ressources:*/
117 xfree((void**)&serial_value);
118
119 /*return:*/
120 *pmarshalled_dataset=marshalled_dataset;
121}
122/*}}}*/
123/*FUNCTION PetscVecExternalResult::MarshallSize{{{1*/
124int PetscVecExternalResult::MarshallSize(){
125
126 int M=0;
127 if(value)VecGetSize(value,&M);
128
129 return sizeof(M)+M*sizeof(double)
130 +sizeof(id)
131 +sizeof(enum_type)
132 +sizeof(step)
133 +sizeof(time)
134 +sizeof(int); //sizeof(int) for enum value
135}
136/*}}}*/
137/*FUNCTION PetscVecExternalResult::Demarshall{{{1*/
138void PetscVecExternalResult::Demarshall(char** pmarshalled_dataset){
139
140 char* marshalled_dataset=NULL;
141 int i;
142 int M;
143 double* serial_vec=NULL;
144 int* idxm=NULL;
145
146 /*recover marshalled_dataset: */
147 marshalled_dataset=*pmarshalled_dataset;
148
149 /*this time, no need to get enum type, the pointer directly points to the beginning of the
150 *object data (thanks to DataSet::Demarshall):*/
151 memcpy(&id,marshalled_dataset,sizeof(id));marshalled_dataset+=sizeof(id);
152 memcpy(&enum_type,marshalled_dataset,sizeof(enum_type));marshalled_dataset+=sizeof(enum_type);
153
154 /*data: */
155 memcpy(&step,marshalled_dataset,sizeof(step));marshalled_dataset+=sizeof(step);
156 memcpy(&time,marshalled_dataset,sizeof(time));marshalled_dataset+=sizeof(time);
157
158 memcpy(&M,marshalled_dataset,sizeof(M));marshalled_dataset+=sizeof(M);
159 if(M){
160 serial_vec=(double*)xmalloc(M*sizeof(double));
161 memcpy(serial_vec,marshalled_dataset,M*sizeof(double));marshalled_dataset+=(M*sizeof(double));
162
163 value=NewVec(M);
164 idxm=(int*)xmalloc(M*sizeof(int));
165 for(i=0;i<M;i++)idxm[i]=i;
166 VecSetValues(value,M,idxm,serial_vec,INSERT_VALUES);
167
168 VecAssemblyBegin(value);
169 VecAssemblyEnd(value);
170
171
172 }
173 else{
174 value=NULL;
175 }
176
177 /*Free ressources:*/
178 xfree((void**)&serial_vec);
179 xfree((void**)&idxm);
180
181 /*return: */
182 *pmarshalled_dataset=marshalled_dataset;
183}
184/*}}}*/
185#endif
186/*FUNCTION PetscVecExternalResult::ObjectEnum{{{1*/
187int PetscVecExternalResult::ObjectEnum(void){
188
189 return PetscVecExternalResultEnum;
190
191}
192/*}}}*/
193/*FUNCTION PetscVecExternalResult::copy{{{1*/
194Object* PetscVecExternalResult::copy() {
195
196 return new PetscVecExternalResult(this->id,this->enum_type,this->value,this->step,this->time);
197
198}
199/*}}}*/
200
201/*PetscVecExternalResult management: */
202/*FUNCTION PetscVecExternalResult::WriteData{{{1*/
203void PetscVecExternalResult::WriteData(FILE* fid,bool io_gather){
204
205 int length;
206 int type;
207 int size;
208 char *name = NULL;
209 double *serialvec = NULL;
210 extern int my_rank;
211
212 /*serialize: */
213 VecGetSize(this->value,&size);
214 VecToMPISerial(&serialvec,this->value);
215
216 /*now, exit if we are not on cpu 0: */
217 if(my_rank)return;
218
219 /*First write enum: */
220 EnumToStringx(&name,this->enum_type);
221 length=(strlen(name)+1)*sizeof(char);
222 fwrite(&length,sizeof(int),1,fid);
223 fwrite(name,length,1,fid);
224 xfree((void**)&name);
225
226 /*Now write time and step: */
227 fwrite(&time,sizeof(double),1,fid);
228 fwrite(&step,sizeof(int),1,fid);
229
230 /*writing a double, type is 1, size is 1: */
231 type=1;
232
233 fwrite(&type,sizeof(int),1,fid);
234 fwrite(&size,sizeof(int),1,fid);
235 fwrite(serialvec,size*sizeof(double),1,fid);
236
237 /*Free ressources:*/
238 xfree((void**)&serialvec);
239}
240/*}}}1*/
241/*FUNCTION PetscVecExternalResult::GetResultName{{{1*/
242void PetscVecExternalResult::GetResultName(char**pname){
243 EnumToStringx(pname,this->enum_type);
244}
245/*}}}*/
246/*FUNCTION PetscVecExternalResult::GetStep{{{1*/
247int PetscVecExternalResult::GetStep(void){
248
249 return this->step;
250}
251/*}}}*/
Note: See TracBrowser for help on using the repository browser.