darshan-mpiio.c 24.9 KB
Newer Older
Philip Carns's avatar
Philip Carns committed
1
/*
Shane Snyder's avatar
Shane Snyder committed
2 3 4
 * Copyright (C) 2015 University of Chicago.
 * See COPYRIGHT notice in top-level directory.
 *
Philip Carns's avatar
Philip Carns committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 */

#include "darshan-runtime-config.h"
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdarg.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/uio.h>
#include <sys/mman.h>
#include <search.h>
#include <assert.h>
#include <libgen.h>
#include <aio.h>
#define __USE_GNU
#include <pthread.h>

#include "uthash.h"
28

Philip Carns's avatar
Philip Carns committed
29 30
#include "darshan.h"
#include "darshan-mpiio-log-format.h"
Shane Snyder's avatar
Shane Snyder committed
31
#include "darshan-dynamic.h"
Philip Carns's avatar
Philip Carns committed
32

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
/* The mpiio_file_runtime structure maintains necessary runtime metadata
 * for the MPIIO file record (darshan_mpiio_file structure, defined in
 * darshan-mpiio-log-format.h) pointed to by 'file_record'. This metadata
 * assists with the instrumenting of specific statistics in the file record.
 * 'hlink' is a hash table link structure used to add/remove this record
 * from the hash table of MPIIO file records for this process. 
 *
 * RATIONALE: the MPIIO module needs to track some stateful, volatile 
 * information about each open file (like the current file offset, most recent 
 * access time, etc.) to aid in instrumentation, but this information can't be
 * stored in the darshan_mpiio_file struct because we don't want it to appear in
 * the final darshan log file.  We therefore associate a mpiio_file_runtime
 * struct with each darshan_mpiio_file struct in order to track this information.
  *
 * NOTE: There is a one-to-one mapping of mpiio_file_runtime structs to
 * darshan_mpiio_file structs.
 *
 * NOTE: The mpiio_file_runtime struct contains a pointer to a darshan_mpiio_file
 * struct (see the *file_record member) rather than simply embedding an entire
 * darshan_mpiio_file struct.  This is done so that all of the darshan_mpiio_file
 * structs can be kept contiguous in memory as a single array to simplify
 * reduction, compression, and storage.
 */
56
struct mpiio_file_runtime
Philip Carns's avatar
Philip Carns committed
57 58
{
    struct darshan_mpiio_file* file_record;
59 60 61
    double last_meta_end;
    double last_read_end;
    double last_write_end;
Philip Carns's avatar
Philip Carns committed
62 63 64
    UT_hash_handle hlink;
};

65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
/* The mpiio_file_runtime_ref structure is used to associate a MPIIO
 * file handle with an already existing MPIIO file record. This is
 * necessary as many MPIIO I/O functions take only a file handle as input,
 * but MPIIO file records are indexed by their full file paths (i.e., darshan
 * record identifiers for MPIIO files are created by hashing the file path).
 * In other words, this structure is necessary as it allows us to look up a
 * file record either by a pathname (mpiio_file_runtime) or by MPIIO file
 * descriptor (mpiio_file_runtime_ref), depending on which parameters are
 * available. This structure includes another hash table link, since separate
 * hashes are maintained for mpiio_file_runtime structures and mpiio_file_runtime_ref
 * structures.
 *
 * RATIONALE: In theory the file handle information could be included in the
 * mpiio_file_runtime struct rather than in a separate structure here.  The
 * reason we don't do that is to handle the potential for an MPI implementation
 * to produce a new file handle instance each time MPI_File_open() is called on a
 * file.  Thus there might be multiple file handles referring to the same
 * underlying record.
 *
 * NOTE: there are potentially multiple mpiio_file_runtime_ref structures
 * referring to a single mpiio_file_runtime structure.  Most of the time there is
 * only one, however.
 */
88
struct mpiio_file_runtime_ref
Philip Carns's avatar
Philip Carns committed
89
{
90
    struct mpiio_file_runtime* file;
91
    MPI_File fh;
Philip Carns's avatar
Philip Carns committed
92 93 94
    UT_hash_handle hlink;
};

95 96 97 98
/* The mpiio_runtime structure maintains necessary state for storing
 * MPI-IO file records and for coordinating with darshan-core at 
 * shutdown time.
 */
