xref: /petsc/src/sys/fileio/sysio.c (revision 0da86b621abcf6b05d43c3ea3d6367cb60fa2dba)
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 /*
105e5c89e4eSSatish Balay   PetscByteSwapScalar - Swap bytes in a double
106e5c89e4eSSatish Balay   Complex is dealt with as if array of double 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*0da86b62SBarry Smith    If running with __float128 precision the output is in __float128 unless one uses the -binary_read_double option
333*0da86b62SBarry 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;
362*0da86b62SBarry Smith #if defined(PETSC_USE_REAL___FLOAT128)
363*0da86b62SBarry Smith   PetscBool      writedouble;
364*0da86b62SBarry Smith   double         *ppp;
365*0da86b62SBarry Smith   PetscReal      *pv;
366*0da86b62SBarry Smith   PetscInt       i;
367*0da86b62SBarry Smith #endif
368e5c89e4eSSatish Balay 
369e5c89e4eSSatish Balay   PetscFunctionBegin;
370e32f2f54SBarry Smith   if (n < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Trying to write a negative amount of data %D",n);
371e5c89e4eSSatish Balay   if (!n) PetscFunctionReturn(0);
372e5c89e4eSSatish Balay 
3732d53ad75SBarry Smith   if (type == PETSC_FUNCTION) {
3742d53ad75SBarry Smith #if defined(PETSC_SERIALIZE_FUNCTIONS)
3752d53ad75SBarry Smith     const char *fnametmp;
3762d53ad75SBarry Smith #endif
3772d53ad75SBarry Smith     m     = 64;
378e25ab156SSatish Balay     fname = (char*)malloc(m*sizeof(char));
37905acbc63SBarry Smith     if (!fname) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MEM,"Cannot allocate space for function name");
38005acbc63SBarry Smith #if defined(PETSC_SERIALIZE_FUNCTIONS)
38105acbc63SBarry Smith     if (n > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Can only binary view a single function at a time");
38205acbc63SBarry Smith     ierr = PetscFPTFind(*(void**)p,&fnametmp);CHKERRQ(ierr);
38305acbc63SBarry Smith     ierr = PetscStrncpy(fname,fnametmp,m);CHKERRQ(ierr);
38405acbc63SBarry Smith #else
38505acbc63SBarry Smith     ierr = PetscStrncpy(fname,"",m);CHKERRQ(ierr);
38605acbc63SBarry Smith #endif
3872d53ad75SBarry Smith     type = PETSC_CHAR;
3882d53ad75SBarry Smith     pp   = (char*)fname;
3892d53ad75SBarry Smith #if !defined(PETSC_WORDS_BIGENDIAN)
3902d53ad75SBarry Smith     ptmp = (void*)fname;
3912d53ad75SBarry Smith #endif
3922d53ad75SBarry Smith   }
3932d53ad75SBarry Smith 
394*0da86b62SBarry Smith #if defined(PETSC_USE_REAL___FLOAT128)
395*0da86b62SBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-binary_write_double",&writedouble,NULL);CHKERRQ(ierr);
396*0da86b62SBarry Smith   /* If using __float128 precision we still write in doubles to file */
397*0da86b62SBarry Smith   if (type == PETSC_SCALAR && writedouble) {
398*0da86b62SBarry Smith     m    = m/2;
399*0da86b62SBarry Smith     ierr = PetscMalloc1(n,&ppp);CHKERRQ(ierr);
400*0da86b62SBarry Smith     pv = (PetscReal*)pp;
401*0da86b62SBarry Smith     for (i=0; i<n; i++) {
402*0da86b62SBarry Smith       ppp[i] = (double) pv[i];
403*0da86b62SBarry Smith     }
404*0da86b62SBarry Smith     pp   = (char*)ppp;
405*0da86b62SBarry Smith     ptmp = (char*)ppp;
406*0da86b62SBarry Smith   }
407*0da86b62SBarry Smith #endif
408*0da86b62SBarry Smith 
4096de02169SBarry Smith   if (type == PETSC_INT)          m *= sizeof(PetscInt);
410e5c89e4eSSatish Balay   else if (type == PETSC_SCALAR)  m *= sizeof(PetscScalar);
411e5c89e4eSSatish Balay   else if (type == PETSC_DOUBLE)  m *= sizeof(double);
412e95bf02fSSatish Balay   else if (type == PETSC_FLOAT)   m *= sizeof(float);
413e5c89e4eSSatish Balay   else if (type == PETSC_SHORT)   m *= sizeof(short);
414e5c89e4eSSatish Balay   else if (type == PETSC_CHAR)    m *= sizeof(char);
415e5c89e4eSSatish Balay   else if (type == PETSC_ENUM)    m *= sizeof(PetscEnum);
416ace3abfcSBarry Smith   else if (type == PETSC_BOOL)   m *= sizeof(PetscBool);
4179f7b6320SBarry Smith   else if (type == PETSC_BIT_LOGICAL) m = PetscBTLength(m)*sizeof(char);
418e32f2f54SBarry Smith   else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Unknown type");
419e5c89e4eSSatish Balay 
420e5c89e4eSSatish Balay #if !defined(PETSC_WORDS_BIGENDIAN)
421e39fd77fSBarry Smith   ierr = PetscByteSwap(ptmp,type,n);CHKERRQ(ierr);
422e5c89e4eSSatish Balay #endif
423e5c89e4eSSatish Balay 
424e5c89e4eSSatish Balay   while (m) {
425e5c89e4eSSatish Balay     wsize = (m < maxblock) ? m : maxblock;
426e5c89e4eSSatish Balay     err   = write(fd,pp,wsize);
427e5c89e4eSSatish Balay     if (err < 0 && errno == EINTR) continue;
42804102261SBarry 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);
429e5c89e4eSSatish Balay     m  -= wsize;
430e5c89e4eSSatish Balay     pp += wsize;
431e5c89e4eSSatish Balay   }
432e5c89e4eSSatish Balay 
433b659c0ceSSatish Balay #if !defined(PETSC_WORDS_BIGENDIAN)
434e5c89e4eSSatish Balay   if (!istemp) {
435e39fd77fSBarry Smith     ierr = PetscByteSwap(ptmp,type,n);CHKERRQ(ierr);
436e5c89e4eSSatish Balay   }
437e5c89e4eSSatish Balay #endif
43805acbc63SBarry Smith   if (type == PETSC_FUNCTION) {
43905acbc63SBarry Smith     free(fname);
44005acbc63SBarry Smith   }
441*0da86b62SBarry Smith #if defined(PETSC_USE_REAL___FLOAT128)
442*0da86b62SBarry Smith   if (type == PETSC_SCALAR && writedouble) {
443*0da86b62SBarry Smith     ierr = PetscFree(ppp);CHKERRQ(ierr);
444*0da86b62SBarry Smith   }
445*0da86b62SBarry Smith #endif
446e5c89e4eSSatish Balay   PetscFunctionReturn(0);
447e5c89e4eSSatish Balay }
448e5c89e4eSSatish Balay 
449e5c89e4eSSatish Balay #undef __FUNCT__
450e5c89e4eSSatish Balay #define __FUNCT__ "PetscBinaryOpen"
451e5c89e4eSSatish Balay /*@C
452e5c89e4eSSatish Balay    PetscBinaryOpen - Opens a PETSc binary file.
453e5c89e4eSSatish Balay 
454e5c89e4eSSatish Balay    Not Collective
455e5c89e4eSSatish Balay 
456e5c89e4eSSatish Balay    Input Parameters:
457e5c89e4eSSatish Balay +  name - filename
45845c64e65SBarry Smith -  type - type of binary file, one of FILE_MODE_READ, FILE_MODE_APPEND, FILE_MODE_WRITE
459e5c89e4eSSatish Balay 
460e5c89e4eSSatish Balay    Output Parameter:
461e5c89e4eSSatish Balay .  fd - the file
462e5c89e4eSSatish Balay 
463e5c89e4eSSatish Balay    Level: advanced
464e5c89e4eSSatish Balay 
465e5c89e4eSSatish Balay   Concepts: files^opening binary
466e5c89e4eSSatish Balay   Concepts: binary files^opening
467e5c89e4eSSatish Balay 
468e5c89e4eSSatish Balay    Notes: Files access with PetscBinaryRead() and PetscBinaryWrite() are ALWAYS written in
469e5c89e4eSSatish Balay    big-endian format. This means the file can be accessed using PetscBinaryOpen() and
470e5c89e4eSSatish Balay    PetscBinaryRead() and PetscBinaryWrite() on any machine.
471e5c89e4eSSatish Balay 
4724ebed01fSBarry Smith .seealso: PetscBinaryRead(), PetscBinaryWrite(), PetscFileMode, PetscViewerFileSetMode(), PetscViewerBinaryGetDescriptor(),
4734ebed01fSBarry Smith           PetscBinarySynchronizedWrite(), PetscBinarySynchronizedRead(), PetscBinarySynchronizedSeek()
47445c64e65SBarry Smith 
475e5c89e4eSSatish Balay @*/
4767087cfbeSBarry Smith PetscErrorCode  PetscBinaryOpen(const char name[],PetscFileMode mode,int *fd)
477e5c89e4eSSatish Balay {
478e5c89e4eSSatish Balay   PetscFunctionBegin;
479e5c89e4eSSatish Balay #if defined(PETSC_HAVE_O_BINARY)
48045c64e65SBarry Smith   if (mode == FILE_MODE_WRITE) {
481a297a907SKarl 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);
48245c64e65SBarry Smith   } else if (mode == FILE_MODE_READ) {
483a297a907SKarl 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);
48445c64e65SBarry Smith   } else if (mode == FILE_MODE_APPEND) {
485a297a907SKarl 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);
486e5c89e4eSSatish Balay #else
48745c64e65SBarry Smith   if (mode == FILE_MODE_WRITE) {
488a297a907SKarl Rupp     if ((*fd = creat(name,0666)) == -1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot create file for writing: %s",name);
48945c64e65SBarry Smith   } else if (mode == FILE_MODE_READ) {
490a297a907SKarl Rupp     if ((*fd = open(name,O_RDONLY,0)) == -1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot open file for reading: %s",name);
491e5c89e4eSSatish Balay   }
49245c64e65SBarry Smith   else if (mode == FILE_MODE_APPEND) {
493a297a907SKarl Rupp     if ((*fd = open(name,O_WRONLY,0)) == -1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot open file for writing: %s",name);
494e5c89e4eSSatish Balay #endif
495e32f2f54SBarry Smith   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Unknown file mode");
496e5c89e4eSSatish Balay   PetscFunctionReturn(0);
497e5c89e4eSSatish Balay }
498e5c89e4eSSatish Balay 
499e5c89e4eSSatish Balay #undef __FUNCT__
500e5c89e4eSSatish Balay #define __FUNCT__ "PetscBinaryClose"
501e30d2299SSatish Balay /*@
502e5c89e4eSSatish Balay    PetscBinaryClose - Closes a PETSc binary file.
503e5c89e4eSSatish Balay 
504e5c89e4eSSatish Balay    Not Collective
505e5c89e4eSSatish Balay 
506e5c89e4eSSatish Balay    Output Parameter:
507e5c89e4eSSatish Balay .  fd - the file
508e5c89e4eSSatish Balay 
509e5c89e4eSSatish Balay    Level: advanced
510e5c89e4eSSatish Balay 
5114ebed01fSBarry Smith .seealso: PetscBinaryRead(), PetscBinaryWrite(), PetscBinaryOpen(), PetscBinarySynchronizedWrite(), PetscBinarySynchronizedRead(),
5124ebed01fSBarry Smith           PetscBinarySynchronizedSeek()
513e5c89e4eSSatish Balay @*/
5147087cfbeSBarry Smith PetscErrorCode  PetscBinaryClose(int fd)
515e5c89e4eSSatish Balay {
516e5c89e4eSSatish Balay   PetscFunctionBegin;
517e5c89e4eSSatish Balay   close(fd);
518e5c89e4eSSatish Balay   PetscFunctionReturn(0);
519e5c89e4eSSatish Balay }
520e5c89e4eSSatish Balay 
521e5c89e4eSSatish Balay 
522e5c89e4eSSatish Balay #undef __FUNCT__
523e5c89e4eSSatish Balay #define __FUNCT__ "PetscBinarySeek"
524e30d2299SSatish Balay /*@
525e5c89e4eSSatish Balay    PetscBinarySeek - Moves the file pointer on a PETSc binary file.
526e5c89e4eSSatish Balay 
527e5c89e4eSSatish Balay    Not Collective
528e5c89e4eSSatish Balay 
529e5c89e4eSSatish Balay    Input Parameters:
530e5c89e4eSSatish Balay +  fd - the file
531ff553b35SMatthew Knepley .  off - number of bytes to move. Use PETSC_BINARY_INT_SIZE, PETSC_BINARY_SCALAR_SIZE,
532e5c89e4eSSatish Balay             etc. in your calculation rather than sizeof() to compute byte lengths.
533ff553b35SMatthew Knepley -  whence - if PETSC_BINARY_SEEK_SET then off is an absolute location in the file
534ff553b35SMatthew Knepley             if PETSC_BINARY_SEEK_CUR then off is an offset from the current location
535ff553b35SMatthew Knepley             if PETSC_BINARY_SEEK_END then off is an offset from the end of file
536e5c89e4eSSatish Balay 
537e5c89e4eSSatish Balay    Output Parameter:
538e5c89e4eSSatish Balay .   offset - new offset in file
539e5c89e4eSSatish Balay 
540e5c89e4eSSatish Balay    Level: developer
541e5c89e4eSSatish Balay 
542e5c89e4eSSatish Balay    Notes:
543e5c89e4eSSatish Balay    Integers are stored on the file as 32 long, regardless of whether
544e5c89e4eSSatish Balay    they are stored in the machine as 32 or 64, this means the same
545e5c89e4eSSatish Balay    binary file may be read on any machine. Hence you CANNOT use sizeof()
546e5c89e4eSSatish Balay    to determine the offset or location.
547e5c89e4eSSatish Balay 
548e5c89e4eSSatish Balay    Concepts: files^binary seeking
549e5c89e4eSSatish Balay    Concepts: binary files^seeking
550e5c89e4eSSatish Balay 
5514ebed01fSBarry Smith .seealso: PetscBinaryRead(), PetscBinaryWrite(), PetscBinaryOpen(), PetscBinarySynchronizedWrite(), PetscBinarySynchronizedRead(),
5524ebed01fSBarry Smith           PetscBinarySynchronizedSeek()
553e5c89e4eSSatish Balay @*/
5547087cfbeSBarry Smith PetscErrorCode  PetscBinarySeek(int fd,off_t off,PetscBinarySeekType whence,off_t *offset)
555e5c89e4eSSatish Balay {
556e5c89e4eSSatish Balay   int iwhence = 0;
557e5c89e4eSSatish Balay 
558e5c89e4eSSatish Balay   PetscFunctionBegin;
559a297a907SKarl Rupp   if (whence == PETSC_BINARY_SEEK_SET) iwhence = SEEK_SET;
560a297a907SKarl Rupp   else if (whence == PETSC_BINARY_SEEK_CUR) iwhence = SEEK_CUR;
561a297a907SKarl Rupp   else if (whence == PETSC_BINARY_SEEK_END) iwhence = SEEK_END;
562a297a907SKarl Rupp   else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Unknown seek location");
563e5c89e4eSSatish Balay #if defined(PETSC_HAVE_LSEEK)
564e5c89e4eSSatish Balay   *offset = lseek(fd,off,iwhence);
565e5c89e4eSSatish Balay #elif defined(PETSC_HAVE__LSEEK)
566e5c89e4eSSatish Balay   *offset = _lseek(fd,(long)off,iwhence);
567e5c89e4eSSatish Balay #else
568e32f2f54SBarry Smith   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP_SYS,"System does not have a way of seeking on a file");
569e5c89e4eSSatish Balay #endif
570e5c89e4eSSatish Balay   PetscFunctionReturn(0);
571e5c89e4eSSatish Balay }
572e5c89e4eSSatish Balay 
573e5c89e4eSSatish Balay #undef __FUNCT__
5741d280d73SBarry Smith #define __FUNCT__ "PetscBinarySynchronizedRead"
575e5c89e4eSSatish Balay /*@C
5761d280d73SBarry Smith    PetscBinarySynchronizedRead - Reads from a binary file.
577e5c89e4eSSatish Balay 
578e5c89e4eSSatish Balay    Collective on MPI_Comm
579e5c89e4eSSatish Balay 
580e5c89e4eSSatish Balay    Input Parameters:
581e5c89e4eSSatish Balay +  comm - the MPI communicator
582e5c89e4eSSatish Balay .  fd - the file
583e5c89e4eSSatish Balay .  n  - the number of items to read
584e5c89e4eSSatish Balay -  type - the type of items to read (PETSC_INT, PETSC_DOUBLE or PETSC_SCALAR)
585e5c89e4eSSatish Balay 
586e5c89e4eSSatish Balay    Output Parameters:
587e5c89e4eSSatish Balay .  p - the buffer
588e5c89e4eSSatish Balay 
589e5c89e4eSSatish Balay    Level: developer
590e5c89e4eSSatish Balay 
591e5c89e4eSSatish Balay    Notes:
592e5c89e4eSSatish Balay    Does a PetscBinaryRead() followed by an MPI_Bcast()
593e5c89e4eSSatish Balay 
5941d280d73SBarry Smith    PetscBinarySynchronizedRead() uses byte swapping to work on all machines.
595e5c89e4eSSatish Balay    Integers are stored on the file as 32 long, regardless of whether
596e5c89e4eSSatish Balay    they are stored in the machine as 32 or 64, this means the same
597e5c89e4eSSatish Balay    binary file may be read on any machine.
598e5c89e4eSSatish Balay 
599e5c89e4eSSatish Balay    Concepts: files^synchronized reading of binary files
600e5c89e4eSSatish Balay    Concepts: binary files^reading, synchronized
601e5c89e4eSSatish Balay 
6024ebed01fSBarry Smith .seealso: PetscBinaryWrite(), PetscBinaryOpen(), PetscBinaryClose(), PetscBinaryRead(), PetscBinarySynchronizedWrite(),
6034ebed01fSBarry Smith           PetscBinarySynchronizedSeek()
604e5c89e4eSSatish Balay @*/
6057087cfbeSBarry Smith PetscErrorCode  PetscBinarySynchronizedRead(MPI_Comm comm,int fd,void *p,PetscInt n,PetscDataType type)
606e5c89e4eSSatish Balay {
607e5c89e4eSSatish Balay   PetscErrorCode ierr;
608e5c89e4eSSatish Balay   PetscMPIInt    rank;
609e5c89e4eSSatish Balay   MPI_Datatype   mtype;
61005acbc63SBarry Smith   char           *fname = NULL;
6110298fd71SBarry Smith   void           *ptmp = NULL;
612e5c89e4eSSatish Balay 
613e5c89e4eSSatish Balay   PetscFunctionBegin;
6142d53ad75SBarry Smith   if (type == PETSC_FUNCTION) {
6152d53ad75SBarry Smith     n            = 64;
6162d53ad75SBarry Smith     type         = PETSC_CHAR;
6172d53ad75SBarry Smith     ptmp         = p;
618e366c363SBarry Smith     fname        = (char*)malloc(n*sizeof(char));
61905acbc63SBarry Smith     if (!fname) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MEM,"Cannot allocate space for function name");
6202d53ad75SBarry Smith     p            = (void*)fname;
6212d53ad75SBarry Smith   }
6222d53ad75SBarry Smith 
623e5c89e4eSSatish Balay   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
624e5c89e4eSSatish Balay   if (!rank) {
625e5c89e4eSSatish Balay     ierr = PetscBinaryRead(fd,p,n,type);CHKERRQ(ierr);
626e5c89e4eSSatish Balay   }
627e5c89e4eSSatish Balay   ierr = PetscDataTypeToMPIDataType(type,&mtype);CHKERRQ(ierr);
628e5c89e4eSSatish Balay   ierr = MPI_Bcast(p,n,mtype,0,comm);CHKERRQ(ierr);
6292d53ad75SBarry Smith 
630e366c363SBarry Smith   if (type == PETSC_FUNCTION) {
6312d53ad75SBarry Smith #if defined(PETSC_SERIALIZE_FUNCTIONS)
6320298fd71SBarry Smith     ierr = PetscDLLibrarySym(PETSC_COMM_SELF,&PetscDLLibrariesLoaded,NULL,fname,(void**)ptmp);CHKERRQ(ierr);
6332d53ad75SBarry Smith #else
6340298fd71SBarry Smith     *(void**)ptmp = NULL;
6352d53ad75SBarry Smith #endif
636e366c363SBarry Smith     free(fname);
6372d53ad75SBarry Smith   }
638e5c89e4eSSatish Balay   PetscFunctionReturn(0);
639e5c89e4eSSatish Balay }
640e5c89e4eSSatish Balay 
641e5c89e4eSSatish Balay #undef __FUNCT__
6421d280d73SBarry Smith #define __FUNCT__ "PetscBinarySynchronizedWrite"
643e5c89e4eSSatish Balay /*@C
6441d280d73SBarry Smith    PetscBinarySynchronizedWrite - writes to a binary file.
645e5c89e4eSSatish Balay 
646e5c89e4eSSatish Balay    Collective on MPI_Comm
647e5c89e4eSSatish Balay 
648e5c89e4eSSatish Balay    Input Parameters:
649e5c89e4eSSatish Balay +  comm - the MPI communicator
650e5c89e4eSSatish Balay .  fd - the file
651e5c89e4eSSatish Balay .  n  - the number of items to write
652e5c89e4eSSatish Balay .  p - the buffer
653e5c89e4eSSatish Balay .  istemp - the buffer may be changed
654e5c89e4eSSatish Balay -  type - the type of items to write (PETSC_INT, PETSC_DOUBLE or PETSC_SCALAR)
655e5c89e4eSSatish Balay 
656e5c89e4eSSatish Balay    Level: developer
657e5c89e4eSSatish Balay 
658e5c89e4eSSatish Balay    Notes:
659e5c89e4eSSatish Balay    Process 0 does a PetscBinaryWrite()
660e5c89e4eSSatish Balay 
6611d280d73SBarry Smith    PetscBinarySynchronizedWrite() uses byte swapping to work on all machines.
662e5c89e4eSSatish Balay    Integers are stored on the file as 32 long, regardless of whether
663e5c89e4eSSatish Balay    they are stored in the machine as 32 or 64, this means the same
664e5c89e4eSSatish Balay    binary file may be read on any machine.
665e5c89e4eSSatish Balay 
666300a7f5bSBarry Smith    Notes: because byte-swapping may be done on the values in data it cannot be declared const
667300a7f5bSBarry Smith 
6681d280d73SBarry Smith    WARNING: This is NOT like PetscSynchronizedFPrintf()! This routine ignores calls on all but process 0,
6691d280d73SBarry Smith    while PetscSynchronizedFPrintf() has all processes print their strings in order.
6701d280d73SBarry Smith 
671e5c89e4eSSatish Balay    Concepts: files^synchronized writing of binary files
672e5c89e4eSSatish Balay    Concepts: binary files^reading, synchronized
673e5c89e4eSSatish Balay 
6744ebed01fSBarry Smith .seealso: PetscBinaryWrite(), PetscBinaryOpen(), PetscBinaryClose(), PetscBinaryRead(), PetscBinarySynchronizedRead(),
6754ebed01fSBarry Smith           PetscBinarySynchronizedSeek()
676e5c89e4eSSatish Balay @*/
6777087cfbeSBarry Smith PetscErrorCode  PetscBinarySynchronizedWrite(MPI_Comm comm,int fd,void *p,PetscInt n,PetscDataType type,PetscBool istemp)
678e5c89e4eSSatish Balay {
679e5c89e4eSSatish Balay   PetscErrorCode ierr;
680e5c89e4eSSatish Balay   PetscMPIInt    rank;
681e5c89e4eSSatish Balay 
682e5c89e4eSSatish Balay   PetscFunctionBegin;
683e5c89e4eSSatish Balay   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
684e5c89e4eSSatish Balay   if (!rank) {
685e5c89e4eSSatish Balay     ierr = PetscBinaryWrite(fd,p,n,type,istemp);CHKERRQ(ierr);
686e5c89e4eSSatish Balay   }
687e5c89e4eSSatish Balay   PetscFunctionReturn(0);
688e5c89e4eSSatish Balay }
689e5c89e4eSSatish Balay 
690e5c89e4eSSatish Balay #undef __FUNCT__
6911d280d73SBarry Smith #define __FUNCT__ "PetscBinarySynchronizedSeek"
692e5c89e4eSSatish Balay /*@C
6931d280d73SBarry Smith    PetscBinarySynchronizedSeek - Moves the file pointer on a PETSc binary file.
694e5c89e4eSSatish Balay 
695e5c89e4eSSatish Balay 
696e5c89e4eSSatish Balay    Input Parameters:
697e5c89e4eSSatish Balay +  fd - the file
698e5c89e4eSSatish Balay .  whence - if PETSC_BINARY_SEEK_SET then size is an absolute location in the file
699e5c89e4eSSatish Balay             if PETSC_BINARY_SEEK_CUR then size is offset from current location
700e5c89e4eSSatish Balay             if PETSC_BINARY_SEEK_END then size is offset from end of file
701e5c89e4eSSatish Balay -  off    - number of bytes to move. Use PETSC_BINARY_INT_SIZE, PETSC_BINARY_SCALAR_SIZE,
702e5c89e4eSSatish Balay             etc. in your calculation rather than sizeof() to compute byte lengths.
703e5c89e4eSSatish Balay 
704e5c89e4eSSatish Balay    Output Parameter:
705e5c89e4eSSatish Balay .   offset - new offset in file
706e5c89e4eSSatish Balay 
707e5c89e4eSSatish Balay    Level: developer
708e5c89e4eSSatish Balay 
709e5c89e4eSSatish Balay    Notes:
710e5c89e4eSSatish Balay    Integers are stored on the file as 32 long, regardless of whether
711e5c89e4eSSatish Balay    they are stored in the machine as 32 or 64, this means the same
712e5c89e4eSSatish Balay    binary file may be read on any machine. Hence you CANNOT use sizeof()
713e5c89e4eSSatish Balay    to determine the offset or location.
714e5c89e4eSSatish Balay 
715e5c89e4eSSatish Balay    Concepts: binary files^seeking
716e5c89e4eSSatish Balay    Concepts: files^seeking in binary
717e5c89e4eSSatish Balay 
7184ebed01fSBarry Smith .seealso: PetscBinaryRead(), PetscBinaryWrite(), PetscBinaryOpen(), PetscBinarySynchronizedWrite(), PetscBinarySynchronizedRead(),
7194ebed01fSBarry Smith           PetscBinarySynchronizedSeek()
720e5c89e4eSSatish Balay @*/
7217087cfbeSBarry Smith PetscErrorCode  PetscBinarySynchronizedSeek(MPI_Comm comm,int fd,off_t off,PetscBinarySeekType whence,off_t *offset)
722e5c89e4eSSatish Balay {
723e5c89e4eSSatish Balay   PetscErrorCode ierr;
724e5c89e4eSSatish Balay   PetscMPIInt    rank;
725e5c89e4eSSatish Balay 
726e5c89e4eSSatish Balay   PetscFunctionBegin;
727e5c89e4eSSatish Balay   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
728e5c89e4eSSatish Balay   if (!rank) {
729e5c89e4eSSatish Balay     ierr = PetscBinarySeek(fd,off,whence,offset);CHKERRQ(ierr);
730e5c89e4eSSatish Balay   }
731e5c89e4eSSatish Balay   PetscFunctionReturn(0);
732e5c89e4eSSatish Balay }
733e5c89e4eSSatish Balay 
7340fc9d207SBarry Smith #if defined(PETSC_HAVE_MPIIO)
735e39fd77fSBarry Smith #if !defined(PETSC_WORDS_BIGENDIAN)
736e39fd77fSBarry Smith 
737951e3c8eSBarry Smith #if defined(PETSC_USE_PETSC_MPI_EXTERNAL32)
738e39fd77fSBarry Smith /*
739e39fd77fSBarry Smith       MPICH does not provide the external32 representation for MPI_File_set_view() so we need to provide the functions.
740e39fd77fSBarry Smith     These are set into MPI in PetscInitialize() via MPI_Register_datarep()
741e39fd77fSBarry Smith 
742e39fd77fSBarry Smith     Note I use PetscMPIInt for the MPI error codes since that is what MPI uses (instead of the standard PetscErrorCode)
743e39fd77fSBarry Smith 
744951e3c8eSBarry Smith     The next three routines are not used because MPICH does not support their use
745e39fd77fSBarry Smith 
746e39fd77fSBarry Smith */
7478cc058d9SJed Brown PETSC_EXTERN PetscMPIInt PetscDataRep_extent_fn(MPI_Datatype datatype,MPI_Aint *file_extent,void *extra_state)
748e39fd77fSBarry Smith {
749e39fd77fSBarry Smith   MPI_Aint    ub;
750e39fd77fSBarry Smith   PetscMPIInt ierr;
751e39fd77fSBarry Smith 
752e39fd77fSBarry Smith   ierr = MPI_Type_get_extent(datatype,&ub,file_extent);
753e39fd77fSBarry Smith   return ierr;
754e39fd77fSBarry Smith }
755e39fd77fSBarry Smith 
7568cc058d9SJed Brown PETSC_EXTERN PetscMPIInt PetscDataRep_read_conv_fn(void *userbuf, MPI_Datatype datatype,PetscMPIInt count,void *filebuf, MPI_Offset position,void *extra_state)
757e39fd77fSBarry Smith {
758e39fd77fSBarry Smith   PetscDataType pdtype;
759e39fd77fSBarry Smith   PetscMPIInt   ierr;
760e39fd77fSBarry Smith   size_t        dsize;
761e39fd77fSBarry Smith 
762e39fd77fSBarry Smith   ierr = PetscMPIDataTypeToPetscDataType(datatype,&pdtype);CHKERRQ(ierr);
763e39fd77fSBarry Smith   ierr = PetscDataTypeGetSize(pdtype,&dsize);CHKERRQ(ierr);
764e39fd77fSBarry Smith 
765e39fd77fSBarry Smith   /* offset is given in units of MPI_Datatype */
766e39fd77fSBarry Smith   userbuf = ((char*)userbuf) + dsize*position;
767e39fd77fSBarry Smith 
768e39fd77fSBarry Smith   ierr = PetscMemcpy(userbuf,filebuf,count*dsize);CHKERRQ(ierr);
769e39fd77fSBarry Smith   ierr = PetscByteSwap(userbuf,pdtype,count);CHKERRQ(ierr);
770e39fd77fSBarry Smith   return ierr;
771e39fd77fSBarry Smith }
772e39fd77fSBarry Smith 
773e39fd77fSBarry Smith PetscMPIInt PetscDataRep_write_conv_fn(void *userbuf, MPI_Datatype datatype,PetscMPIInt count,void *filebuf, MPI_Offset position,void *extra_state)
774e39fd77fSBarry Smith {
775e39fd77fSBarry Smith   PetscDataType pdtype;
776e39fd77fSBarry Smith   PetscMPIInt   ierr;
777e39fd77fSBarry Smith   size_t        dsize;
778e39fd77fSBarry Smith 
779e39fd77fSBarry Smith   ierr = PetscMPIDataTypeToPetscDataType(datatype,&pdtype);CHKERRQ(ierr);
780e39fd77fSBarry Smith   ierr = PetscDataTypeGetSize(pdtype,&dsize);CHKERRQ(ierr);
781e39fd77fSBarry Smith 
782e39fd77fSBarry Smith   /* offset is given in units of MPI_Datatype */
783e39fd77fSBarry Smith   userbuf = ((char*)userbuf) + dsize*position;
784e39fd77fSBarry Smith 
785e39fd77fSBarry Smith   ierr = PetscMemcpy(filebuf,userbuf,count*dsize);CHKERRQ(ierr);
786e39fd77fSBarry Smith   ierr = PetscByteSwap(filebuf,pdtype,count);CHKERRQ(ierr);
787e39fd77fSBarry Smith   return ierr;
788e39fd77fSBarry Smith }
789951e3c8eSBarry Smith #endif
790e39fd77fSBarry Smith 
7911c91e6bcSJed Brown #undef __FUNCT__
7921c91e6bcSJed Brown #define __FUNCT__ "MPIU_File_write_all"
793e39fd77fSBarry Smith PetscErrorCode MPIU_File_write_all(MPI_File fd,void *data,PetscMPIInt cnt,MPI_Datatype dtype,MPI_Status *status)
794e39fd77fSBarry Smith {
795e39fd77fSBarry Smith   PetscErrorCode ierr;
796e39fd77fSBarry Smith   PetscDataType  pdtype;
797e39fd77fSBarry Smith 
798e39fd77fSBarry Smith   PetscFunctionBegin;
799e39fd77fSBarry Smith   ierr = PetscMPIDataTypeToPetscDataType(dtype,&pdtype);CHKERRQ(ierr);
800e39fd77fSBarry Smith   ierr = PetscByteSwap(data,pdtype,cnt);CHKERRQ(ierr);
801e39fd77fSBarry Smith   ierr = MPI_File_write_all(fd,data,cnt,dtype,status);CHKERRQ(ierr);
802e39fd77fSBarry Smith   ierr = PetscByteSwap(data,pdtype,cnt);CHKERRQ(ierr);
803a6796414SBarry Smith   PetscFunctionReturn(0);
804e39fd77fSBarry Smith }
805e39fd77fSBarry Smith 
8061c91e6bcSJed Brown #undef __FUNCT__
8071c91e6bcSJed Brown #define __FUNCT__ "MPIU_File_read_all"
808e39fd77fSBarry Smith PetscErrorCode MPIU_File_read_all(MPI_File fd,void *data,PetscMPIInt cnt,MPI_Datatype dtype,MPI_Status *status)
809e39fd77fSBarry Smith {
810e39fd77fSBarry Smith   PetscErrorCode ierr;
811e39fd77fSBarry Smith   PetscDataType  pdtype;
812e39fd77fSBarry Smith 
813e39fd77fSBarry Smith   PetscFunctionBegin;
814e39fd77fSBarry Smith   ierr = PetscMPIDataTypeToPetscDataType(dtype,&pdtype);CHKERRQ(ierr);
815e39fd77fSBarry Smith   ierr = MPI_File_read_all(fd,data,cnt,dtype,status);CHKERRQ(ierr);
816e39fd77fSBarry Smith   ierr = PetscByteSwap(data,pdtype,cnt);CHKERRQ(ierr);
817a6796414SBarry Smith   PetscFunctionReturn(0);
818e39fd77fSBarry Smith }
819e39fd77fSBarry Smith #endif
820951e3c8eSBarry Smith #endif
821