AMPI #952: update ROMIO to MPICH2-1.4.1p1
[charm.git] / src / libs / ck-libs / ampi / romio / adio / ad_bgl / ad_bgl_getsh.c
blob2c51b1f35763ff04bb31a78cf18fc6d61d269a44
1 /* ---------------------------------------------------------------- */
2 /* (C)Copyright IBM Corp. 2007, 2008 */
3 /* ---------------------------------------------------------------- */
4 /**
5 * \file ad_bgl_getsh.c
6 * \brief ???
7 */
9 /* -*- Mode: C; c-basic-offset:4 ; -*- */
10 /*
11 * Copyright (C) 1997 University of Chicago.
12 * See COPYRIGHT notice in top-level directory.
15 #include "ad_bgl.h"
17 /* returns the current location of the shared_fp in terms of the
18 no. of etypes relative to the current view, and also increments the
19 shared_fp by the number of etypes to be accessed (incr) in the read
20 or write following this function. */
22 void ADIOI_BGL_Get_shared_fp(ADIO_File fd, int incr, ADIO_Offset *shared_fp,
23 int *error_code)
25 ADIO_Offset new_fp;
26 int err;
27 MPI_Comm dupcommself;
28 static char myname[] = "ADIOI_BGL_GET_SHARED_FP";
30 if (fd->shared_fp_fd == ADIO_FILE_NULL) {
31 MPI_Comm_dup(MPI_COMM_SELF, &dupcommself);
32 fd->shared_fp_fd = ADIO_Open(MPI_COMM_SELF,
33 dupcommself,
34 fd->shared_fp_fname,
35 fd->file_system,
36 fd->fns,
37 ADIO_CREATE | ADIO_RDWR | ADIO_DELETE_ON_CLOSE,
38 0,
39 MPI_BYTE,
40 MPI_BYTE,
41 MPI_INFO_NULL,
42 ADIO_PERM_NULL,
43 error_code);
44 if (*error_code != MPI_SUCCESS) return;
45 *shared_fp = 0;
46 ADIOI_WRITE_LOCK(fd->shared_fp_fd, 0, SEEK_SET, sizeof(ADIO_Offset));
47 err = read(fd->shared_fp_fd->fd_sys, shared_fp, sizeof(ADIO_Offset));
48 /* if the file is empty, the above read may return error
49 (reading beyond end of file). In that case, shared_fp = 0,
50 set above, is the correct value. */
52 else {
53 ADIOI_WRITE_LOCK(fd->shared_fp_fd, 0, SEEK_SET, sizeof(ADIO_Offset));
55 err = lseek(fd->shared_fp_fd->fd_sys, 0, SEEK_SET);
56 if (err == 0) {
57 err = read(fd->shared_fp_fd->fd_sys, shared_fp,
58 sizeof(ADIO_Offset));
60 if (err == -1) {
61 ADIOI_UNLOCK(fd->shared_fp_fd, 0, SEEK_SET, sizeof(ADIO_Offset));
62 *error_code = MPIO_Err_create_code(MPI_SUCCESS,
63 MPIR_ERR_RECOVERABLE, myname,
64 __LINE__, MPI_ERR_IO, "**io",
65 "**io %s", strerror(errno));
66 return;
70 new_fp = *shared_fp + incr;
72 err = lseek(fd->shared_fp_fd->fd_sys, 0, SEEK_SET);
73 if (err == 0) {
74 err = write(fd->shared_fp_fd->fd_sys, &new_fp, sizeof(ADIO_Offset));
76 ADIOI_UNLOCK(fd->shared_fp_fd, 0, SEEK_SET, sizeof(ADIO_Offset));
77 if (err == -1) {
78 *error_code = MPIO_Err_create_code(MPI_SUCCESS, MPIR_ERR_RECOVERABLE,
79 myname, __LINE__, MPI_ERR_IO,
80 "**io",
81 "**io %s", strerror(errno));
83 else *error_code = MPI_SUCCESS;