Philip Carns's avatar
Philip Carns committed
99 100
struct mpiio_runtime
{
101
    struct mpiio_file_runtime* file_runtime_array;
Philip Carns's avatar
Philip Carns committed
102 103 104
    struct darshan_mpiio_file* file_record_array;
    int file_array_size;
    int file_array_ndx;
105 106
    struct mpiio_file_runtime* file_hash;
    struct mpiio_file_runtime_ref* fh_hash;
Philip Carns's avatar
Philip Carns committed
107 108 109 110 111 112 113 114 115
    void *red_buf;
    int shared_rec_count;
};

static struct mpiio_runtime *mpiio_runtime = NULL;
static pthread_mutex_t mpiio_runtime_mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
static int instrumentation_disabled = 0;
static int my_rank = -1;

Philip Carns's avatar
Philip Carns committed
116
static void mpiio_runtime_initialize(void);
117
static struct mpiio_file_runtime* mpiio_file_by_name(const char *name);
118 119 120 121 122
static struct mpiio_file_runtime* mpiio_file_by_name_setfh(const char* name, MPI_File fh);
static struct mpiio_file_runtime* mpiio_file_by_fh(MPI_File fh);
static void mpiio_file_close_fh(MPI_File fh);
static int mpiio_file_compare(const void* a, const void* b);

123
static void mpiio_begin_shutdown(void);
124 125
static void mpiio_setup_reduction(darshan_record_id *shared_recs, int *shared_rec_count,
    void **send_buf, void **recv_buf, int *rec_size);
Philip Carns's avatar
Philip Carns committed
126 127
static void mpiio_record_reduction_op(void* infile_v, void* inoutfile_v,
    int *len, MPI_Datatype *datatype);
128 129 130 131 132 133
static void mpiio_get_output_data(void **buffer, int *size);
static void mpiio_shutdown(void);

#define MPIIO_LOCK() pthread_mutex_lock(&mpiio_runtime_mutex)
#define MPIIO_UNLOCK() pthread_mutex_unlock(&mpiio_runtime_mutex)

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
#if 0
/* Some old versions of MPI don't provide all of these COMBINER definitions.  
 * If any are missing then we define them to an arbitrary value just to 
 * prevent compile errors in DATATYPE_INC().
 */
#ifndef MPI_COMBINER_NAMED
    #define MPI_COMBINER_NAMED MPIIO_COMBINER_NAMED
#endif
#ifndef MPI_COMBINER_DUP
    #define MPI_COMBINER_DUP MPIIO_COMBINER_DUP
#endif
#ifndef MPI_COMBINER_CONTIGUOUS
    #define MPI_COMBINER_CONTIGUOUS MPIIO_COMBINER_CONTIGUOUS
#endif
#ifndef MPI_COMBINER_VECTOR
    #define MPI_COMBINER_VECTOR MPIIO_COMBINER_VECTOR
#endif
#ifndef MPI_COMBINER_HVECTOR_INTEGER
    #define MPI_COMBINER_HVECTOR_INTEGER MPIIO_COMBINER_HVECTOR_INTEGER
#endif
#ifndef MPI_COMBINER_HVECTOR
    #define MPI_COMBINER_HVECTOR MPIIO_COMBINER_HVECTOR
#endif
#ifndef MPI_COMBINER_INDEXED
    #define MPI_COMBINER_INDEXED MPIIO_COMBINER_INDEXED
#endif
#ifndef MPI_COMBINER_HINDEXED_INTEGER
    #define MPI_COMBINER_HINDEXED_INTEGER MPIIO_COMBINER_HINDEXED_INTEGER
#endif
#ifndef MPI_COMBINER_HINDEXED
    #define MPI_COMBINER_HINDEXED MPIIO_COMBINER_HINDEXED
#endif
#ifndef MPI_COMBINER_INDEXED_BLOCK
    #define MPI_COMBINER_INDEXED_BLOCK MPIIO_COMBINER_INDEXED_BLOCK
#endif
#ifndef MPI_COMBINER_STRUCT_INTEGER
    #define MPI_COMBINER_STRUCT_INTEGER MPIIO_COMBINER_STRUCT_INTEGER
