Logo Cineca Logo SCAI

You are here

Exercise 13

 

Write a code that writes and reads a binary file in parallel according to the following steps:

I) First process writes integers 0-9 from the beginning of the file, the second process writes integer 10-19 from the position 10 in the file and so on. Use the individual file pointers. 

II) Re-open the file. Each process reads the data just written by using an explicit offset. Check that the reading has been performed correctly. 

III) Each process writes the data just read in the following way (supponing that there are 4 processors) :

Data per processor:

          

File layout:



NOTE: to see the binary file in ASCII use the command 

             od -i <binary_file>


HINTS:


 

C

MPI_FILE_OPEN

int MPI_File_open(MPI_Comm comm, char *filename, int amode, MPI_Info info, MPI_File *FH)

MPI_FILE_WRITE

int MPI_File_write(MPI_File fh, void *buf, int count, MPI_Datatype datatype, MPI_Status *status)

MPI_FILE_SEEK

int MPI_File_seek(MPI_File fh, MPI_Offset offset, int whence)

MPI_FILE_CLOSE

int MPI_File_close(MPI_File *fh)

MPI_FILE_GET_SIZE

int MPI_File_get_size(MPI_File fh, MPI_Offset *size)

MPI_FILE_READ_AT

MPI_File_read_at(MPI_File fh, MPI_Offset offset, void *buf, int count, MPI_Datatype datatype, MPI_Status *status)

MPI_TYPE_VECTOR

int MPI_Type_vector(int count, int blocklength, int stride, MPI_Datatype oldtype, MPI_Datatype *newtype)

MPI_TYPE_COMMIT

int MPI_Type_commit(MPI_Datatype *datatype)

MPI_TYPE_FREE

 

int MPI_Type_free(MPI_Datatype *datatype)

 

MPI_TYPE_SIZE

 

int MPI_Type_size(MPI_Datatype *datatype, int *size)

 

MPI_FILE_SET_VIEW

int MPI_File_set_view(MPI_File fh, MPI_Offset disp, MPI_Datatype etype, MPI_Datatype filetype, char *datarep, MPI_Info info)

MPI_FILE_WRITE_ALL

int MPI_File_write_all(MPI_File fh, void *buf, int count, MPI_Datatype datatype, MPI_Status *status)

MPI_INIT

int MPI_Init(int *argc, char ***argv)

MPI_COMM_SIZE

int MPI_Comm_size(MPI_Comm comm, int *size)

MPI_COMM_RANK

int MPI_Comm_rank(MPI_Comm comm, int *rank)

MPI_FINALIZE

int MPI_Finalize(void)

 


FORTRAN

MPI_FILE_OPEN

MPI_FILE_OPEN(COMM, FILENAME, AMODE, INFO, FH, IERROR)

CHARACTER*(*) FILENAME

INTEGER COMM, AMODE, INFO, FH, IERROR

MPI_FILE_WRITE

MPI_FILE_WRITE(FH, BUF, COUNT, DATATYPE, STATUS, IERROR)

<type> BUF(*)
INTEGER FH, COUNT, DATATYPE, STATUS(MPI_STATUS_SIZE), IERROR

MPI_FILE_SEEK

MPI_FILE_SEEK(FH, OFFSET, WHENCE, IERROR)

INTEGER(KIND=MPI_OFFSET_KIND) OFFSET
INTEGER FH, WHENCE, IERROR

MPI_FILE_CLOSE

MPI_FILE_CLOSE(FH, IERROR)
INTEGER FH, IERROR

MPI_FILE_GET_SIZE

MPI_FILE_GET_SIZE(FH, SIZE, IERROR)

INTEGER(KIND=MPI_OFFSET_KIND) SIZE
INTEGER FH, IERROR

MPI_FILE_READ_AT

MPI_FILE_READ_AT(FH, OFFSET, BUF, COUNT, DATATYPE, STATUS, IERROR)

<type> BUF(*)

INTEGER(KIND=MPI_OFFSET_KIND) OFFSET
INTEGER FH, COUNT, DATATYPE, STATUS(MPI_STATUS_SIZE), IERROR

MPI_TYPE_VECTOR

MPI_TYPE_VECTOR(COUNT, BLOCKLENGTH, STRIDE, OLDTYPE, NEWTYPE, IERROR)
INTEGER COUNT, BLOCKLENGTH, STRIDE, OLDTYPE, NEWTYPE, IERROR

MPI_TYPE_COMMIT

 MPI_TYPE_COMMIT(DATATYPE, IERROR)
INTEGER DATATYPE, IERROR

MPI_TYPE_FREE

MPI_TYPE_FREE(DATATYPE, IERROR)
INTEGER DATATYPE, IERROR

MPI_TYPE_SIZE

MPI_TYPE_SIZE(DATATYPE, SIZE, IERROR)
INTEGER DATATYPE, SIZE, IERROR

MPI_FILE_SET_VIEW

MPI_FILE_SET_VIEW(FH, DISP, ETYPE, FILETYPE, DATAREP, INFO, IERROR)

CHARACTER*(*) DATAREP

INTEGER(KIND=MPI_OFFSET_KIND) DISP

INTEGER FH, ETYPE, FILETYPE, INFO, IERROR

MPI_FILE_WRITE_ALL

MPI_FILE_WRITE_ALL(FH, BUF, COUNT, DATATYPE, STATUS, IERROR)

<type> BUF(*)

INTEGER FH, COUNT, DATATYPE, STATUS(MPI_STATUS_SIZE), IERROR

MPI_INIT

MPI_INIT(IERROR)
INTEGER IERROR

MPI_COMM_SIZE

MPI_COMM_SIZE(COMM, SIZE, IERROR)
INTEGER COMM, SIZE, IERROR

MPI_COMM_RANK

MPI_COMM_RANK(COMM, RANK, IERROR)
INTEGER COMM, RANK, IERROR

MPI_FINALIZE

MPI_FINALIZE(IERROR)
INTEGER IERROR