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 | Template creation

PrevNext


Creation a template for parallel program

The task solving process using Programming Taskbook starts with creation of a template for selected task. It is particularly convenient to use a template for tasks on parallel programming, because this template already contains important fragments of code that are required in any parallel program. In addition, all necessary libraries connected with the Programming Taskbook and with the selected MPICH system will be linked to this project template.

To create a template one should use the PT4Load tool of Programming Taskbook. The PT4Load tool can be launched by means of the Load.lnk shortcut that is automatically created in the student's working directory. When PT4Load is launched the loading dialog box appears (all task groups are displayed at the bottom of this dialog box):

The title of the dialog box shows that the current IDE is Microsoft Visual Studio 2015 for C++ language. To change the current IDE, it is enough to click the right mouse button and choose a new IDE from the pop-up menu:

In addition to the list of available IDEs, the pop-up menu contains a list of available MPICH versions (the selected version is marked) and allows to select the interface language (Russian or English). The last pop-up menu items are intended for changing the working directory settings.

It should pay attention to the task groups starting with the MPI prefix (MPI1Proc, etc.). They appear in the task group list only if two conditions are fulfilled: (1) the Programming Taskbook extension PT for MPI-2 is successfully installed, (2) any C++ IDE is selected as the current IDE.

After choosing the IDE one should enter text "MPI1Proc2" into the Task textbox. Тhe Load button will be available:

After clicking the Load button (or pressing [Enter]) a template for the specified task will be created and loaded into the selected IDE.

The project template created by the Programming Taskbook for the C ++ language is always named ptprj; this allows, in particular, to significantly reduce the number of files created in the working directory when solving a large number of different tasks. The project includes some files, the principal one of which is a cpp-file, whose name coincides with the name of the task (in our case MPI1Proc2.cpp). This file is automatically loaded into the code editor of the IDE; a student should enter the task solution in this file. Here is the contents of the MPI1Proc2.cpp file:

#include "pt4.h"
#include "mpi.h"

void Solve()
{
    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);

}

At the beginning of the file, there are directives for including the auxiliary header files pt4.h and mpi.h. Then the Solve function is located, which should contain the solution of the task.

The analysis of the MPI1Proc2.cpp file raises a natural question: where is the "start" function of the application (usually named main or WinMain)? It is placed in another project file, because its contents do not need editing. This function initializes the Programming Taskbook components, then calls the Solve function with the task solution, catches the exceptions that might occur when the Solve function is executed, and completes the final steps associated with analyzing the solution.

The project template for tasks on parallel programming contains additional operators, which should be used in almost any parallel MPI program. Let's discuss the contents of the Solve function in more detail. Its first statement calls the Task function, which initializes the required task. This statement is present in any project template including ones that do not related with parallel programming. The Task function is implemented in the core of the Programming Taskbook (the pt4.dll dynamic library) and is available in the student program due to the header file pt4.h attached to it. In addition to the pt4.h header file, the student's working directory must contain a pt4.cpp file containing the definitions of the functions declared in the pt4.h file.

The remaining statements of the Solve function are associated with the MPI library. We have already noted that the Programming Taskbook uses the MPI library included in the MPICH system. Functions and constants of the MPI library are available to the program due to the header file mpi.h attached to it. The implementation of functions from the mpi.h file is contained in the mpich.lib object file that must be linked to any C/C ++ project that uses the MPI library. However, in our case, this linking has already been made during the creation of the project template.

Calling the MPI_Initialized function allows to determine whether the program is initialized for the parallel mode or not. If the parallel mode is initialized, the output parameter of the function (named flag) takes a value other than zero; otherwise the parameter is assumed to be zero. The initialization of the parallel mode is performed by the MPI_Init function, which is not present in the code. This is because this initialization is performed by the Programming Taskbook itself, and it is executed before the program proceeds to execute the student's code. However, sometimes this initialization is not required. For example, if the program is started in the demo mode (for this purpose it is enough to supply the task name with the "?" suffix, for instance, Task("MPI1Proc2?")), the Programming Taskbook does not initialize the parallel mode, because it is not necessary. In this situation, calling the MPI functions (other than MPI_Initialized) in the student's code will lead to incorrect result. The call to MPI_Initialized and the conditional statement following it are intended to "skip" all subsequent statements, if the program is not running in parallel mode.

The last two statements of the project template allow to define two properties that are necessary for any parallel program: the total number of processes (function MPI_Comm_size) and the rank of the current process (function MPI_Comm_rank). The current process is the process that calls this function. The required value is returned in the second parameter of the corresponding function; the first parameter is the communicator that defines the set of processes. Due to the call of these functions, one can immediately use in the program the variables named size (the total number of processes in the MPI_COMM_WORLD communicator) and rank (the rank of the current process in the MPI_COMM_WORLD communicator; the value of the rank is in the range from 0 to size-1). Note that the second parameter of these functions is a pointer to the corresponding variable.

Remark 1. The MPI library contains the MPI_Finalize function, which completes the parallel part of the program. After calling this function, you cannot use other functions of the MPI library. However, in the part of the program code that is available to the student, this function cannot be called, since after the execution of this part of the program the Programming Taskbook must collect all the results obtained in the slave processes, and for this purpose the program must be in parallel mode. Therefore, the Programming Taskbook not only initializes the parallel mode (by calling the MPI_Init function), but also finalizes it (by calling the MPI_Finalize function at the end of the program execution).

Remark 2. Almost any MPI function returns an error code (or the successful return code MPI_SUCCESS). However, the return value of MPI functions is typically not analyzed, so the functions are usually called as procedures (in the form of a statement). The error information can be obtain by a more convenient way with so called error handler. The Programming Taskbook uses a special error handler that output error information in the debug section of the Programming Taskbook window.


PrevNext

 

  Рейтинг@Mail.ru

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

Last revised:
01.01.2024