#endif
#ifndef MPI_COMBINER_STRUCT
    #define MPI_COMBINER_STRUCT MPIIO_COMBINER_STRUCT
#endif
#ifndef MPI_COMBINER_SUBARRAY
    #define MPI_COMBINER_SUBARRAY MPIIO_COMBINER_SUBARRAY
#endif
#ifndef MPI_COMBINER_DARRAY
    #define MPI_COMBINER_DARRAY MPIIO_COMBINER_DARRAY
#endif
#ifndef MPI_COMBINER_F90_REAL
    #define MPI_COMBINER_F90_REAL MPIIO_COMBINER_F90_REAL
#endif
#ifndef MPI_COMBINER_F90_COMPLEX
    #define MPI_COMBINER_F90_COMPLEX MPIIO_COMBINER_F90_COMPLEX
#endif
#ifndef MPI_COMBINER_F90_INTEGER
    #define MPI_COMBINER_F90_INTEGER MPIIO_COMBINER_F90_INTEGER
#endif
#ifndef MPI_COMBINER_RESIZED
    #define MPI_COMBINER_RESIZED MPIIO_COMBINER_RESIZED
#endif
#endif

#define MPIIO_DATATYPE_INC(__file, __datatype) do {\
    int num_integers, num_addresses, num_datatypes, combiner, ret; \
    struct darshan_mpiio_file* rec = (__file)->file_record; \
    ret = DARSHAN_MPI_CALL(PMPI_Type_get_envelope)(__datatype, &num_integers, \
        &num_addresses, &num_datatypes, &combiner); \
    if(ret == MPI_SUCCESS) { \
        switch(combiner) { \
            case MPI_COMBINER_NAMED:\
                DARSHAN_COUNTER_INC(rec,MPIIO_COMBINER_NAMED,1); break; \
            case MPI_COMBINER_DUP:\
                DARSHAN_COUNTER_INC(rec,MPIIO_COMBINER_DUP,1); break; \
            case MPI_COMBINER_CONTIGUOUS:\
                DARSHAN_COUNTER_INC(rec,MPIIO_COMBINER_CONTIGUOUS,1); break; \
            case MPI_COMBINER_VECTOR:\
                DARSHAN_COUNTER_INC(rec,MPIIO_COMBINER_VECTOR,1); break; \
            case MPI_COMBINER_HVECTOR_INTEGER:\
                DARSHAN_COUNTER_INC(rec,MPIIO_COMBINER_HVECTOR_INTEGER,1); break; \
            case MPI_COMBINER_HVECTOR:\
                DARSHAN_COUNTER_INC(rec,MPIIO_COMBINER_HVECTOR,1); break; \
            case MPI_COMBINER_INDEXED:\
                DARSHAN_COUNTER_INC(rec,MPIIO_COMBINER_INDEXED,1); break; \
            case MPI_COMBINER_HINDEXED_INTEGER:\
                DARSHAN_COUNTER_INC(rec,MPIIO_COMBINER_HINDEXED_INTEGER,1); break; \
            case MPI_COMBINER_HINDEXED:\
                DARSHAN_COUNTER_INC(rec,MPIIO_COMBINER_HINDEXED,1); break; \
            case MPI_COMBINER_INDEXED_BLOCK:\
                DARSHAN_COUNTER_INC(rec,MPIIO_COMBINER_INDEXED_BLOCK,1); break; \
            case MPI_COMBINER_STRUCT_INTEGER:\
                DARSHAN_COUNTER_INC(rec,MPIIO_COMBINER_STRUCT_INTEGER,1); break; \
            case MPI_COMBINER_STRUCT:\
                DARSHAN_COUNTER_INC(rec,MPIIO_COMBINER_STRUCT,1); break; \
            case MPI_COMBINER_SUBARRAY:\
                DARSHAN_COUNTER_INC(rec,MPIIO_COMBINER_SUBARRAY,1); break; \
            case MPI_COMBINER_DARRAY:\
                DARSHAN_COUNTER_INC(rec,MPIIO_COMBINER_DARRAY,1); break; \
            case MPI_COMBINER_F90_REAL:\
                DARSHAN_COUNTER_INC(rec,MPIIO_COMBINER_F90_REAL,1); break; \
            case MPI_COMBINER_F90_COMPLEX:\
                DARSHAN_COUNTER_INC(rec,MPIIO_COMBINER_F90_COMPLEX,1); break; \
            case MPI_COMBINER_F90_INTEGER:\
                DARSHAN_COUNTER_INC(rec,MPIIO_COMBINER_F90_INTEGER,1); break; \
            case MPI_COMBINER_RESIZED:\
                DARSHAN_COUNTER_INC(rec,MPIIO_COMBINER_RESIZED,1); break; \
        } \
    } \
} while(0)

