6.3. NETWORK TYPE


6.2. NETWORKS

So far we considered mpC programs that involve in computations either all processes or only the host-process. However, the number of processes involved in parallel solution of a problem often depends on the problem itself and/or on the parallel algorithm of its solution. Moreover the number is often determined by input data.

To see this, consider a parallel algorithm that simulates the evolution of N groups of bodies under the influence of Newtonian gravitational attraction, using a single process for a single group of bodies. In the corresponding parallel computations exactly N processes will have to be involved independent of the total number of processes that would constitute the parallel mpC program implementing the algorithm. Recall that an mpC program is started by mechanisms external to the mpC language. The mpC programmer cannot specify the total number of processes of the program; the programmer can only obtain this number.

The following program gives a first introduction to such a language means that allows the programmer to describe the parallel computations on the necessary number of processes:

#include <mpc.h> #define N 3 int [*]main() {    net SimpleNet(N) mynet;    [mynet]MPC_Printf("Hello, world!\n"); }

The computations themselves are quite simple: each participating process just outputs “Hello, world!” to the user’s terminal. But the number of participating processes, N=3, is defined by the programmer and does not depend on the total number of processes of the parallel program.

In mpC the notion of network corresponds to a group of processes jointly performing some parallel computations. An mpC network is an abstraction facilitating the work with actual processes of the parallel program (like the notion of data object and variable in programming languages facilitate the work with memory).

In the simplest case an mpC network is just a set of abstract processors. In order to program parallel computations, which must be executed by a given number of processes of the program, the programmer must first define a network consisting of this number of abstract processors, and then describe the parallel computations on this network.

The definition of an mpC network causes the creation of a group of processes that represent the network, whereby each abstract processor of the network is represented by a single process of the parallel program. The programmer’s description of the parallel computations on the network leads to the execution of these parallel computations by exactly those processes, that represent the abstract processors of this network.

An important difference in the actual processes of the mpC program that separates them from the abstract mpC processors is that at different times of program execution the same process can represent different abstract processors of different mpC networks. In other words, the definition of an mpC network causes mapping of the abstract processors of this network to the actual processes of the parallel program, and this mapping is constant during the lifetime of this network.

The preceding program first defines the network mynet of N abstract processors, and then calls the nodal library function MPC_Printf on this network. The execution of this program consists in a parallel call of function MPC_Printf by those N processes of the program to which abstract processors of network mynet has been mapped. This mapping is performed at runtime. If the mapping cannot be performed (e.g., if N is greater than the total number of processes of the program), the program will terminate abnormally with corresponding diagnostics.

Note the similarity of language constructs [mynet] and [host]. Indeed, keyword host can be considered as the name of a pre-defined mpC network consisting of exactly one abstract processor that is always mapped to the host-process associated with the user’s terminal.

The next program outputs messages from those processes of the parallel program to which abstract processors of network mynet are mapped:

#include <mpc.h> #include <stdlib.h> #define N 3 int [*]main() {    net SimpleNet(N) mynet;    char *[mynet]nodename;    int [mynet]namelen;    [mynet]MPC_Processor_name(&nodename, &namelen);    [mynet]MPC_Printf("Hello world! I’m on \"%s\".\n",            nodename);    [mynet]free(nodename); }

In addition to “Hello, world!” each involved process outputs the name of the computer that executes the process. In so doing, the program defines variables nodename and namelen, both distributed over network mynet. Only a process that represents one of abstract processors of network mynet holds in its memory a copy of variables nodename and namelen. Only those processes call function MPC_Processor_name that are specified with construct [mynet] before the function name. After this call each projection of the distributed variable nodename will point to the name of the computer running the corresponding process.

The semantics of the next mpC program is completely equivalent to that of the previous program:

#include <mpc.h> #include <stdlib.h> #define N 3 int [*]main() {    net SimpleNet(N) mynet;    char *[mynet]nodename;    int [mynet]namelen;    [mynet]:      {       MPC_Processor_name(&nodename, &namelen);       MPC_Printf("Hello world! I’m on \"%s\".\n",                   nodename);       free(nodename);      } }

However, because of the use of a special distributed label, [mynet], this program has a simpler syntax. Any statement labeled with a distributed label (here it is a compound statement) will be executed only by the abstract processors of the network, whose name is used in the label.

The next mpC program demonstrates that the number of abstract processors of an mpC network can be specified dynamically, that is, at runtime:

#include <stdlib.h> #include <mpc.h> int [*]main(int [host]argc, char **[host]argv) {    repl n;    if(argc<2)      n=1;    else      n=[host]atoi(argv[1]);    if(n<1)       [host]printf("Wrong input (%d processes required).\n", [host]n);    else if(n>MPC_Total_nodes())       [host]printf("Too many processes required (%d against %d available).\n",                    [host]n,                    [host]MPC_Total_nodes());    else    {       net SimpleNet(n) mynet;       char *[mynet]nodename;       int [mynet]namelen;       [mynet]MPC_Processor_name(&nodename, &namelen);       [mynet]MPC_Printf("Hello world! I’m on \"%s\".\n",                    nodename);       [mynet]free(nodename);    }    MPC_Printf("* "); }

