Programming Taskbook


E-mail:

Password:

User registration   Restore password

Russian

SFedU

1100 training tasks on programming

©  M. E. Abramyan (Southern Federal University), 1998–2018

 

PT for MPI-2 | Task groups | MPI6File

PrevNext


Parallel file input-output (MPI-2)

Use the char[12] array to store the filename, use the MPI_Bcast function with the MPI_CHAR datatype parameter to send the filename from the master process to the slave processes.

You do not need to set the view of the data in the file by means the MPI_File_set_view function in the initial two subgroups (MPI6File1–MPI6File16); it is enough to use the default view, in which both the elementary datatype and the filetype are of the MPI_BYTE type, the initial displacement is equal to 0 for all processes, and the "native" data representation is used. The same data representation should be specified for the file view in the tasks of the third subgroup (MPI6File17–MPI6File30).

Use the MPI_Type_get_extent function to determine the extent of the MPI_INT and MPI_DOUBLE types.

Use the function MPI_Type_create_resized to specify the additional empty space in tasks devoted to the file view setting. One can use also the zero-size upper-bound marker MPI_UB but this pseudo-datatype is deprecated in the MPI-2 standard.

Local functions for file input-output

MPI6File1. The name of existing file of integers is given in the master process; the amount of file items, which should be read, and their ordinal numbers are given in each slave process (the file items are numbered from 1). File items with some numbers may be missing in the source file. Using the required number of the MPI_File_read_at local function calls in each process, read existing file items with the specified ordinal numbers from the source file and output them in the same order. To check existence of file item with the specified ordinal number, you can either use the MPI_File_get_size function, or analyze the MPI_Status parameter of the MPI_File_read_at function.

MPI6File2. The name of file is given in the master process; the amount of pairs of integers and the pairs themselves are given in each slave process; the first term of the pair is the ordinal number of the file item, the second term of the pair is the value of this file item (the file items are numbered from 1, all ordinal numbers are different and cover range from 1 to some integer). Create a new file of integers with the given name and write the given data to this file using the required number of the MPI_File_write_at local function calls in each slave process.

MPI6File3. The name of existing file of real numbers is given in the master process. The file contains elements of the K × N matrix, where K is the number of slave processes. Using one call of the MPI_File_read_at local function in each slave process, read and output elements of Rth matrix row in the process of rank R (rows are numbered from 1). Use the MPI_File_get_size function to determine the file size.

MPI6File4. The name of file is given in the master process; a sequence of R real numbers is given in each slave process, where R is the process rank. Create a new file of real numbers with the given name and write the given data to this file in ascending order of ranks of processes containing these data. Use one call of the MPI_File_write_at local function in each slave process.

MPI6File5. The name of existing file of integers is given in the master process. The file contains all integers in the range from 1 to K, where K is the maximal rank of process. Read and output two sequences of the file items in each slave process. The first sequence contains the initial part of the file items until the first item whose value is equal to the process rank (including this item); the second sequence contains the final part of the file items and has the same size as the first one. Elements of each sequence should be output in the order they are stored in the file. Use the required number of the MPI_File_read local function calls in each process (without using arrays) and also the MPI_File_get_position function call to determine the size of the first sequence and the MPI_File_seek function call with the MPI_SEEK_END parameter to move the file pointer to the beginning of the second sequence.

MPI6File6. The name of file is given in the master process; an integer is given in each slave process. Create a new file of integers with the given name and write K successive copies of each given integer to this file, where K is the number of slave processes. The order of integers in the file should be the inverse of the order of the slave processes (i. e., K copies of the integer from the process 1 should be written at the end of file, K copies of the integer from the process 2 should be written before them, and so on). Use one call of the MPI_File_write local function in each slave process and also the MPI_File_seek function with the MPI_SEEK_SET parameter.

MPI6File7. The name of existing file of integers is given in the master process. The sum of all file item values is greater than K, where K is the number of slave processes. Read initial file items in each slave process until the sum of their values exceeds the rank of process and output this sum and the amount N of read numbers. After that, in addition, read and output the values of the last N file items in the order they are stored in the file. Use the required number of the MPI_File_read local function calls in each slave process (without using arrays) and also the MPI_File_get_position function call to determine the amount N and the MPI_File_seek function call with the MPI_SEEK_END parameter to move the file pointer to beginning of the group of the last N items.

MPI6File8. The name of file is given in the master process; a real number is given in each slave process. Create a new file of real numbers with the given name and write R successive copies of each given real number to this file, where R is equal to the rank of the process in which this number is given. The order of numbers in the file should be the inverse of the order of the slave processes (i. e., single copy of the real number from the process 1 should be written at the end of file, two copies of the real number from the process 2 should be written before it, and so on). Use one call of the MPI_File_write local function in each slave process and also the MPI_File_seek function with the MPI_SEEK_SET parameter.