242 243 244
/**********************************************************
 *        Wrappers for MPI-IO functions of interest       * 
 **********************************************************/
Philip Carns's avatar
Philip Carns committed
245

Philip Carns's avatar
Philip Carns committed
246 247 248 249 250 251 252
#ifdef HAVE_MPIIO_CONST
int MPI_File_open(MPI_Comm comm, const char *filename, int amode, MPI_Info info, MPI_File *fh) 
#else
int MPI_File_open(MPI_Comm comm, char *filename, int amode, MPI_Info info, MPI_File *fh) 
#endif
{
    int ret;
253
    struct mpiio_file_runtime* file;
Philip Carns's avatar
Philip Carns committed
254 255 256 257 258 259 260 261 262 263 264
    char* tmp;
    int comm_size;
    double tm1, tm2;

    tm1 = darshan_core_wtime();
    ret = DARSHAN_MPI_CALL(PMPI_File_open)(comm, filename, amode, info, fh);
    tm2 = darshan_core_wtime();

    if(ret == MPI_SUCCESS)
    {
        MPIIO_LOCK();
Philip Carns's avatar
Philip Carns committed
265
        mpiio_runtime_initialize();
Philip Carns's avatar
Philip Carns committed
266 267 268 269 270 271 272 273 274 275 276

        /* use ROMIO approach to strip prefix if present */
        /* strip off prefix if there is one, but only skip prefixes
         * if they are greater than length one to allow for windows
         * drive specifications (e.g. c:\...) 
         */
        tmp = strchr(filename, ':');
        if (tmp > filename + 1) {
            filename = tmp + 1;
        }

277
        file = mpiio_file_by_name_setfh(filename, (*fh));
Philip Carns's avatar
Philip Carns committed
278 279
        if(file)
        {
280
            file->file_record->rank = my_rank;
Philip Carns's avatar
Philip Carns committed
281 282 283
            DARSHAN_MPI_CALL(PMPI_Comm_size)(comm, &comm_size);
            if(comm_size == 1)
            {
284
                DARSHAN_COUNTER_INC(file->file_record, MPIIO_INDEP_OPENS, 1);
Philip Carns's avatar
Philip Carns committed
285 286 287
            }
            else
            {
288
                DARSHAN_COUNTER_INC(file->file_record, MPIIO_COLL_OPENS, 1);
Philip Carns's avatar
Philip Carns committed
289 290 291
            }
            if(info != MPI_INFO_NULL)
            {
292
                DARSHAN_COUNTER_INC(file->file_record, MPIIO_HINTS, 1);
Philip Carns's avatar
Philip Carns committed
293
            }
294 295 296
            if(DARSHAN_COUNTER_F_VALUE(file->file_record, MPIIO_F_OPEN_TIMESTAMP) == 0)
                DARSHAN_COUNTER_F_SET(file->file_record, MPIIO_F_OPEN_TIMESTAMP, tm1);
            DARSHAN_COUNTER_F_INC_NO_OVERLAP(file->file_record, tm1, tm2,
297
                file->last_meta_end, MPIIO_F_META_TIME);
Philip Carns's avatar
Philip Carns committed
298 299 300 301 302 303 304 305
        }

        MPIIO_UNLOCK();
    }

    return(ret);
}

306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
/* TODO: reads and writes */

