xref: /petsc/src/sys/fileio/sysio.c (revision 41f502e3b3980086d9aa0453a762821d8b0fa6fd)
1e5c89e4eSSatish Balay 
2e5c89e4eSSatish Balay /*
3e5c89e4eSSatish Balay    This file contains simple binary read/write routines.
4e5c89e4eSSatish Balay  */
5e5c89e4eSSatish Balay 
6c6db04a5SJed Brown #include <petscsys.h>
7e5c89e4eSSatish Balay #include <errno.h>
8e5c89e4eSSatish Balay #include <fcntl.h>
9e5c89e4eSSatish Balay #if defined(PETSC_HAVE_UNISTD_H)
10e5c89e4eSSatish Balay #include <unistd.h>
11e5c89e4eSSatish Balay #endif
12e5c89e4eSSatish Balay #if defined(PETSC_HAVE_IO_H)
13e5c89e4eSSatish Balay #include <io.h>
14e5c89e4eSSatish Balay #endif
15c6db04a5SJed Brown #include <petscbt.h>
16e5c89e4eSSatish Balay 
17d6a4318aSJed Brown const char *const PetscFileModes[] = {"READ","WRITE","APPEND","UPDATE","APPEND_UPDATE","PetscFileMode","PETSC_FILE_",0};
18d6a4318aSJed Brown 
196de02169SBarry Smith /* --------------------------------------------------------- */
20e5c89e4eSSatish Balay #undef __FUNCT__
216de02169SBarry Smith #define __FUNCT__ "PetscByteSwapEnum"
22e5c89e4eSSatish Balay /*
236de02169SBarry Smith   PetscByteSwapEnum - Swap bytes in a  PETSc Enum
24e5c89e4eSSatish Balay 
25e5c89e4eSSatish Balay */
267087cfbeSBarry Smith PetscErrorCode  PetscByteSwapEnum(PetscEnum *buff,PetscInt n)
27e5c89e4eSSatish Balay {
286de02169SBarry Smith   PetscInt  i,j;
290b20345dSBarry Smith   PetscEnum tmp = ENUM_DUMMY;
30e0890e22SSatish Balay   char      *ptr1,*ptr2 = (char*)&tmp;
31e5c89e4eSSatish Balay 
32e5c89e4eSSatish Balay   PetscFunctionBegin;
33e5c89e4eSSatish Balay   for (j=0; j<n; j++) {
34e5c89e4eSSatish Balay     ptr1 = (char*)(buff + j);
35a297a907SKarl Rupp     for (i=0; i<(PetscInt)sizeof(PetscEnum); i++) ptr2[i] = ptr1[sizeof(PetscEnum)-1-i];
36a297a907SKarl Rupp     for (i=0; i<(PetscInt)sizeof(PetscEnum); i++) ptr1[i] = ptr2[i];
37e5c89e4eSSatish Balay   }
38e5c89e4eSSatish Balay   PetscFunctionReturn(0);
39e5c89e4eSSatish Balay }
406de02169SBarry Smith 
416de02169SBarry Smith #undef __FUNCT__
42acfcf0e5SJed Brown #define __FUNCT__ "PetscByteSwapBool"
436de02169SBarry Smith /*
44acfcf0e5SJed Brown   PetscByteSwapBool - Swap bytes in a  PETSc Bool
456de02169SBarry Smith 
466de02169SBarry Smith */
477087cfbeSBarry Smith PetscErrorCode  PetscByteSwapBool(PetscBool *buff,PetscInt n)
486de02169SBarry Smith {
496de02169SBarry Smith   PetscInt  i,j;
50ace3abfcSBarry Smith   PetscBool tmp = PETSC_FALSE;
51e0890e22SSatish Balay   char      *ptr1,*ptr2 = (char*)&tmp;
526de02169SBarry Smith 
536de02169SBarry Smith   PetscFunctionBegin;
546de02169SBarry Smith   for (j=0; j<n; j++) {
556de02169SBarry Smith     ptr1 = (char*)(buff + j);
56a297a907SKarl Rupp     for (i=0; i<(PetscInt)sizeof(PetscBool); i++) ptr2[i] = ptr1[sizeof(PetscBool)-1-i];
57a297a907SKarl Rupp     for (i=0; i<(PetscInt)sizeof(PetscBool); i++) ptr1[i] = ptr2[i];
586de02169SBarry Smith   }
596de02169SBarry Smith   PetscFunctionReturn(0);
606de02169SBarry Smith }
616de02169SBarry Smith 
62e5c89e4eSSatish Balay #undef __FUNCT__
63bd1d2e58SBarry Smith #define __FUNCT__ "PetscByteSwapInt"
64bd1d2e58SBarry Smith /*
656de02169SBarry Smith   PetscByteSwapInt - Swap bytes in a  PETSc integer (which may be 32 or 64 bits)
66bd1d2e58SBarry Smith 
67bd1d2e58SBarry Smith */
687087cfbeSBarry Smith PetscErrorCode  PetscByteSwapInt(PetscInt *buff,PetscInt n)
69bd1d2e58SBarry Smith {
70bd1d2e58SBarry Smith   PetscInt i,j,tmp = 0;
71e0890e22SSatish Balay   char     *ptr1,*ptr2 = (char*)&tmp;
72bd1d2e58SBarry Smith 
73bd1d2e58SBarry Smith   PetscFunctionBegin;
74bd1d2e58SBarry Smith   for (j=0; j<n; j++) {
75bd1d2e58SBarry Smith     ptr1 = (char*)(buff + j);
76a297a907SKarl Rupp     for (i=0; i<(PetscInt)sizeof(PetscInt); i++) ptr2[i] = ptr1[sizeof(PetscInt)-1-i];
77a297a907SKarl Rupp     for (i=0; i<(PetscInt)sizeof(PetscInt); i++) ptr1[i] = ptr2[i];
78bd1d2e58SBarry Smith   }
79bd1d2e58SBarry Smith   PetscFunctionReturn(0);
80bd1d2e58SBarry Smith }
81bd1d2e58SBarry Smith /* --------------------------------------------------------- */
82bd1d2e58SBarry Smith #undef __FUNCT__
83e5c89e4eSSatish Balay #define __FUNCT__ "PetscByteSwapShort"
84e5c89e4eSSatish Balay /*
85e5c89e4eSSatish Balay   PetscByteSwapShort - Swap bytes in a short
86e5c89e4eSSatish Balay */
877087cfbeSBarry Smith PetscErrorCode  PetscByteSwapShort(short *buff,PetscInt n)
88e5c89e4eSSatish Balay {
89e5c89e4eSSatish Balay   PetscInt i,j;
90e5c89e4eSSatish Balay   short    tmp;
91e5c89e4eSSatish Balay   char     *ptr1,*ptr2 = (char*)&tmp;
92e5c89e4eSSatish Balay 
93e5c89e4eSSatish Balay   PetscFunctionBegin;
94e5c89e4eSSatish Balay   for (j=0; j<n; j++) {
95e5c89e4eSSatish Balay     ptr1 = (char*)(buff + j);
96a297a907SKarl Rupp     for (i=0; i<(PetscInt) sizeof(short); i++) ptr2[i] = ptr1[sizeof(short)-1-i];
97a297a907SKarl Rupp     for (i=0; i<(PetscInt) sizeof(short); i++) ptr1[i] = ptr2[i];
98e5c89e4eSSatish Balay   }
99e5c89e4eSSatish Balay   PetscFunctionReturn(0);
100e5c89e4eSSatish Balay }
101e5c89e4eSSatish Balay /* --------------------------------------------------------- */
102e5c89e4eSSatish Balay #undef __FUNCT__
103e5c89e4eSSatish Balay #define __FUNCT__ "PetscByteSwapScalar"
104e5c89e4eSSatish Balay /*
105*41f502e3SPatrick Sanan   PetscByteSwapScalar - Swap bytes in a PetscScalar
106*41f502e3SPatrick Sanan   The complex case is dealt with with an array of PetscReal, twice as long.
107e5c89e4eSSatish Balay */
1087087cfbeSBarry Smith PetscErrorCode  PetscByteSwapScalar(PetscScalar *buff,PetscInt n)
109e5c89e4eSSatish Balay {
110e5c89e4eSSatish Balay   PetscInt  i,j;
111e5c89e4eSSatish Balay   PetscReal tmp,*buff1 = (PetscReal*)buff;
112e5c89e4eSSatish Balay   char      *ptr1,*ptr2 = (char*)&tmp;
113e5c89e4eSSatish Balay 
114e5c89e4eSSatish Balay   PetscFunctionBegin;
115e5c89e4eSSatish Balay #if defined(PETSC_USE_COMPLEX)
116e5c89e4eSSatish Balay   n *= 2;
117e5c89e4eSSatish Balay #endif
118e5c89e4eSSatish Balay   for (j=0; j<n; j++) {
119e5c89e4eSSatish Balay     ptr1 = (char*)(buff1 + j);
120a297a907SKarl Rupp     for (i=0; i<(PetscInt) sizeof(PetscReal); i++) ptr2[i] = ptr1[sizeof(PetscReal)-1-i];
121a297a907SKarl Rupp     for (i=0; i<(PetscInt) sizeof(PetscReal); i++) ptr1[i] = ptr2[i];
122e5c89e4eSSatish Balay   }
123e5c89e4eSSatish Balay   PetscFunctionReturn(0);
124e5c89e4eSSatish Balay }
125e5c89e4eSSatish Balay /* --------------------------------------------------------- */
126e5c89e4eSSatish Balay #undef __FUNCT__
127e5c89e4eSSatish Balay #define __FUNCT__ "PetscByteSwapDouble"
128e5c89e4eSSatish Balay /*
129e5c89e4eSSatish Balay   PetscByteSwapDouble - Swap bytes in a double
130e5c89e4eSSatish Balay */
1317087cfbeSBarry Smith PetscErrorCode  PetscByteSwapDouble(double *buff,PetscInt n)
132e5c89e4eSSatish Balay {
133e5c89e4eSSatish Balay   PetscInt i,j;
134e5c89e4eSSatish Balay   double   tmp,*buff1 = (double*)buff;
135e5c89e4eSSatish Balay   char     *ptr1,*ptr2 = (char*)&tmp;
136e5c89e4eSSatish Balay 
137e5c89e4eSSatish Balay   PetscFunctionBegin;
138e5c89e4eSSatish Balay   for (j=0; j<n; j++) {
139e5c89e4eSSatish Balay     ptr1 = (char*)(buff1 + j);
140a297a907SKarl Rupp     for (i=0; i<(PetscInt) sizeof(double); i++) ptr2[i] = ptr1[sizeof(double)-1-i];
141a297a907SKarl Rupp     for (i=0; i<(PetscInt) sizeof(double); i++) ptr1[i] = ptr2[i];
142e5c89e4eSSatish Balay   }
143e5c89e4eSSatish Balay   PetscFunctionReturn(0);
144e5c89e4eSSatish Balay }
145e39fd77fSBarry Smith 
146e39fd77fSBarry Smith #undef __FUNCT__
147e95bf02fSSatish Balay #define __FUNCT__ "PetscByteSwapFloat"
148e95bf02fSSatish Balay /*
149e95bf02fSSatish Balay   PetscByteSwapFloat - Swap bytes in a float
150e95bf02fSSatish Balay */
151e95bf02fSSatish Balay PetscErrorCode PetscByteSwapFloat(float *buff,PetscInt n)
152e95bf02fSSatish Balay {
153e95bf02fSSatish Balay   PetscInt i,j;
154e95bf02fSSatish Balay   float    tmp,*buff1 = (float*)buff;
155e95bf02fSSatish Balay   char     *ptr1,*ptr2 = (char*)&tmp;
156e95bf02fSSatish Balay 
157e95bf02fSSatish Balay   PetscFunctionBegin;
158e95bf02fSSatish Balay   for (j=0; j<n; j++) {
159e95bf02fSSatish Balay     ptr1 = (char*)(buff1 + j);
160a297a907SKarl Rupp     for (i=0; i<(PetscInt) sizeof(float); i++) ptr2[i] = ptr1[sizeof(float)-1-i];
161a297a907SKarl Rupp     for (i=0; i<(PetscInt) sizeof(float); i++) ptr1[i] = ptr2[i];
162e95bf02fSSatish Balay   }
163e95bf02fSSatish Balay   PetscFunctionReturn(0);
164e95bf02fSSatish Balay }
165e95bf02fSSatish Balay 
166e95bf02fSSatish Balay #undef __FUNCT__
167e39fd77fSBarry Smith #define __FUNCT__ "PetscByteSwap"
168e39fd77fSBarry Smith PetscErrorCode PetscByteSwap(void *data,PetscDataType pdtype,PetscInt count)
169e39fd77fSBarry Smith {
170e39fd77fSBarry Smith   PetscErrorCode ierr;
171e39fd77fSBarry Smith 
172e39fd77fSBarry Smith   PetscFunctionBegin;
173e39fd77fSBarry Smith   if      (pdtype == PETSC_INT)    {ierr = PetscByteSwapInt((PetscInt*)data,count);CHKERRQ(ierr);}
174e39fd77fSBarry Smith   else if (pdtype == PETSC_ENUM)   {ierr = PetscByteSwapEnum((PetscEnum*)data,count);CHKERRQ(ierr);}
175acfcf0e5SJed Brown   else if (pdtype == PETSC_BOOL)   {ierr = PetscByteSwapBool((PetscBool*)data,count);CHKERRQ(ierr);}
176e39fd77fSBarry Smith   else if (pdtype == PETSC_SCALAR) {ierr = PetscByteSwapScalar((PetscScalar*)data,count);CHKERRQ(ierr);}
177e39fd77fSBarry Smith   else if (pdtype == PETSC_DOUBLE) {ierr = PetscByteSwapDouble((double*)data,count);CHKERRQ(ierr);}
178e95bf02fSSatish Balay   else if (pdtype == PETSC_FLOAT)  {ierr = PetscByteSwapFloat((float*)data,count);CHKERRQ(ierr);}
179e39fd77fSBarry Smith   else if (pdtype == PETSC_SHORT)  {ierr = PetscByteSwapShort((short*)data,count);CHKERRQ(ierr);}
180e39fd77fSBarry Smith   PetscFunctionReturn(0);
181e39fd77fSBarry Smith }
182e39fd77fSBarry Smith 
183e5c89e4eSSatish Balay /* --------------------------------------------------------- */
184e5c89e4eSSatish Balay #undef __FUNCT__
185e5c89e4eSSatish Balay #define __FUNCT__ "PetscBinaryRead"
186e30d2299SSatish Balay /*@
187e5c89e4eSSatish Balay    PetscBinaryRead - Reads from a binary file.
188e5c89e4eSSatish Balay 
189e5c89e4eSSatish Balay    Not Collective
190e5c89e4eSSatish Balay 
191e5c89e4eSSatish Balay    Input Parameters:
192e5c89e4eSSatish Balay +  fd - the file
193e5c89e4eSSatish Balay .  n  - the number of items to read
194e5c89e4eSSatish Balay -  type - the type of items to read (PETSC_INT, PETSC_DOUBLE or PETSC_SCALAR)
195e5c89e4eSSatish Balay 
196e5c89e4eSSatish Balay    Output Parameters:
197e5c89e4eSSatish Balay .  p - the buffer
198e5c89e4eSSatish Balay 
199e5c89e4eSSatish Balay 
200e5c89e4eSSatish Balay 
201e5c89e4eSSatish Balay    Level: developer
202e5c89e4eSSatish Balay 
203e5c89e4eSSatish Balay    Notes:
204e5c89e4eSSatish Balay    PetscBinaryRead() uses byte swapping to work on all machines; the files
205e5c89e4eSSatish Balay    are written to file ALWAYS using big-endian ordering. On small-endian machines the numbers
206e5c89e4eSSatish Balay    are converted to the small-endian format when they are read in from the file.
207e2e64c6bSBarry Smith    When PETSc is ./configure with --with-64bit-indices the integers are written to the
20854f21887SBarry Smith    file as 64 bit integers, this means they can only be read back in when the option --with-64bit-indices
20954f21887SBarry Smith    is used.
210e5c89e4eSSatish Balay 
211e5c89e4eSSatish Balay    Concepts: files^reading binary
212e5c89e4eSSatish Balay    Concepts: binary files^reading
213e5c89e4eSSatish Balay 
2144ebed01fSBarry Smith .seealso: PetscBinaryWrite(), PetscBinaryOpen(), PetscBinaryClose(), PetscViewerBinaryGetDescriptor(), PetscBinarySynchronizedWrite(),
2154ebed01fSBarry Smith           PetscBinarySynchronizedRead(), PetscBinarySynchronizedSeek()
216e5c89e4eSSatish Balay @*/
2177087cfbeSBarry Smith PetscErrorCode  PetscBinaryRead(int fd,void *p,PetscInt n,PetscDataType type)
218e5c89e4eSSatish Balay {
219e5c89e4eSSatish Balay   int               wsize,err;
220e5c89e4eSSatish Balay   size_t            m = (size_t) n,maxblock = 65536;
221e5c89e4eSSatish Balay   char              *pp = (char*)p;
2227a881295SBarry Smith #if defined(PETSC_USE_REAL___FLOAT128)
223cba51d77SBarry Smith   PetscBool         readdouble = PETSC_FALSE;
2247a881295SBarry Smith   double            *ppp;
2257a881295SBarry Smith #endif
2267a881295SBarry Smith #if !defined(PETSC_WORDS_BIGENDIAN) || defined(PETSC_USE_REAL___FLOAT128)
2276de02169SBarry Smith   PetscErrorCode    ierr;
2287a881295SBarry Smith #endif
2297a881295SBarry Smith #if !defined(PETSC_WORDS_BIGENDIAN)
230e5c89e4eSSatish Balay   void              *ptmp = p;
231e5c89e4eSSatish Balay #endif
23205acbc63SBarry Smith   char              *fname = NULL;
233e5c89e4eSSatish Balay 
234e5c89e4eSSatish Balay   PetscFunctionBegin;
2352d53ad75SBarry Smith   if (n < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Trying to write a negative amount of data %D",n);
236e5c89e4eSSatish Balay   if (!n) PetscFunctionReturn(0);
237e5c89e4eSSatish Balay 
2382d53ad75SBarry Smith   if (type == PETSC_FUNCTION) {
2392d53ad75SBarry Smith     m            = 64;
2402d53ad75SBarry Smith     type         = PETSC_CHAR;
24105acbc63SBarry Smith     fname        = (char*) malloc(m*sizeof(char));
24205acbc63SBarry Smith     if (!fname) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MEM,"Cannot allocate space for function name");
2432d53ad75SBarry Smith     pp           = (char*)fname;
2442d53ad75SBarry Smith #if !defined(PETSC_WORDS_BIGENDIAN)
2452d53ad75SBarry Smith     ptmp         = (void*)fname;
2462d53ad75SBarry Smith #endif
2472d53ad75SBarry Smith   }
2482d53ad75SBarry Smith 
2496de02169SBarry Smith   if (type == PETSC_INT)          m *= sizeof(PetscInt);
250e5c89e4eSSatish Balay   else if (type == PETSC_SCALAR)  m *= sizeof(PetscScalar);
251e5c89e4eSSatish Balay   else if (type == PETSC_DOUBLE)  m *= sizeof(double);
252e95bf02fSSatish Balay   else if (type == PETSC_FLOAT)   m *= sizeof(float);
253e5c89e4eSSatish Balay   else if (type == PETSC_SHORT)   m *= sizeof(short);
254e5c89e4eSSatish Balay   else if (type == PETSC_CHAR)    m *= sizeof(char);
255e5c89e4eSSatish Balay   else if (type == PETSC_ENUM)    m *= sizeof(PetscEnum);
256ace3abfcSBarry Smith   else if (type == PETSC_BOOL)   m *= sizeof(PetscBool);
2579f7b6320SBarry Smith   else if (type == PETSC_BIT_LOGICAL) m  = PetscBTLength(m)*sizeof(char);
258e32f2f54SBarry Smith   else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Unknown type");
259e5c89e4eSSatish Balay 
2607a881295SBarry Smith #if defined(PETSC_USE_REAL___FLOAT128)
261c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-binary_read_double",&readdouble,NULL);CHKERRQ(ierr);
2627a881295SBarry Smith   /* If using __float128 precision we still read in doubles from file */
263cba51d77SBarry Smith   if (type == PETSC_SCALAR && readdouble) {
2647a881295SBarry Smith     m    = m/2;
265785e854fSJed Brown     ierr = PetscMalloc1(n,&ppp);CHKERRQ(ierr);
2667a881295SBarry Smith     pp   = (char*)ppp;
2677a881295SBarry Smith   }
2687a881295SBarry Smith #endif
2697a881295SBarry Smith 
270e5c89e4eSSatish Balay   while (m) {
271e5c89e4eSSatish Balay     wsize = (m < maxblock) ? m : maxblock;
272e5c89e4eSSatish Balay     err   = read(fd,pp,wsize);
273e5c89e4eSSatish Balay     if (err < 0 && errno == EINTR) continue;
274e32f2f54SBarry Smith     if (!err && wsize > 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_READ,"Read past end of file");
275e32f2f54SBarry Smith     if (err < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_READ,"Error reading from file, errno %d",errno);
276e5c89e4eSSatish Balay     m  -= err;
277e5c89e4eSSatish Balay     pp += err;
278e5c89e4eSSatish Balay   }
2797a881295SBarry Smith 
2807a881295SBarry Smith #if defined(PETSC_USE_REAL___FLOAT128)
281cba51d77SBarry Smith   if (type == PETSC_SCALAR && readdouble) {
2827a881295SBarry Smith     PetscScalar *pv = (PetscScalar*) p;
2837a881295SBarry Smith     PetscInt    i;
2847a881295SBarry Smith #if !defined(PETSC_WORDS_BIGENDIAN)
2857a881295SBarry Smith     ierr = PetscByteSwapDouble(ppp,n);CHKERRQ(ierr);
2867a881295SBarry Smith #endif
287a297a907SKarl Rupp     for (i=0; i<n; i++) pv[i] = ppp[i];
2887a881295SBarry Smith     ierr = PetscFree(ppp);CHKERRQ(ierr);
2897a881295SBarry Smith     PetscFunctionReturn(0);
2907a881295SBarry Smith   }
2917a881295SBarry Smith #endif
2927a881295SBarry Smith 
293e5c89e4eSSatish Balay #if !defined(PETSC_WORDS_BIGENDIAN)
294e39fd77fSBarry Smith   ierr = PetscByteSwap(ptmp,type,n);CHKERRQ(ierr);
295e5c89e4eSSatish Balay #endif
296e5c89e4eSSatish Balay 
29705acbc63SBarry Smith   if (type == PETSC_FUNCTION) {
2982d53ad75SBarry Smith #if defined(PETSC_SERIALIZE_FUNCTIONS)
2990298fd71SBarry Smith     ierr = PetscDLSym(NULL,fname,(void**)p);CHKERRQ(ierr);
3002d53ad75SBarry Smith #else
3010298fd71SBarry Smith     *(void**)p = NULL;
3022d53ad75SBarry Smith #endif
30305acbc63SBarry Smith     free(fname);
3042d53ad75SBarry Smith   }
305e5c89e4eSSatish Balay   PetscFunctionReturn(0);
306e5c89e4eSSatish Balay }
307e5c89e4eSSatish Balay /* --------------------------------------------------------- */
308e5c89e4eSSatish Balay #undef __FUNCT__
309e5c89e4eSSatish Balay #define __FUNCT__ "PetscBinaryWrite"
310e30d2299SSatish Balay /*@
311e5c89e4eSSatish Balay    PetscBinaryWrite - Writes to a binary file.
312e5c89e4eSSatish Balay 
313e5c89e4eSSatish Balay    Not Collective
314e5c89e4eSSatish Balay 
315e5c89e4eSSatish Balay    Input Parameters:
316e5c89e4eSSatish Balay +  fd     - the file
317e5c89e4eSSatish Balay .  p      - the buffer
318e5c89e4eSSatish Balay .  n      - the number of items to write
319e5c89e4eSSatish Balay .  type   - the type of items to read (PETSC_INT, PETSC_DOUBLE or PETSC_SCALAR)
320e5c89e4eSSatish Balay -  istemp - PETSC_FALSE if buffer data should be preserved, PETSC_TRUE otherwise.
321e5c89e4eSSatish Balay 
322e5c89e4eSSatish Balay    Level: advanced
323e5c89e4eSSatish Balay 
324e5c89e4eSSatish Balay    Notes:
325e5c89e4eSSatish Balay    PetscBinaryWrite() uses byte swapping to work on all machines; the files
326e5c89e4eSSatish Balay    are written using big-endian ordering to the file. On small-endian machines the numbers
327e5c89e4eSSatish Balay    are converted to the big-endian format when they are written to disk.
328e2e64c6bSBarry Smith    When PETSc is ./configure with --with-64bit-indices the integers are written to the
32954f21887SBarry Smith    file as 64 bit integers, this means they can only be read back in when the option --with-64bit-indices
33054f21887SBarry Smith    is used.
331e5c89e4eSSatish Balay 
332*41f502e3SPatrick Sanan    If running with __float128 precision the output is in __float128 unless one uses the -binary_write_double option
3330da86b62SBarry Smith 
334e5c89e4eSSatish Balay    The Buffer p should be read-write buffer, and not static data.
335e5c89e4eSSatish Balay    This way, byte-swapping is done in-place, and then the buffer is
336e5c89e4eSSatish Balay    written to the file.
337e5c89e4eSSatish Balay 
338e5c89e4eSSatish Balay    This routine restores the original contents of the buffer, after
339e5c89e4eSSatish Balay    it is written to the file. This is done by byte-swapping in-place
340e5c89e4eSSatish Balay    the second time. If the flag istemp is set to PETSC_TRUE, the second
341e5c89e4eSSatish Balay    byte-swapping operation is not done, thus saving some computation,
342e5f36e38SBarry Smith    but the buffer is left corrupted.
343e5c89e4eSSatish Balay 
344300a7f5bSBarry Smith    Because byte-swapping may be done on the values in data it cannot be declared const
345300a7f5bSBarry Smith 
346e5c89e4eSSatish Balay    Concepts: files^writing binary
347e5c89e4eSSatish Balay    Concepts: binary files^writing
348e5c89e4eSSatish Balay 
3494ebed01fSBarry Smith .seealso: PetscBinaryRead(), PetscBinaryOpen(), PetscBinaryClose(), PetscViewerBinaryGetDescriptor(), PetscBinarySynchronizedWrite(),
3504ebed01fSBarry Smith           PetscBinarySynchronizedRead(), PetscBinarySynchronizedSeek()
351e5c89e4eSSatish Balay @*/
3527087cfbeSBarry Smith PetscErrorCode  PetscBinaryWrite(int fd,void *p,PetscInt n,PetscDataType type,PetscBool  istemp)
353e5c89e4eSSatish Balay {
354e5c89e4eSSatish Balay   char           *pp = (char*)p;
355e5c89e4eSSatish Balay   int            err,wsize;
356e5c89e4eSSatish Balay   size_t         m = (size_t)n,maxblock=65536;
357e5c89e4eSSatish Balay   PetscErrorCode ierr;
358f5351476SHong Zhang #if !defined(PETSC_WORDS_BIGENDIAN)
359e5c89e4eSSatish Balay   void           *ptmp = p;
360e5c89e4eSSatish Balay #endif
36105acbc63SBarry Smith   char           *fname = NULL;
3620da86b62SBarry Smith #if defined(PETSC_USE_REAL___FLOAT128)
363df40af56SBarry Smith   PetscBool      writedouble = PETSC_FALSE;
3640da86b62SBarry Smith   double         *ppp;
3650da86b62SBarry Smith   PetscReal      *pv;
3660da86b62SBarry Smith   PetscInt       i;
3670da86b62SBarry Smith #endif
368*41f502e3SPatrick Sanan   PetscDataType  wtype = type;
369e5c89e4eSSatish Balay 
370e5c89e4eSSatish Balay   PetscFunctionBegin;
371e32f2f54SBarry Smith   if (n < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Trying to write a negative amount of data %D",n);
372e5c89e4eSSatish Balay   if (!n) PetscFunctionReturn(0);
373e5c89e4eSSatish Balay 
3742d53ad75SBarry Smith   if (type == PETSC_FUNCTION) {
3752d53ad75SBarry Smith #if defined(PETSC_SERIALIZE_FUNCTIONS)
3762d53ad75SBarry Smith     const char *fnametmp;
3772d53ad75SBarry Smith #endif
3782d53ad75SBarry Smith     m     = 64;
379e25ab156SSatish Balay     fname = (char*)malloc(m*sizeof(char));
38005acbc63SBarry Smith     if (!fname) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MEM,"Cannot allocate space for function name");
38105acbc63SBarry Smith #if defined(PETSC_SERIALIZE_FUNCTIONS)
38205acbc63SBarry Smith     if (n > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Can only binary view a single function at a time");
38305acbc63SBarry Smith     ierr = PetscFPTFind(*(void**)p,&fnametmp);CHKERRQ(ierr);
38405acbc63SBarry Smith     ierr = PetscStrncpy(fname,fnametmp,m);CHKERRQ(ierr);
38505acbc63SBarry Smith #else
38605acbc63SBarry Smith     ierr = PetscStrncpy(fname,"",m);CHKERRQ(ierr);
38705acbc63SBarry Smith #endif
3882d53ad75SBarry Smith     type = PETSC_CHAR;
3892d53ad75SBarry Smith     pp   = (char*)fname;
3902d53ad75SBarry Smith #if !defined(PETSC_WORDS_BIGENDIAN)
3912d53ad75SBarry Smith     ptmp = (void*)fname;
3922d53ad75SBarry Smith #endif
3932d53ad75SBarry Smith   }
3942d53ad75SBarry Smith 
3950da86b62SBarry Smith #if defined(PETSC_USE_REAL___FLOAT128)
3960da86b62SBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-binary_write_double",&writedouble,NULL);CHKERRQ(ierr);
3970da86b62SBarry Smith   /* If using __float128 precision we still write in doubles to file */
3980da86b62SBarry Smith   if (type == PETSC_SCALAR && writedouble) {
399*41f502e3SPatrick Sanan     wtype = PETSC_DOUBLE;
4000da86b62SBarry Smith     ierr = PetscMalloc1(n,&ppp);CHKERRQ(ierr);
4010da86b62SBarry Smith     pv = (PetscReal*)pp;
4020da86b62SBarry Smith     for (i=0; i<n; i++) {
4030da86b62SBarry Smith       ppp[i] = (double) pv[i];
4040da86b62SBarry Smith     }
4050da86b62SBarry Smith     pp   = (char*)ppp;
4060da86b62SBarry Smith     ptmp = (char*)ppp;
4070da86b62SBarry Smith   }
4080da86b62SBarry Smith #endif
4090da86b62SBarry Smith 
410*41f502e3SPatrick Sanan   if (wtype == PETSC_INT)          m *= sizeof(PetscInt);
411*41f502e3SPatrick Sanan   else if (wtype == PETSC_SCALAR)  m *= sizeof(PetscScalar);
412*41f502e3SPatrick Sanan   else if (wtype == PETSC_DOUBLE)  m *= sizeof(double);
413*41f502e3SPatrick Sanan   else if (wtype == PETSC_FLOAT)   m *= sizeof(float);
414*41f502e3SPatrick Sanan   else if (wtype == PETSC_SHORT)   m *= sizeof(short);
415*41f502e3SPatrick Sanan   else if (wtype == PETSC_CHAR)    m *= sizeof(char);
416*41f502e3SPatrick Sanan   else if (wtype == PETSC_ENUM)    m *= sizeof(PetscEnum);
417*41f502e3SPatrick Sanan   else if (wtype == PETSC_BOOL)    m *= sizeof(PetscBool);
418*41f502e3SPatrick Sanan   else if (wtype == PETSC_BIT_LOGICAL) m = PetscBTLength(m)*sizeof(char);
419e32f2f54SBarry Smith   else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Unknown type");
420e5c89e4eSSatish Balay 
421e5c89e4eSSatish Balay #if !defined(PETSC_WORDS_BIGENDIAN)
422*41f502e3SPatrick Sanan   ierr = PetscByteSwap(ptmp,wtype,n);CHKERRQ(ierr);
423e5c89e4eSSatish Balay #endif
424e5c89e4eSSatish Balay 
425e5c89e4eSSatish Balay   while (m) {
426e5c89e4eSSatish Balay     wsize = (m < maxblock) ? m : maxblock;
427e5c89e4eSSatish Balay     err   = write(fd,pp,wsize);
428e5c89e4eSSatish Balay     if (err < 0 && errno == EINTR) continue;
42904102261SBarry Smith     if (err != wsize) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_FILE_WRITE,"Error writing to file total size %d err %d wsize %d",(int)n,(int)err,(int)wsize);
430e5c89e4eSSatish Balay     m  -= wsize;
431e5c89e4eSSatish Balay     pp += wsize;
432e5c89e4eSSatish Balay   }
433e5c89e4eSSatish Balay 
434b659c0ceSSatish Balay #if !defined(PETSC_WORDS_BIGENDIAN)
435e5c89e4eSSatish Balay   if (!istemp) {
436*41f502e3SPatrick Sanan     ierr = PetscByteSwap(ptmp,wtype,n);CHKERRQ(ierr);
437e5c89e4eSSatish Balay   }
438e5c89e4eSSatish Balay #endif
43905acbc63SBarry Smith   if (type == PETSC_FUNCTION) {
44005acbc63SBarry Smith     free(fname);
44105acbc63SBarry Smith   }
4420da86b62SBarry Smith #if defined(PETSC_USE_REAL___FLOAT128)
4430da86b62SBarry Smith   if (type == PETSC_SCALAR && writedouble) {
4440da86b62SBarry Smith     ierr = PetscFree(ppp);CHKERRQ(ierr);
4450da86b62SBarry Smith   }
4460da86b62SBarry Smith #endif
447e5c89e4eSSatish Balay   PetscFunctionReturn(0);
448e5c89e4eSSatish Balay }
449e5c89e4eSSatish Balay 
450e5c89e4eSSatish Balay #undef __FUNCT__
451e5c89e4eSSatish Balay #define __FUNCT__ "PetscBinaryOpen"
452e5c89e4eSSatish Balay /*@C
453e5c89e4eSSatish Balay    PetscBinaryOpen - Opens a PETSc binary file.
454e5c89e4eSSatish Balay 
455e5c89e4eSSatish Balay    Not Collective
456e5c89e4eSSatish Balay 
457e5c89e4eSSatish Balay    Input Parameters:
458e5c89e4eSSatish Balay +  name - filename
45945c64e65SBarry Smith -  type - type of binary file, one of FILE_MODE_READ, FILE_MODE_APPEND, FILE_MODE_WRITE
460e5c89e4eSSatish Balay 
461e5c89e4eSSatish Balay    Output Parameter:
462e5c89e4eSSatish Balay .  fd - the file
463e5c89e4eSSatish Balay 
464e5c89e4eSSatish Balay    Level: advanced
465e5c89e4eSSatish Balay 
466e5c89e4eSSatish Balay   Concepts: files^opening binary
467e5c89e4eSSatish Balay   Concepts: binary files^opening
468e5c89e4eSSatish Balay 
469e5c89e4eSSatish Balay    Notes: Files access with PetscBinaryRead() and PetscBinaryWrite() are ALWAYS written in
470e5c89e4eSSatish Balay    big-endian format. This means the file can be accessed using PetscBinaryOpen() and
471e5c89e4eSSatish Balay    PetscBinaryRead() and PetscBinaryWrite() on any machine.
472e5c89e4eSSatish Balay 
4734ebed01fSBarry Smith .seealso: PetscBinaryRead(), PetscBinaryWrite(), PetscFileMode, PetscViewerFileSetMode(), PetscViewerBinaryGetDescriptor(),
4744ebed01fSBarry Smith           PetscBinarySynchronizedWrite(), PetscBinarySynchronizedRead(), PetscBinarySynchronizedSeek()
47545c64e65SBarry Smith 
476e5c89e4eSSatish Balay @*/
4777087cfbeSBarry Smith PetscErrorCode  PetscBinaryOpen(const char name[],PetscFileMode mode,int *fd)
478e5c89e4eSSatish Balay {
479e5c89e4eSSatish Balay   PetscFunctionBegin;
480e5c89e4eSSatish Balay #if defined(PETSC_HAVE_O_BINARY)
48145c64e65SBarry Smith   if (mode == FILE_MODE_WRITE) {
482a297a907SKarl Rupp     if ((*fd = open(name,O_WRONLY|O_CREAT|O_TRUNC|O_BINARY,0666)) == -1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot create file for writing: %s",name);
48345c64e65SBarry Smith   } else if (mode == FILE_MODE_READ) {
484a297a907SKarl Rupp     if ((*fd = open(name,O_RDONLY|O_BINARY,0)) == -1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot open file for reading: %s",name);
48545c64e65SBarry Smith   } else if (mode == FILE_MODE_APPEND) {
486a297a907SKarl Rupp     if ((*fd = open(name,O_WRONLY|O_BINARY,0)) == -1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot open file for writing: %s",name);
487e5c89e4eSSatish Balay #else
48845c64e65SBarry Smith   if (mode == FILE_MODE_WRITE) {
489a297a907SKarl Rupp     if ((*fd = creat(name,0666)) == -1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot create file for writing: %s",name);
49045c64e65SBarry Smith   } else if (mode == FILE_MODE_READ) {
491a297a907SKarl Rupp     if ((*fd = open(name,O_RDONLY,0)) == -1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot open file for reading: %s",name);
492e5c89e4eSSatish Balay   }
49345c64e65SBarry Smith   else if (mode == FILE_MODE_APPEND) {
494a297a907SKarl Rupp     if ((*fd = open(name,O_WRONLY,0)) == -1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot open file for writing: %s",name);
495e5c89e4eSSatish Balay #endif
496e32f2f54SBarry Smith   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Unknown file mode");
497e5c89e4eSSatish Balay   PetscFunctionReturn(0);
498e5c89e4eSSatish Balay }
499e5c89e4eSSatish Balay 
500e5c89e4eSSatish Balay #undef __FUNCT__
501e5c89e4eSSatish Balay #define __FUNCT__ "PetscBinaryClose"
502e30d2299SSatish Balay /*@
503e5c89e4eSSatish Balay    PetscBinaryClose - Closes a PETSc binary file.
504e5c89e4eSSatish Balay 
505e5c89e4eSSatish Balay    Not Collective
506e5c89e4eSSatish Balay 
507e5c89e4eSSatish Balay    Output Parameter:
508e5c89e4eSSatish Balay .  fd - the file
509e5c89e4eSSatish Balay 
510e5c89e4eSSatish Balay    Level: advanced
511e5c89e4eSSatish Balay 
5124ebed01fSBarry Smith .seealso: PetscBinaryRead(), PetscBinaryWrite(), PetscBinaryOpen(), PetscBinarySynchronizedWrite(), PetscBinarySynchronizedRead(),
5134ebed01fSBarry Smith           PetscBinarySynchronizedSeek()
514e5c89e4eSSatish Balay @*/
5157087cfbeSBarry Smith PetscErrorCode  PetscBinaryClose(int fd)
516e5c89e4eSSatish Balay {
517e5c89e4eSSatish Balay   PetscFunctionBegin;
518e5c89e4eSSatish Balay   close(fd);
519e5c89e4eSSatish Balay   PetscFunctionReturn(0);
520e5c89e4eSSatish Balay }
521e5c89e4eSSatish Balay 
522e5c89e4eSSatish Balay 
523e5c89e4eSSatish Balay #undef __FUNCT__
524e5c89e4eSSatish Balay #define __FUNCT__ "PetscBinarySeek"
525e30d2299SSatish Balay /*@
526e5c89e4eSSatish Balay    PetscBinarySeek - Moves the file pointer on a PETSc binary file.
527e5c89e4eSSatish Balay 
528e5c89e4eSSatish Balay    Not Collective
529e5c89e4eSSatish Balay 
530e5c89e4eSSatish Balay    Input Parameters:
531e5c89e4eSSatish Balay +  fd - the file
532ff553b35SMatthew Knepley .  off - number of bytes to move. Use PETSC_BINARY_INT_SIZE, PETSC_BINARY_SCALAR_SIZE,
533e5c89e4eSSatish Balay             etc. in your calculation rather than sizeof() to compute byte lengths.
534ff553b35SMatthew Knepley -  whence - if PETSC_BINARY_SEEK_SET then off is an absolute location in the file
535ff553b35SMatthew Knepley             if PETSC_BINARY_SEEK_CUR then off is an offset from the current location
536ff553b35SMatthew Knepley             if PETSC_BINARY_SEEK_END then off is an offset from the end of file
537e5c89e4eSSatish Balay 
538e5c89e4eSSatish Balay    Output Parameter:
539e5c89e4eSSatish Balay .   offset - new offset in file
540e5c89e4eSSatish Balay 
541e5c89e4eSSatish Balay    Level: developer
542e5c89e4eSSatish Balay 
543e5c89e4eSSatish Balay    Notes:
544e5c89e4eSSatish Balay    Integers are stored on the file as 32 long, regardless of whether
545e5c89e4eSSatish Balay    they are stored in the machine as 32 or 64, this means the same
546e5c89e4eSSatish Balay    binary file may be read on any machine. Hence you CANNOT use sizeof()
547e5c89e4eSSatish Balay    to determine the offset or location.
548e5c89e4eSSatish Balay 
549e5c89e4eSSatish Balay    Concepts: files^binary seeking
550e5c89e4eSSatish Balay    Concepts: binary files^seeking
551e5c89e4eSSatish Balay 
5524ebed01fSBarry Smith .seealso: PetscBinaryRead(), PetscBinaryWrite(), PetscBinaryOpen(), PetscBinarySynchronizedWrite(), PetscBinarySynchronizedRead(),
5534ebed01fSBarry Smith           PetscBinarySynchronizedSeek()
554e5c89e4eSSatish Balay @*/
5557087cfbeSBarry Smith PetscErrorCode  PetscBinarySeek(int fd,off_t off,PetscBinarySeekType whence,off_t *offset)
556e5c89e4eSSatish Balay {
557e5c89e4eSSatish Balay   int iwhence = 0;
558e5c89e4eSSatish Balay 
559e5c89e4eSSatish Balay   PetscFunctionBegin;
560a297a907SKarl Rupp   if (whence == PETSC_BINARY_SEEK_SET) iwhence = SEEK_SET;
561a297a907SKarl Rupp   else if (whence == PETSC_BINARY_SEEK_CUR) iwhence = SEEK_CUR;
562a297a907SKarl Rupp   else if (whence == PETSC_BINARY_SEEK_END) iwhence = SEEK_END;
563a297a907SKarl Rupp   else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Unknown seek location");
564e5c89e4eSSatish Balay #if defined(PETSC_HAVE_LSEEK)
565e5c89e4eSSatish Balay   *offset = lseek(fd,off,iwhence);
566e5c89e4eSSatish Balay #elif defined(PETSC_HAVE__LSEEK)
567e5c89e4eSSatish Balay   *offset = _lseek(fd,(long)off,iwhence);
568e5c89e4eSSatish Balay #else
569e32f2f54SBarry Smith   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP_SYS,"System does not have a way of seeking on a file");
570e5c89e4eSSatish Balay #endif
571e5c89e4eSSatish Balay   PetscFunctionReturn(0);
572e5c89e4eSSatish Balay }
573e5c89e4eSSatish Balay 
574e5c89e4eSSatish Balay #undef __FUNCT__
5751d280d73SBarry Smith #define __FUNCT__ "PetscBinarySynchronizedRead"
576e5c89e4eSSatish Balay /*@C
5771d280d73SBarry Smith    PetscBinarySynchronizedRead - Reads from a binary file.
578e5c89e4eSSatish Balay 
579e5c89e4eSSatish Balay    Collective on MPI_Comm
580e5c89e4eSSatish Balay 
581e5c89e4eSSatish Balay    Input Parameters:
582e5c89e4eSSatish Balay +  comm - the MPI communicator
583e5c89e4eSSatish Balay .  fd - the file
584e5c89e4eSSatish Balay .  n  - the number of items to read
585e5c89e4eSSatish Balay -  type - the type of items to read (PETSC_INT, PETSC_DOUBLE or PETSC_SCALAR)
586e5c89e4eSSatish Balay 
587e5c89e4eSSatish Balay    Output Parameters:
588e5c89e4eSSatish Balay .  p - the buffer
589e5c89e4eSSatish Balay 
590e5c89e4eSSatish Balay    Level: developer
591e5c89e4eSSatish Balay 
592e5c89e4eSSatish Balay    Notes:
593e5c89e4eSSatish Balay    Does a PetscBinaryRead() followed by an MPI_Bcast()
594e5c89e4eSSatish Balay 
5951d280d73SBarry Smith    PetscBinarySynchronizedRead() uses byte swapping to work on all machines.
596e5c89e4eSSatish Balay    Integers are stored on the file as 32 long, regardless of whether
597e5c89e4eSSatish Balay    they are stored in the machine as 32 or 64, this means the same
598e5c89e4eSSatish Balay    binary file may be read on any machine.
599e5c89e4eSSatish Balay 
600e5c89e4eSSatish Balay    Concepts: files^synchronized reading of binary files
601e5c89e4eSSatish Balay    Concepts: binary files^reading, synchronized
602e5c89e4eSSatish Balay 
6034ebed01fSBarry Smith .seealso: PetscBinaryWrite(), PetscBinaryOpen(), PetscBinaryClose(), PetscBinaryRead(), PetscBinarySynchronizedWrite(),
6044ebed01fSBarry Smith           PetscBinarySynchronizedSeek()
605e5c89e4eSSatish Balay @*/
6067087cfbeSBarry Smith PetscErrorCode  PetscBinarySynchronizedRead(MPI_Comm comm,int fd,void *p,PetscInt n,PetscDataType type)
607e5c89e4eSSatish Balay {
608e5c89e4eSSatish Balay   PetscErrorCode ierr;
609e5c89e4eSSatish Balay   PetscMPIInt    rank;
610e5c89e4eSSatish Balay   MPI_Datatype   mtype;
61105acbc63SBarry Smith   char           *fname = NULL;
6120298fd71SBarry Smith   void           *ptmp = NULL;
613e5c89e4eSSatish Balay 
614e5c89e4eSSatish Balay   PetscFunctionBegin;
6152d53ad75SBarry Smith   if (type == PETSC_FUNCTION) {
6162d53ad75SBarry Smith     n            = 64;
6172d53ad75SBarry Smith     type         = PETSC_CHAR;
6182d53ad75SBarry Smith     ptmp         = p;
619e366c363SBarry Smith     fname        = (char*)malloc(n*sizeof(char));
62005acbc63SBarry Smith     if (!fname) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MEM,"Cannot allocate space for function name");
6212d53ad75SBarry Smith     p            = (void*)fname;
6222d53ad75SBarry Smith   }
6232d53ad75SBarry Smith 
624e5c89e4eSSatish Balay   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
625e5c89e4eSSatish Balay   if (!rank) {
626e5c89e4eSSatish Balay     ierr = PetscBinaryRead(fd,p,n,type);CHKERRQ(ierr);
627e5c89e4eSSatish Balay   }
628e5c89e4eSSatish Balay   ierr = PetscDataTypeToMPIDataType(type,&mtype);CHKERRQ(ierr);
629e5c89e4eSSatish Balay   ierr = MPI_Bcast(p,n,mtype,0,comm);CHKERRQ(ierr);
6302d53ad75SBarry Smith 
631e366c363SBarry Smith   if (type == PETSC_FUNCTION) {
6322d53ad75SBarry Smith #if defined(PETSC_SERIALIZE_FUNCTIONS)
6330298fd71SBarry Smith     ierr = PetscDLLibrarySym(PETSC_COMM_SELF,&PetscDLLibrariesLoaded,NULL,fname,(void**)ptmp);CHKERRQ(ierr);
6342d53ad75SBarry Smith #else
6350298fd71SBarry Smith     *(void**)ptmp = NULL;
6362d53ad75SBarry Smith #endif
637e366c363SBarry Smith     free(fname);
6382d53ad75SBarry Smith   }
639e5c89e4eSSatish Balay   PetscFunctionReturn(0);
640e5c89e4eSSatish Balay }
641e5c89e4eSSatish Balay 
642e5c89e4eSSatish Balay #undef __FUNCT__
6431d280d73SBarry Smith #define __FUNCT__ "PetscBinarySynchronizedWrite"
644e5c89e4eSSatish Balay /*@C
6451d280d73SBarry Smith    PetscBinarySynchronizedWrite - writes to a binary file.
646e5c89e4eSSatish Balay 
647e5c89e4eSSatish Balay    Collective on MPI_Comm
648e5c89e4eSSatish Balay 
649e5c89e4eSSatish Balay    Input Parameters:
650e5c89e4eSSatish Balay +  comm - the MPI communicator
651e5c89e4eSSatish Balay .  fd - the file
652e5c89e4eSSatish Balay .  n  - the number of items to write
653e5c89e4eSSatish Balay .  p - the buffer
654e5c89e4eSSatish Balay .  istemp - the buffer may be changed
655e5c89e4eSSatish Balay -  type - the type of items to write (PETSC_INT, PETSC_DOUBLE or PETSC_SCALAR)
656e5c89e4eSSatish Balay 
657e5c89e4eSSatish Balay    Level: developer
658e5c89e4eSSatish Balay 
659e5c89e4eSSatish Balay    Notes:
660e5c89e4eSSatish Balay    Process 0 does a PetscBinaryWrite()
661e5c89e4eSSatish Balay 
6621d280d73SBarry Smith    PetscBinarySynchronizedWrite() uses byte swapping to work on all machines.
663e5c89e4eSSatish Balay    Integers are stored on the file as 32 long, regardless of whether
664e5c89e4eSSatish Balay    they are stored in the machine as 32 or 64, this means the same
665e5c89e4eSSatish Balay    binary file may be read on any machine.
666e5c89e4eSSatish Balay 
667300a7f5bSBarry Smith    Notes: because byte-swapping may be done on the values in data it cannot be declared const
668300a7f5bSBarry Smith 
6691d280d73SBarry Smith    WARNING: This is NOT like PetscSynchronizedFPrintf()! This routine ignores calls on all but process 0,
6701d280d73SBarry Smith    while PetscSynchronizedFPrintf() has all processes print their strings in order.
6711d280d73SBarry Smith 
672e5c89e4eSSatish Balay    Concepts: files^synchronized writing of binary files
673e5c89e4eSSatish Balay    Concepts: binary files^reading, synchronized
674e5c89e4eSSatish Balay 
6754ebed01fSBarry Smith .seealso: PetscBinaryWrite(), PetscBinaryOpen(), PetscBinaryClose(), PetscBinaryRead(), PetscBinarySynchronizedRead(),
6764ebed01fSBarry Smith           PetscBinarySynchronizedSeek()
677e5c89e4eSSatish Balay @*/
6787087cfbeSBarry Smith PetscErrorCode  PetscBinarySynchronizedWrite(MPI_Comm comm,int fd,void *p,PetscInt n,PetscDataType type,PetscBool istemp)
679e5c89e4eSSatish Balay {
680e5c89e4eSSatish Balay   PetscErrorCode ierr;
681e5c89e4eSSatish Balay   PetscMPIInt    rank;
682e5c89e4eSSatish Balay 
683e5c89e4eSSatish Balay   PetscFunctionBegin;
684e5c89e4eSSatish Balay   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
685e5c89e4eSSatish Balay   if (!rank) {
686e5c89e4eSSatish Balay     ierr = PetscBinaryWrite(fd,p,n,type,istemp);CHKERRQ(ierr);
687e5c89e4eSSatish Balay   }
688e5c89e4eSSatish Balay   PetscFunctionReturn(0);
689e5c89e4eSSatish Balay }
690e5c89e4eSSatish Balay 
691e5c89e4eSSatish Balay #undef __FUNCT__
6921d280d73SBarry Smith #define __FUNCT__ "PetscBinarySynchronizedSeek"
693e5c89e4eSSatish Balay /*@C
6941d280d73SBarry Smith    PetscBinarySynchronizedSeek - Moves the file pointer on a PETSc binary file.
695e5c89e4eSSatish Balay 
696e5c89e4eSSatish Balay 
697e5c89e4eSSatish Balay    Input Parameters:
698e5c89e4eSSatish Balay +  fd - the file
699e5c89e4eSSatish Balay .  whence - if PETSC_BINARY_SEEK_SET then size is an absolute location in the file
700e5c89e4eSSatish Balay             if PETSC_BINARY_SEEK_CUR then size is offset from current location
701e5c89e4eSSatish Balay             if PETSC_BINARY_SEEK_END then size is offset from end of file
702e5c89e4eSSatish Balay -  off    - number of bytes to move. Use PETSC_BINARY_INT_SIZE, PETSC_BINARY_SCALAR_SIZE,
703e5c89e4eSSatish Balay             etc. in your calculation rather than sizeof() to compute byte lengths.
704e5c89e4eSSatish Balay 
705e5c89e4eSSatish Balay    Output Parameter:
706e5c89e4eSSatish Balay .   offset - new offset in file
707e5c89e4eSSatish Balay 
708e5c89e4eSSatish Balay    Level: developer
709e5c89e4eSSatish Balay 
710e5c89e4eSSatish Balay    Notes:
711e5c89e4eSSatish Balay    Integers are stored on the file as 32 long, regardless of whether
712e5c89e4eSSatish Balay    they are stored in the machine as 32 or 64, this means the same
713e5c89e4eSSatish Balay    binary file may be read on any machine. Hence you CANNOT use sizeof()
714e5c89e4eSSatish Balay    to determine the offset or location.
715e5c89e4eSSatish Balay 
716e5c89e4eSSatish Balay    Concepts: binary files^seeking
717e5c89e4eSSatish Balay    Concepts: files^seeking in binary
718e5c89e4eSSatish Balay 
7194ebed01fSBarry Smith .seealso: PetscBinaryRead(), PetscBinaryWrite(), PetscBinaryOpen(), PetscBinarySynchronizedWrite(), PetscBinarySynchronizedRead(),
7204ebed01fSBarry Smith           PetscBinarySynchronizedSeek()
721e5c89e4eSSatish Balay @*/
7227087cfbeSBarry Smith PetscErrorCode  PetscBinarySynchronizedSeek(MPI_Comm comm,int fd,off_t off,PetscBinarySeekType whence,off_t *offset)
723e5c89e4eSSatish Balay {
724e5c89e4eSSatish Balay   PetscErrorCode ierr;
725e5c89e4eSSatish Balay   PetscMPIInt    rank;
726e5c89e4eSSatish Balay 
727e5c89e4eSSatish Balay   PetscFunctionBegin;
728e5c89e4eSSatish Balay   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
729e5c89e4eSSatish Balay   if (!rank) {
730e5c89e4eSSatish Balay     ierr = PetscBinarySeek(fd,off,whence,offset);CHKERRQ(ierr);
731e5c89e4eSSatish Balay   }
732e5c89e4eSSatish Balay   PetscFunctionReturn(0);
733e5c89e4eSSatish Balay }
734e5c89e4eSSatish Balay 
7350fc9d207SBarry Smith #if defined(PETSC_HAVE_MPIIO)
736e39fd77fSBarry Smith #if !defined(PETSC_WORDS_BIGENDIAN)
737e39fd77fSBarry Smith 
738951e3c8eSBarry Smith #if defined(PETSC_USE_PETSC_MPI_EXTERNAL32)
739e39fd77fSBarry Smith /*
740e39fd77fSBarry Smith       MPICH does not provide the external32 representation for MPI_File_set_view() so we need to provide the functions.
741e39fd77fSBarry Smith     These are set into MPI in PetscInitialize() via MPI_Register_datarep()
742e39fd77fSBarry Smith 
743e39fd77fSBarry Smith     Note I use PetscMPIInt for the MPI error codes since that is what MPI uses (instead of the standard PetscErrorCode)
744e39fd77fSBarry Smith 
745951e3c8eSBarry Smith     The next three routines are not used because MPICH does not support their use
746e39fd77fSBarry Smith 
747e39fd77fSBarry Smith */
7488cc058d9SJed Brown PETSC_EXTERN PetscMPIInt PetscDataRep_extent_fn(MPI_Datatype datatype,MPI_Aint *file_extent,void *extra_state)
749e39fd77fSBarry Smith {
750e39fd77fSBarry Smith   MPI_Aint    ub;
751e39fd77fSBarry Smith   PetscMPIInt ierr;
752e39fd77fSBarry Smith 
753e39fd77fSBarry Smith   ierr = MPI_Type_get_extent(datatype,&ub,file_extent);
754e39fd77fSBarry Smith   return ierr;
755e39fd77fSBarry Smith }
756e39fd77fSBarry Smith 
7578cc058d9SJed Brown PETSC_EXTERN PetscMPIInt PetscDataRep_read_conv_fn(void *userbuf, MPI_Datatype datatype,PetscMPIInt count,void *filebuf, MPI_Offset position,void *extra_state)
758e39fd77fSBarry Smith {
759e39fd77fSBarry Smith   PetscDataType pdtype;
760e39fd77fSBarry Smith   PetscMPIInt   ierr;
761e39fd77fSBarry Smith   size_t        dsize;
762e39fd77fSBarry Smith 
763e39fd77fSBarry Smith   ierr = PetscMPIDataTypeToPetscDataType(datatype,&pdtype);CHKERRQ(ierr);
764e39fd77fSBarry Smith   ierr = PetscDataTypeGetSize(pdtype,&dsize);CHKERRQ(ierr);
765e39fd77fSBarry Smith 
766e39fd77fSBarry Smith   /* offset is given in units of MPI_Datatype */
767e39fd77fSBarry Smith   userbuf = ((char*)userbuf) + dsize*position;
768e39fd77fSBarry Smith 
769e39fd77fSBarry Smith   ierr = PetscMemcpy(userbuf,filebuf,count*dsize);CHKERRQ(ierr);
770e39fd77fSBarry Smith   ierr = PetscByteSwap(userbuf,pdtype,count);CHKERRQ(ierr);
771e39fd77fSBarry Smith   return ierr;
772e39fd77fSBarry Smith }
773e39fd77fSBarry Smith 
774e39fd77fSBarry Smith PetscMPIInt PetscDataRep_write_conv_fn(void *userbuf, MPI_Datatype datatype,PetscMPIInt count,void *filebuf, MPI_Offset position,void *extra_state)
775e39fd77fSBarry Smith {
776e39fd77fSBarry Smith   PetscDataType pdtype;
777e39fd77fSBarry Smith   PetscMPIInt   ierr;
778e39fd77fSBarry Smith   size_t        dsize;
779e39fd77fSBarry Smith 
780e39fd77fSBarry Smith   ierr = PetscMPIDataTypeToPetscDataType(datatype,&pdtype);CHKERRQ(ierr);
781e39fd77fSBarry Smith   ierr = PetscDataTypeGetSize(pdtype,&dsize);CHKERRQ(ierr);
782e39fd77fSBarry Smith 
783e39fd77fSBarry Smith   /* offset is given in units of MPI_Datatype */
784e39fd77fSBarry Smith   userbuf = ((char*)userbuf) + dsize*position;
785e39fd77fSBarry Smith 
786e39fd77fSBarry Smith   ierr = PetscMemcpy(filebuf,userbuf,count*dsize);CHKERRQ(ierr);
787e39fd77fSBarry Smith   ierr = PetscByteSwap(filebuf,pdtype,count);CHKERRQ(ierr);
788e39fd77fSBarry Smith   return ierr;
789e39fd77fSBarry Smith }
790951e3c8eSBarry Smith #endif
791e39fd77fSBarry Smith 
7921c91e6bcSJed Brown #undef __FUNCT__
7931c91e6bcSJed Brown #define __FUNCT__ "MPIU_File_write_all"
794e39fd77fSBarry Smith PetscErrorCode MPIU_File_write_all(MPI_File fd,void *data,PetscMPIInt cnt,MPI_Datatype dtype,MPI_Status *status)
795e39fd77fSBarry Smith {
796e39fd77fSBarry Smith   PetscErrorCode ierr;
797e39fd77fSBarry Smith   PetscDataType  pdtype;
798e39fd77fSBarry Smith 
799e39fd77fSBarry Smith   PetscFunctionBegin;
800e39fd77fSBarry Smith   ierr = PetscMPIDataTypeToPetscDataType(dtype,&pdtype);CHKERRQ(ierr);
801e39fd77fSBarry Smith   ierr = PetscByteSwap(data,pdtype,cnt);CHKERRQ(ierr);
802e39fd77fSBarry Smith   ierr = MPI_File_write_all(fd,data,cnt,dtype,status);CHKERRQ(ierr);
803e39fd77fSBarry Smith   ierr = PetscByteSwap(data,pdtype,cnt);CHKERRQ(ierr);
804a6796414SBarry Smith   PetscFunctionReturn(0);
805e39fd77fSBarry Smith }
806e39fd77fSBarry Smith 
8071c91e6bcSJed Brown #undef __FUNCT__
8081c91e6bcSJed Brown #define __FUNCT__ "MPIU_File_read_all"
809e39fd77fSBarry Smith PetscErrorCode MPIU_File_read_all(MPI_File fd,void *data,PetscMPIInt cnt,MPI_Datatype dtype,MPI_Status *status)
810e39fd77fSBarry Smith {
811e39fd77fSBarry Smith   PetscErrorCode ierr;
812e39fd77fSBarry Smith   PetscDataType  pdtype;
813e39fd77fSBarry Smith 
814e39fd77fSBarry Smith   PetscFunctionBegin;
815e39fd77fSBarry Smith   ierr = PetscMPIDataTypeToPetscDataType(dtype,&pdtype);CHKERRQ(ierr);
816e39fd77fSBarry Smith   ierr = MPI_File_read_all(fd,data,cnt,dtype,status);CHKERRQ(ierr);
817e39fd77fSBarry Smith   ierr = PetscByteSwap(data,pdtype,cnt);CHKERRQ(ierr);
818a6796414SBarry Smith   PetscFunctionReturn(0);
819e39fd77fSBarry Smith }
820e39fd77fSBarry Smith #endif
821951e3c8eSBarry Smith #endif
822