Collective functions for file input-output

MPI6File9. The name of existing file of integers is given in the master process. Read and output the R + 1 file items in each process starting with the item with the ordinal number R + 1, where R is the process rank (0, 1, …). File items are numbered from 1; thus, you should read only the initial file item in the process 0, the two following items in the process 1, the three items starting with the third one in the process 2, and so on. If the file does not contain enough items then, in some processes, the number of output items may be less than the required one. Use one call of the MPI_File_read_all collective function call and also the MPI_File_get_size function to determine the file size.

Note. In MPICH2 1.3, the function MPI_File_read_all does not allow to determine the number of actually read file items based on the information contained in the MPI_Status parameter: this parameter always contain the number of items to be read, and if there are not enough items in the file then zero values are appended to the output array.

MPI6File10. The name of file is given in the master process; a sequence of R integers is given in each slave process, where R is the process rank (1, 2, …). Create a new file of integers with the given name and write the given sequences to this file in ascending order of ranks of processes containing these sequences. Use one call of the MPI_File_write_all collective function (for all processes including the process 0) and also the MPI_File_seek function with the MPI_SEEK_SET parameter.

MPI6File11. The name of existing file of real numbers is given in the master process. In addition, an integer is given in each process. This integer is equal to 0 or it is equal to the ordinal number of one of the existing items of the file (the file items are numbered from 1). Using the MPI_Comm_split function, create a new communicator containing only those processes in which a non-zero integer is given. Using the MPI_File_read_at_all collective function for all processes of this communicator, read and output a file item located at a position with the given ordinal number.

MPI6File12. The name of existing file of real numbers is given in the master process. In addition, an integer is given in each process. This integer is equal to 0 or it is equal to the ordinal number of one of the existing items of the file (the file items are numbered from 1). Using the MPI_Comm_split function, create a new communicator containing only those processes in which a non-zero integer is given. Using the MPI_File_write_at_all collective function for all processes of this communicator, replace the value of the file item, that has the given ordinal number, by the value of the process rank in the new communicator (the rank should be converted to a real number).

MPI6File13. The name of existing file of integers is given in the master process. In addition, an integer is given in each process. This integer is equal to 0 or 1. Using the MPI_Comm_split function, create a new communicator containing only those processes in which the number 1 is given. Using the MPI_File_read_ordered collective function for all processes of this communicator, read and output the R + 1 file item, where R is the process rank in the new communicator (items should be read in ascending order: the first item in the process 0, two next items in the process 1, three next items in the process 2, and so on). If the file does not contain enough items, then, in some processes, the number N of output items may be less than required one or even may be equal to zero. In addition, output the number N of actually read items and the new value P of the shared file pointer in each process of the new communicator. Use the MPI_Status parameter of the MPI_File_read_ordered function to determine the number N of actually read items. Use the MPI_File_get_position_shared function to determine the current value of P (this value should be the same in all processes).

MPI6File14. The name of file is given in the master process. In addition, an integer N is given in each process. Create a new file of integers with the given name. Using the MPI_Comm_split function, create a new communicator containing only those processes in which a non-zero integer is given. Using the MPI_File_write_ordered collective function for all processes of the new communicator, write K successive copies of each given integer N to this file, where K is the number of processes in the new communicator. The integers N should be written to the file in the ascending order of ranks of processes containing these integers.

MPI6File15. The name of existing file of integers is given in the master process. The file contains at least K items, where K is the number of processes. Using the MPI_Comm_split function, create a new communicator containing only processes with odd rank (1, 3, …). Using one call of the MPI_File_seek_shared and MPI_File_read_ordered collective functions, read and output two file items at a time in each process of the new communicator: the second and the first item from the end (in this order) should be read and output in the process of the rank 1 in the MPI_COMM_WORLD communicator, the fourth and third item from the end should be read and output in the process of the rank 3, and so on.

Note. To ensure the required order of data reading in the MPI_File_read_ordered function, you should inverse the order of the processes in the created communicator (in comparison with the processes in the MPI_COMM_WORLD communicator).

MPI6File16. The name of file is given in the master process. In addition, an integer N (≥ 0) and N real numbers are given in each process. Create a new file of real numbers with the given name. Using the MPI_Comm_split function, create a new communicator containing only those processes in which a non-zero integer N is given. Using one call of the MPI_File_write_ordered collective function for all processes of the new communicator, write the given real numbers to the file in the inverse order: at first, all the real numbers from the process with the maximal rank in the communicator MPI_COMM_WORLD should be written (in inverse order), after that, all the numbers from the process with the previous rank, and so on.

Note. To ensure the required order of data writing in the MPI_File_write_ordered function, you should inverse the order of the processes in the created communicator (in comparison with the processes in the MPI_COMM_WORLD communicator).