int MPI_File_sync(MPI_File fh)
{
    int ret;
    struct mpiio_file_runtime* file;
    double tm1, tm2;

    tm1 = darshan_core_wtime();
    ret = DARSHAN_MPI_CALL(PMPI_File_sync)(fh);
    tm2 = darshan_core_wtime();

    if(ret == MPI_SUCCESS)
    {
        MPIIO_LOCK();
        mpiio_runtime_initialize();
        file = mpiio_file_by_fh(fh);
        if(file)
        {
            DARSHAN_COUNTER_INC(file->file_record, MPIIO_SYNCS, 1);
            DARSHAN_COUNTER_F_INC_NO_OVERLAP(file->file_record, tm1, tm2,
                file->last_write_end, MPIIO_F_WRITE_TIME);
        }
        MPIIO_UNLOCK();
    }

    return(ret);
}

/* TODO: test */
#ifdef HAVE_MPIIO_CONST
int MPI_File_set_view(MPI_File fh, MPI_Offset disp, MPI_Datatype etype,
    MPI_Datatype filetype, const char *datarep, MPI_Info info)
#else
int MPI_File_set_view(MPI_File fh, MPI_Offset disp, MPI_Datatype etype,
    MPI_Datatype filetype, char *datarep, MPI_Info info)
#endif
{
    int ret;
    struct mpiio_file_runtime* file;
    double tm1, tm2;

    tm1 = darshan_core_wtime();
    ret = DARSHAN_MPI_CALL(PMPI_File_set_view)(fh, disp, etype, filetype,
        datarep, info);
    tm2 = darshan_core_wtime();

    if(ret == MPI_SUCCESS)
    {
        int num_integers, num_addresses, num_datatypes, combiner;
        DARSHAN_MPI_CALL(PMPI_Type_get_envelope)(filetype, &num_integers,
            &num_addresses, &num_datatypes, &combiner);

        MPIIO_LOCK();
        mpiio_runtime_initialize();
        file = mpiio_file_by_fh(fh);
        if(file)
        {
            DARSHAN_COUNTER_INC(file->file_record, MPIIO_VIEWS, 1);
            if(info != MPI_INFO_NULL)
            {
                DARSHAN_COUNTER_INC(file->file_record, MPIIO_HINTS, 1);
                DARSHAN_COUNTER_F_INC_NO_OVERLAP(file->file_record, tm1, tm2,
                    file->last_meta_end, MPIIO_F_META_TIME);
           }
           MPIIO_DATATYPE_INC(file, filetype);
        }
        MPIIO_UNLOCK();
    }

    return(ret);
}

379 380 381 382
int MPI_File_close(MPI_File *fh)
{
    int ret;
    struct mpiio_file_runtime* file;
383
    MPI_File tmp_fh = *fh;
384 385 386 387 388 389 390 391
    double tm1, tm2;

    tm1 = darshan_core_wtime();
    ret = DARSHAN_MPI_CALL(PMPI_File_close)(fh);
    tm2 = darshan_core_wtime();

    MPIIO_LOCK();
    mpiio_runtime_initialize();
392
    file = mpiio_file_by_fh(tmp_fh);
393 394 395 396 397
    if(file)
    {
        DARSHAN_COUNTER_F_SET(file->file_record, MPIIO_F_CLOSE_TIMESTAMP,
            darshan_core_wtime());
        DARSHAN_COUNTER_F_INC_NO_OVERLAP(file->file_record, tm1, tm2,
398 399
            file->last_meta_end, MPIIO_F_META_TIME);
        mpiio_file_close_fh(tmp_fh);
400 401 402 403 404 405 406 407 408 409 410
    }
    MPIIO_UNLOCK();

    return(ret);
}

/***********************************************************
 * Internal functions for manipulating MPI-IO module state *
 ***********************************************************/

