Recherche…


Frayer

Le processus maître génère deux processus de travail et diffuse sendbuf aux travailleurs.

master.c

#include "mpi.h"

int main(int argc, char *argv[])
{ 
   int n_spawns = 2;
   MPI_Comm intercomm;

   MPI_Init(&argc, &argv);

   MPI_Comm_spawn("worker_program", MPI_ARGV_NULL, n_spawns, MPI_INFO_NULL, 0, MPI_COMM_SELF, &intercomm, MPI_ERRCODES_IGNORE); 

   int sendbuf[2] = {3, 5};
   int recvbuf; // redundant for master.

   MPI_Scatter(sendbuf, 1, MPI_INT, &recvbuf, 1, MPI_INT, MPI_ROOT, intercomm);

   MPI_Finalize();
   return 0;
}

worker.c

#include "mpi.h"
#include <stdio.h>

int main(int argc, char *argv[])
{  
   MPI_Init(&argc, &argv);

   MPI_Comm intercomm; 
   MPI_Comm_get_parent(&intercomm);

   int sendbuf[2]; // redundant for worker.
   int recvbuf;

   MPI_Scatter(sendbuf, 1, MPI_INT, &recvbuf, 1, MPI_INT, 0, intercomm);
   printf("recvbuf = %d\n", recvbuf);

   MPI_Finalize();
   return 0;
}

Exécution

mpicc master.c -o master_program
mpicc worker.c -o worker_program
mpirun -n 1 master_program

Établir la connexion entre deux applications indépendantes

Le processus maître génère des applications serveur et client avec un processus unique pour chaque application. Le serveur ouvre un port et le client se connecte à ce port. Ensuite, le client envoie des données au serveur avec MPI_Send pour vérifier que la connexion est établie.

master.c

#include "mpi.h"

int main(int argc, char *argv[])
{   
   MPI_Init(&argc, &argv);

   MPI_Comm intercomm;
   // Spawn two applications with a single process for each application.
   // Server must be spawned before client otherwise the client will complain at MPI_Lookup_name().
   MPI_Comm_spawn("server", MPI_ARGV_NULL, 1, MPI_INFO_NULL, 0, MPI_COMM_SELF, &intercomm, MPI_ERRCODES_IGNORE);
   MPI_Comm_spawn("client", MPI_ARGV_NULL, 1, MPI_INFO_NULL, 0, MPI_COMM_SELF, &intercomm, MPI_ERRCODES_IGNORE);
   
   MPI_Finalize();
   return 0;
}

serveur.c

#include "mpi.h"
#include <stdio.h>

int main(int argc, char *argv[])
{  
   MPI_Init(&argc, &argv);

   // Open port.
   char port_name[MPI_MAX_PORT_NAME];
   MPI_Open_port(MPI_INFO_NULL, port_name);

   // Publish port name and accept client.
   MPI_Comm client;
   MPI_Publish_name("name", MPI_INFO_NULL, port_name);
   MPI_Comm_accept(port_name, MPI_INFO_NULL, 0, MPI_COMM_WORLD, &client);
   
   // Receive data from client.
   int recvbuf;
   MPI_Recv(&recvbuf, 1, MPI_INT, 0, 0, client, MPI_STATUS_IGNORE);
   printf("recvbuf = %d\n", recvbuf);

   MPI_Unpublish_name("name", MPI_INFO_NULL, port_name);

   MPI_Finalize();
   return 0;
}

client.c

#include "mpi.h"

int main(int argc, char *argv[])
{   
   MPI_Init(&argc, &argv);

   // Look up for server's port name.
   char port_name[MPI_MAX_PORT_NAME];
   MPI_Lookup_name("name", MPI_INFO_NULL, port_name);
   
   // Connect to server.
   MPI_Comm server;
   MPI_Comm_connect(port_name, MPI_INFO_NULL, 0, MPI_COMM_SELF, &server);

   // Send data to server.
   int sendbuf = 3;
   MPI_Send(&sendbuf, 1, MPI_INT, 0, 0, server);
   
   MPI_Finalize();
   return 0;
}

Ligne de commande

mpicc master.c -o master_program
mpicc server.c -o server
mpicc client.c -o client
mpirun -n 1 master_program


Modified text is an extract of the original Stack Overflow Documentation
Sous licence CC BY-SA 3.0
Non affilié à Stack Overflow