我试图理解以下示例的含义:

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

int main(int argc, char *argv[]) {
  int numtasks, rank, dest, source, rc, count, tag = 1;
  char inmsg, outmsg = 'x';
  MPI_Status Stat;

  MPI_Init(&argc, &argv);
  MPI_Comm_size(MPI_COMM_WORLD, &numtasks);
  MPI_Comm_rank(MPI_COMM_WORLD, &rank);

  if (rank == 0) {
    dest = 1;
    source = 1;
    rc = MPI_Send(&outmsg, 1, MPI_CHAR, dest, tag, MPI_COMM_WORLD);
    rc = MPI_Recv(&inmsg, 1, MPI_CHAR, source, tag, MPI_COMM_WORLD, &Stat);
  } else if (rank == 1) {
    dest = 0;
    source = 0;
    rc = MPI_Recv(&inmsg, 1, MPI_CHAR, source, tag, MPI_COMM_WORLD, &Stat);
    rc = MPI_Send(&outmsg, 1, MPI_CHAR, dest, tag, MPI_COMM_WORLD);
  }

  rc = MPI_Get_count(&Stat, MPI_CHAR, &count);
  printf("Task %d: Received %d char(s) from task %d with tag %d \n",
         rank, count, Stat.MPI_SOURCE, Stat.MPI_TAG);

  MPI_Finalize();
}

基本上是从这里复制的:https://computing.llnl.gov/tutorials/mpi/
编译如下:
$ mpicc -o ./mpi_test ./mpi_test.c

像这样跑:
$ ./mpi_test

出现此错误:
Fatal error in MPI_Send: Invalid rank, error stack:
MPI_Send(171): MPI_Send(buf=0x7fff787b4d93, count=1, MPI_CHAR, dest=1, tag=1, MPI_COMM_WORLD) failed
MPI_Send(97).: Invalid rank has value 1 but must be nonnegative and less than 1

老实说,我不明白为什么这个例子一开始就应该奏效。你能启发我吗这个例子是错的,还是预期的错误?也许我的设置是错误的?
PS.在Fedora 18 amd64上使用gcc 4.7编译

最佳答案

您必须使用mpirun运行程序,如下所示:

mpirun -np 2 mpi_test

关于c - MPI基本示例不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19870111/

10-16 05:01