如何在mpi中运行C代码:无法连接到本地mpd(/tmp/mpd2.console_karim);可能的原因?

时间:2019-04-19 15:17:14

标签: c mpi ubuntu-18.04

无法使用ubuntu在终端上运行用c编写的mpi代码

karim@karim:~/Desktop/greetings$ mpicc main.c -o test
karim@karim:~/Desktop/greetings$ mpirun -np 3 test
mpiexec_karim: cannot connect to local mpd (/tmp/mpd2.console_karim); possible causes:
  1. no mpd is running on this host
  2. an mpd is running but was started without a "console" (-n option)
In case 1, you can start an mpd on this host with:
    mpd &

and you will be able to run jobs just on this host.
For more details on starting mpds on a set of hosts, see
the MPICH2 Installation Guide.

#include <stdio.h>
#include <string.h>
#include "mpi.h"
int main(int argc , char * argv[])
{
int my_rank;    /* rank of process */
int p;      /* number of process */
int source; /* rank of sender */
int dest;   /* rank of reciever */
int tag = 0;    /* tag for messages */
char message[100];  /* storage for message */
MPI_Status status;  /* return status for recieve */

/* Start up MPI */
MPI_Init( &argc , &argv );

/* Find out process rank */
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);

/* Find out number of process */
MPI_Comm_size(MPI_COMM_WORLD, &p);

if( my_rank != 0)
{
    /* create message */
    sprintf( message, "Greetings from process %d !",my_rank);
    dest = 0;
    /* use Strlen+1 to transmit /0 */
    MPI_Send( message, strlen(message)+1, MPI_CHAR, dest, tag,
    MPI_COMM_WORLD);
}else
{
    for( source = 1; source < p ; source++)
    {
        MPI_Recv(message, 100, MPI_CHAR, source, tag, MPI_COMM_WORLD,
        &status);
        printf("%s\n" , message);
    }
}
/* shutdown MPI */
MPI_Finalize();
return 0;
}

输出:

Greetings from process 1 !
Greetings from process 2 !
Greetings from process 3 !

1 个答案:

答案 0 :(得分:0)

搜索后,我用这段代码解决了问题

mpd --ncpus=2 &
mpirun -np 2 --host localhost ./try.exe