File: sharedfp_individual_file_open.c

package info (click to toggle)
openmpi 5.0.7-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 202,312 kB
  • sloc: ansic: 612,441; makefile: 42,495; sh: 11,230; javascript: 9,244; f90: 7,052; java: 6,404; perl: 5,154; python: 1,856; lex: 740; fortran: 61; cpp: 20; tcl: 12
file content (257 lines) | stat: -rw-r--r-- 9,932 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
 * Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
 *                         University Research and Technology
 *                         Corporation.  All rights reserved.
 * Copyright (c) 2004-2005 The University of Tennessee and The University
 *                         of Tennessee Research Foundation.  All rights
 *                         reserved.
 * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
 *                         University of Stuttgart.  All rights reserved.
 * Copyright (c) 2004-2005 The Regents of the University of California.
 *                         All rights reserved.
 * Copyright (c) 2013-2019 University of Houston. All rights reserved.
 * Copyright (c) 2015-2018 Research Organization for Information Science
 *                         and Technology (RIST). All rights reserved.
 * Copyright (c) 2016-2017 IBM Corporation. All rights reserved.
 * $COPYRIGHT$
 *
 * Additional copyrights may follow
 *
 * $HEADER$
 */


#include "ompi_config.h"
#include "sharedfp_individual.h"

#include "mpi.h"
#include "ompi/constants.h"
#include "ompi/mca/sharedfp/sharedfp.h"
#include "ompi/mca/sharedfp/base/base.h"
#include "ompi/file/file.h"


int mca_sharedfp_individual_file_open (struct ompi_communicator_t *comm,
				       const char* filename,
				       int amode,
				       struct opal_info_t *info,
				       ompio_file_t *fh)
{
    int err = 0;
    char * datafilename;	/*The array size would change as it is based on the current path*/
    char * metadatafilename;	/*The array size would change as it is based on the current path*/
    ompio_file_t * datafilehandle;
    ompio_file_t * metadatafilehandle;
    mca_sharedfp_individual_header_record* headnode = NULL;
    struct mca_sharedfp_base_data_t* sh;
    size_t len=0;

    sh = (struct mca_sharedfp_base_data_t*) malloc ( sizeof(struct mca_sharedfp_base_data_t));
    if ( NULL == sh ){
        opal_output(0, "mca_sharedfp_individual_file_open: Error, unable to malloc "
		    "f_sharedfp_ptr struct\n");
        return OMPI_ERR_OUT_OF_RESOURCE;
    }


    /*Populate the sh file structure based on the implementation*/
    sh->global_offset = 0;			/* Global Offset*/
    sh->selected_module_data = NULL;

    /* Assign the metadatalinked list to sh->handle */
    sh->selected_module_data = (mca_sharedfp_individual_header_record *)mca_sharedfp_individual_insert_headnode();

    /*--------------------------------------------------------*/
    /* Open an individual data file for each process          */
    /* NOTE: Open the data file without shared file pointer   */
    /*--------------------------------------------------------*/
    if ( mca_sharedfp_individual_verbose ) {
       opal_output(ompi_sharedfp_base_framework.framework_output,
                "mca_sharedfp_individual_file_open: open data file.\n");
    }

    /* data filename created by appending .data.$rank to the original filename*/
    len = strlen (filename ) + 64;
    datafilename = (char*)malloc( len );
    if ( NULL == datafilename ) {
        opal_output(0, "mca_sharedfp_individual_file_open: unable to allocate memory\n");
        free ( sh );
        return OMPI_ERR_OUT_OF_RESOURCE;
    }
    snprintf(datafilename, len, "%s%s%d",filename,".data.",fh->f_rank);


    datafilehandle = (ompio_file_t *)malloc(sizeof(ompio_file_t));
    if ( NULL == datafilehandle ) {
        opal_output(0, "mca_sharedfp_individual_file_open: unable to allocate memory\n");
        free ( sh );
        free ( datafilename );
        return OMPI_ERR_OUT_OF_RESOURCE;
    }
    err = mca_common_ompio_file_open(MPI_COMM_SELF, datafilename,
                                     MPI_MODE_RDWR | MPI_MODE_CREATE | MPI_MODE_DELETE_ON_CLOSE,
                                     &(MPI_INFO_NULL->super), datafilehandle, false);
    if ( OMPI_SUCCESS != err) {
       opal_output(ompi_sharedfp_base_framework.framework_output,
                   "mca_sharedfp_individual_file_open: Error during datafile file open. Continuing anyway. \n");
        free (sh);
	free (datafilename);
        free (datafilehandle);

        // We reset the error code here to OMPI_SUCCESS since the individual component can act as
        // a dummy component, in case no sharedfp operations are used by the code. Invoking any write/read
        // operations will however lead to an error, since the sharedfp_data pointer will be NULL.
        sh = NULL;
        err = OMPI_SUCCESS;
        goto exit;
    }

    /*----------------------------------------------------------*/
    /* Open an individual metadata file for each of the process */
    /* NOTE: Open the meta file without shared file pointer     */
    /*----------------------------------------------------------*/
    if ( mca_sharedfp_individual_verbose ) {
        opal_output(ompi_sharedfp_base_framework.framework_output,
                "mca_sharedfp_individual_file_open: metadata file.\n");
    }

    /* metadata filename created by appending .metadata.$rank to the original filename*/
    metadatafilename = (char*) malloc ( len );
    if ( NULL == metadatafilename ) {
        free (sh);
	free (datafilename);
        mca_common_ompio_file_close ( datafilehandle);
        free (datafilehandle);
        opal_output(0, "mca_sharedfp_individual_file_open: Error during memory allocation\n");

        sh=NULL;
        err = OMPI_ERR_OUT_OF_RESOURCE;
        goto exit;
    }
    snprintf ( metadatafilename, len, "%s%s%d", filename, ".metadata.",fh->f_rank);

    metadatafilehandle = (ompio_file_t *)malloc(sizeof(ompio_file_t));
    if ( NULL == metadatafilehandle ) {
        free (sh);
        free (datafilename);
        mca_common_ompio_file_close ( datafilehandle);
        free (datafilehandle);
        free (metadatafilename);
        opal_output(0, "mca_sharedfp_individual_file_open: Error during memory allocation\n");

        sh = NULL;
        err = OMPI_ERR_OUT_OF_RESOURCE;
        goto exit;
    }
    err = mca_common_ompio_file_open ( MPI_COMM_SELF,metadatafilename,
                                       MPI_MODE_RDWR | MPI_MODE_CREATE | MPI_MODE_DELETE_ON_CLOSE,
                                       &(MPI_INFO_NULL->super), metadatafilehandle, false);
    if ( OMPI_SUCCESS != err) {
       opal_output(ompi_sharedfp_base_framework.framework_output,
                   "mca_sharedfp_individual_file_open: Error during metadatafile file open. Continuing anyway. \n");
        free (sh);
        free (datafilename);
        mca_common_ompio_file_close ( datafilehandle);
        free (datafilehandle);
        free (metadatafilename);
        free (metadatafilehandle);

        // We reset the error code here to OMPI_SUCCESS since the individual component can act as
        // a dummy component, in case no sharedfp operations are used by the code. Invoking any write/read
        // operations will however lead to an error, since the sharedfp_data pointer will be NULL.
        sh = NULL;
        err = OMPI_SUCCESS;
        goto exit;
    }

    /*save the datafilehandle and metadatahandle in the sharedfp individual module data structure*/
    headnode = (mca_sharedfp_individual_header_record*)(sh->selected_module_data);
    if ( headnode) {
        headnode->datafilehandle     = datafilehandle;
        headnode->metadatafilehandle = metadatafilehandle;
        headnode->datafilename       = datafilename;
        headnode->metadatafilename   = metadatafilename;
    }


exit:
    /*save the sharedfp individual module data structure in the ompio filehandle structure*/
    fh->f_sharedfp_data = sh;

    return err;
}

