darshan-pnetcdf.c 5.47 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 *  (C) 2009 by Argonne National Laboratory.
 *      See COPYRIGHT in top-level directory.
 */

#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include "mpi.h"
#include "darshan.h"
#include "darshan-config.h"

extern int __real_ncmpi_create(MPI_Comm comm, const char *path, 
    int cmode, MPI_Info info, int *ncidp);
extern int __real_ncmpi_open(MPI_Comm comm, const char *path, 
    int omode, MPI_Info info, int *ncidp);
extern int __real_ncmpi_close(int ncid);

19
static struct darshan_file_runtime* darshan_file_by_ncid(int ncid);
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

int __wrap_ncmpi_create(MPI_Comm comm, const char *path, 
    int cmode, MPI_Info info, int *ncidp)
{
    int ret;
    struct darshan_file_runtime* file;
    char* tmp;
    int comm_size;
    int hash_index;

    ret = __real_ncmpi_create(comm, path, cmode, info, ncidp);
    if(ret == 0)
    {  
        CP_LOCK();
        /* 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(path, ':');
        if (tmp > path + 1) {
            path = tmp + 1;
        }

        file = darshan_file_by_name(path);
        /* TODO: handle the case of multiple concurrent opens */
46
        if(file && (file->ncid == -1))
47 48 49
        {
            file->ncid = *ncidp;

50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
            PMPI_Comm_size(comm, &comm_size);
            if(comm_size == 1)
            {
                CP_INC(file, CP_INDEP_NC_OPENS, 1);
            }
            else
            {
                CP_INC(file, CP_COLL_NC_OPENS, 1);
            }

            hash_index = file->ncid & CP_HASH_MASK;
            file->ncid_prev = NULL;
            file->ncid_next = darshan_global_job->ncid_table[hash_index];
            if(file->ncid_next) 
                file->ncid_next->ncid_prev = file;
            darshan_global_job->ncid_table[hash_index] = file;
66 67 68 69 70 71 72
        }
        CP_UNLOCK();
    }

    return(ret);
}

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
int __wrap_ncmpi_open(MPI_Comm comm, const char *path, 
    int omode, MPI_Info info, int *ncidp)
{
    int ret;
    struct darshan_file_runtime* file;
    char* tmp;
    int comm_size;
    int hash_index;

    ret = __real_ncmpi_open(comm, path, omode, info, ncidp);
    if(ret == 0)
    {  
        CP_LOCK();
        /* 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(path, ':');
        if (tmp > path + 1) {
            path = tmp + 1;
        }

        file = darshan_file_by_name(path);
        /* TODO: handle the case of multiple concurrent opens */
        if(file && (file->ncid == -1))
        {
            file->ncid = *ncidp;

102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
            PMPI_Comm_size(comm, &comm_size);
            if(comm_size == 1)
            {
                CP_INC(file, CP_INDEP_NC_OPENS, 1);
            }
            else
            {
                CP_INC(file, CP_COLL_NC_OPENS, 1);
            }

            hash_index = file->ncid & CP_HASH_MASK;
            file->ncid_prev = NULL;
            file->ncid_next = darshan_global_job->ncid_table[hash_index];
            if(file->ncid_next) 
                file->ncid_next->ncid_prev = file;
            darshan_global_job->ncid_table[hash_index] = file;
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
        }
        CP_UNLOCK();
    }

    return(ret);

}

int __wrap_ncmpi_close(int ncid)
{
    struct darshan_file_runtime* file;
    int hash_index;
    int tmp_ncid = ncid;
    int ret;

    ret = __real_ncmpi_close(ncid);

    CP_LOCK();
    file = darshan_file_by_ncid(ncid);
    if(file)
    {
        file->ncid = -1;
        if(file->ncid_prev == NULL)
        {
            /* head of ncid hash table list */
            hash_index = tmp_ncid & CP_HASH_MASK;
            darshan_global_job->ncid_table[hash_index] = file->ncid_next;
            if(file->ncid_next)
                file->ncid_next->ncid_prev = NULL;
        }
        else
        {
            if(file->ncid_prev)
                file->ncid_prev->ncid_next = file->ncid_next;
            if(file->ncid_next)
                file->ncid_next->ncid_prev = file->ncid_prev;
        }
        file->ncid_prev = NULL;
        file->ncid_next = NULL;
        darshan_global_job->darshan_mru_file = file; /* in case we open it again */
    }
    CP_UNLOCK();

    return(ret);

}

static struct darshan_file_runtime* darshan_file_by_ncid(int ncid)
{
    int hash_index;
    struct darshan_file_runtime* tmp_file;

    if(!darshan_global_job)
    {
        return(NULL);
    }

    /* if we have already condensed the data, then just hand the first file
     * back
     */
    if(darshan_global_job->flags & CP_FLAG_CONDENSED)
    {
        return(&darshan_global_job->file_runtime_array[0]);
    }

    /* try mru first */
    if(darshan_global_job->darshan_mru_file && darshan_global_job->darshan_mru_file->ncid == ncid)
    {
        return(darshan_global_job->darshan_mru_file);
    }

    /* search hash table */
    hash_index = ncid & CP_HASH_MASK;
    tmp_file = darshan_global_job->ncid_table[hash_index];
    while(tmp_file)
    {
        if(tmp_file->ncid == ncid)
        {
            darshan_global_job->darshan_mru_file = tmp_file;
            return(tmp_file);
        }
        tmp_file = tmp_file->ncid_next;
    }

    return(NULL);
}


206 207 208 209 210 211 212 213
/*
 * Local variables:
 *  c-indent-level: 4
 *  c-basic-offset: 4
 * End:
 *
 * vim: ts=8 sts=4 sw=4 expandtab
 */