root/ompi/mca/sharedfp/individual/sharedfp_individual_file_open.c

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

DEFINITIONS

This source file includes following definitions.
  1. mca_sharedfp_individual_file_open
  2. mca_sharedfp_individual_file_close
  3. mca_sharedfp_individual_insert_headnode

   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-2005 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_individual.h"
  26 
  27 #include "mpi.h"
  28 #include "ompi/constants.h"
  29 #include "ompi/mca/sharedfp/sharedfp.h"
  30 #include "ompi/mca/sharedfp/base/base.h"
  31 #include "ompi/file/file.h"
  32 
  33 
  34 int mca_sharedfp_individual_file_open (struct ompi_communicator_t *comm,
  35                                        const char* filename,
  36                                        int amode,
  37                                        struct opal_info_t *info,
  38                                        ompio_file_t *fh)
  39 {
  40     int err = 0;
  41     char * datafilename;        /*The array size would change as it is based on the current path*/
  42     char * metadatafilename;    /*The array size would change as it is based on the current path*/
  43     ompio_file_t * datafilehandle;
  44     ompio_file_t * metadatafilehandle;
  45     mca_sharedfp_individual_header_record* headnode = NULL;
  46     struct mca_sharedfp_base_data_t* sh;
  47     size_t len=0;
  48 
  49     sh = (struct mca_sharedfp_base_data_t*) malloc ( sizeof(struct mca_sharedfp_base_data_t));
  50     if ( NULL == sh ){
  51         opal_output(0, "mca_sharedfp_individual_file_open: Error, unable to malloc "
  52                     "f_sharedfp_ptr struct\n");
  53         return OMPI_ERR_OUT_OF_RESOURCE;
  54     }
  55 
  56 
  57     /*Populate the sh file structure based on the implementation*/
  58     sh->global_offset = 0;                      /* Global Offset*/
  59     sh->selected_module_data = NULL;
  60 
  61     /* Assign the metadatalinked list to sh->handle */
  62     sh->selected_module_data = (mca_sharedfp_individual_header_record *)mca_sharedfp_individual_insert_headnode();
  63 
  64     /*--------------------------------------------------------*/
  65     /* Open an individual data file for each process          */
  66     /* NOTE: Open the data file without shared file pointer   */
  67     /*--------------------------------------------------------*/
  68     if ( mca_sharedfp_individual_verbose ) {
  69        opal_output(ompi_sharedfp_base_framework.framework_output,
  70                 "mca_sharedfp_individual_file_open: open data file.\n");
  71     }
  72 
  73     /* data filename created by appending .data.$rank to the original filename*/
  74     len = strlen (filename ) + 64;
  75     datafilename = (char*)malloc( len );
  76     if ( NULL == datafilename ) {
  77         opal_output(0, "mca_sharedfp_individual_file_open: unable to allocate memory\n");
  78         free ( sh );
  79         return OMPI_ERR_OUT_OF_RESOURCE;
  80     }
  81     snprintf(datafilename, len, "%s%s%d",filename,".data.",fh->f_rank);
  82 
  83 
  84     datafilehandle = (ompio_file_t *)malloc(sizeof(ompio_file_t));
  85     if ( NULL == datafilehandle ) {
  86         opal_output(0, "mca_sharedfp_individual_file_open: unable to allocate memory\n");
  87         free ( sh );
  88         free ( datafilename );
  89         return OMPI_ERR_OUT_OF_RESOURCE;
  90     }
  91     err = mca_common_ompio_file_open(MPI_COMM_SELF, datafilename,
  92                                      MPI_MODE_RDWR | MPI_MODE_CREATE | MPI_MODE_DELETE_ON_CLOSE,
  93                                      &(MPI_INFO_NULL->super), datafilehandle, false);
  94     if ( OMPI_SUCCESS != err) {
  95         opal_output(0, "mca_sharedfp_individual_file_open: Error during datafile file open\n");
  96         free (sh);
  97         free (datafilename);
  98         free (datafilehandle);
  99         return err;
 100     }
 101 
 102     /*----------------------------------------------------------*/
 103     /* Open an individual metadata file for each of the process */
 104     /* NOTE: Open the meta file without shared file pointer     */
 105     /*----------------------------------------------------------*/
 106     if ( mca_sharedfp_individual_verbose ) {
 107         opal_output(ompi_sharedfp_base_framework.framework_output,
 108                 "mca_sharedfp_individual_file_open: metadata file.\n");
 109     }
 110 
 111     /* metadata filename created by appending .metadata.$rank to the original filename*/
 112     metadatafilename = (char*) malloc ( len );
 113     if ( NULL == metadatafilename ) {
 114         free (sh);
 115         free (datafilename);
 116         free (datafilehandle);
 117         opal_output(0, "mca_sharedfp_individual_file_open: Error during memory allocation\n");
 118         return OMPI_ERR_OUT_OF_RESOURCE;
 119     }
 120     snprintf ( metadatafilename, len, "%s%s%d", filename, ".metadata.",fh->f_rank);
 121 
 122     metadatafilehandle = (ompio_file_t *)malloc(sizeof(ompio_file_t));
 123     if ( NULL == metadatafilehandle ) {
 124         free (sh);
 125         free (datafilename);
 126         free (datafilehandle);
 127         free (metadatafilename);
 128         opal_output(0, "mca_sharedfp_individual_file_open: Error during memory allocation\n");
 129         return OMPI_ERR_OUT_OF_RESOURCE;
 130     }
 131     err = mca_common_ompio_file_open ( MPI_COMM_SELF,metadatafilename,
 132                                        MPI_MODE_RDWR | MPI_MODE_CREATE | MPI_MODE_DELETE_ON_CLOSE,
 133                                        &(MPI_INFO_NULL->super), metadatafilehandle, false);
 134     if ( OMPI_SUCCESS != err) {
 135         opal_output(0, "mca_sharedfp_individual_file_open: Error during metadatafile file open\n");
 136         free (sh);
 137         free (datafilename);
 138         free (datafilehandle);
 139         free (metadatafilename);
 140         free (metadatafilehandle);
 141         return err;
 142     }
 143 
 144     /*save the datafilehandle and metadatahandle in the sharedfp individual module data structure*/
 145     headnode = (mca_sharedfp_individual_header_record*)(sh->selected_module_data);
 146     if ( headnode) {
 147         headnode->datafilehandle     = datafilehandle;
 148         headnode->metadatafilehandle = metadatafilehandle;
 149         headnode->datafilename       = datafilename;
 150         headnode->metadatafilename   = metadatafilename;
 151     }
 152 
 153     /*save the sharedfp individual module data structure in the ompio filehandle structure*/
 154     fh->f_sharedfp_data = sh;
 155 
 156     return err;
 157 }
 158 
 159 int mca_sharedfp_individual_file_close (ompio_file_t *fh)
 160 {
 161     mca_sharedfp_individual_header_record* headnode = NULL;
 162     struct mca_sharedfp_base_data_t *sh;
 163     int err = OMPI_SUCCESS;
 164 
 165     if ( NULL == fh->f_sharedfp_data ){
 166         return OMPI_SUCCESS;
 167     }
 168     sh = fh->f_sharedfp_data;
 169 
 170     /* Merge data from individal files to final output file */
 171     err = mca_sharedfp_individual_collaborate_data (sh, fh);
 172 
 173     headnode = (mca_sharedfp_individual_header_record*)(sh->selected_module_data);
 174     if (headnode)  {
 175         /*Close datafile*/
 176         if (headnode->datafilehandle)  {
 177             /*TODO: properly deal with returned error code*/
 178             err = mca_common_ompio_file_close(headnode->datafilehandle);
 179             /* NOTE: No neeed to manually delete the file,
 180             ** the amode should have been set to delete on close
 181             */
 182         }
 183         if(headnode->datafilename){
 184             free(headnode->datafilename);
 185         }
 186 
 187         /*Close metadatafile*/
 188         if (headnode->metadatafilehandle)  {
 189             /*TODO: properly deal with returned error code*/
 190             err = mca_common_ompio_file_close(headnode->metadatafilehandle);
 191             /* NOTE: No neeed to manually delete the file,
 192             ** the amode should have been set to delete on close
 193             */
 194         }
 195         if(headnode->metadatafilename){
 196             free(headnode->metadatafilename);
 197         }
 198     }
 199 
 200     /*free shared file pointer data struct*/
 201     free(sh);
 202     fh->f_sharedfp_data=NULL;
 203 
 204     return err;
 205 }
 206 
 207 
 208 mca_sharedfp_individual_header_record* mca_sharedfp_individual_insert_headnode ( void )
 209 {
 210     mca_sharedfp_individual_header_record *headnode = NULL;
 211     if ( !headnode )  {
 212         /*Allocate a headnode*/
 213         headnode = (mca_sharedfp_individual_header_record*)malloc(sizeof(mca_sharedfp_individual_header_record));
 214         if (!headnode)
 215             return NULL;
 216     }
 217 
 218     headnode->numofrecords = 0;                 /* No records in the linked list */
 219     headnode->numofrecordsonfile = 0;           /* No records in the metadatafile for this file */
 220 
 221     headnode->datafile_offset = 0;
 222     headnode->metadatafile_offset = 0;
 223 
 224     headnode->metafile_start_offset = 0;
 225     headnode->datafile_start_offset = 0;
 226 
 227     headnode->metadatafilehandle = 0;
 228     headnode->datafilehandle     = 0;
 229     headnode->next   = NULL;
 230 
 231     return headnode;
 232 }

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