/* initialize data structures and register with darshan-core component */
Philip Carns's avatar
Philip Carns committed
411 412 413 414 415
static void mpiio_runtime_initialize()
{
    int mem_limit;
    struct darshan_module_funcs mpiio_mod_fns =
    {
416 417
        .begin_shutdown = &mpiio_begin_shutdown,
        .setup_reduction = &mpiio_setup_reduction,
Philip Carns's avatar
Philip Carns committed
418
        .record_reduction_op = &mpiio_record_reduction_op,
Philip Carns's avatar
Philip Carns committed
419 420 421 422 423 424 425 426 427 428 429 430
        .get_output_data = &mpiio_get_output_data,
        .shutdown = &mpiio_shutdown
    };

    /* don't do anything if already initialized or instrumenation is disabled */
    if(mpiio_runtime || instrumentation_disabled)
        return;

    /* register the mpiio module with darshan core */
    darshan_core_register_module(
        DARSHAN_MPIIO_MOD,
        &mpiio_mod_fns,
431 432
        &mem_limit,
        NULL);
Philip Carns's avatar
Philip Carns committed
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449

    /* return if no memory assigned by darshan core */
    if(mem_limit == 0)
        return;

    mpiio_runtime = malloc(sizeof(*mpiio_runtime));
    if(!mpiio_runtime)
        return;
    memset(mpiio_runtime, 0, sizeof(*mpiio_runtime));

    /* set maximum number of file records according to max memory limit */
    /* NOTE: maximum number of records is based on the size of a mpiio file record */
    mpiio_runtime->file_array_size = mem_limit / sizeof(struct darshan_mpiio_file);
    mpiio_runtime->file_array_ndx = 0;

    /* allocate array of runtime file records */
    mpiio_runtime->file_runtime_array = malloc(mpiio_runtime->file_array_size *
450
                                               sizeof(struct mpiio_file_runtime));
Philip Carns's avatar
Philip Carns committed
451 452 453 454 455 456 457 458
    mpiio_runtime->file_record_array = malloc(mpiio_runtime->file_array_size *
                                              sizeof(struct darshan_mpiio_file));
    if(!mpiio_runtime->file_runtime_array || !mpiio_runtime->file_record_array)
    {
        mpiio_runtime->file_array_size = 0;
        return;
    }
    memset(mpiio_runtime->file_runtime_array, 0, mpiio_runtime->file_array_size *
459
           sizeof(struct mpiio_file_runtime));
Philip Carns's avatar
Philip Carns committed
460 461 462 463 464 465 466 467 468
    memset(mpiio_runtime->file_record_array, 0, mpiio_runtime->file_array_size *
           sizeof(struct darshan_mpiio_file));

    /* TODO: can we move this out of here? perhaps register_module returns rank? */
    DARSHAN_MPI_CALL(PMPI_Comm_rank)(MPI_COMM_WORLD, &my_rank);

    return;
}

469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
/* get a MPIIO file record for the given file path */
static struct mpiio_file_runtime* mpiio_file_by_name(const char *name)
{
    struct mpiio_file_runtime *file = NULL;
    char *newname = NULL;
    darshan_record_id file_id;

    if(!mpiio_runtime || instrumentation_disabled)
        return(NULL);

    newname = darshan_clean_file_path(name);
    if(!newname)
        newname = (char*)name;

    /* get a unique id for this file from darshan core */
    darshan_core_register_record(
        (void*)newname,
        strlen(newname),
        1,
        DARSHAN_MPIIO_MOD,
489 490
        &file_id,
        NULL);
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521

    /* search the hash table for this file record, and return if found */
    HASH_FIND(hlink, mpiio_runtime->file_hash, &file_id, sizeof(darshan_record_id), file);
    if(file)
    {
        if(newname != name)
            free(newname);
        return(file);
    }

    if(mpiio_runtime->file_array_ndx < mpiio_runtime->file_array_size);
    {
        /* no existing record, assign a new file record from the global array */
        file = &(mpiio_runtime->file_runtime_array[mpiio_runtime->file_array_ndx]);
        file->file_record = &(mpiio_runtime->file_record_array[mpiio_runtime->file_array_ndx]);
        file->file_record->f_id = file_id;

        /* add new record to file hash table */
        HASH_ADD(hlink, mpiio_runtime->file_hash, file_record->f_id, sizeof(darshan_record_id), file);

        mpiio_runtime->file_array_ndx++;
    }

    if(newname != name)
        free(newname);
    return(file);
}

/* get an MPIIO file record for the given file path, and also create a
 * reference structure using the corresponding file handle
 */
