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-2011 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) 2008-2015 University of Houston. All rights reserved.
13  * Copyright (c) 2015      Research Organization for Information Science
14  *                         and Technology (RIST). All rights reserved.
15  * $COPYRIGHT$
16  *
17  * Additional copyrights may follow
18  *
19  * $HEADER$
20  */
21 
22 
23 #include "ompi_config.h"
24 #include "fbtl_posix.h"
25 
26 #include <unistd.h>
27 #include <sys/uio.h>
28 #if HAVE_AIO_H
29 #include <aio.h>
30 #endif
31 
32 #include "mpi.h"
33 #include "ompi/constants.h"
34 #include "ompi/mca/fbtl/fbtl.h"
35 
mca_fbtl_posix_ipreadv(mca_io_ompio_file_t * fh,ompi_request_t * request)36 ssize_t mca_fbtl_posix_ipreadv (mca_io_ompio_file_t *fh,
37 			       ompi_request_t *request)
38 {
39 #if defined (FBTL_POSIX_HAVE_AIO)
40     mca_fbtl_posix_request_data_t *data;
41     mca_ompio_request_t *req = (mca_ompio_request_t *) request;
42     int i=0, ret;
43     off_t start_offset, end_offset, total_length;
44 
45     data = (mca_fbtl_posix_request_data_t *) malloc ( sizeof (mca_fbtl_posix_request_data_t));
46     if ( NULL == data ) {
47         opal_output (1,"could not allocate memory\n");
48         return 0;
49     }
50 
51     data->aio_req_count = fh->f_num_of_io_entries;
52     data->aio_open_reqs = fh->f_num_of_io_entries;
53     data->aio_req_type  = FBTL_POSIX_READ;
54     data->aio_req_chunks = fbtl_posix_max_aio_active_reqs;
55     data->aio_total_len = 0;
56     data->aio_reqs = (struct aiocb *) malloc (sizeof(struct aiocb) *
57                                               fh->f_num_of_io_entries);
58     if (NULL == data->aio_reqs) {
59         opal_output(1, "OUT OF MEMORY\n");
60         free(data);
61         return 0;
62     }
63 
64     data->aio_req_status = (int *) malloc (sizeof(int) * fh->f_num_of_io_entries);
65     if (NULL == data->aio_req_status) {
66         opal_output(1, "OUT OF MEMORY\n");
67         free(data->aio_reqs);
68         free(data);
69         return 0;
70     }
71     data->aio_fh = fh;
72 
73     for ( i=0; i<fh->f_num_of_io_entries; i++ ) {
74         data->aio_reqs[i].aio_offset  = (OMPI_MPI_OFFSET_TYPE)(intptr_t)
75             fh->f_io_array[i].offset;
76         data->aio_reqs[i].aio_buf     = fh->f_io_array[i].memory_address;
77         data->aio_reqs[i].aio_nbytes  = fh->f_io_array[i].length;
78         data->aio_reqs[i].aio_fildes  = fh->fd;
79         data->aio_reqs[i].aio_reqprio = 0;
80         data->aio_reqs[i].aio_sigevent.sigev_notify = SIGEV_NONE;
81 	data->aio_req_status[i]        = EINPROGRESS;
82     }
83 
84     data->aio_first_active_req = 0;
85     if ( data->aio_req_count > data->aio_req_chunks ) {
86 	data->aio_last_active_req = data->aio_req_chunks;
87     }
88     else {
89 	data->aio_last_active_req = data->aio_req_count;
90     }
91 
92     start_offset = data->aio_reqs[data->aio_first_active_req].aio_offset;
93     end_offset   = data->aio_reqs[data->aio_last_active_req-1].aio_offset + data->aio_reqs[data->aio_last_active_req-1].aio_nbytes;
94     total_length = (end_offset - start_offset);
95     ret = mca_fbtl_posix_lock( &data->aio_lock, data->aio_fh, F_RDLCK, start_offset, total_length, OMPIO_LOCK_ENTIRE_REGION );
96     if ( 0 < ret ) {
97         opal_output(1, "mca_fbtl_posix_ipreadv: error in mca_fbtl_posix_lock() error ret=%d  %s", ret, strerror(errno));
98         mca_fbtl_posix_unlock ( &data->aio_lock, data->aio_fh );
99         free(data->aio_reqs);
100         free(data->aio_req_status);
101         free(data);
102         return OMPI_ERROR;
103     }
104 
105     for (i=0; i < data->aio_last_active_req; i++) {
106         if (-1 == aio_read(&data->aio_reqs[i])) {
107             opal_output(1, "mca_fbtl_posix_ipreadv: error in aio_read(): %s", strerror(errno));
108             mca_fbtl_posix_unlock ( &data->aio_lock, data->aio_fh );
109             free(data->aio_reqs);
110             free(data->aio_req_status);
111             free(data);
112             return OMPI_ERROR;
113         }
114     }
115 
116     req->req_data = data;
117     req->req_progress_fn = mca_fbtl_posix_progress;
118     req->req_free_fn     = mca_fbtl_posix_request_free;
119 #endif
120     return OMPI_SUCCESS;
121 }
122