This program treats its only external argument as a processor number. The user specifies the argument when starting up the program. This argument is accessible to the host-process.

The expression [host]atoi(argv[1]) is evaluated on the host-process, and its value is then assigned to the integer variable n, which is replicated over all processes of the parallel program. The execution of this assignment consists in broadcasting this value to all processes of the program, where it is assigned to the corresponding projection of variable n.

Before the program defines (creates) a network of the user-specified number of abstract processors and executes the described computations on the network, it checks the correctness of input data. If the specified number of abstract processors is incorrect (less than 1 or greater than the total number of processes of the parallel program), the program outputs to the corresponding diagnostics.

Otherwise, the program defines network mynet consisting of n abstract processors as well as variables nodename and namelen distributed over this network. Then the nodal function MPC_Processor_name is called on network mynet, resulting in each projection of the distributed variable nodename pointing to the name of the computer where the corresponding abstract processor has been placed. Next a call to the nodal function MPC_Printf on network mynet outputs from each abstract processor the greeting “Hello, world!” together with the name of the computer hosting this abstract processor. Finally, a call to the nodal function free on network mynet releases the memory allocated for the computer name on each of abstract processors of this network.

The lifetimes of both network mynet and variables nodename and namelen are limited by the block in which they are defined. When the execution of the block ends, all processes of the program, which have been taken for abstract processors of network mynet, are freed and can be used for other networks. Such mpC networks are called automatic.

The lifetimes of static networks are only limited by the time of program execution. The next two mpC programs demonstrate the difference between static and automatic networks. The programs look almost identical. Both consist in a cyclic execution of the block that defines a network and executes already familiar computations on the network. The only essential difference is that the first program defines an automatic network, whereas the second defines a static network.

The first program is as follows:

#include <mpc.h> #define Nmin 3 #define Nmax 5 int [*]main() {    repl n;    for(n=Nmin; n<=Nmax; n++)    {       auto net SimpleNet(n) mynet;       char *[mynet]nodename;       int [mynet]namelen;       [mynet]MPC_Processor_name(&nodename, &namelen);       [mynet]MPC_Printf("I’m from a %d-processor automatic network"              " on\"%s\".\n",             [mynet]n,nodename);       [mynet]free(nodename);    } }

During execution of this program, at the first loop iteration (n=Nmin=3) a network of three abstract processors is created on the entry into the block, and this network is destructed when execution of the block ends. At the second loop iteration (n=4) a new network of four abstract processors is created on the entry into the block, and that network is also destructed when execution of the block ends. So at the time of the repeated initialization of the loop (evaluation of expression n++), the four-processor network no longer exists. Finally, at the last iteration an automatic network of five virtual processors (n=Nmax=5) is created on the entry into the block.

The mpC program demonstrating the use of static networks is as follows:

#include <mpc.h> #define Nmin 3 #define Nmax 5 int [*]main() {    repl n;    for(n=Nmin; n<=Nmax; n++)    {       static net SimpleNet(n) mynet;       char *[mynet]nodename;       int [mynet]namelen;       [mynet]MPC_Processor_name(&nodename, &namelen);       [mynet]MPC_Printf("I’m from the %d-processor static network"           " on \"%s\".\n",          [mynet]n, nodename);       [mynet]free(nodename);    } }

During execution of the program, at the first loop iteration a network of three virtual processors is also created on the entry into the block, but this network is not deconstructed when the execution of the block ends. It simply becomes invisible. Thus here the block is not a region where the network exists but a region of its visibility. Now, at the time of the repeated initialization of the loop and evaluation of the loop condition, the static three-processor network exists but is not available because these points of the program are out of scope of the network name mynet. On the next entries into the block at subsequent loop iterations, no new networks are created, but the static network, which was created on the first entry into the block, becomes visible.

Thus, whereas in the first program the same name mynet denotes absolutely different networks at different loop iterations, in the second program this name denotes a unique network existing from the first entry in the block, in which it is defined, until the end of program execution.

If the kind of mpC network is not specified explicitly by using keyword auto or static in its definition, the network will be considered either automatic, when declared inside a function, or static, when declared out of any function. So all networks in mpC programs in this section, except the last two programs, are implicitly declared automatic.




Parallel Computing on Heterogeneous Networks
Parallel Computing on Heterogeneous Networks (Wiley Series on Parallel and Distributed Computing)
ISBN: B000W7ZQBO
EAN: N/A
Year: 2005
Pages: 95

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net