1 #define PETSC_DLL 2 3 /* 4 This file contains simple binary read/write routines. 5 */ 6 7 #include "petsc.h" 8 #include "petscsys.h" /*I "petscsys.h" I*/ 9 10 #include <errno.h> 11 #include <fcntl.h> 12 #if defined(PETSC_HAVE_UNISTD_H) 13 #include <unistd.h> 14 #endif 15 #if defined (PETSC_HAVE_IO_H) 16 #include <io.h> 17 #endif 18 #include "petscbt.h" 19 20 #if !defined(PETSC_WORDS_BIGENDIAN) 21 22 /* --------------------------------------------------------- */ 23 #undef __FUNCT__ 24 #define __FUNCT__ "PetscByteSwapEnum" 25 /* 26 PetscByteSwapEnum - Swap bytes in a PETSc Enum 27 28 */ 29 PetscErrorCode PETSC_DLLEXPORT PetscByteSwapEnum(PetscEnum *buff,PetscInt n) 30 { 31 PetscInt i,j; 32 PetscEnum tmp = ENUM_DUMMY; 33 char *ptr1,*ptr2 = (char*)&tmp; 34 35 PetscFunctionBegin; 36 for (j=0; j<n; j++) { 37 ptr1 = (char*)(buff + j); 38 for (i=0; i<(PetscInt)sizeof(PetscEnum); i++) { 39 ptr2[i] = ptr1[sizeof(PetscEnum)-1-i]; 40 } 41 for (i=0; i<(PetscInt)sizeof(PetscEnum); i++) { 42 ptr1[i] = ptr2[i]; 43 } 44 } 45 PetscFunctionReturn(0); 46 } 47 48 #undef __FUNCT__ 49 #define __FUNCT__ "PetscByteSwapTruth" 50 /* 51 PetscByteSwapTruth - Swap bytes in a PETSc Truth 52 53 */ 54 PetscErrorCode PETSC_DLLEXPORT PetscByteSwapTruth(PetscTruth *buff,PetscInt n) 55 { 56 PetscInt i,j; 57 PetscTruth tmp = PETSC_FALSE; 58 char *ptr1,*ptr2 = (char*)&tmp; 59 60 PetscFunctionBegin; 61 for (j=0; j<n; j++) { 62 ptr1 = (char*)(buff + j); 63 for (i=0; i<(PetscInt)sizeof(PetscTruth); i++) { 64 ptr2[i] = ptr1[sizeof(PetscTruth)-1-i]; 65 } 66 for (i=0; i<(PetscInt)sizeof(PetscTruth); i++) { 67 ptr1[i] = ptr2[i]; 68 } 69 } 70 PetscFunctionReturn(0); 71 } 72 73 #undef __FUNCT__ 74 #define __FUNCT__ "PetscByteSwapInt" 75 /* 76 PetscByteSwapInt - Swap bytes in a PETSc integer (which may be 32 or 64 bits) 77 78 */ 79 PetscErrorCode PETSC_DLLEXPORT PetscByteSwapInt(PetscInt *buff,PetscInt n) 80 { 81 PetscInt i,j,tmp = 0; 82 char *ptr1,*ptr2 = (char*)&tmp; 83 84 PetscFunctionBegin; 85 for (j=0; j<n; j++) { 86 ptr1 = (char*)(buff + j); 87 for (i=0; i<(PetscInt)sizeof(PetscInt); i++) { 88 ptr2[i] = ptr1[sizeof(PetscInt)-1-i]; 89 } 90 for (i=0; i<(PetscInt)sizeof(PetscInt); i++) { 91 ptr1[i] = ptr2[i]; 92 } 93 } 94 PetscFunctionReturn(0); 95 } 96 /* --------------------------------------------------------- */ 97 #undef __FUNCT__ 98 #define __FUNCT__ "PetscByteSwapShort" 99 /* 100 PetscByteSwapShort - Swap bytes in a short 101 */ 102 PetscErrorCode PETSC_DLLEXPORT PetscByteSwapShort(short *buff,PetscInt n) 103 { 104 PetscInt i,j; 105 short tmp; 106 char *ptr1,*ptr2 = (char*)&tmp; 107 108 PetscFunctionBegin; 109 for (j=0; j<n; j++) { 110 ptr1 = (char*)(buff + j); 111 for (i=0; i<(PetscInt) sizeof(short); i++) { 112 ptr2[i] = ptr1[sizeof(int)-1-i]; 113 } 114 for (i=0; i<(PetscInt) sizeof(short); i++) { 115 ptr1[i] = ptr2[i]; 116 } 117 } 118 PetscFunctionReturn(0); 119 } 120 /* --------------------------------------------------------- */ 121 #undef __FUNCT__ 122 #define __FUNCT__ "PetscByteSwapScalar" 123 /* 124 PetscByteSwapScalar - Swap bytes in a double 125 Complex is dealt with as if array of double twice as long. 126 */ 127 PetscErrorCode PETSC_DLLEXPORT PetscByteSwapScalar(PetscScalar *buff,PetscInt n) 128 { 129 PetscInt i,j; 130 PetscReal tmp,*buff1 = (PetscReal*)buff; 131 char *ptr1,*ptr2 = (char*)&tmp; 132 133 PetscFunctionBegin; 134 #if defined(PETSC_USE_COMPLEX) 135 n *= 2; 136 #endif 137 for (j=0; j<n; j++) { 138 ptr1 = (char*)(buff1 + j); 139 for (i=0; i<(PetscInt) sizeof(PetscReal); i++) { 140 ptr2[i] = ptr1[sizeof(PetscReal)-1-i]; 141 } 142 for (i=0; i<(PetscInt) sizeof(PetscReal); i++) { 143 ptr1[i] = ptr2[i]; 144 } 145 } 146 PetscFunctionReturn(0); 147 } 148 /* --------------------------------------------------------- */ 149 #undef __FUNCT__ 150 #define __FUNCT__ "PetscByteSwapDouble" 151 /* 152 PetscByteSwapDouble - Swap bytes in a double 153 */ 154 PetscErrorCode PETSC_DLLEXPORT PetscByteSwapDouble(double *buff,PetscInt n) 155 { 156 PetscInt i,j; 157 double tmp,*buff1 = (double*)buff; 158 char *ptr1,*ptr2 = (char*)&tmp; 159 160 PetscFunctionBegin; 161 for (j=0; j<n; j++) { 162 ptr1 = (char*)(buff1 + j); 163 for (i=0; i<(PetscInt) sizeof(double); i++) { 164 ptr2[i] = ptr1[sizeof(double)-1-i]; 165 } 166 for (i=0; i<(PetscInt) sizeof(double); i++) { 167 ptr1[i] = ptr2[i]; 168 } 169 } 170 PetscFunctionReturn(0); 171 } 172 173 #undef __FUNCT__ 174 #define __FUNCT__ "PetscByteSwap" 175 PetscErrorCode PetscByteSwap(void *data,PetscDataType pdtype,PetscInt count) 176 { 177 PetscErrorCode ierr; 178 179 PetscFunctionBegin; 180 if (pdtype == PETSC_INT) {ierr = PetscByteSwapInt((PetscInt*)data,count);CHKERRQ(ierr);} 181 else if (pdtype == PETSC_ENUM) {ierr = PetscByteSwapEnum((PetscEnum*)data,count);CHKERRQ(ierr);} 182 else if (pdtype == PETSC_TRUTH) {ierr = PetscByteSwapTruth((PetscTruth*)data,count);CHKERRQ(ierr);} 183 else if (pdtype == PETSC_SCALAR) {ierr = PetscByteSwapScalar((PetscScalar*)data,count);CHKERRQ(ierr);} 184 else if (pdtype == PETSC_DOUBLE) {ierr = PetscByteSwapDouble((double*)data,count);CHKERRQ(ierr);} 185 else if (pdtype == PETSC_SHORT) {ierr = PetscByteSwapShort((short*)data,count);CHKERRQ(ierr);} 186 PetscFunctionReturn(0); 187 } 188 189 #endif 190 /* --------------------------------------------------------- */ 191 #undef __FUNCT__ 192 #define __FUNCT__ "PetscBinaryRead" 193 /*@ 194 PetscBinaryRead - Reads from a binary file. 195 196 Not Collective 197 198 Input Parameters: 199 + fd - the file 200 . n - the number of items to read 201 - type - the type of items to read (PETSC_INT, PETSC_DOUBLE or PETSC_SCALAR) 202 203 Output Parameters: 204 . p - the buffer 205 206 207 208 Level: developer 209 210 Notes: 211 PetscBinaryRead() uses byte swapping to work on all machines; the files 212 are written to file ALWAYS using big-endian ordering. On small-endian machines the numbers 213 are converted to the small-endian format when they are read in from the file. 214 When PETSc is config/configure.py with --with-64bit-indices the integers are written to the 215 file as 64 bit integers, this means they can only be read back in when the option --with-64bit-indices 216 is used. 217 218 Concepts: files^reading binary 219 Concepts: binary files^reading 220 221 .seealso: PetscBinaryWrite(), PetscBinaryOpen(), PetscBinaryClose(), PetscViewerBinaryGetDescriptor(), PetscBinarySynchronizedWrite(), 222 PetscBinarySynchronizedRead(), PetscBinarySynchronizedSeek() 223 @*/ 224 PetscErrorCode PETSC_DLLEXPORT PetscBinaryRead(int fd,void *p,PetscInt n,PetscDataType type) 225 { 226 int wsize,err; 227 size_t m = (size_t) n,maxblock = 65536; 228 char *pp = (char*)p; 229 #if !defined(PETSC_WORDS_BIGENDIAN) 230 PetscErrorCode ierr; 231 void *ptmp = p; 232 #endif 233 234 PetscFunctionBegin; 235 if (!n) PetscFunctionReturn(0); 236 237 if (type == PETSC_INT) m *= sizeof(PetscInt); 238 else if (type == PETSC_SCALAR) m *= sizeof(PetscScalar); 239 else if (type == PETSC_DOUBLE) m *= sizeof(double); 240 else if (type == PETSC_SHORT) m *= sizeof(short); 241 else if (type == PETSC_CHAR) m *= sizeof(char); 242 else if (type == PETSC_ENUM) m *= sizeof(PetscEnum); 243 else if (type == PETSC_TRUTH) m *= sizeof(PetscTruth); 244 else if (type == PETSC_LOGICAL) m = PetscBTLength(m)*sizeof(char); 245 else SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Unknown type"); 246 247 while (m) { 248 wsize = (m < maxblock) ? m : maxblock; 249 err = read(fd,pp,wsize); 250 if (err < 0 && errno == EINTR) continue; 251 if (!err && wsize > 0) SETERRQ(PETSC_ERR_FILE_READ,"Read past end of file"); 252 if (err < 0) SETERRQ1(PETSC_ERR_FILE_READ,"Error reading from file, errno %d",errno); 253 m -= err; 254 pp += err; 255 } 256 #if !defined(PETSC_WORDS_BIGENDIAN) 257 ierr = PetscByteSwap(ptmp,type,n);CHKERRQ(ierr); 258 #endif 259 260 PetscFunctionReturn(0); 261 } 262 /* --------------------------------------------------------- */ 263 #undef __FUNCT__ 264 #define __FUNCT__ "PetscBinaryWrite" 265 /*@ 266 PetscBinaryWrite - Writes to a binary file. 267 268 Not Collective 269 270 Input Parameters: 271 + fd - the file 272 . p - the buffer 273 . n - the number of items to write 274 . type - the type of items to read (PETSC_INT, PETSC_DOUBLE or PETSC_SCALAR) 275 - istemp - PETSC_FALSE if buffer data should be preserved, PETSC_TRUE otherwise. 276 277 Level: advanced 278 279 Notes: 280 PetscBinaryWrite() uses byte swapping to work on all machines; the files 281 are written using big-endian ordering to the file. On small-endian machines the numbers 282 are converted to the big-endian format when they are written to disk. 283 When PETSc is config/configure.py with --with-64bit-indices the integers are written to the 284 file as 64 bit integers, this means they can only be read back in when the option --with-64bit-indices 285 is used. 286 287 The Buffer p should be read-write buffer, and not static data. 288 This way, byte-swapping is done in-place, and then the buffer is 289 written to the file. 290 291 This routine restores the original contents of the buffer, after 292 it is written to the file. This is done by byte-swapping in-place 293 the second time. If the flag istemp is set to PETSC_TRUE, the second 294 byte-swapping operation is not done, thus saving some computation, 295 but the buffer corrupted is corrupted. 296 297 Concepts: files^writing binary 298 Concepts: binary files^writing 299 300 .seealso: PetscBinaryRead(), PetscBinaryOpen(), PetscBinaryClose(), PetscViewerBinaryGetDescriptor(), PetscBinarySynchronizedWrite(), 301 PetscBinarySynchronizedRead(), PetscBinarySynchronizedSeek() 302 @*/ 303 PetscErrorCode PETSC_DLLEXPORT PetscBinaryWrite(int fd,void *p,PetscInt n,PetscDataType type,PetscTruth istemp) 304 { 305 char *pp = (char*)p; 306 int err,wsize; 307 size_t m = (size_t)n,maxblock=65536; 308 #if !defined(PETSC_WORDS_BIGENDIAN) 309 PetscErrorCode ierr; 310 void *ptmp = p; 311 #endif 312 313 PetscFunctionBegin; 314 if (n < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Trying to write a negative amount of data %D",n); 315 if (!n) PetscFunctionReturn(0); 316 317 if (type == PETSC_INT) m *= sizeof(PetscInt); 318 else if (type == PETSC_SCALAR) m *= sizeof(PetscScalar); 319 else if (type == PETSC_DOUBLE) m *= sizeof(double); 320 else if (type == PETSC_SHORT) m *= sizeof(short); 321 else if (type == PETSC_CHAR) m *= sizeof(char); 322 else if (type == PETSC_ENUM) m *= sizeof(PetscEnum); 323 else if (type == PETSC_TRUTH) m *= sizeof(PetscTruth); 324 else if (type == PETSC_LOGICAL) m = PetscBTLength(m)*sizeof(char); 325 else SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Unknown type"); 326 327 #if !defined(PETSC_WORDS_BIGENDIAN) 328 ierr = PetscByteSwap(ptmp,type,n);CHKERRQ(ierr); 329 #endif 330 331 while (m) { 332 wsize = (m < maxblock) ? m : maxblock; 333 err = write(fd,pp,wsize); 334 if (err < 0 && errno == EINTR) continue; 335 if (err != wsize) SETERRQ(PETSC_ERR_FILE_WRITE,"Error writing to file."); 336 m -= wsize; 337 pp += wsize; 338 } 339 340 #if !defined(PETSC_WORDS_BIGENDIAN) 341 if (!istemp) { 342 ierr = PetscByteSwap(ptmp,type,n);CHKERRQ(ierr); 343 } 344 #endif 345 PetscFunctionReturn(0); 346 } 347 348 #undef __FUNCT__ 349 #define __FUNCT__ "PetscBinaryOpen" 350 /*@C 351 PetscBinaryOpen - Opens a PETSc binary file. 352 353 Not Collective 354 355 Input Parameters: 356 + name - filename 357 - type - type of binary file, one of FILE_MODE_READ, FILE_MODE_APPEND, FILE_MODE_WRITE 358 359 Output Parameter: 360 . fd - the file 361 362 Level: advanced 363 364 Concepts: files^opening binary 365 Concepts: binary files^opening 366 367 Notes: Files access with PetscBinaryRead() and PetscBinaryWrite() are ALWAYS written in 368 big-endian format. This means the file can be accessed using PetscBinaryOpen() and 369 PetscBinaryRead() and PetscBinaryWrite() on any machine. 370 371 .seealso: PetscBinaryRead(), PetscBinaryWrite(), PetscFileMode, PetscViewerFileSetMode(), PetscViewerBinaryGetDescriptor(), 372 PetscBinarySynchronizedWrite(), PetscBinarySynchronizedRead(), PetscBinarySynchronizedSeek() 373 374 @*/ 375 PetscErrorCode PETSC_DLLEXPORT PetscBinaryOpen(const char name[],PetscFileMode mode,int *fd) 376 { 377 PetscFunctionBegin; 378 #if defined(PETSC_HAVE_O_BINARY) 379 if (mode == FILE_MODE_WRITE) { 380 if ((*fd = open(name,O_WRONLY|O_CREAT|O_TRUNC|O_BINARY,0666)) == -1) { 381 SETERRQ1(PETSC_ERR_FILE_OPEN,"Cannot create file for writing: %s",name); 382 } 383 } else if (mode == FILE_MODE_READ) { 384 if ((*fd = open(name,O_RDONLY|O_BINARY,0)) == -1) { 385 SETERRQ1(PETSC_ERR_FILE_OPEN,"Cannot open file for reading: %s",name); 386 } 387 } else if (mode == FILE_MODE_APPEND) { 388 if ((*fd = open(name,O_WRONLY|O_BINARY,0)) == -1) { 389 SETERRQ1(PETSC_ERR_FILE_OPEN,"Cannot open file for writing: %s",name); 390 } 391 #else 392 if (mode == FILE_MODE_WRITE) { 393 if ((*fd = creat(name,0666)) == -1) { 394 SETERRQ1(PETSC_ERR_FILE_OPEN,"Cannot create file for writing: %s",name); 395 } 396 } else if (mode == FILE_MODE_READ) { 397 if ((*fd = open(name,O_RDONLY,0)) == -1) { 398 SETERRQ1(PETSC_ERR_FILE_OPEN,"Cannot open file for reading: %s",name); 399 } 400 } 401 else if (mode == FILE_MODE_APPEND) { 402 if ((*fd = open(name,O_WRONLY,0)) == -1) { 403 SETERRQ1(PETSC_ERR_FILE_OPEN,"Cannot open file for writing: %s",name); 404 } 405 #endif 406 } else SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Unknown file mode"); 407 PetscFunctionReturn(0); 408 } 409 410 #undef __FUNCT__ 411 #define __FUNCT__ "PetscBinaryClose" 412 /*@ 413 PetscBinaryClose - Closes a PETSc binary file. 414 415 Not Collective 416 417 Output Parameter: 418 . fd - the file 419 420 Level: advanced 421 422 .seealso: PetscBinaryRead(), PetscBinaryWrite(), PetscBinaryOpen(), PetscBinarySynchronizedWrite(), PetscBinarySynchronizedRead(), 423 PetscBinarySynchronizedSeek() 424 @*/ 425 PetscErrorCode PETSC_DLLEXPORT PetscBinaryClose(int fd) 426 { 427 PetscFunctionBegin; 428 close(fd); 429 PetscFunctionReturn(0); 430 } 431 432 433 #undef __FUNCT__ 434 #define __FUNCT__ "PetscBinarySeek" 435 /*@ 436 PetscBinarySeek - Moves the file pointer on a PETSc binary file. 437 438 Not Collective 439 440 Input Parameters: 441 + fd - the file 442 . off - number of bytes to move. Use PETSC_BINARY_INT_SIZE, PETSC_BINARY_SCALAR_SIZE, 443 etc. in your calculation rather than sizeof() to compute byte lengths. 444 - whence - if PETSC_BINARY_SEEK_SET then off is an absolute location in the file 445 if PETSC_BINARY_SEEK_CUR then off is an offset from the current location 446 if PETSC_BINARY_SEEK_END then off is an offset from the end of file 447 448 Output Parameter: 449 . offset - new offset in file 450 451 Level: developer 452 453 Notes: 454 Integers are stored on the file as 32 long, regardless of whether 455 they are stored in the machine as 32 or 64, this means the same 456 binary file may be read on any machine. Hence you CANNOT use sizeof() 457 to determine the offset or location. 458 459 Concepts: files^binary seeking 460 Concepts: binary files^seeking 461 462 .seealso: PetscBinaryRead(), PetscBinaryWrite(), PetscBinaryOpen(), PetscBinarySynchronizedWrite(), PetscBinarySynchronizedRead(), 463 PetscBinarySynchronizedSeek() 464 @*/ 465 PetscErrorCode PETSC_DLLEXPORT PetscBinarySeek(int fd,off_t off,PetscBinarySeekType whence,off_t *offset) 466 { 467 int iwhence = 0; 468 469 PetscFunctionBegin; 470 if (whence == PETSC_BINARY_SEEK_SET) { 471 iwhence = SEEK_SET; 472 } else if (whence == PETSC_BINARY_SEEK_CUR) { 473 iwhence = SEEK_CUR; 474 } else if (whence == PETSC_BINARY_SEEK_END) { 475 iwhence = SEEK_END; 476 } else { 477 SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Unknown seek location"); 478 } 479 #if defined(PETSC_HAVE_LSEEK) 480 *offset = lseek(fd,off,iwhence); 481 #elif defined(PETSC_HAVE__LSEEK) 482 *offset = _lseek(fd,(long)off,iwhence); 483 #else 484 SETERRQ(PETSC_ERR_SUP_SYS,"System does not have a way of seeking on a file"); 485 #endif 486 PetscFunctionReturn(0); 487 } 488 489 #undef __FUNCT__ 490 #define __FUNCT__ "PetscBinarySynchronizedRead" 491 /*@C 492 PetscBinarySynchronizedRead - Reads from a binary file. 493 494 Collective on MPI_Comm 495 496 Input Parameters: 497 + comm - the MPI communicator 498 . fd - the file 499 . n - the number of items to read 500 - type - the type of items to read (PETSC_INT, PETSC_DOUBLE or PETSC_SCALAR) 501 502 Output Parameters: 503 . p - the buffer 504 505 Options Database Key: 506 . -binary_longints - indicates the file was generated on a Cray vector 507 machine (not the T3E/D) and the ints are stored as 64 bit 508 quantities, otherwise they are stored as 32 bit 509 510 Level: developer 511 512 Notes: 513 Does a PetscBinaryRead() followed by an MPI_Bcast() 514 515 PetscBinarySynchronizedRead() uses byte swapping to work on all machines. 516 Integers are stored on the file as 32 long, regardless of whether 517 they are stored in the machine as 32 or 64, this means the same 518 binary file may be read on any machine. 519 520 Concepts: files^synchronized reading of binary files 521 Concepts: binary files^reading, synchronized 522 523 .seealso: PetscBinaryWrite(), PetscBinaryOpen(), PetscBinaryClose(), PetscBinaryRead(), PetscBinarySynchronizedWrite(), 524 PetscBinarySynchronizedSeek() 525 @*/ 526 PetscErrorCode PETSC_DLLEXPORT PetscBinarySynchronizedRead(MPI_Comm comm,int fd,void *p,PetscInt n,PetscDataType type) 527 { 528 PetscErrorCode ierr; 529 PetscMPIInt rank; 530 MPI_Datatype mtype; 531 532 PetscFunctionBegin; 533 ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 534 if (!rank) { 535 ierr = PetscBinaryRead(fd,p,n,type);CHKERRQ(ierr); 536 } 537 ierr = PetscDataTypeToMPIDataType(type,&mtype);CHKERRQ(ierr); 538 ierr = MPI_Bcast(p,n,mtype,0,comm);CHKERRQ(ierr); 539 PetscFunctionReturn(0); 540 } 541 542 #undef __FUNCT__ 543 #define __FUNCT__ "PetscBinarySynchronizedWrite" 544 /*@C 545 PetscBinarySynchronizedWrite - writes to a binary file. 546 547 Collective on MPI_Comm 548 549 Input Parameters: 550 + comm - the MPI communicator 551 . fd - the file 552 . n - the number of items to write 553 . p - the buffer 554 . istemp - the buffer may be changed 555 - type - the type of items to write (PETSC_INT, PETSC_DOUBLE or PETSC_SCALAR) 556 557 Level: developer 558 559 Notes: 560 Process 0 does a PetscBinaryWrite() 561 562 PetscBinarySynchronizedWrite() uses byte swapping to work on all machines. 563 Integers are stored on the file as 32 long, regardless of whether 564 they are stored in the machine as 32 or 64, this means the same 565 binary file may be read on any machine. 566 567 WARNING: This is NOT like PetscSynchronizedFPrintf()! This routine ignores calls on all but process 0, 568 while PetscSynchronizedFPrintf() has all processes print their strings in order. 569 570 Concepts: files^synchronized writing of binary files 571 Concepts: binary files^reading, synchronized 572 573 .seealso: PetscBinaryWrite(), PetscBinaryOpen(), PetscBinaryClose(), PetscBinaryRead(), PetscBinarySynchronizedRead(), 574 PetscBinarySynchronizedSeek() 575 @*/ 576 PetscErrorCode PETSC_DLLEXPORT PetscBinarySynchronizedWrite(MPI_Comm comm,int fd,void *p,PetscInt n,PetscDataType type,PetscTruth istemp) 577 { 578 PetscErrorCode ierr; 579 PetscMPIInt rank; 580 581 PetscFunctionBegin; 582 ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 583 if (!rank) { 584 ierr = PetscBinaryWrite(fd,p,n,type,istemp);CHKERRQ(ierr); 585 } 586 PetscFunctionReturn(0); 587 } 588 589 #undef __FUNCT__ 590 #define __FUNCT__ "PetscBinarySynchronizedSeek" 591 /*@C 592 PetscBinarySynchronizedSeek - Moves the file pointer on a PETSc binary file. 593 594 595 Input Parameters: 596 + fd - the file 597 . whence - if PETSC_BINARY_SEEK_SET then size is an absolute location in the file 598 if PETSC_BINARY_SEEK_CUR then size is offset from current location 599 if PETSC_BINARY_SEEK_END then size is offset from end of file 600 - off - number of bytes to move. Use PETSC_BINARY_INT_SIZE, PETSC_BINARY_SCALAR_SIZE, 601 etc. in your calculation rather than sizeof() to compute byte lengths. 602 603 Output Parameter: 604 . offset - new offset in file 605 606 Level: developer 607 608 Notes: 609 Integers are stored on the file as 32 long, regardless of whether 610 they are stored in the machine as 32 or 64, this means the same 611 binary file may be read on any machine. Hence you CANNOT use sizeof() 612 to determine the offset or location. 613 614 Concepts: binary files^seeking 615 Concepts: files^seeking in binary 616 617 .seealso: PetscBinaryRead(), PetscBinaryWrite(), PetscBinaryOpen(), PetscBinarySynchronizedWrite(), PetscBinarySynchronizedRead(), 618 PetscBinarySynchronizedSeek() 619 @*/ 620 PetscErrorCode PETSC_DLLEXPORT PetscBinarySynchronizedSeek(MPI_Comm comm,int fd,off_t off,PetscBinarySeekType whence,off_t *offset) 621 { 622 PetscErrorCode ierr; 623 PetscMPIInt rank; 624 625 PetscFunctionBegin; 626 ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 627 if (!rank) { 628 ierr = PetscBinarySeek(fd,off,whence,offset);CHKERRQ(ierr); 629 } 630 PetscFunctionReturn(0); 631 } 632 633 #if defined(PETSC_HAVE_MPIIO) 634 #if !defined(PETSC_WORDS_BIGENDIAN) 635 636 #if defined(PETSC_USE_PETSC_MPI_EXTERNAL32) 637 EXTERN_C_BEGIN 638 /* 639 MPICH does not provide the external32 representation for MPI_File_set_view() so we need to provide the functions. 640 These are set into MPI in PetscInitialize() via MPI_Register_datarep() 641 642 Note I use PetscMPIInt for the MPI error codes since that is what MPI uses (instead of the standard PetscErrorCode) 643 644 The next three routines are not used because MPICH does not support their use 645 646 */ 647 PetscMPIInt PetscDataRep_extent_fn(MPI_Datatype datatype,MPI_Aint *file_extent,void *extra_state) 648 { 649 MPI_Aint ub; 650 PetscMPIInt ierr; 651 652 ierr = MPI_Type_get_extent(datatype,&ub,file_extent); 653 return ierr; 654 } 655 656 PetscMPIInt PetscDataRep_read_conv_fn(void *userbuf, MPI_Datatype datatype,PetscMPIInt count,void *filebuf, MPI_Offset position,void *extra_state) 657 { 658 PetscDataType pdtype; 659 PetscMPIInt ierr; 660 size_t dsize; 661 662 ierr = PetscMPIDataTypeToPetscDataType(datatype,&pdtype);CHKERRQ(ierr); 663 ierr = PetscDataTypeGetSize(pdtype,&dsize);CHKERRQ(ierr); 664 665 /* offset is given in units of MPI_Datatype */ 666 userbuf = ((char *)userbuf) + dsize*position; 667 668 ierr = PetscMemcpy(userbuf,filebuf,count*dsize);CHKERRQ(ierr); 669 ierr = PetscByteSwap(userbuf,pdtype,count);CHKERRQ(ierr); 670 return ierr; 671 } 672 673 PetscMPIInt PetscDataRep_write_conv_fn(void *userbuf, MPI_Datatype datatype,PetscMPIInt count,void *filebuf, MPI_Offset position,void *extra_state) 674 { 675 PetscDataType pdtype; 676 PetscMPIInt ierr; 677 size_t dsize; 678 679 ierr = PetscMPIDataTypeToPetscDataType(datatype,&pdtype);CHKERRQ(ierr); 680 ierr = PetscDataTypeGetSize(pdtype,&dsize);CHKERRQ(ierr); 681 682 /* offset is given in units of MPI_Datatype */ 683 userbuf = ((char *)userbuf) + dsize*position; 684 685 ierr = PetscMemcpy(filebuf,userbuf,count*dsize);CHKERRQ(ierr); 686 ierr = PetscByteSwap(filebuf,pdtype,count);CHKERRQ(ierr); 687 return ierr; 688 } 689 EXTERN_C_END 690 #endif 691 692 PetscErrorCode MPIU_File_write_all(MPI_File fd,void *data,PetscMPIInt cnt,MPI_Datatype dtype,MPI_Status *status) 693 { 694 PetscErrorCode ierr; 695 PetscDataType pdtype; 696 697 PetscFunctionBegin; 698 ierr = PetscMPIDataTypeToPetscDataType(dtype,&pdtype);CHKERRQ(ierr); 699 ierr = PetscByteSwap(data,pdtype,cnt);CHKERRQ(ierr); 700 ierr = MPI_File_write_all(fd,data,cnt,dtype,status);CHKERRQ(ierr); 701 ierr = PetscByteSwap(data,pdtype,cnt);CHKERRQ(ierr); 702 PetscFunctionReturn(0); 703 } 704 705 PetscErrorCode MPIU_File_read_all(MPI_File fd,void *data,PetscMPIInt cnt,MPI_Datatype dtype,MPI_Status *status) 706 { 707 PetscErrorCode ierr; 708 PetscDataType pdtype; 709 710 PetscFunctionBegin; 711 ierr = PetscMPIDataTypeToPetscDataType(dtype,&pdtype);CHKERRQ(ierr); 712 ierr = MPI_File_read_all(fd,data,cnt,dtype,status);CHKERRQ(ierr); 713 ierr = PetscByteSwap(data,pdtype,cnt);CHKERRQ(ierr); 714 PetscFunctionReturn(0); 715 } 716 #endif 717 #endif 718