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

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

DEFINITIONS

This source file includes following definitions.
  1. mca_sharedfp_lockedfile_file_open
  2. mca_sharedfp_lockedfile_file_close

   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) 2015-2018 Research Organization for Information Science
  14  *                         and Technology (RIST). All rights reserved.
  15  * Copyright (c) 2016-2017 IBM Corporation. All rights reserved.
  16  * $COPYRIGHT$
  17  *
  18  * Additional copyrights may follow
  19  *
  20  * $HEADER$
  21  */
  22 
  23 
  24 #include "ompi_config.h"
  25 #include "sharedfp_lockedfile.h"
  26 
  27 #include "mpi.h"
  28 #include "ompi/constants.h"
  29 #include "ompi/group/group.h"
  30 #include "ompi/proc/proc.h"
  31 #include "ompi/mca/sharedfp/sharedfp.h"
  32 #include "ompi/mca/sharedfp/base/base.h"
  33 
  34 #ifdef HAVE_SYS_STAT_H
  35 #include <sys/stat.h>
  36 #endif
  37 #include <fcntl.h>
  38 #include <unistd.h>
  39 
  40 int mca_sharedfp_lockedfile_file_open (struct ompi_communicator_t *comm,
  41                                        const char* filename,
  42                                        int amode,
  43                                        struct opal_info_t *info,
  44                                        ompio_file_t *fh)
  45 {
  46     int err = MPI_SUCCESS;
  47     char * lockedfilename;
  48     int handle;
  49     struct mca_sharedfp_lockedfile_data * module_data = NULL;
  50     struct mca_sharedfp_base_data_t* sh;
  51     pid_t my_pid;
  52     int int_pid;
  53     
  54     /*Memory is allocated here for the sh structure*/
  55     sh = (struct mca_sharedfp_base_data_t*)malloc(sizeof(struct mca_sharedfp_base_data_t));
  56     if ( NULL == sh){
  57         opal_output(0, "mca_sharedfp_lockedfile_file_open: Error, unable to malloc f_sharedfp struct\n");
  58         return OMPI_ERR_OUT_OF_RESOURCE;
  59     }
  60     /*Populate the sh file structure based on the implementation*/
  61     sh->global_offset = 0;                      /* Global Offset*/
  62     sh->selected_module_data = NULL;
  63 
  64     /*Open a new file which will maintain the pointer for this file open*/
  65     if ( mca_sharedfp_lockedfile_verbose ) {
  66         opal_output(ompi_sharedfp_base_framework.framework_output,
  67                     "mca_sharedfp_lockedfile_file_open: open locked file.\n");
  68     }
  69 
  70 
  71     module_data = (struct mca_sharedfp_lockedfile_data*)malloc(sizeof(struct mca_sharedfp_lockedfile_data));
  72     if ( NULL == module_data ) {
  73         opal_output(ompi_sharedfp_base_framework.framework_output,
  74                     "mca_sharedfp_lockedfile_file_open: Error, unable to malloc lockedfile_data struct\n");
  75         free (sh);
  76         return OMPI_ERR_OUT_OF_RESOURCE;
  77     }
  78 
  79     opal_jobid_t masterjobid;
  80     if ( 0 == comm->c_my_rank  ) {
  81         ompi_proc_t *masterproc = ompi_group_peer_lookup(comm->c_local_group, 0 );
  82         masterjobid = OMPI_CAST_RTE_NAME(&masterproc->super.proc_name)->jobid;
  83     }
  84     err = comm->c_coll->coll_bcast ( &masterjobid, 1, MPI_UNSIGNED, 0, comm, 
  85                                      comm->c_coll->coll_bcast_module );
  86     if ( OMPI_SUCCESS != err ) {
  87         opal_output(0, "[%d]mca_sharedfp_lockedfile_file_open: Error in bcast operation\n", fh->f_rank);
  88         free (sh);
  89         free(module_data);
  90         return err;
  91     }
  92 
  93     if ( 0 == fh->f_rank ) {
  94         my_pid = getpid();
  95         int_pid = (int) my_pid;
  96     }
  97     err = comm->c_coll->coll_bcast (&int_pid, 1, MPI_INT, 0, comm, comm->c_coll->coll_bcast_module );
  98     if ( OMPI_SUCCESS != err ) {
  99         opal_output(0, "[%d]mca_sharedfp_lockedfile_file_open: Error in bcast operation\n", fh->f_rank);
 100         free (sh);
 101         free(module_data);
 102         return err;
 103     }
 104 
 105     size_t filenamelen = strlen(filename) + 24;
 106     lockedfilename = (char*)malloc(sizeof(char) * filenamelen);
 107     if ( NULL == lockedfilename ) {
 108         free (sh);
 109         free (module_data);
 110         return OMPI_ERR_OUT_OF_RESOURCE;
 111     }
 112     snprintf(lockedfilename, filenamelen, "%s-%u-%d%s",filename,masterjobid,int_pid,".lock");
 113     module_data->filename = lockedfilename;
 114     
 115     /*-------------------------------------------------*/
 116     /*Open the lockedfile without shared file pointer  */
 117     /*-------------------------------------------------*/
 118     if ( 0 == comm->c_my_rank ) {
 119         OMPI_MPI_OFFSET_TYPE position=0;
 120         /*only let main process initialize file pointer,
 121          *therefore there is no need to lock the file
 122          */
 123         handle = open ( lockedfilename, O_RDWR | O_CREAT, 0644 );
 124         if ( -1 == handle ){
 125             opal_output(0, "[%d]mca_sharedfp_lockedfile_file_open: Error during file open\n", 
 126                         fh->f_rank);
 127             free (sh);
 128             free(module_data);
 129             free (lockedfilename);
 130             return OMPI_ERROR;
 131         }
 132         write ( handle, &position, sizeof(OMPI_MPI_OFFSET_TYPE) );
 133         close ( handle );
 134     }
 135     err = comm->c_coll->coll_barrier ( comm, comm->c_coll->coll_barrier_module );
 136     if ( OMPI_SUCCESS != err ) {
 137         opal_output(0, "[%d]mca_sharedfp_lockedfile_file_open: Error in barrier operation\n", fh->f_rank);
 138         free (sh);
 139         free(module_data);
 140         free (lockedfilename);
 141         return err;
 142     }
 143 
 144     handle = open ( lockedfilename, O_RDWR, 0644  );
 145     if ( -1 == handle ) {
 146         opal_output(0, "[%d]mca_sharedfp_lockedfile_file_open: Error during file open\n", 
 147                     fh->f_rank);
 148         free (sh);
 149         free(module_data);
 150         free (lockedfilename);
 151         return OMPI_ERROR;
 152     }
 153 
 154     /*Store the new file handle*/
 155     module_data->handle = handle;
 156     /* Assign the lockedfile_data to sh->handle*/
 157     sh->selected_module_data   = module_data;
 158     /*remember the shared file handle*/
 159     fh->f_sharedfp_data = sh;
 160 
 161     return comm->c_coll->coll_barrier ( comm, comm->c_coll->coll_barrier_module );
 162 }
 163 
 164 int mca_sharedfp_lockedfile_file_close (ompio_file_t *fh)
 165 {
 166     int err = OMPI_SUCCESS;
 167     struct mca_sharedfp_lockedfile_data * module_data = NULL;
 168     struct mca_sharedfp_base_data_t *sh;
 169 
 170     if ( fh->f_sharedfp_data==NULL){
 171         return OMPI_SUCCESS;
 172     }
 173     sh = fh->f_sharedfp_data;
 174 
 175     module_data = (lockedfile_data*)(sh->selected_module_data);
 176     if ( module_data)   {
 177         /*Close lockedfile handle*/
 178         if ( module_data->handle)  {
 179             close (module_data->handle );
 180             if ( 0 == fh->f_rank ) {
 181                 unlink ( module_data->filename);
 182             }
 183         }
 184         if ( NULL != module_data->filename ){
 185             free ( module_data->filename);
 186         }
 187         free ( module_data );
 188     }
 189 
 190     /*free shared file pointer data struct*/
 191     free(sh);
 192 
 193     return err;
 194 
 195 }
 196 

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