522
static struct mpiio_file_runtime* mpiio_file_by_name_setfh(const char* name, MPI_File fh)
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
{
    struct mpiio_file_runtime* file;
    struct mpiio_file_runtime_ref* ref;

    if(!mpiio_runtime || instrumentation_disabled)
        return(NULL);

    /* find file record by name first */
    file = mpiio_file_by_name(name);

    if(!file)
        return(NULL);

    /* search hash table for existing file ref for this fh */
    HASH_FIND(hlink, mpiio_runtime->fh_hash, &fh, sizeof(fh), ref);
    if(ref)
    {
        /* we have a reference.  Make sure it points to the correct file
         * and return it
         */
        ref->file = file;
        return(file);
    }

    /* if we hit this point, then we don't have a reference for this fh
     * in the table yet.  Add it.
     */
    ref = malloc(sizeof(*ref));
    if(!ref)
        return(NULL);
    memset(ref, 0, sizeof(*ref));

    ref->file = file;
    ref->fh = fh;    
    HASH_ADD(hlink, mpiio_runtime->fh_hash, fh, sizeof(fh), ref);

    return(file);
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
}

/* get an MPIIO file record for the given file handle */
static struct mpiio_file_runtime* mpiio_file_by_fh(MPI_File fh)
{
    struct mpiio_file_runtime_ref* ref;

    if(!mpiio_runtime || instrumentation_disabled)
        return(NULL);

    /* search hash table for existing file ref for this file handle */
    HASH_FIND(hlink, mpiio_runtime->fh_hash, &fh, sizeof(fh), ref);
    if(ref)
        return(ref->file);

    return(NULL);
}

/* free up reference data structures for the given file handle */
static void mpiio_file_close_fh(MPI_File fh)
{
    struct mpiio_file_runtime_ref* ref;

    if(!mpiio_runtime || instrumentation_disabled)
        return;

    /* search hash table for this fd */
    HASH_FIND(hlink, mpiio_runtime->fh_hash, &fh, sizeof(fh), ref);
    if(ref)
    {
        /* we have a reference, delete it */
        HASH_DELETE(hlink, mpiio_runtime->fh_hash, ref);
        free(ref);
    }

    return;
596 597
}

598 599
/* compare function for sorting file records by descending rank */
static int mpiio_file_compare(const void* a_p, const void* b_p)
Philip Carns's avatar
Philip Carns committed
600
{
601 602
    const struct darshan_mpiio_file* a = a_p;
    const struct darshan_mpiio_file* b = b_p;
Philip Carns's avatar
Philip Carns committed
603

604 605 606 607
    if(a->rank < b->rank)
        return 1;
    if(a->rank > b->rank)
        return -1;
Philip Carns's avatar
Philip Carns committed
608

609 610
    return 0;
}
Philip Carns's avatar
Philip Carns committed
611

612 613 614
/**************************************************************************
 * Functions exported by MPI-IO module for coordinating with darshan-core *
 **************************************************************************/
Philip Carns's avatar
Philip Carns committed
615

616 617 618
static void mpiio_begin_shutdown()
{
    assert(mpiio_runtime);
Philip Carns's avatar
Philip Carns committed
619

620 621 622
    MPIIO_LOCK();
    instrumentation_disabled = 1;
    MPIIO_UNLOCK();
Philip Carns's avatar
Philip Carns committed
623 624 625 626

    return;
}

627
static void mpiio_setup_reduction(
Philip Carns's avatar
Philip Carns committed
628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677
    darshan_record_id *shared_recs,
    int *shared_rec_count,
    void **send_buf,
    void **recv_buf,
    int *rec_size)
{
    struct mpiio_file_runtime *file;
    int i;

    assert(mpiio_runtime);

    /* necessary initialization of shared records (e.g., change rank to -1) */
    for(i = 0; i < *shared_rec_count; i++)
    {
        HASH_FIND(hlink, mpiio_runtime->file_hash, &shared_recs[i],
            sizeof(darshan_record_id), file);
        assert(file);

        file->file_record->rank = -1;
    }

    /* sort the array of files descending by rank so that we get all of the 
     * shared files (marked by rank -1) in a contiguous portion at end 
     * of the array
     */
    qsort(mpiio_runtime->file_record_array, mpiio_runtime->file_array_ndx,
        sizeof(struct darshan_mpiio_file), mpiio_file_compare);

    /* make *send_buf point to the shared files at the end of sorted array */
    *send_buf =
        &(mpiio_runtime->file_record_array[mpiio_runtime->file_array_ndx-(*shared_rec_count)]);

    /* allocate memory for the reduction output on rank 0 */
    if(my_rank == 0)
    {
        *recv_buf = malloc(*shared_rec_count * sizeof(struct darshan_mpiio_file));
        if(!(*recv_buf))
            return;
    }

    *rec_size = sizeof(struct darshan_mpiio_file);

    /* TODO: cleaner way to do this? */
    if(my_rank == 0)
        mpiio_runtime->red_buf = *recv_buf;
    mpiio_runtime->shared_rec_count = *shared_rec_count;

    return;
}

