Using the Intel® MPI Library in a server/client setup

http://www.mpi-forum.org/docs/mpi-20-html/node106.htm

http://software.intel.com/zh-cn/articles/using-the-intel-mpi-library-in-a-serverclient-setup


如何用MPI创建server client架构

1) Build 2 applications. Both uses MSMPI and share the same COMM WORLD in the API.

2) Run the 2 application at the same time like below:  

job submited /numodes:3 /askednodes:server1,node1,node2 mpiexec -hosts 1 server1 1 win-form-app.exe : -hosts 2 node1 1 node2 1 console-app.exe

where:

- totally 3 nodes are used. each node runs  1 process. If you want to run more than 1 process on certain node. you can do -hosts 2 node1 M -node2 N ...

- server1 will run your win form application; node1 and node2 will run the console application.


http://www.mpi-forum.org/docs/mpi-20-html/node106.htm

Overview

In some instances, it can be advantageous to have an MPI program join a job after it has started. Additional resources can be added to a long job as they become available, or a more traditional server/client program can be created. This can be facilitated with the MPI_Comm_accept and MPI_Comm_connect functions.

Key Functions

  • MPI_Open_port - Creates the port that is used for the communications. This port is given a name that is used to reference it later, both by the server and the client. Only the server program calls MPI_Open_port
  • MPI_Comm_accept - Uses the previously opened port to listen for a connecting MPI program. This is called by the server and will create an intercommunicator once it completes.
  • MPI_Comm_connect - Connects to another MPI program at the named port. This is called by the client and will create an intercommunicator once it completes.

Notes

  • The programs must use the same fabric in order to connect, as the port is dependent on the fabric.
  • The programs must be on the same operating system in order to connect. Different versions/distributions of the same operating systems could work, this has not been tested and is not supported.
  • The method of getting the port name from the server to the client can vary. In the sample provided, a text file is written containing the port name.

Example

A very simple example is attached to this article. The server opens a port, writes the name of the port to a file, and waits for the client. The client will read the file and attempt to connect to the port. To verify that the two programs are connected, each sends a pre-defined value to the other. To compile and run the example, download the files and place them in the same folder. Open two terminals and navigate to the folder where the files are located.  In the first terminal, use:

     
     
1mpiicpc server.cpp -o server
2mpirun -n 1 ./server

And in the second terminal:

     
     
1mpiicpc client.cpp -o client
2mpirun -n 1 ./client

In Windows*, change mpirun to mpiexec. With the code as provided, the server should show:

     
     
1Waiting for a client
2A client has connected
3The server sent the value: 25
4The server received the value: 42

And the client should show:

     
     
1Attempting to connect
2Connected to the server
3The client sent the value: 42
4The client received the value: 25
5.4.6.3. Simple Client-Server Example.


Up: Client/Server Examples Next: Other Functionality Previous: Ocean/Atmosphere - Relies on Name Publishing
This is a simple example; the server accepts only a single connection at a time and serves that connection until the client requests to be disconnected. The server is a single process.

Here is the server. It accepts a single connection and then processes data until it receives a message with tag 1. A message with tag 0 tells the server to exit. 
#include "mpi.h" 
int main( int argc, char **argv ) 
{ 
    MPI_Comm client; 
    MPI_Status status; 
    char port_name[MPI_MAX_PORT_NAME]; 
    double buf[MAX_DATA]; 
    int    size, again; 
 
    MPI_Init( &argc, &argv ); 
    MPI_Comm_size(MPI_COMM_WORLD, &size); 
    if (size != 1) error(FATAL, "Server too big"); 
    MPI_Open_port(MPI_INFO_NULL, port_name); 
    printf("server available at %s\n",port_name); 
    while (1) { 
        MPI_Comm_accept( port_name, MPI_INFO_NULL, 0, MPI_COMM_WORLD,  
                         &client ); 
        again = 1; 
        while (again) { 
            MPI_Recv( buf, MAX_DATA, MPI_DOUBLE,  
                      MPI_ANY_SOURCE, MPI_ANY_TAG, client, &status ); 
            switch (status.MPI_TAG) { 
                case 0: MPI_Comm_free( &client ); 
                        MPI_Close_port(port_name); 
                        MPI_Finalize(); 
                        return 0; 
                case 1: MPI_Comm_disconnect( &client ); 
                        again = 0; 
                        break; 
                case 2: /* do something */ 
                ... 
                default: 
                        /* Unexpected message type */ 
                        MPI_Abort( MPI_COMM_WORLD, 1 ); 
                } 
            } 
        } 
} 
Here is the client.

#include "mpi.h" 
int main( int argc, char **argv ) 
{ 
    MPI_Comm server; 
    double buf[MAX_DATA]; 
    char port_name[MPI_MAX_PORT_NAME]; 
 
    MPI_Init( &argc, &argv ); 
    strcpy(port_name, argv[1] );/* assume server's name is cmd-line arg */ 
 
    MPI_Comm_connect( port_name, MPI_INFO_NULL, 0, MPI_COMM_WORLD,  
                      &server ); 
 
    while (!done) { 
        tag = 2; /* Action to perform */ 
        MPI_Send( buf, n, MPI_DOUBLE, 0, tag, server ); 
        /* etc */ 
        } 
    MPI_Send( buf, 0, MPI_DOUBLE, 0, 1, server ); 
    MPI_Comm_disconnect( &server ); 
    MPI_Finalize(); 
    return 0; 
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值