root/ompi/mca/sharedfp/lockedfile/sharedfp_lockedfile_seek.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. mca_sharedfp_lockedfile_seek

   1 /*
   2  * Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
   3  *                         University Research and Technology
   4  *                         Corporation.  All rights reserved.
   5  * Copyright (c) 2004-2017 The University of Tennessee and The University
   6  *                         of Tennessee Research Foundation.  All rights
   7  *                         reserved.
   8  * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
   9  *                         University of Stuttgart.  All rights reserved.
  10  * Copyright (c) 2004-2005 The Regents of the University of California.
  11  *                         All rights reserved.
  12  * Copyright (c) 2013-2018 University of Houston. All rights reserved.
  13  * Copyright (c) 2018      Research Organization for Information Science
  14  *                         and Technology (RIST). All rights reserved.
  15  * $COPYRIGHT$
  16  *
  17  * Additional copyrights may follow
  18  *
  19  * $HEADER$
  20  */
  21 
  22 
  23 #include "ompi_config.h"
  24 #include "sharedfp_lockedfile.h"
  25 
  26 #include "mpi.h"
  27 #include "ompi/constants.h"
  28 #include "ompi/mca/sharedfp/sharedfp.h"
  29 #include "ompi/mca/sharedfp/base/base.h"
  30 
  31 /*Use fcntl to lock the file which stores the current position*/
  32 #include <fcntl.h>
  33 
  34 int
  35 mca_sharedfp_lockedfile_seek (ompio_file_t *fh,
  36                               OMPI_MPI_OFFSET_TYPE off, int whence)
  37 {
  38     int ret = OMPI_SUCCESS;
  39     struct mca_sharedfp_base_data_t *sh = NULL;
  40     struct mca_sharedfp_lockedfile_data * lockedfile_data;
  41     int fd_lockedfilehandle;
  42     /* flock structure that is used to setup the desired fcntl operation */
  43     struct flock fl;
  44     OMPI_MPI_OFFSET_TYPE offset, end_position=0;
  45 
  46     if(fh->f_sharedfp_data==NULL){
  47         opal_output(ompi_sharedfp_base_framework.framework_output,
  48                     "sharedfp_lockedfile_seek: module not initialized\n");
  49         return OMPI_ERROR;
  50     }
  51 
  52     sh = fh->f_sharedfp_data;
  53     offset = off * fh->f_etype_size;
  54 
  55     if( 0 == fh->f_rank ){
  56         if ( MPI_SEEK_SET == whence ){
  57             /*don't need to read current value*/
  58             if(offset < 0){
  59                 opal_output(0,"sharedfp_lockedfile_seek - MPI_SEEK_SET, offset must be > 0,"
  60                             " got offset=%lld.\n",offset);
  61                 return OMPI_ERROR;
  62             }
  63         }
  64         else if ( MPI_SEEK_CUR == whence){
  65             OMPI_MPI_OFFSET_TYPE current_position;
  66             ret = mca_sharedfp_lockedfile_get_position(fh,&current_position);
  67             if ( OMPI_SUCCESS != ret ) {
  68                 return OMPI_ERROR;
  69             }
  70 
  71             offset = current_position + offset;
  72             fflush(stdout);
  73             if(offset < 0){
  74                 opal_output(0,"sharedfp_lockedfile_seek - MPI_SEEK_CUR, offset must be > 0, got offset=%lld.\n",offset);
  75                 return OMPI_ERROR;
  76             }
  77         }
  78         else if( MPI_SEEK_END == whence ){
  79             mca_common_ompio_file_get_size( fh,&end_position);
  80             offset = end_position + offset;
  81 
  82             if ( offset < 0){
  83                 opal_output(0,"sharedfp_lockedfile_seek - MPI_SEEK_CUR, offset must be > 0, got offset=%lld.\n",offset);
  84                 return OMPI_ERROR;
  85             }
  86         }else{
  87             opal_output(0,"sharedfp_lockedfile_seek - whence=%i is not supported\n",whence);
  88             return OMPI_ERROR;
  89         }
  90 
  91 
  92         /* Set Shared file pointer  */
  93         lockedfile_data = sh->selected_module_data;
  94         fd_lockedfilehandle = lockedfile_data->handle;
  95 
  96         opal_output(ompi_sharedfp_base_framework.framework_output,
  97                     "sharedfp_lockedfile_seek: Aquiring lock...");
  98 
  99         /* set up the flock structure */
 100         fl.l_type   = F_WRLCK;
 101         fl.l_whence = SEEK_SET;
 102         fl.l_start  = 0;
 103         fl.l_len    = 0;
 104         fl.l_pid    = getpid();
 105 
 106         /* Aquire an exclusive lock */
 107         if ( fcntl(fd_lockedfilehandle, F_SETLKW, &fl) == -1) {
 108             opal_output(0, "Erorr acquiring lock: fcntl(%d,F_SETLKW,&fl)\n",fd_lockedfilehandle);
 109             opal_output(0,"error(%i): %s", errno, strerror(errno));
 110             return OMPI_ERROR;
 111         }
 112         else{
 113             opal_output(ompi_sharedfp_base_framework.framework_output,
 114                         "sharedfp_lockedfile_seek: Success! acquired lock.for fd: %d\n",fd_lockedfilehandle);
 115         }
 116 
 117         /*-- -----------------
 118          *write to the file
 119          *--------------------
 120          */
 121         lseek ( fd_lockedfilehandle, 0, SEEK_SET);
 122         write ( fd_lockedfilehandle, &offset, sizeof(OMPI_MPI_OFFSET_TYPE));
 123 
 124         /*-------------------
 125          * unlock the file
 126          *--------------------
 127          */
 128         if ( mca_sharedfp_lockedfile_verbose ) {
 129             opal_output(ompi_sharedfp_base_framework.framework_output,
 130                         "sharedfp_lockedfile_seek: Releasing lock...");
 131         }
 132         fl.l_type   = F_UNLCK;  /* set to unlock same region */
 133         fl.l_whence = SEEK_SET;
 134         fl.l_start  = 0;
 135         fl.l_len    = 0;
 136         fl.l_pid    = getpid();
 137 
 138         if (fcntl(fd_lockedfilehandle, F_SETLK, &fl) == -1) {
 139             opal_output(0,"Failed to release lock for fd: %d\n",fd_lockedfilehandle);
 140             opal_output(0,"error(%i): %s", errno, strerror(errno));
 141             return OMPI_ERROR;
 142         }
 143         else{
 144             opal_output(ompi_sharedfp_base_framework.framework_output,
 145                         "sharedfp_lockedfile_seek: released lock.for fd: %d\n",fd_lockedfilehandle);
 146         }
 147     }
 148 
 149     fh->f_comm->c_coll->coll_barrier ( fh->f_comm , fh->f_comm->c_coll->coll_barrier_module );
 150     return OMPI_SUCCESS;
 151 }

/* [<][>][^][v][top][bottom][index][help] */