💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
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 Submitted by [James Tullos (Intel)](http://software.intel.com/en-us/user/540584 "View user profile.") on Fri, 07/13/2012 - 08:59 Categories:  - [Cluster Computing](http://software.intel.com/en-us/search/site/?f[0]=im_field_topic%3A20862)  -  [Intel® MPI Library](http://software.intel.com/en-us/search/site/?f[0]=im_field_software_product%3A20823&f[1]=im_field_software_product%3A20827)  -  [Intel® Cluster Studio](http://software.intel.com/en-us/search/site/?f[0]=im_field_software_product%3A20834&f[1]=im_field_software_product%3A20844)  -  [Intel® Cluster Studio XE](http://software.intel.com/en-us/search/site/?f[0]=im_field_software_product%3A20834&f[1]=im_field_software_product%3A35847)  -  [Linux*](http://software.intel.com/en-us/search/site/?f[0]=im_field_operating_system%3A20787)  -  [Microsoft Windows* (XP, Vista, 7)](http://software.intel.com/en-us/search/site/?f[0]=im_field_operating_system%3A36914)  -  [Intermediate](http://software.intel.com/en-us/search/site/?f[0]=im_field_skill_level%3A20808) Tags:  - [server](http://software.intel.com/en-us/search/site/?f[0]=im_field_tags%3A17339)  -  [MPI](http://software.intel.com/en-us/search/site/?f[0]=im_field_tags%3A19522)  -  [client](http://software.intel.com/en-us/search/site/?f[0]=im_field_tags%3A19916)  -  [Intel® MPI Library](http://software.intel.com/en-us/search/site/?f[0]=im_field_tags%3A41531)  -  [Intel® Cluster Studio XE](http://software.intel.com/en-us/search/site/?f[0]=im_field_tags%3A41532) 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: ~~~ mpiicpc server.cpp -o server mpirun -n 1 ./server ~~~ And in the second terminal: ~~~ mpiicpc client.cpp -o client mpirun -n 1 ./client ~~~ In Windows*, change **mpirun**to **mpiexec**. With the code as provided, the server should show: ~~~ Waiting for a client A client has connected The server sent the value: 25 The server received the value: 42 ~~~ And the client should show: ~~~ Attempting to connect Connected to the server The client sent the value: 42 The 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; } ~~~