File view setting

MPI6File17. The name of existing file of integers is given in the master process. The file contains 2K items, where K is the number of processes. Using one call of the MPI_File_read_all collective function (and without using the MPI_File_seek function), read and output two file items at a time in each process. The file items should be read and output in the order in which they are stored in a file. To do this, use the MPI_File_set_view function to define a new file view with the MPI_INT elementary datatype, the same filetype, and the appropriate displacement (the displacement will be different for different processes).

MPI6File18. The name of existing file of integers is given in the master process. The file contains elements of the K × 5 matrix, where K is the number of processes. In addition, an integer N (1 ≤ N ≤ 5) is given in each process; this integer determines the ordinal number of a selected element in some matrix row, namely, in the first row for the process 0, in the second row for the process 1, and so on (the row elements are numbered from 1). Using one call of the MPI_File_write_at_all collective function with the second parameter equal to N − 1, replace the value of the selected element in each matrix row by the rank of the corresponding process (the selected element in the first row should be replaced by 0, the selected element in the second row should be replaced by 1, and so on). To do this, use the MPI_File_set_view function to define a new file view with the MPI_INT elementary datatype, the same filetype, and the appropriate displacement (the displacement will be different for different processes).

MPI6File19. The name of existing file of real numbers is given in the master process. The file contains elements of the K × 6 matrix, where K is the number of processes. In addition, an integer N (1 ≤ N ≤ 6) is given in each process; this integer determines the ordinal number of a selected element in some matrix row, namely, in the last row for the process 0, in the last but one row for the process 1, and so on (the row elements are numbered from 1). Using one call of the MPI_File_read_at_all collective function with the second parameter equal to N − 1, read and output the value of the selected row element in the corresponding process (the selected element in the first row should be output in the last process, the selected element in the second row should be output in the last but one process, and so on). To do this, use the MPI_File_set_view function to define a new file view with the MPI_DOUBLE elementary datatype, the same filetype, and the appropriate displacement (the displacement will be different for different processes).

MPI6File20. The name of file is given in the master process. In addition, a sequence of R + 1 real numbers is given in each process, where R is the process rank (0, 1, …). Create a new file of real numbers with the given name. Using one call of the MPI_File_write_all collective function (and without using the MPI_File_seek function), write all given numbers to the file in the order that is inverse to the order in which they are given in processes: at first, the numbers from the last process (in the inverse order) should be written, after that, the numbers from the last but one process (in the inverse order), and so on. To do this, use the MPI_File_set_view function to define a new file view with the MPI_DOUBLE elementary datatype, the same filetype, and the appropriate displacement (the displacement will be different for different processes).

MPI6File21. The name of existing file of integers is given in the master process. The file contains 3K items, where K is the number of processes. Read and output three file items, namely, A, B, C, in each process. These items are located in the given file as follows: A0, A1, …, AK-1, B0, B1, …, BK-1, C0, C1, …, CK-1 (an index indicates the process rank). To do this, use one call of the MPI_File_read_all collective function and a new file view with the MPI_INT elementary datatype, the appropriate displacement (the displacement will be different for different processes), and a new filetype that consists of an integer and a terminal empty space of a size equal to the extent of K − 1 integers.

MPI6File22. The name of file is given in the master process. In addition, three integers, namely, A, B, C, are given in each process. The number of processes is equal to K. Create a new file of integers with the given name and write the given integers to this file as follows: AK-1, AK-2, …, A0, BK-1, BK-2, …, B0, CK-1, CK-2, …, C0 (an index indicates the process rank). To do this, use one call of the MPI_File_write_all collective function and a new file view with the MPI_INT elementary datatype, the appropriate displacement (the displacement will be different for different processes), and a new filetype that consists of an integer and a terminal empty space of a size equal to the extent of K − 1 integers.

MPI6File23. The name of existing file of real numbers is given in the master process. The file contains 6K items, where K is the number of processes. Read and output six file items, namely, A, B, C, D, E, F, in each process. These items are located in the given file as follows: A0, B0, C0, A1, B1, C1, …, AK-1, BK-1, CK-1, D0, E0, F0, D1, E1, F1, …, DK-1, EK-1, FK-1 (an index indicates the process rank). To do this, use one call of the MPI_File_read_all collective function and a new file view with the MPI_DOUBLE elementary datatype, the appropriate displacement (the displacement will be different for different processes), and a new filetype that consists of three real numbers and a terminal empty space of the appropriate size.

