1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  *   Copyright (C) 1997 University of Chicago.
4  *   See COPYRIGHT notice in top-level directory.
5  */
6 
7 #include "mpioimpl.h"
8 
9 #ifdef HAVE_WEAK_SYMBOLS
10 
11 #if defined(HAVE_PRAGMA_WEAK)
12 #pragma weak MPI_File_read = PMPI_File_read
13 #elif defined(HAVE_PRAGMA_HP_SEC_DEF)
14 #pragma _HP_SECONDARY_DEF PMPI_File_read MPI_File_read
15 #elif defined(HAVE_PRAGMA_CRI_DUP)
16 #pragma _CRI duplicate MPI_File_read as PMPI_File_read
17 /* end of weak pragmas */
18 #elif defined(HAVE_WEAK_ATTRIBUTE)
19 int MPI_File_read(MPI_File fh, void *buf, int count, MPI_Datatype datatype, MPI_Status *status)
20     __attribute__((weak,alias("PMPI_File_read")));
21 #endif
22 
23 /* Include mapping from MPI->PMPI */
24 #define MPIO_BUILD_PROFILING
25 #include "mpioprof.h"
26 #endif
27 
28 /* status object not filled currently */
29 
30 /*@
31     MPI_File_read - Read using individual file pointer
32 
33 Input Parameters:
34 . fh - file handle (handle)
35 . count - number of elements in buffer (nonnegative integer)
36 . datatype - datatype of each buffer element (handle)
37 
38 Output Parameters:
39 . buf - initial address of buffer (choice)
40 . status - status object (Status)
41 
42 .N fortran
43 @*/
MPI_File_read(MPI_File fh,void * buf,int count,MPI_Datatype datatype,MPI_Status * status)44 int MPI_File_read(MPI_File fh, void *buf, int count,
45                   MPI_Datatype datatype, MPI_Status *status)
46 {
47     int error_code;
48     static char myname[] = "MPI_FILE_READ";
49 #ifdef MPI_hpux
50     int fl_xmpi;
51 
52     HPMP_IO_START(fl_xmpi, BLKMPIFILEREAD, TRDTBLOCK, fh, datatype, count);
53 #endif /* MPI_hpux */
54 
55     error_code = MPIOI_File_read(fh, (MPI_Offset) 0, ADIO_INDIVIDUAL, buf,
56 				 count, datatype, myname, status);
57 
58 #ifdef MPI_hpux
59     HPMP_IO_END(fl_xmpi, fh, datatype, count);
60 #endif /* MPI_hpux */
61 
62     return error_code;
63 }
64 
65 /* prevent multiple definitions of this routine */
66 #ifdef MPIO_BUILD_PROFILING
MPIOI_File_read(MPI_File fh,MPI_Offset offset,int file_ptr_type,void * buf,int count,MPI_Datatype datatype,char * myname,MPI_Status * status)67 int MPIOI_File_read(MPI_File fh,
68 		    MPI_Offset offset,
69 		    int file_ptr_type,
70 		    void *buf,
71 		    int count,
72 		    MPI_Datatype datatype,
73 		    char *myname,
74 		    MPI_Status *status)
75 {
76     int error_code, buftype_is_contig, filetype_is_contig;
77     MPI_Count datatype_size;
78     ADIO_File adio_fh;
79     ADIO_Offset off, bufsize;
80     void *xbuf=NULL, *e32_buf=NULL;
81 
82     MPIU_THREAD_CS_ENTER(ALLFUNC,);
83 
84     adio_fh = MPIO_File_resolve(fh);
85 
86     /* --BEGIN ERROR HANDLING-- */
87     MPIO_CHECK_FILE_HANDLE(adio_fh, myname, error_code);
88     MPIO_CHECK_COUNT(adio_fh, count, myname, error_code);
89     MPIO_CHECK_DATATYPE(adio_fh, datatype, myname, error_code);
90 
91     if (file_ptr_type == ADIO_EXPLICIT_OFFSET && offset < 0)
92     {
93 	error_code = MPIO_Err_create_code(MPI_SUCCESS, MPIR_ERR_RECOVERABLE,
94 					  myname, __LINE__, MPI_ERR_ARG,
95 					  "**iobadoffset", 0);
96 	error_code = MPIO_Err_return_file(adio_fh, error_code);
97 	goto fn_exit;
98     }
99     /* --END ERROR HANDLING-- */
100 
101     MPI_Type_size_x(datatype, &datatype_size);
102 
103     /* --BEGIN ERROR HANDLING-- */
104     MPIO_CHECK_COUNT_SIZE(adio_fh, count, datatype_size, myname, error_code);
105     /* --END ERROR HANDLING-- */
106 
107     if (count*datatype_size == 0)
108     {
109 #ifdef HAVE_STATUS_SET_BYTES
110        MPIR_Status_set_bytes(status, datatype, 0);
111 #endif
112 	error_code = MPI_SUCCESS;
113 	goto fn_exit;
114     }
115 
116     /* --BEGIN ERROR HANDLING-- */
117     MPIO_CHECK_INTEGRAL_ETYPE(adio_fh, count, datatype_size, myname, error_code);
118     MPIO_CHECK_READABLE(adio_fh, myname, error_code);
119     MPIO_CHECK_NOT_SEQUENTIAL_MODE(adio_fh, myname, error_code);
120     /* --END ERROR HANDLING-- */
121 
122     ADIOI_Datatype_iscontig(datatype, &buftype_is_contig);
123     ADIOI_Datatype_iscontig(adio_fh->filetype, &filetype_is_contig);
124 
125     ADIOI_TEST_DEFERRED(adio_fh, myname, &error_code);
126 
127     xbuf = buf;
128     if (adio_fh->is_external32)
129     {
130         MPI_Aint e32_size = 0;
131         error_code = MPIU_datatype_full_size(datatype, &e32_size);
132         if (error_code != MPI_SUCCESS)
133             goto fn_exit;
134 
135         e32_buf = ADIOI_Malloc(e32_size*count);
136 	xbuf = e32_buf;
137     }
138 
139     if (buftype_is_contig && filetype_is_contig)
140     {
141     /* convert count and offset to bytes */
142 	bufsize = datatype_size * count;
143 	if (file_ptr_type == ADIO_EXPLICIT_OFFSET) {
144 	    off = adio_fh->disp + adio_fh->etype_size * offset;
145 	}
146 	else /* ADIO_INDIVIDUAL */ {
147 	    off = adio_fh->fp_ind;
148 	}
149 
150         /* if atomic mode requested, lock (exclusive) the region, because
151            there could be a concurrent noncontiguous request.
152 	 */
153         if ((adio_fh->atomicity) && ADIO_Feature(adio_fh, ADIO_LOCKS)) {
154             ADIOI_WRITE_LOCK(adio_fh, off, SEEK_SET, bufsize);
155 	}
156 
157 	ADIO_ReadContig(adio_fh, xbuf, count, datatype, file_ptr_type,
158 			off, status, &error_code);
159 
160         if ((adio_fh->atomicity) && ADIO_Feature(adio_fh, ADIO_LOCKS)) {
161             ADIOI_UNLOCK(adio_fh, off, SEEK_SET, bufsize);
162 	}
163     }
164     else
165     {
166 	ADIO_ReadStrided(adio_fh, xbuf, count, datatype, file_ptr_type,
167 			  offset, status, &error_code);
168 	/* For strided and atomic mode, locking is done in ADIO_ReadStrided */
169     }
170 
171     /* --BEGIN ERROR HANDLING-- */
172     if (error_code != MPI_SUCCESS)
173 	error_code = MPIO_Err_return_file(adio_fh, error_code);
174     /* --END ERROR HANDLING-- */
175 
176     if (e32_buf != NULL) {
177         error_code = MPIU_read_external32_conversion_fn(buf, datatype,
178                 count, e32_buf);
179 	ADIOI_Free(e32_buf);
180     }
181 
182 fn_exit:
183     MPIU_THREAD_CS_EXIT(ALLFUNC,);
184 
185     return error_code;
186 }
187 #endif
188