00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #define _XOPEN_SOURCE 600
00012 #include <stdlib.h>
00013 #include <malloc.h>
00014 #include "ad_lustre.h"
00015
00016 #define LUSTRE_MEMALIGN (1<<12)
00017
00018 static void ADIOI_LUSTRE_Aligned_Mem_File_Write(ADIO_File fd, void *buf, int len,
00019 ADIO_Offset offset, int *err);
00020 static void ADIOI_LUSTRE_Aligned_Mem_File_Write(ADIO_File fd, void *buf, int len,
00021 ADIO_Offset offset, int *err)
00022 {
00023 int rem, size, nbytes;
00024 if (!(len % fd->d_miniosz) && (len >= fd->d_miniosz)) {
00025 *err = pwrite(fd->fd_direct, buf, len, offset);
00026 } else if (len < fd->d_miniosz) {
00027 *err = pwrite(fd->fd_sys, buf, len, offset);
00028 } else {
00029 rem = len % fd->d_miniosz;
00030 size = len - rem;
00031 nbytes = pwrite(fd->fd_direct, buf, size, offset);
00032 nbytes += pwrite(fd->fd_sys, ((char *)buf) + size, rem, offset+size);
00033 *err = nbytes;
00034 }
00035 }
00036
00037 static void ADIOI_LUSTRE_Aligned_Mem_File_Read(ADIO_File fd, void *buf, int len,
00038 ADIO_Offset offset, int *err);
00039 static void ADIOI_LUSTRE_Aligned_Mem_File_Read(ADIO_File fd, void *buf, int len,
00040 ADIO_Offset offset, int *err)
00041 {
00042 int rem, size, nbytes;
00043 if (!(len % fd->d_miniosz) && (len >= fd->d_miniosz))
00044 *err = pread(fd->fd_direct, buf, len, offset);
00045 else if (len < fd->d_miniosz)
00046 *err = pread(fd->fd_sys, buf, len, offset);
00047 else {
00048 rem = len % fd->d_miniosz;
00049 size = len - rem;
00050 nbytes = pread(fd->fd_direct, buf, size, offset);
00051 nbytes += pread(fd->fd_sys, ((char *)buf) + size, rem, offset+size);
00052 *err = nbytes;
00053 }
00054 }
00055
00056
00057 static int ADIOI_LUSTRE_Directio(ADIO_File fd, void *buf, int len,
00058 off_t offset, int rw);
00059 static int ADIOI_LUSTRE_Directio(ADIO_File fd, void *buf, int len,
00060 off_t offset, int rw)
00061 {
00062 int err=-1, diff, size=len, nbytes = 0;
00063 void *newbuf;
00064
00065 if (offset % fd->d_miniosz) {
00066 diff = fd->d_miniosz - (offset % fd->d_miniosz);
00067 diff = ADIOI_MIN(diff, len);
00068 if (rw)
00069 nbytes = pwrite(fd->fd_sys, buf, diff, offset);
00070 else
00071 nbytes = pread(fd->fd_sys, buf, diff, offset);
00072 buf = ((char *) buf) + diff;
00073 offset += diff;
00074 size = len - diff;
00075 }
00076
00077 if (!size) {
00078 return diff;
00079 }
00080
00081 if (rw) {
00082 if (!(((long) buf) % fd->d_mem)) {
00083 ADIOI_LUSTRE_Aligned_Mem_File_Write(fd, buf, size, offset, &err);
00084 nbytes += err;
00085 } else {
00086 newbuf = (void *) memalign(LUSTRE_MEMALIGN, size);
00087 if (newbuf) {
00088 memcpy(newbuf, buf, size);
00089 ADIOI_LUSTRE_Aligned_Mem_File_Write(fd, newbuf, size, offset, &err);
00090 nbytes += err;
00091 ADIOI_Free(newbuf);
00092 }
00093 else nbytes += pwrite(fd->fd_sys, buf, size, offset);
00094 }
00095 err = nbytes;
00096 } else {
00097 if (!(((long) buf) % fd->d_mem)) {
00098 ADIOI_LUSTRE_Aligned_Mem_File_Read(fd, buf, size, offset, &err);
00099 nbytes += err;
00100 } else {
00101 newbuf = (void *) memalign(LUSTRE_MEMALIGN, size);
00102 if (newbuf) {
00103 ADIOI_LUSTRE_Aligned_Mem_File_Read(fd, newbuf, size, offset, &err);
00104 if (err > 0) memcpy(buf, newbuf, err);
00105 nbytes += err;
00106 ADIOI_Free(newbuf);
00107 }
00108 else nbytes += pread(fd->fd_sys, buf, size, offset);
00109 }
00110 err = nbytes;
00111 }
00112 return err;
00113 }
00114
00115 static void ADIOI_LUSTRE_IOContig(ADIO_File fd, void *buf, int count,
00116 MPI_Datatype datatype, int file_ptr_type,
00117 ADIO_Offset offset, ADIO_Status *status,
00118 int io_mode, int *error_code);
00119 static void ADIOI_LUSTRE_IOContig(ADIO_File fd, void *buf, int count,
00120 MPI_Datatype datatype, int file_ptr_type,
00121 ADIO_Offset offset, ADIO_Status *status,
00122 int io_mode, int *error_code)
00123 {
00124 int err=-1, datatype_size, len;
00125 static char myname[] = "ADIOI_LUSTRE_IOCONTIG";
00126
00127 MPI_Type_size(datatype, &datatype_size);
00128 len = datatype_size * count;
00129
00130 if (file_ptr_type == ADIO_INDIVIDUAL) {
00131 offset = fd->fp_ind;
00132 }
00133
00134 if (!(fd->direct_read || fd->direct_write)) {
00135 if (fd->fp_sys_posn != offset) {
00136 err = lseek(fd->fd_sys, offset, SEEK_SET);
00137 if (err == -1) goto ioerr;
00138 }
00139
00140 if (io_mode) {
00141 #ifdef ADIOI_MPE_LOGGING
00142 MPE_Log_event(ADIOI_MPE_write_a, 0, NULL);
00143 #endif
00144 err = write(fd->fd_sys, buf, len);
00145 #ifdef ADIOI_MPE_LOGGING
00146 MPE_Log_event(ADIOI_MPE_write_b, 0, NULL);
00147 #endif
00148 } else {
00149 #ifdef ADIOI_MPE_LOGGING
00150 MPE_Log_event(ADIOI_MPE_read_a, 0, NULL);
00151 #endif
00152 err = read(fd->fd_sys, buf, len);
00153 #ifdef ADIOI_MPE_LOGGING
00154 MPE_Log_event(ADIOI_MPE_read_b, 0, NULL);
00155 #endif
00156 }
00157 } else {
00158 err = ADIOI_LUSTRE_Directio(fd, buf, len, offset, io_mode);
00159 }
00160
00161 if (err == -1) goto ioerr;
00162 fd->fp_sys_posn = offset + err;
00163
00164 if (file_ptr_type == ADIO_INDIVIDUAL) {
00165 fd->fp_ind += err;
00166 }
00167
00168 #ifdef HAVE_STATUS_SET_BYTES
00169 if (status) MPIR_Status_set_bytes(status, datatype, err);
00170 #endif
00171 *error_code = MPI_SUCCESS;
00172
00173 ioerr:
00174
00175 if (err == -1) {
00176 *error_code = MPIO_Err_create_code(MPI_SUCCESS,
00177 MPIR_ERR_RECOVERABLE,
00178 myname, __LINE__,
00179 MPI_ERR_IO, "**io",
00180 "**io %s", strerror(errno));
00181 fd->fp_sys_posn = -1;
00182 return;
00183 }
00184
00185 }
00186
00187 void ADIOI_LUSTRE_WriteContig(ADIO_File fd, void *buf, int count,
00188 MPI_Datatype datatype, int file_ptr_type,
00189 ADIO_Offset offset, ADIO_Status *status, int *error_code)
00190 {
00191 ADIOI_LUSTRE_IOContig(fd, buf, count, datatype, file_ptr_type,
00192 offset, status, 1, error_code);
00193 }
00194
00195 void ADIOI_LUSTRE_ReadContig(ADIO_File fd, void *buf, int count,
00196 MPI_Datatype datatype, int file_ptr_type,
00197 ADIO_Offset offset, ADIO_Status *status, int *error_code)
00198 {
00199 ADIOI_LUSTRE_IOContig(fd, buf, count, datatype, file_ptr_type,
00200 offset, status, 0, error_code);
00201 }