File: ad_quobytefs_aio.c

package info (click to toggle)
openmpi 5.0.8-4
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 201,684 kB
  • sloc: ansic: 613,078; makefile: 42,353; sh: 11,194; javascript: 9,244; f90: 7,052; java: 6,404; perl: 5,179; python: 1,859; lex: 740; fortran: 61; cpp: 20; tcl: 12
file content (248 lines) | stat: -rw-r--r-- 8,564 bytes parent folder | download | duplicates (6)
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
/*
 * Copyright (C) by Argonne National Laboratory
 *     See COPYRIGHT in top-level directory
 */


#define READ 0
#define WRITE 1
#define QUOBYTE_CONCURRENT_REQS 8

#include "ad_quobytefs.h"
#include "mpiu_greq.h"

static int ADIOI_QUOBYTEFS_greq_class = 0;
int global_quobyte_io_context;

int ADIOI_QUOBYTEFS_aio_free_fn(void *extra_state);
int ADIOI_QUOBYTEFS_aio_poll_fn(void *extra_state, MPI_Status * status);
int ADIOI_QUOBYTEFS_aio_wait_fn(int count, void **array_of_states, double timeout,
                                MPI_Status * status);

static void quobyte_io_event_finished(void *event, int ret)
{
    struct quobyte_io_event *aio_event = (struct quobyte_io_event *) event;
    aio_event->result = ret;
    if (ret >= 0) {
        aio_event->errorcode = 0;
    } else {
        aio_event->errorcode = EIO;
    }
}

int ADIOI_QUOBYTEFS_aio(ADIO_File fd, void *buf, int count, MPI_Datatype type,
                        ADIO_Offset offset, int wr, MPI_Request * request)
{

    int err = -1;
    static char myname[] = "ADIOI_QUOBYTEFS_aio";
    struct quobyte_iocb *aiocbp = NULL;
    struct quobyte_io_event *quobyte_aio = NULL;
    ADIOI_AIO_Request *aio_req = NULL;
    MPI_Count len, typesize;

    MPI_Type_size_x(type, &typesize);
    len = count * typesize;
    if (global_quobyte_io_context == -1) {
        global_quobyte_io_context = quobyte_aio_setup(QUOBYTE_CONCURRENT_REQS);
    }
    aio_req = (ADIOI_AIO_Request *) ADIOI_Calloc(sizeof(ADIOI_AIO_Request), 1);
    aiocbp = (struct quobyte_iocb *) ADIOI_Calloc(sizeof(struct quobyte_iocb), 1);
    quobyte_aio = (struct quobyte_io_event *) ADIOI_Calloc(sizeof(struct quobyte_io_event), 1);
    if (wr == WRITE) {
        aiocbp->op_code = QB_WRITE;
    } else {
        aiocbp->op_code = QB_READ;
    }
    aiocbp->io_context = global_quobyte_io_context;
    aiocbp->file_handle = fd->file_handle;
    aiocbp->buffer = buf;
    aiocbp->offset = offset;
    aiocbp->length = len;
    quobyte_aio->iocb = aiocbp;
    quobyte_aio->errorcode = EINPROGRESS;
    quobyte_aio->result = -1;
    aio_req->qaiocbp = quobyte_aio;

    err =
        quobyte_aio_submit_with_callback(global_quobyte_io_context, aiocbp,
                                         quobyte_io_event_finished, quobyte_aio);
    if (err == -1) {
        ADIOI_Free(aio_req);
        ADIOI_Free(aiocbp);
        return MPIO_Err_create_code(MPI_SUCCESS, MPIR_ERR_FATAL, myname,
                                    __LINE__, MPI_ERR_IO,
                                    "Quobyte failed to submit aio context", 0);
    }

    if (ADIOI_QUOBYTEFS_greq_class == 0) {
        MPIX_Grequest_class_create(ADIOI_GEN_aio_query_fn,
                                   ADIOI_QUOBYTEFS_aio_free_fn, MPIU_Greq_cancel_fn,
                                   ADIOI_QUOBYTEFS_aio_poll_fn, ADIOI_QUOBYTEFS_aio_wait_fn,
                                   &ADIOI_QUOBYTEFS_greq_class);
    }
    MPIX_Grequest_class_allocate(ADIOI_QUOBYTEFS_greq_class, aio_req, request);
    memcpy(&(aio_req->req), request, sizeof(MPI_Request));
    return 0;
}

void ADIOI_QUOBYTEFS_IreadContig(ADIO_File fd, void *buf, int count,
                                 MPI_Datatype datatype, int file_ptr_type,
                                 ADIO_Offset offset, MPI_Request * request, int *error_code)
{
    MPI_Count len, typesize;
    int aio_errno = 0;
    static char myname[] = "ADIOI_QUOBYTEFS_IREADCONTIG";

    MPI_Type_size_x(datatype, &typesize);
    len = count * typesize;

    if (file_ptr_type == ADIO_INDIVIDUAL)
        offset = fd->fp_ind;
    aio_errno = ADIOI_QUOBYTEFS_aio(fd, buf, count, datatype, offset, READ, request);


    /* --BEGIN ERROR HANDLING-- */
    if (aio_errno != 0) {
        MPIO_ERR_CREATE_CODE_ERRNO(myname, aio_errno, error_code);
        return;
    }
    /* --END ERROR HANDLING-- */

    if (file_ptr_type == ADIO_INDIVIDUAL)
        fd->fp_ind += len;

    fd->fp_sys_posn = -1;
    *error_code = MPI_SUCCESS;
}