MPI6File24. The name of file is given in the master process. In addition, four real numbers, namely, A, B, C, D, are given in each process. The number of processes is equal to K. Create a new file of real numbers with the given name and write the given real numbers to this file as follows: AK-1, BK-1, AK-2, BK-2, …, A0, B0, CK-1, DK-1, CK-2, DK-2, …, C0, D0 (an index indicates the process rank). To do this, use one call of the MPI_File_write_all collective function and a new file view with the MPI_DOUBLE elementary datatype, the appropriate displacement (the displacement will be different for different processes), and a new filetype that consists of two real numbers and a terminal empty space of the appropriate size.

MPI6File25. The name of file is given in the master process. In addition, 3·(R + 1) integers are given in the process of rank R (R = 0, 1, …, K − 1, where K is the number of processes): 3 integers, namely, A, B, C, are given in the process 0, 6 integers, namely, A, A, B, B, C, C, are given in the process 1, 9 integers, namely, A, A, A, B, B, B, C, C, C, are given in the process 2, and so on. Create a new file of real numbers with the given name and write the given integers to this file as follows: A0, A1, A1, A2, A2, A2, …, B0, B1, B1, B2, B2, B2, … (an index indicates the process rank). To do this, use one call of the MPI_File_write_all collective function and a new file view with the MPI_INT elementary datatype, the appropriate displacement (the displacement will be different for different processes), and a new filetype that consists of R + 1 integers and a terminal empty space of the appropriate size.

MPI6File26. The name of file is given in the master process. In addition, four real numbers, namely, A, B, C, D, are given in each process. The number of processes is equal to K. Create a new file of real numbers with the given name and write the given real numbers to this file as follows: A0, A1, …, AK-1, BK-1, …, B1, B0, C0, C1, …, CK-1, DK-1, …, D0 (an index indicates the process rank). To do this, use one call of the MPI_File_write_all collective function and a new file view with the MPI_DOUBLE elementary datatype, the appropriate displacement (the displacement will be different for different processes), and a new filetype that consists of two real numbers (with an additional empty space between these numbers) and a terminal empty space of the appropriate size.

MPI6File27. The name of existing file of real numbers is given in the master process. The file contains elements of the (K/2) × K matrix, where K is the number of processes (K is an even number). Read and output elements of (R + 1)th matrix column in the process of rank R (R = 0, …, K − 1, columns are numbered from 1). To do this, use one call of the MPI_File_read_all collective function and a new file view with the MPI_DOUBLE elementary datatype, the appropriate displacement (the displacement will be different for different processes), and a new filetype that consists of one real number and a terminal empty space of the appropriate size.

MPI6File28. The name of file is given in the master process. In addition, an integer N and a sequence of K/2 real numbers are given in each process, where K is the number of processes (K is an even number). The numbers N are different for all processes and are in the range from 1 to K. Create a new file of real numbers with the given name. Write a (K/2) × K matrix to this file; each process should write its sequence of real numbers into a column of the matrix with the ordinal number N (the columns are numbered from 1). To do this, use one call of the MPI_File_write_all collective function and a new file view with the MPI_DOUBLE elementary datatype, the appropriate displacement (the displacement will be different for different processes), and a new filetype that consists of one real number and a terminal empty space of the appropriate size.

MPI6File29. The name of existing file of integers is given in the master process. The file contains elements of a block matrix of the size (K/3) × 3 (the size is indicated in blocks), where K is the number of processes (K is a multiple of 3). Each block is a square matrix of order N (all the numbers N are the same and are in the range from 2 to 5). Read and output one block of the given matrix in each process in a row-major order of blocks. To do this, use one call of the MPI_File_read_all collective function and a new file view with the MPI_INT elementary datatype, the appropriate displacement (the displacement will be different for different processes), and a new filetype that consists of N integers and a terminal empty space of the appropriate size. Use the MPI_File_get_size function to determine the number N.

MPI6File30. The name of file and an integer N are given in the master process (N is in the range 2 to 5). In addition, two integers I and J are given in each process. The integers I and J determine a position (that is, row and column numbers) of some square block of a block matrix of the size (K/3) × 3 (the size is indicated in blocks), where K is the number of processes (K is a multiple of 3). The integers I are in the range of 1 to K/3, the integers J are in the range 1 to 3; all processes contain different positions of blocks. Each block is a square matrix of order N. Create a new file of integers with the given name. Write a (K/3) × 3 block matrix to this file; each process should write a matrix block to the block position (IJ). All the elements of the block written by the process of rank R (R = 0, 1, …, K − 1) should be equal to the number R. To do this, use one call of the MPI_File_write_all collective function and a new file view with the MPI_INT elementary datatype, the appropriate displacement (the displacement will be different for different processes), and a new filetype that consists of N integers and a terminal empty space of the appropriate size. Use the MPI_Bcast collective function to send the value of N to all processes.


PrevNext

 

  @Mail.ru

Designed by
M. E. Abramyan and V. N. Braguilevsky

Last revised:
06.05.2018