int mca_sharedfp_individual_file_close (ompio_file_t *fh)
{
    mca_sharedfp_individual_header_record* headnode = NULL;
    struct mca_sharedfp_base_data_t *sh;
    int err = OMPI_SUCCESS;

    if ( NULL == fh->f_sharedfp_data ){
        return OMPI_SUCCESS;
    }
    sh = fh->f_sharedfp_data;

    /* Merge data from individual files to final output file */
    err = mca_sharedfp_individual_collaborate_data (sh, fh);

    headnode = (mca_sharedfp_individual_header_record*)(sh->selected_module_data);
    if (headnode)  {
        /*Close datafile*/
        if (headnode->datafilehandle)  {
            /*TODO: properly deal with returned error code*/
            err = mca_common_ompio_file_close(headnode->datafilehandle);
            /* NOTE: No need to manually delete the file,
	    ** the amode should have been set to delete on close
	    */
        }
        if(headnode->datafilename){
            free(headnode->datafilename);
        }

        /*Close metadatafile*/
        if (headnode->metadatafilehandle)  {
            /*TODO: properly deal with returned error code*/
            err = mca_common_ompio_file_close(headnode->metadatafilehandle);
            /* NOTE: No need to manually delete the file,
	    ** the amode should have been set to delete on close
	    */
        }
        if(headnode->metadatafilename){
            free(headnode->metadatafilename);
        }
    }

    /*free shared file pointer data struct*/
    free(sh);
    fh->f_sharedfp_data=NULL;

    return err;
}


mca_sharedfp_individual_header_record* mca_sharedfp_individual_insert_headnode ( void )
{
    mca_sharedfp_individual_header_record *headnode = NULL;
    if ( !headnode )  {
	/*Allocate a headnode*/
	headnode = (mca_sharedfp_individual_header_record*)malloc(sizeof(mca_sharedfp_individual_header_record));
	if (!headnode)
	    return NULL;
    }

    headnode->numofrecords = 0;			/* No records in the linked list */
    headnode->numofrecordsonfile = 0;		/* No records in the metadatafile for this file */

    headnode->datafile_offset = 0;
    headnode->metadatafile_offset = 0;

    headnode->metafile_start_offset = 0;
    headnode->datafile_start_offset = 0;

    headnode->metadatafilehandle = 0;
    headnode->datafilehandle     = 0;
    headnode->next   = NULL;

    return headnode;
}