void ADIOI_QUOBYTEFS_IwriteContig(ADIO_File fd, const void *buf, int count,
                                  MPI_Datatype datatype, int file_ptr_type,
                                  ADIO_Offset offset, ADIO_Request * request, int *error_code)
{
    MPI_Count len, typesize;
    int aio_errno = 0;
    static char myname[] = "ADIOI_QUOBYTEFS_IWRITECONTIG";

    MPI_Type_size_x(datatype, &typesize);
    len = count * typesize;

    if (file_ptr_type == ADIO_INDIVIDUAL)
        offset = fd->fp_ind;
    /* Cast away the const'ness of 'buf' as ADIOI_GEN_aio is used for
     * both read and write calls */
    aio_errno = ADIOI_QUOBYTEFS_aio(fd, (char *) buf, count, datatype, offset, WRITE, request);

    /* --BEGIN ERROR HANDLING-- */
    if (aio_errno != 0) {
        MPIO_ERR_CREATE_CODE_ERRNO(myname, aio_errno, error_code);
        return;
    }
    /* --END ERROR HANDLING-- */

    if (file_ptr_type == ADIO_INDIVIDUAL)
        fd->fp_ind += len;

    fd->fp_sys_posn = -1;

    *error_code = MPI_SUCCESS;
}


int ADIOI_QUOBYTEFS_aio_free_fn(void *extra_state)
{
    ADIOI_AIO_Request *aio_req;
    aio_req = (ADIOI_AIO_Request *) extra_state;

    if (aio_req != NULL && aio_req->qaiocbp != NULL && aio_req->qaiocbp->iocb != NULL) {

        ADIOI_Free(aio_req->qaiocbp->iocb);
        ADIOI_Free(aio_req->qaiocbp);
        ADIOI_Free(aio_req);
    } else {
        return MPIO_Err_create_code(MPI_SUCCESS, MPIR_ERR_FATAL,
                                    "ADIOI_QUOBYTEFS_aio_free_fn",
                                    __LINE__, MPI_ERR_IO, "Quobyte aio destruction failed", 0);
    }

    return MPI_SUCCESS;
}

int ADIOI_QUOBYTEFS_aio_poll_fn(void *extra_state, MPI_Status * status)
{
    ADIOI_AIO_Request *aio_req;
    int errcode = MPI_SUCCESS;

    aio_req = (ADIOI_AIO_Request *) extra_state;

    if (aio_req != NULL && aio_req->qaiocbp != NULL) {
        if (aio_req->qaiocbp->errorcode == 0) {
            aio_req->nbytes = aio_req->qaiocbp->result;
            errcode = MPI_Grequest_complete(aio_req->req);
            if (errcode != MPI_SUCCESS) {
                errcode = MPIO_Err_create_code(MPI_SUCCESS,
                                               MPIR_ERR_RECOVERABLE,
                                               "ADIOI_QUOYBTEFS_aio_poll_fn", __LINE__,
                                               MPI_ERR_IO, "**mpi_grequest_complete", 0);
            }
        } else {
            if (aio_req->qaiocbp->errorcode == EIO) {
                errcode = MPIO_Err_create_code(MPI_SUCCESS,
                                               MPIR_ERR_RECOVERABLE,
                                               "ADIOI_QUOYBTEFS_aio_poll_fn", __LINE__,
                                               MPI_ERR_IO, "Quobyte aio failed", 0);
            }
        }
    }
    return errcode;
}

/* wait for multiple requests to complete */
int ADIOI_QUOBYTEFS_aio_wait_fn(int count, void **array_of_states, double timeout,
                                MPI_Status * status)
{
    ADIOI_AIO_Request **aio_reqlist;
    struct quobyte_io_event **events =
        (struct quobyte_io_event **) ADIOI_Calloc(sizeof(struct quobyte_io_event *), count);
    int i = 0;
    int errcode = MPI_SUCCESS;
    int num_in_progress = 0;
    aio_reqlist = (ADIOI_AIO_Request **) array_of_states;

    while (i < count && aio_reqlist[i] != NULL) {
        struct quobyte_io_event *current_event = aio_reqlist[i]->qaiocbp;
        if (current_event->errorcode == EINPROGRESS) {
            events[i] = current_event;
            num_in_progress++;
        } else {
            errcode = MPI_Grequest_complete(aio_reqlist[i]->req);
        }
        i++;
    }

    i = 0;

    double start_time = MPI_Wtime();
    int no_timeout = timeout > 0 ? 0 : 1;       /* when timeout is <= 0 the loop will run until all events are done */
    while (num_in_progress > 0 && (no_timeout || MPI_Wtime() - start_time < timeout)) {
        if (events[i] != NULL && events[i]->errorcode != EINPROGRESS) {
            errcode = MPI_Grequest_complete(aio_reqlist[i]->req);
            events[i] = NULL;
            num_in_progress--;
        }
        if (i >= count) {
            i = 0;
        } else {
            i++;
        }
    }
    ADIOI_Free(events);

    if (errcode != MPI_SUCCESS) {
        errcode = MPIO_Err_create_code(MPI_SUCCESS,
                                       MPIR_ERR_RECOVERABLE,
                                       "ADIOI_QUOBYTEFS_aio_wait_fn",
                                       __LINE__, MPI_ERR_IO, "**mpi_grequest_complete", 0);
    }
    return errcode;
}