Programming Taskbook


E-mail:

Password:

User registration   Restore password

Russian

SFedU SMBU

1100 training tasks on programming

©  M. E. Abramyan (Southern Federal University, Shenzhen MSU-BIT University), 1998–2024

 

PT for MPI-2 | Solution example | Task solution

PrevNext


Solution of the MPI1Proc2 task

Now let's start to solve the task.

Before solving tasks, we should input initial data. In the MPI1Proc2 task, the initial data consists of one integer in each process. The input statement should be placed after the MPI_Comm_rank function call (here and further we will show only the body of the Solve function):

Task("MPI1Proc2");
int flag;
MPI_Initialized(&flag);
if (flag == 0)
  return;
int rank, size;
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
int a;
pt >> a;

We have used special input-output stream called pt, which is implemented in the Programming Taskbook. After the resulting program launching, we will see the Programming Taskbook window on the screen:

The Programming Taskbook found that all initial data are input, thus the program has begun to solve the task. However, no data has been output. Strictly speaking, this indicates an erroneous solution, but the first step to a correct solution was made: all initial numbers are correctly input. In this situation, the Programming Taskbook displays a message on a light blue background "Correct data input: all required data are input, no data are output".

Note that the input operations added to our program are performed in all processes of the parallel application. Note also that the number of processes has changed. When running a parallel program with the task solution, the number of processes can change, so the solution will be considered as a correct one only if it gives the correct result for any allowed number of processes.

Close the Programming Taskbook window and return to our program code. In each process, we need to output doubled value of the input number, so we add one more operator to the end of the Solve function:

pt << 2 * a;

We use the same pt stream to output data because this stream is an input-output one.

Running the corrected version will result in a window with an error message:

Now the required results are displayed in all slave processes. In addition, the doubled number is also output in the master process. These data are correct, as can be verified by comparing the values shown in the results section and in the section with an example of the correct solution.

However, in the master process it was also required to output the number of processes, and this was not done. Therefore, the information panel contains the message "Some data are not output. The error has occurred in the process 0", and the message is displayed on an orange background. Orange color is used to highlight errors related to the input or output of insufficient data. When program tries to input or output superfluous data, the information panel is highlighted in crimson, if program tries to input or output data of wrong type then the panel color becomes purple. For all other errors, the red color is used.

The number of processes is stored in the size variable. Try to output the value of this variable at the end of the Solve function:

pt << size;

The task window will look like this:

One can check that all resulting data are output. However, the solution is still considered to be erroneous, because now program tries to output extra value (namely, the number of processes) in the slave processes. As noted above, Programming Taskbook uses a crimson color to highlight errors due to input/output superfluous data,.

If errors are detected in the slave processes then an additional debug section is displayed in the Programming Taskbook window, in which more detailed information about the error is displayed for each slave process.

To determine the process associated with a message displayed in the debug section, one can use the number indicated on the left side of the line (before the "|" symbol). All lines associated with a particular process are numbered independently; their numbers are displayed after the process number and are separated from the message text by the ">" symbol. In order to display only messages related to one process, it is enough to click on the marker with the number (rank) of this process (all markers are located on the lower border of the window) or press the corresponding number key. To display summary information on all processes, you should select a marker with the "*" symbol or enter this symbol from the keyboard (you can also select markers using the Left and Right arrow keys). If the line of the message in the debug section begins with the "!" symbol then it means that this message is an error message and is added to the debug section by the Programming Taskbook itself. The student’s program can output its own messages to the debug section; this feature will be described in detail later (see the next section "Using the debug section").

If the Programming Taskbook has detected an error in at least one slave process then it does not analyze results obtained in the master process (see the corresponding information in the debug section).

The value of the size variable should be output only in the master process. So we must analyze the rank of the current process. Adding the appropriate conditional statement, we get the solution, which the Programming Taskbook will be considered correct:

Task("MPI1Proc2");
int flag;
MPI_Initialized(&flag);
if (flag == 0)
  return;
int rank, size;
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
int a;
pt >> a;
pt << 2 * a;
if (rank == 0)
  pt << size;

When this variant of the solution will be launched, five console windows will be displayed successively on the screen. Thus, a single launch of the program from the IDE leads to a series of runs of this program in parallel mode, which allows to immediately test the solution on several sets of input data. A series of tests is completed either when an error is detected or when the required number of tests passes successfully (for all tasks included in PT for MPI-2, the number of tests is equal to five). This Programming Taskbook feature further simplifies the process of verifying the solution.

After five successful tests, a Programming Taskbook window will appear on the screen with a message about the successful solution of the task:

In this case, all indicators located on the indicator panel (under the information panel) are green.

Each time the student’s program is run, the results of its running are saved in the special log file named results.dat. This file can be viewed by means of the PT4Results module of the Programming Taskbook (the shortcut Results.lnk is provided for running this module). Also the contents of the results.dat file can be viewed directly from the Programming Taskbook window by clicking the label "Results (F2)" or the [F2] key. A window will appear on the screen with the information about all program runs. In our case, it will contain something like this:

= Smith John                (C:\PT4Work)
MPI1Proc2   c04/09 15:44 Acquaintance with the task.
MPI1Proc2   c04/09 15:49 Correct data input.
MPI1Proc2   c04/09 15:54 Some data are not output.
MPI1Proc2   c04/09 15:59 An attempt to output superfluous data.
MPI1Proc2   c04/09 16:03 The task is solved!

The "c" symbol after the task name indicates programming language C++, the next information includes the date and time of the program run and description of the result of its running.


PrevNext

 

  Ðåéòèíã@Mail.ru

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

Last revised:
01.01.2024