678 679 680 681 682
static void mpiio_record_reduction_op(
    void* infile_v,
    void* inoutfile_v,
    int *len,
    MPI_Datatype *datatype)
Philip Carns's avatar
Philip Carns committed
683
{
684 685 686 687
    struct darshan_mpiio_file tmp_file;
    struct darshan_mpiio_file *infile = infile_v;
    struct darshan_mpiio_file *inoutfile = inoutfile_v;
    int i, j;
Philip Carns's avatar
Philip Carns committed
688

689
    assert(mpiio_runtime);
Philip Carns's avatar
Philip Carns committed
690

691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770
    for(i=0; i<*len; i++)
    {
        memset(&tmp_file, 0, sizeof(struct darshan_mpiio_file));

        tmp_file.f_id = infile->f_id;
        tmp_file.rank = -1;

        /* sum */
        for(j=MPIIO_INDEP_OPENS; j<=MPIIO_HINTS; j++)
        {
            tmp_file.counters[j] = infile->counters[j] + inoutfile->counters[j];
        }

        /* sum (floating point) */
        for(j=MPIIO_F_META_TIME; j<=MPIIO_F_META_TIME; j++)
        {
            tmp_file.fcounters[j] = infile->fcounters[j] + inoutfile->fcounters[j];
        }

        /* min non-zero (if available) value */
        for(j=MPIIO_F_OPEN_TIMESTAMP; j<=MPIIO_F_OPEN_TIMESTAMP; j++)
        {
            if(infile->fcounters[j] > inoutfile->fcounters[j] && inoutfile->fcounters[j] > 0)
                tmp_file.fcounters[j] = inoutfile->fcounters[j];
            else
                tmp_file.fcounters[j] = infile->fcounters[j];
        }

        /* update pointers */
        *inoutfile = tmp_file;
        inoutfile++;
        infile++;
    }

    return;
}

static void mpiio_get_output_data(
    void **buffer,
    int *size)
{
    assert(mpiio_runtime);

    /* TODO: clean up reduction stuff */
    if(my_rank == 0)
    {
        int tmp_ndx = mpiio_runtime->file_array_ndx - mpiio_runtime->shared_rec_count;
        memcpy(&(mpiio_runtime->file_record_array[tmp_ndx]), mpiio_runtime->red_buf,
            mpiio_runtime->shared_rec_count * sizeof(struct darshan_mpiio_file));
        free(mpiio_runtime->red_buf);
    }
    else
    {
        mpiio_runtime->file_array_ndx -= mpiio_runtime->shared_rec_count;
    }

    *buffer = (void *)(mpiio_runtime->file_record_array);
    *size = mpiio_runtime->file_array_ndx * sizeof(struct darshan_mpiio_file);

    return;
}

static void mpiio_shutdown()
{
    struct mpiio_file_runtime_ref *ref, *tmp;

    HASH_ITER(hlink, mpiio_runtime->fh_hash, ref, tmp)
    {
        HASH_DELETE(hlink, mpiio_runtime->fh_hash, ref);
        free(ref);
    }

    HASH_CLEAR(hlink, mpiio_runtime->file_hash); /* these entries are freed all at once below */

    free(mpiio_runtime->file_runtime_array);
    free(mpiio_runtime->file_record_array);
    free(mpiio_runtime);
    mpiio_runtime = NULL;

    return;
Philip Carns's avatar
Philip Carns committed
771 772
}

Philip Carns's avatar
Philip Carns committed
773 774 775 776 777 778 779 780
/*
 * Local variables:
 *  c-indent-level: 4
 *  c-basic-offset: 4
 * End:
 *
 * vim: ts=8 sts=4 sw=4 expandtab
 */