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) 223*cba51d77SBarry 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 2322d53ad75SBarry Smith char fname[64]; 2332d53ad75SBarry Smith PetscBool functionload = PETSC_FALSE; 234e5c89e4eSSatish Balay 235e5c89e4eSSatish Balay PetscFunctionBegin; 2362d53ad75SBarry Smith if (n < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Trying to write a negative amount of data %D",n); 237e5c89e4eSSatish Balay if (!n) PetscFunctionReturn(0); 238e5c89e4eSSatish Balay 2392d53ad75SBarry Smith if (type == PETSC_FUNCTION) { 2402d53ad75SBarry Smith functionload = PETSC_TRUE; 2412d53ad75SBarry Smith m = 64; 2422d53ad75SBarry Smith type = PETSC_CHAR; 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) 261*cba51d77SBarry Smith ierr = PetscOptionsGetBool(NULL,"-binary_read_double",&readdouble,NULL);CHKERRQ(ierr); 2627a881295SBarry Smith /* If using __float128 precision we still read in doubles from file */ 263*cba51d77SBarry 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) 281*cba51d77SBarry 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 2972d53ad75SBarry Smith if (functionload) { 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 3032d53ad75SBarry Smith } 304e5c89e4eSSatish Balay PetscFunctionReturn(0); 305e5c89e4eSSatish Balay } 306e5c89e4eSSatish Balay /* --------------------------------------------------------- */ 307e5c89e4eSSatish Balay #undef __FUNCT__ 308e5c89e4eSSatish Balay #define __FUNCT__ "PetscBinaryWrite" 309e30d2299SSatish Balay /*@ 310e5c89e4eSSatish Balay PetscBinaryWrite - Writes to a binary file. 311e5c89e4eSSatish Balay 312e5c89e4eSSatish Balay Not Collective 313e5c89e4eSSatish Balay 314e5c89e4eSSatish Balay Input Parameters: 315e5c89e4eSSatish Balay + fd - the file 316e5c89e4eSSatish Balay . p - the buffer 317e5c89e4eSSatish Balay . n - the number of items to write 318e5c89e4eSSatish Balay . type - the type of items to read (PETSC_INT, PETSC_DOUBLE or PETSC_SCALAR) 319e5c89e4eSSatish Balay - istemp - PETSC_FALSE if buffer data should be preserved, PETSC_TRUE otherwise. 320e5c89e4eSSatish Balay 321e5c89e4eSSatish Balay Level: advanced 322e5c89e4eSSatish Balay 323e5c89e4eSSatish Balay Notes: 324e5c89e4eSSatish Balay PetscBinaryWrite() uses byte swapping to work on all machines; the files 325e5c89e4eSSatish Balay are written using big-endian ordering to the file. On small-endian machines the numbers 326e5c89e4eSSatish Balay are converted to the big-endian format when they are written to disk. 327e2e64c6bSBarry Smith When PETSc is ./configure with --with-64bit-indices the integers are written to the 32854f21887SBarry Smith file as 64 bit integers, this means they can only be read back in when the option --with-64bit-indices 32954f21887SBarry Smith is used. 330e5c89e4eSSatish Balay 331e5c89e4eSSatish Balay The Buffer p should be read-write buffer, and not static data. 332e5c89e4eSSatish Balay This way, byte-swapping is done in-place, and then the buffer is 333e5c89e4eSSatish Balay written to the file. 334e5c89e4eSSatish Balay 335e5c89e4eSSatish Balay This routine restores the original contents of the buffer, after 336e5c89e4eSSatish Balay it is written to the file. This is done by byte-swapping in-place 337e5c89e4eSSatish Balay the second time. If the flag istemp is set to PETSC_TRUE, the second 338e5c89e4eSSatish Balay byte-swapping operation is not done, thus saving some computation, 339e5f36e38SBarry Smith but the buffer is left corrupted. 340e5c89e4eSSatish Balay 341300a7f5bSBarry Smith Because byte-swapping may be done on the values in data it cannot be declared const 342300a7f5bSBarry Smith 343e5c89e4eSSatish Balay Concepts: files^writing binary 344e5c89e4eSSatish Balay Concepts: binary files^writing 345e5c89e4eSSatish Balay 3464ebed01fSBarry Smith .seealso: PetscBinaryRead(), PetscBinaryOpen(), PetscBinaryClose(), PetscViewerBinaryGetDescriptor(), PetscBinarySynchronizedWrite(), 3474ebed01fSBarry Smith PetscBinarySynchronizedRead(), PetscBinarySynchronizedSeek() 348e5c89e4eSSatish Balay @*/ 3497087cfbeSBarry Smith PetscErrorCode PetscBinaryWrite(int fd,void *p,PetscInt n,PetscDataType type,PetscBool istemp) 350e5c89e4eSSatish Balay { 351e5c89e4eSSatish Balay char *pp = (char*)p; 352e5c89e4eSSatish Balay int err,wsize; 353e5c89e4eSSatish Balay size_t m = (size_t)n,maxblock=65536; 354e5c89e4eSSatish Balay PetscErrorCode ierr; 355f5351476SHong Zhang #if !defined(PETSC_WORDS_BIGENDIAN) 356e5c89e4eSSatish Balay void *ptmp = p; 357e5c89e4eSSatish Balay #endif 3582d53ad75SBarry Smith char fname[64]; 359e5c89e4eSSatish Balay 360e5c89e4eSSatish Balay PetscFunctionBegin; 361e32f2f54SBarry Smith if (n < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Trying to write a negative amount of data %D",n); 362e5c89e4eSSatish Balay if (!n) PetscFunctionReturn(0); 363e5c89e4eSSatish Balay 3642d53ad75SBarry Smith if (type == PETSC_FUNCTION) { 3652d53ad75SBarry Smith #if defined(PETSC_SERIALIZE_FUNCTIONS) 3662d53ad75SBarry Smith const char *fnametmp; 3672d53ad75SBarry Smith 3682d53ad75SBarry Smith if (n > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Can only binary view a single function at a time"); 3699200755eSBarry Smith ierr = PetscFPTFind(*(void**)p,&fnametmp);CHKERRQ(ierr); 3702d53ad75SBarry Smith ierr = PetscStrncpy(fname,fnametmp,64);CHKERRQ(ierr); 3712d53ad75SBarry Smith #else 3722d53ad75SBarry Smith ierr = PetscStrncpy(fname,"",64);CHKERRQ(ierr); 3732d53ad75SBarry Smith #endif 3742d53ad75SBarry Smith m = 64; 3752d53ad75SBarry Smith type = PETSC_CHAR; 3762d53ad75SBarry Smith pp = (char*)fname; 3772d53ad75SBarry Smith #if !defined(PETSC_WORDS_BIGENDIAN) 3782d53ad75SBarry Smith ptmp = (void*)fname; 3792d53ad75SBarry Smith #endif 3802d53ad75SBarry Smith } 3812d53ad75SBarry Smith 3826de02169SBarry Smith if (type == PETSC_INT) m *= sizeof(PetscInt); 383e5c89e4eSSatish Balay else if (type == PETSC_SCALAR) m *= sizeof(PetscScalar); 384e5c89e4eSSatish Balay else if (type == PETSC_DOUBLE) m *= sizeof(double); 385e95bf02fSSatish Balay else if (type == PETSC_FLOAT) m *= sizeof(float); 386e5c89e4eSSatish Balay else if (type == PETSC_SHORT) m *= sizeof(short); 387e5c89e4eSSatish Balay else if (type == PETSC_CHAR) m *= sizeof(char); 388e5c89e4eSSatish Balay else if (type == PETSC_ENUM) m *= sizeof(PetscEnum); 389ace3abfcSBarry Smith else if (type == PETSC_BOOL) m *= sizeof(PetscBool); 3909f7b6320SBarry Smith else if (type == PETSC_BIT_LOGICAL) m = PetscBTLength(m)*sizeof(char); 391e32f2f54SBarry Smith else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Unknown type"); 392e5c89e4eSSatish Balay 393e5c89e4eSSatish Balay #if !defined(PETSC_WORDS_BIGENDIAN) 394e39fd77fSBarry Smith ierr = PetscByteSwap(ptmp,type,n);CHKERRQ(ierr); 395e5c89e4eSSatish Balay #endif 396e5c89e4eSSatish Balay 397e5c89e4eSSatish Balay while (m) { 398e5c89e4eSSatish Balay wsize = (m < maxblock) ? m : maxblock; 399e5c89e4eSSatish Balay err = write(fd,pp,wsize); 400e5c89e4eSSatish Balay if (err < 0 && errno == EINTR) continue; 401e32f2f54SBarry Smith if (err != wsize) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_WRITE,"Error writing to file."); 402e5c89e4eSSatish Balay m -= wsize; 403e5c89e4eSSatish Balay pp += wsize; 404e5c89e4eSSatish Balay } 405e5c89e4eSSatish Balay 406b659c0ceSSatish Balay #if !defined(PETSC_WORDS_BIGENDIAN) 407e5c89e4eSSatish Balay if (!istemp) { 408e39fd77fSBarry Smith ierr = PetscByteSwap(ptmp,type,n);CHKERRQ(ierr); 409e5c89e4eSSatish Balay } 410e5c89e4eSSatish Balay #endif 411e5c89e4eSSatish Balay PetscFunctionReturn(0); 412e5c89e4eSSatish Balay } 413e5c89e4eSSatish Balay 414e5c89e4eSSatish Balay #undef __FUNCT__ 415e5c89e4eSSatish Balay #define __FUNCT__ "PetscBinaryOpen" 416e5c89e4eSSatish Balay /*@C 417e5c89e4eSSatish Balay PetscBinaryOpen - Opens a PETSc binary file. 418e5c89e4eSSatish Balay 419e5c89e4eSSatish Balay Not Collective 420e5c89e4eSSatish Balay 421e5c89e4eSSatish Balay Input Parameters: 422e5c89e4eSSatish Balay + name - filename 42345c64e65SBarry Smith - type - type of binary file, one of FILE_MODE_READ, FILE_MODE_APPEND, FILE_MODE_WRITE 424e5c89e4eSSatish Balay 425e5c89e4eSSatish Balay Output Parameter: 426e5c89e4eSSatish Balay . fd - the file 427e5c89e4eSSatish Balay 428e5c89e4eSSatish Balay Level: advanced 429e5c89e4eSSatish Balay 430e5c89e4eSSatish Balay Concepts: files^opening binary 431e5c89e4eSSatish Balay Concepts: binary files^opening 432e5c89e4eSSatish Balay 433e5c89e4eSSatish Balay Notes: Files access with PetscBinaryRead() and PetscBinaryWrite() are ALWAYS written in 434e5c89e4eSSatish Balay big-endian format. This means the file can be accessed using PetscBinaryOpen() and 435e5c89e4eSSatish Balay PetscBinaryRead() and PetscBinaryWrite() on any machine. 436e5c89e4eSSatish Balay 4374ebed01fSBarry Smith .seealso: PetscBinaryRead(), PetscBinaryWrite(), PetscFileMode, PetscViewerFileSetMode(), PetscViewerBinaryGetDescriptor(), 4384ebed01fSBarry Smith PetscBinarySynchronizedWrite(), PetscBinarySynchronizedRead(), PetscBinarySynchronizedSeek() 43945c64e65SBarry Smith 440e5c89e4eSSatish Balay @*/ 4417087cfbeSBarry Smith PetscErrorCode PetscBinaryOpen(const char name[],PetscFileMode mode,int *fd) 442e5c89e4eSSatish Balay { 443e5c89e4eSSatish Balay PetscFunctionBegin; 444e5c89e4eSSatish Balay #if defined(PETSC_HAVE_O_BINARY) 44545c64e65SBarry Smith if (mode == FILE_MODE_WRITE) { 446a297a907SKarl 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); 44745c64e65SBarry Smith } else if (mode == FILE_MODE_READ) { 448a297a907SKarl 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); 44945c64e65SBarry Smith } else if (mode == FILE_MODE_APPEND) { 450a297a907SKarl 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); 451e5c89e4eSSatish Balay #else 45245c64e65SBarry Smith if (mode == FILE_MODE_WRITE) { 453a297a907SKarl Rupp if ((*fd = creat(name,0666)) == -1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot create file for writing: %s",name); 45445c64e65SBarry Smith } else if (mode == FILE_MODE_READ) { 455a297a907SKarl Rupp if ((*fd = open(name,O_RDONLY,0)) == -1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot open file for reading: %s",name); 456e5c89e4eSSatish Balay } 45745c64e65SBarry Smith else if (mode == FILE_MODE_APPEND) { 458a297a907SKarl Rupp if ((*fd = open(name,O_WRONLY,0)) == -1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot open file for writing: %s",name); 459e5c89e4eSSatish Balay #endif 460e32f2f54SBarry Smith } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Unknown file mode"); 461e5c89e4eSSatish Balay PetscFunctionReturn(0); 462e5c89e4eSSatish Balay } 463e5c89e4eSSatish Balay 464e5c89e4eSSatish Balay #undef __FUNCT__ 465e5c89e4eSSatish Balay #define __FUNCT__ "PetscBinaryClose" 466e30d2299SSatish Balay /*@ 467e5c89e4eSSatish Balay PetscBinaryClose - Closes a PETSc binary file. 468e5c89e4eSSatish Balay 469e5c89e4eSSatish Balay Not Collective 470e5c89e4eSSatish Balay 471e5c89e4eSSatish Balay Output Parameter: 472e5c89e4eSSatish Balay . fd - the file 473e5c89e4eSSatish Balay 474e5c89e4eSSatish Balay Level: advanced 475e5c89e4eSSatish Balay 4764ebed01fSBarry Smith .seealso: PetscBinaryRead(), PetscBinaryWrite(), PetscBinaryOpen(), PetscBinarySynchronizedWrite(), PetscBinarySynchronizedRead(), 4774ebed01fSBarry Smith PetscBinarySynchronizedSeek() 478e5c89e4eSSatish Balay @*/ 4797087cfbeSBarry Smith PetscErrorCode PetscBinaryClose(int fd) 480e5c89e4eSSatish Balay { 481e5c89e4eSSatish Balay PetscFunctionBegin; 482e5c89e4eSSatish Balay close(fd); 483e5c89e4eSSatish Balay PetscFunctionReturn(0); 484e5c89e4eSSatish Balay } 485e5c89e4eSSatish Balay 486e5c89e4eSSatish Balay 487e5c89e4eSSatish Balay #undef __FUNCT__ 488e5c89e4eSSatish Balay #define __FUNCT__ "PetscBinarySeek" 489e30d2299SSatish Balay /*@ 490e5c89e4eSSatish Balay PetscBinarySeek - Moves the file pointer on a PETSc binary file. 491e5c89e4eSSatish Balay 492e5c89e4eSSatish Balay Not Collective 493e5c89e4eSSatish Balay 494e5c89e4eSSatish Balay Input Parameters: 495e5c89e4eSSatish Balay + fd - the file 496ff553b35SMatthew Knepley . off - number of bytes to move. Use PETSC_BINARY_INT_SIZE, PETSC_BINARY_SCALAR_SIZE, 497e5c89e4eSSatish Balay etc. in your calculation rather than sizeof() to compute byte lengths. 498ff553b35SMatthew Knepley - whence - if PETSC_BINARY_SEEK_SET then off is an absolute location in the file 499ff553b35SMatthew Knepley if PETSC_BINARY_SEEK_CUR then off is an offset from the current location 500ff553b35SMatthew Knepley if PETSC_BINARY_SEEK_END then off is an offset from the end of file 501e5c89e4eSSatish Balay 502e5c89e4eSSatish Balay Output Parameter: 503e5c89e4eSSatish Balay . offset - new offset in file 504e5c89e4eSSatish Balay 505e5c89e4eSSatish Balay Level: developer 506e5c89e4eSSatish Balay 507e5c89e4eSSatish Balay Notes: 508e5c89e4eSSatish Balay Integers are stored on the file as 32 long, regardless of whether 509e5c89e4eSSatish Balay they are stored in the machine as 32 or 64, this means the same 510e5c89e4eSSatish Balay binary file may be read on any machine. Hence you CANNOT use sizeof() 511e5c89e4eSSatish Balay to determine the offset or location. 512e5c89e4eSSatish Balay 513e5c89e4eSSatish Balay Concepts: files^binary seeking 514e5c89e4eSSatish Balay Concepts: binary files^seeking 515e5c89e4eSSatish Balay 5164ebed01fSBarry Smith .seealso: PetscBinaryRead(), PetscBinaryWrite(), PetscBinaryOpen(), PetscBinarySynchronizedWrite(), PetscBinarySynchronizedRead(), 5174ebed01fSBarry Smith PetscBinarySynchronizedSeek() 518e5c89e4eSSatish Balay @*/ 5197087cfbeSBarry Smith PetscErrorCode PetscBinarySeek(int fd,off_t off,PetscBinarySeekType whence,off_t *offset) 520e5c89e4eSSatish Balay { 521e5c89e4eSSatish Balay int iwhence = 0; 522e5c89e4eSSatish Balay 523e5c89e4eSSatish Balay PetscFunctionBegin; 524a297a907SKarl Rupp if (whence == PETSC_BINARY_SEEK_SET) iwhence = SEEK_SET; 525a297a907SKarl Rupp else if (whence == PETSC_BINARY_SEEK_CUR) iwhence = SEEK_CUR; 526a297a907SKarl Rupp else if (whence == PETSC_BINARY_SEEK_END) iwhence = SEEK_END; 527a297a907SKarl Rupp else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Unknown seek location"); 528e5c89e4eSSatish Balay #if defined(PETSC_HAVE_LSEEK) 529e5c89e4eSSatish Balay *offset = lseek(fd,off,iwhence); 530e5c89e4eSSatish Balay #elif defined(PETSC_HAVE__LSEEK) 531e5c89e4eSSatish Balay *offset = _lseek(fd,(long)off,iwhence); 532e5c89e4eSSatish Balay #else 533e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP_SYS,"System does not have a way of seeking on a file"); 534e5c89e4eSSatish Balay #endif 535e5c89e4eSSatish Balay PetscFunctionReturn(0); 536e5c89e4eSSatish Balay } 537e5c89e4eSSatish Balay 538e5c89e4eSSatish Balay #undef __FUNCT__ 5391d280d73SBarry Smith #define __FUNCT__ "PetscBinarySynchronizedRead" 540e5c89e4eSSatish Balay /*@C 5411d280d73SBarry Smith PetscBinarySynchronizedRead - Reads from a binary file. 542e5c89e4eSSatish Balay 543e5c89e4eSSatish Balay Collective on MPI_Comm 544e5c89e4eSSatish Balay 545e5c89e4eSSatish Balay Input Parameters: 546e5c89e4eSSatish Balay + comm - the MPI communicator 547e5c89e4eSSatish Balay . fd - the file 548e5c89e4eSSatish Balay . n - the number of items to read 549e5c89e4eSSatish Balay - type - the type of items to read (PETSC_INT, PETSC_DOUBLE or PETSC_SCALAR) 550e5c89e4eSSatish Balay 551e5c89e4eSSatish Balay Output Parameters: 552e5c89e4eSSatish Balay . p - the buffer 553e5c89e4eSSatish Balay 554e5c89e4eSSatish Balay Options Database Key: 555e5c89e4eSSatish Balay . -binary_longints - indicates the file was generated on a Cray vector 556e5c89e4eSSatish Balay machine (not the T3E/D) and the ints are stored as 64 bit 557e5c89e4eSSatish Balay quantities, otherwise they are stored as 32 bit 558e5c89e4eSSatish Balay 559e5c89e4eSSatish Balay Level: developer 560e5c89e4eSSatish Balay 561e5c89e4eSSatish Balay Notes: 562e5c89e4eSSatish Balay Does a PetscBinaryRead() followed by an MPI_Bcast() 563e5c89e4eSSatish Balay 5641d280d73SBarry Smith PetscBinarySynchronizedRead() uses byte swapping to work on all machines. 565e5c89e4eSSatish Balay Integers are stored on the file as 32 long, regardless of whether 566e5c89e4eSSatish Balay they are stored in the machine as 32 or 64, this means the same 567e5c89e4eSSatish Balay binary file may be read on any machine. 568e5c89e4eSSatish Balay 569e5c89e4eSSatish Balay Concepts: files^synchronized reading of binary files 570e5c89e4eSSatish Balay Concepts: binary files^reading, synchronized 571e5c89e4eSSatish Balay 5724ebed01fSBarry Smith .seealso: PetscBinaryWrite(), PetscBinaryOpen(), PetscBinaryClose(), PetscBinaryRead(), PetscBinarySynchronizedWrite(), 5734ebed01fSBarry Smith PetscBinarySynchronizedSeek() 574e5c89e4eSSatish Balay @*/ 5757087cfbeSBarry Smith PetscErrorCode PetscBinarySynchronizedRead(MPI_Comm comm,int fd,void *p,PetscInt n,PetscDataType type) 576e5c89e4eSSatish Balay { 577e5c89e4eSSatish Balay PetscErrorCode ierr; 578e5c89e4eSSatish Balay PetscMPIInt rank; 579e5c89e4eSSatish Balay MPI_Datatype mtype; 5802d53ad75SBarry Smith char *fname; 5812d53ad75SBarry Smith PetscBool functionload = PETSC_FALSE; 5820298fd71SBarry Smith void *ptmp = NULL; 583e5c89e4eSSatish Balay 584e5c89e4eSSatish Balay PetscFunctionBegin; 5852d53ad75SBarry Smith if (type == PETSC_FUNCTION) { 5862d53ad75SBarry Smith functionload = PETSC_TRUE; 5872d53ad75SBarry Smith n = 64; 5882d53ad75SBarry Smith type = PETSC_CHAR; 5892d53ad75SBarry Smith ptmp = p; 5902d53ad75SBarry Smith /* warning memory leak */ 59198da9c48SSatish Balay fname = (char*)malloc(64*sizeof(char)); 5922d53ad75SBarry Smith p = (void*)fname; 5932d53ad75SBarry Smith } 5942d53ad75SBarry Smith 595e5c89e4eSSatish Balay ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 596e5c89e4eSSatish Balay if (!rank) { 597e5c89e4eSSatish Balay ierr = PetscBinaryRead(fd,p,n,type);CHKERRQ(ierr); 598e5c89e4eSSatish Balay } 599e5c89e4eSSatish Balay ierr = PetscDataTypeToMPIDataType(type,&mtype);CHKERRQ(ierr); 600e5c89e4eSSatish Balay ierr = MPI_Bcast(p,n,mtype,0,comm);CHKERRQ(ierr); 6012d53ad75SBarry Smith 6022d53ad75SBarry Smith if (functionload) { 6032d53ad75SBarry Smith #if defined(PETSC_SERIALIZE_FUNCTIONS) 6040298fd71SBarry Smith ierr = PetscDLLibrarySym(PETSC_COMM_SELF,&PetscDLLibrariesLoaded,NULL,fname,(void**)ptmp);CHKERRQ(ierr); 6052d53ad75SBarry Smith #else 6060298fd71SBarry Smith *(void**)ptmp = NULL; 6072d53ad75SBarry Smith #endif 6082d53ad75SBarry Smith } 609e5c89e4eSSatish Balay PetscFunctionReturn(0); 610e5c89e4eSSatish Balay } 611e5c89e4eSSatish Balay 612e5c89e4eSSatish Balay #undef __FUNCT__ 6131d280d73SBarry Smith #define __FUNCT__ "PetscBinarySynchronizedWrite" 614e5c89e4eSSatish Balay /*@C 6151d280d73SBarry Smith PetscBinarySynchronizedWrite - writes to a binary file. 616e5c89e4eSSatish Balay 617e5c89e4eSSatish Balay Collective on MPI_Comm 618e5c89e4eSSatish Balay 619e5c89e4eSSatish Balay Input Parameters: 620e5c89e4eSSatish Balay + comm - the MPI communicator 621e5c89e4eSSatish Balay . fd - the file 622e5c89e4eSSatish Balay . n - the number of items to write 623e5c89e4eSSatish Balay . p - the buffer 624e5c89e4eSSatish Balay . istemp - the buffer may be changed 625e5c89e4eSSatish Balay - type - the type of items to write (PETSC_INT, PETSC_DOUBLE or PETSC_SCALAR) 626e5c89e4eSSatish Balay 627e5c89e4eSSatish Balay Level: developer 628e5c89e4eSSatish Balay 629e5c89e4eSSatish Balay Notes: 630e5c89e4eSSatish Balay Process 0 does a PetscBinaryWrite() 631e5c89e4eSSatish Balay 6321d280d73SBarry Smith PetscBinarySynchronizedWrite() uses byte swapping to work on all machines. 633e5c89e4eSSatish Balay Integers are stored on the file as 32 long, regardless of whether 634e5c89e4eSSatish Balay they are stored in the machine as 32 or 64, this means the same 635e5c89e4eSSatish Balay binary file may be read on any machine. 636e5c89e4eSSatish Balay 637300a7f5bSBarry Smith Notes: because byte-swapping may be done on the values in data it cannot be declared const 638300a7f5bSBarry Smith 6391d280d73SBarry Smith WARNING: This is NOT like PetscSynchronizedFPrintf()! This routine ignores calls on all but process 0, 6401d280d73SBarry Smith while PetscSynchronizedFPrintf() has all processes print their strings in order. 6411d280d73SBarry Smith 642e5c89e4eSSatish Balay Concepts: files^synchronized writing of binary files 643e5c89e4eSSatish Balay Concepts: binary files^reading, synchronized 644e5c89e4eSSatish Balay 6454ebed01fSBarry Smith .seealso: PetscBinaryWrite(), PetscBinaryOpen(), PetscBinaryClose(), PetscBinaryRead(), PetscBinarySynchronizedRead(), 6464ebed01fSBarry Smith PetscBinarySynchronizedSeek() 647e5c89e4eSSatish Balay @*/ 6487087cfbeSBarry Smith PetscErrorCode PetscBinarySynchronizedWrite(MPI_Comm comm,int fd,void *p,PetscInt n,PetscDataType type,PetscBool istemp) 649e5c89e4eSSatish Balay { 650e5c89e4eSSatish Balay PetscErrorCode ierr; 651e5c89e4eSSatish Balay PetscMPIInt rank; 652e5c89e4eSSatish Balay 653e5c89e4eSSatish Balay PetscFunctionBegin; 654e5c89e4eSSatish Balay ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 655e5c89e4eSSatish Balay if (!rank) { 656e5c89e4eSSatish Balay ierr = PetscBinaryWrite(fd,p,n,type,istemp);CHKERRQ(ierr); 657e5c89e4eSSatish Balay } 658e5c89e4eSSatish Balay PetscFunctionReturn(0); 659e5c89e4eSSatish Balay } 660e5c89e4eSSatish Balay 661e5c89e4eSSatish Balay #undef __FUNCT__ 6621d280d73SBarry Smith #define __FUNCT__ "PetscBinarySynchronizedSeek" 663e5c89e4eSSatish Balay /*@C 6641d280d73SBarry Smith PetscBinarySynchronizedSeek - Moves the file pointer on a PETSc binary file. 665e5c89e4eSSatish Balay 666e5c89e4eSSatish Balay 667e5c89e4eSSatish Balay Input Parameters: 668e5c89e4eSSatish Balay + fd - the file 669e5c89e4eSSatish Balay . whence - if PETSC_BINARY_SEEK_SET then size is an absolute location in the file 670e5c89e4eSSatish Balay if PETSC_BINARY_SEEK_CUR then size is offset from current location 671e5c89e4eSSatish Balay if PETSC_BINARY_SEEK_END then size is offset from end of file 672e5c89e4eSSatish Balay - off - number of bytes to move. Use PETSC_BINARY_INT_SIZE, PETSC_BINARY_SCALAR_SIZE, 673e5c89e4eSSatish Balay etc. in your calculation rather than sizeof() to compute byte lengths. 674e5c89e4eSSatish Balay 675e5c89e4eSSatish Balay Output Parameter: 676e5c89e4eSSatish Balay . offset - new offset in file 677e5c89e4eSSatish Balay 678e5c89e4eSSatish Balay Level: developer 679e5c89e4eSSatish Balay 680e5c89e4eSSatish Balay Notes: 681e5c89e4eSSatish Balay Integers are stored on the file as 32 long, regardless of whether 682e5c89e4eSSatish Balay they are stored in the machine as 32 or 64, this means the same 683e5c89e4eSSatish Balay binary file may be read on any machine. Hence you CANNOT use sizeof() 684e5c89e4eSSatish Balay to determine the offset or location. 685e5c89e4eSSatish Balay 686e5c89e4eSSatish Balay Concepts: binary files^seeking 687e5c89e4eSSatish Balay Concepts: files^seeking in binary 688e5c89e4eSSatish Balay 6894ebed01fSBarry Smith .seealso: PetscBinaryRead(), PetscBinaryWrite(), PetscBinaryOpen(), PetscBinarySynchronizedWrite(), PetscBinarySynchronizedRead(), 6904ebed01fSBarry Smith PetscBinarySynchronizedSeek() 691e5c89e4eSSatish Balay @*/ 6927087cfbeSBarry Smith PetscErrorCode PetscBinarySynchronizedSeek(MPI_Comm comm,int fd,off_t off,PetscBinarySeekType whence,off_t *offset) 693e5c89e4eSSatish Balay { 694e5c89e4eSSatish Balay PetscErrorCode ierr; 695e5c89e4eSSatish Balay PetscMPIInt rank; 696e5c89e4eSSatish Balay 697e5c89e4eSSatish Balay PetscFunctionBegin; 698e5c89e4eSSatish Balay ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 699e5c89e4eSSatish Balay if (!rank) { 700e5c89e4eSSatish Balay ierr = PetscBinarySeek(fd,off,whence,offset);CHKERRQ(ierr); 701e5c89e4eSSatish Balay } 702e5c89e4eSSatish Balay PetscFunctionReturn(0); 703e5c89e4eSSatish Balay } 704e5c89e4eSSatish Balay 7050fc9d207SBarry Smith #if defined(PETSC_HAVE_MPIIO) 706e39fd77fSBarry Smith #if !defined(PETSC_WORDS_BIGENDIAN) 707e39fd77fSBarry Smith 708951e3c8eSBarry Smith #if defined(PETSC_USE_PETSC_MPI_EXTERNAL32) 709e39fd77fSBarry Smith /* 710e39fd77fSBarry Smith MPICH does not provide the external32 representation for MPI_File_set_view() so we need to provide the functions. 711e39fd77fSBarry Smith These are set into MPI in PetscInitialize() via MPI_Register_datarep() 712e39fd77fSBarry Smith 713e39fd77fSBarry Smith Note I use PetscMPIInt for the MPI error codes since that is what MPI uses (instead of the standard PetscErrorCode) 714e39fd77fSBarry Smith 715951e3c8eSBarry Smith The next three routines are not used because MPICH does not support their use 716e39fd77fSBarry Smith 717e39fd77fSBarry Smith */ 7188cc058d9SJed Brown PETSC_EXTERN PetscMPIInt PetscDataRep_extent_fn(MPI_Datatype datatype,MPI_Aint *file_extent,void *extra_state) 719e39fd77fSBarry Smith { 720e39fd77fSBarry Smith MPI_Aint ub; 721e39fd77fSBarry Smith PetscMPIInt ierr; 722e39fd77fSBarry Smith 723e39fd77fSBarry Smith ierr = MPI_Type_get_extent(datatype,&ub,file_extent); 724e39fd77fSBarry Smith return ierr; 725e39fd77fSBarry Smith } 726e39fd77fSBarry Smith 7278cc058d9SJed Brown PETSC_EXTERN PetscMPIInt PetscDataRep_read_conv_fn(void *userbuf, MPI_Datatype datatype,PetscMPIInt count,void *filebuf, MPI_Offset position,void *extra_state) 728e39fd77fSBarry Smith { 729e39fd77fSBarry Smith PetscDataType pdtype; 730e39fd77fSBarry Smith PetscMPIInt ierr; 731e39fd77fSBarry Smith size_t dsize; 732e39fd77fSBarry Smith 733e39fd77fSBarry Smith ierr = PetscMPIDataTypeToPetscDataType(datatype,&pdtype);CHKERRQ(ierr); 734e39fd77fSBarry Smith ierr = PetscDataTypeGetSize(pdtype,&dsize);CHKERRQ(ierr); 735e39fd77fSBarry Smith 736e39fd77fSBarry Smith /* offset is given in units of MPI_Datatype */ 737e39fd77fSBarry Smith userbuf = ((char*)userbuf) + dsize*position; 738e39fd77fSBarry Smith 739e39fd77fSBarry Smith ierr = PetscMemcpy(userbuf,filebuf,count*dsize);CHKERRQ(ierr); 740e39fd77fSBarry Smith ierr = PetscByteSwap(userbuf,pdtype,count);CHKERRQ(ierr); 741e39fd77fSBarry Smith return ierr; 742e39fd77fSBarry Smith } 743e39fd77fSBarry Smith 744e39fd77fSBarry Smith PetscMPIInt PetscDataRep_write_conv_fn(void *userbuf, MPI_Datatype datatype,PetscMPIInt count,void *filebuf, MPI_Offset position,void *extra_state) 745e39fd77fSBarry Smith { 746e39fd77fSBarry Smith PetscDataType pdtype; 747e39fd77fSBarry Smith PetscMPIInt ierr; 748e39fd77fSBarry Smith size_t dsize; 749e39fd77fSBarry Smith 750e39fd77fSBarry Smith ierr = PetscMPIDataTypeToPetscDataType(datatype,&pdtype);CHKERRQ(ierr); 751e39fd77fSBarry Smith ierr = PetscDataTypeGetSize(pdtype,&dsize);CHKERRQ(ierr); 752e39fd77fSBarry Smith 753e39fd77fSBarry Smith /* offset is given in units of MPI_Datatype */ 754e39fd77fSBarry Smith userbuf = ((char*)userbuf) + dsize*position; 755e39fd77fSBarry Smith 756e39fd77fSBarry Smith ierr = PetscMemcpy(filebuf,userbuf,count*dsize);CHKERRQ(ierr); 757e39fd77fSBarry Smith ierr = PetscByteSwap(filebuf,pdtype,count);CHKERRQ(ierr); 758e39fd77fSBarry Smith return ierr; 759e39fd77fSBarry Smith } 760951e3c8eSBarry Smith #endif 761e39fd77fSBarry Smith 7621c91e6bcSJed Brown #undef __FUNCT__ 7631c91e6bcSJed Brown #define __FUNCT__ "MPIU_File_write_all" 764e39fd77fSBarry Smith PetscErrorCode MPIU_File_write_all(MPI_File fd,void *data,PetscMPIInt cnt,MPI_Datatype dtype,MPI_Status *status) 765e39fd77fSBarry Smith { 766e39fd77fSBarry Smith PetscErrorCode ierr; 767e39fd77fSBarry Smith PetscDataType pdtype; 768e39fd77fSBarry Smith 769e39fd77fSBarry Smith PetscFunctionBegin; 770e39fd77fSBarry Smith ierr = PetscMPIDataTypeToPetscDataType(dtype,&pdtype);CHKERRQ(ierr); 771e39fd77fSBarry Smith ierr = PetscByteSwap(data,pdtype,cnt);CHKERRQ(ierr); 772e39fd77fSBarry Smith ierr = MPI_File_write_all(fd,data,cnt,dtype,status);CHKERRQ(ierr); 773e39fd77fSBarry Smith ierr = PetscByteSwap(data,pdtype,cnt);CHKERRQ(ierr); 774a6796414SBarry Smith PetscFunctionReturn(0); 775e39fd77fSBarry Smith } 776e39fd77fSBarry Smith 7771c91e6bcSJed Brown #undef __FUNCT__ 7781c91e6bcSJed Brown #define __FUNCT__ "MPIU_File_read_all" 779e39fd77fSBarry Smith PetscErrorCode MPIU_File_read_all(MPI_File fd,void *data,PetscMPIInt cnt,MPI_Datatype dtype,MPI_Status *status) 780e39fd77fSBarry Smith { 781e39fd77fSBarry Smith PetscErrorCode ierr; 782e39fd77fSBarry Smith PetscDataType pdtype; 783e39fd77fSBarry Smith 784e39fd77fSBarry Smith PetscFunctionBegin; 785e39fd77fSBarry Smith ierr = PetscMPIDataTypeToPetscDataType(dtype,&pdtype);CHKERRQ(ierr); 786e39fd77fSBarry Smith ierr = MPI_File_read_all(fd,data,cnt,dtype,status);CHKERRQ(ierr); 787e39fd77fSBarry Smith ierr = PetscByteSwap(data,pdtype,cnt);CHKERRQ(ierr); 788a6796414SBarry Smith PetscFunctionReturn(0); 789e39fd77fSBarry Smith } 790e39fd77fSBarry Smith #endif 791951e3c8eSBarry Smith #endif 792