C Mpi代码无法正确同步,可能存在争用情况?

C Mpi代码无法正确同步,可能存在争用情况?,c,mpi,openmpi,hpc,C,Mpi,Openmpi,Hpc,嗨,我已经为这段代码挣扎了一段时间了。这有点乱,但我会把它清理干净,所以请容忍我。这段代码本来应该模拟一艘船的运动,但我无法让它正常工作。我在循环中使用集合函数Broadcast,但它只在for循环的前两次迭代中有效。我将给出slurm的输出以及代码。提前谢谢 Code #include <stdio.h> #include <mpi.h> #include <stdlib.h> #define SIZE 16 #define UP 0 #define DO

嗨,我已经为这段代码挣扎了一段时间了。这有点乱,但我会把它清理干净,所以请容忍我。这段代码本来应该模拟一艘船的运动,但我无法让它正常工作。我在循环中使用集合函数Broadcast,但它只在for循环的前两次迭代中有效。我将给出slurm的输出以及代码。提前谢谢

Code

#include <stdio.h>
#include <mpi.h>
#include <stdlib.h>
#define SIZE 16
#define UP 0
#define DOWN 1
#define LEFT 2
#define RIGHT 3
#define LAND 1
#define SEA 0
#define PORT 12

struct Cell {
  int up;
  int down;
  int left;
  int right;
};

struct Boat {
  int nextDir;
  int currCell;
  int amountFish;
  int isFishing;
};

int randomDir(int rank){
  srand(time(NULL)^rank);
  return rand() % (3 + 1);
}

int main (int argc, char** argv) {
  int numtasks, rank, source, dest, outbuf, i, tag=1;
  int inbuf[4]={MPI_PROC_NULL,MPI_PROC_NULL, MPI_PROC_NULL,
        MPI_PROC_NULL};
  int nbrs[4];
  int dims[2] = {5,5}, periods[2] = {1,1}, reorder=1;
  int coords[2];
  int enviromentType;
  int arrBuffer[4];
  struct Cell cells[dims[0]*dims[1]];
  MPI_Comm cartcomm;

  MPI_Request reqs[8];
  MPI_Status stats[8];

 // starting with MPI program
  MPI_Init(&argc, &argv);

  MPI_Comm_size(MPI_COMM_WORLD, &numtasks);
  MPI_Cart_create(MPI_COMM_WORLD, 2, dims, periods,reorder, &cartcomm);

  MPI_Comm_rank(cartcomm, &rank);

  MPI_Cart_coords(cartcomm, rank, 2, coords);

  if(rank == PORT) enviromentType = LAND;
  else enviromentType = SEA;

  MPI_Cart_shift(cartcomm, 0, 1, &nbrs[UP], &nbrs[DOWN] );
  MPI_Cart_shift(cartcomm, 1, 1, &nbrs[LEFT], &nbrs[RIGHT] );
   // do some work with MPI communication operations...

   outbuf = enviromentType;

   for (i=0; i<4;i++) {
      dest=nbrs[i];
      source=nbrs[i];
      // perform non-blocking communication
      MPI_Isend(&outbuf, 1, MPI_INT, dest, tag, MPI_COMM_WORLD, &reqs[i]);
      MPI_Irecv(&inbuf[i], 1, MPI_INT, source, tag, MPI_COMM_WORLD, &reqs[i+4]); // 4 as a kind of offset
    }

    // wait for non-blocking communication to be completed for output
    MPI_Waitall(8, reqs, stats);

    struct Cell cell = {inbuf[UP], inbuf[DOWN], inbuf[LEFT], inbuf[RIGHT]};

    cells[rank] = cell;
    int buffArr[4];
    int inArr[4];
    source = PORT;
    MPI_Request req[2];
    MPI_Status stat[2];
    //Simulate
    struct Boat boat = {0, PORT, 0, 0};
    for(int iter = 0; iter < 3; iter++){
      MPI_Barrier(MPI_COMM_WORLD);
      if(rank == boat.currCell){
        printf("Boat is at cell: %d\n", rank);
        int nextDirection = randomDir(rank);
        source = rank;
        switch(nextDirection){
          case UP:
            boat.currCell = nbrs[UP];
            break;
          case DOWN:
            boat.currCell = nbrs[DOWN];
            break;
          case LEFT:
            boat.currCell = nbrs[LEFT];
            break;
          case RIGHT:
            boat.currCell = nbrs[RIGHT];
            break;
        }

        printf("Next direction %d leads to cell %d\n", nextDirection, boat.currCell);
        buffArr[0] = boat.nextDir;
        buffArr[1] = boat.currCell;
        buffArr[2] = boat.amountFish;
        buffArr[3] = boat.isFishing;
        dest = buffArr[1];
        source = rank;

      }

      MPI_Barrier(MPI_COMM_WORLD);
      MPI_Bcast(&buffArr, 4, MPI_INT, source, MPI_COMM_WORLD);
      MPI_Barrier(MPI_COMM_WORLD);
      boat.nextDir = buffArr[0];
      boat.currCell = buffArr[1];
      boat.amountFish = buffArr[2];
      boat.isFishing = buffArr[3];
      printf("Rank %d has currcell %d\n", rank,buffArr[1]);

    }
    MPI_Finalize();

    return 0;
  }


所有列组都必须使用相同的
root
参数调用
MPI\u Bcast()
。当提出问题时,请至少包括您的
mpirun
命令行,以及对未按预期工作的内容的简要描述。如果解释过于笼统,请道歉。从bcast传递的消息没有像我希望的那样从if语句传递变量。当我开始分配端口变量,然后在每次迭代中随机分配一个新的列组。
Output:
Boat is at cell: 12
Next direction 0 leads to cell 7
Rank 20 has currcell 7
Rank 22 has currcell 7
Rank 12 has currcell 7
Rank 14 has currcell 7
Rank 16 has currcell 7
Rank 18 has currcell 7
Rank 6 has currcell 7
Rank 8 has currcell 7
Rank 10 has currcell 7
Rank 13 has currcell 7
Rank 17 has currcell 7
Rank 21 has currcell 7
Rank 23 has currcell 7
Rank 0 has currcell 7
Rank 1 has currcell 7
Rank 2 has currcell 7
Rank 3 has currcell 7
Rank 4 has currcell 7
Rank 5 has currcell 7
Rank 7 has currcell 7
Rank 9 has currcell 7
Rank 11 has currcell 7
Rank 15 has currcell 7
Rank 19 has currcell 7
Boat is at cell: 7
Next direction 1 leads to cell 12
Rank 24 has currcell 7
Rank 18 has currcell 7
Rank 6 has currcell 7
Rank 8 has currcell 7
Rank 10 has currcell 7
Rank 12 has currcell 7
Rank 14 has currcell 7
Rank 16 has currcell 7
Rank 22 has currcell 7
Rank 20 has currcell 7
Rank 23 has currcell 7
Rank 0 has currcell 7
Rank 1 has currcell 7
Rank 2 has currcell 7
Rank 3 has currcell 7
Rank 4 has currcell 7
Rank 7 has currcell 12
Rank 5 has currcell 7
Rank 19 has currcell 7
Rank 9 has currcell 7
Rank 11 has currcell 7
Rank 13 has currcell 7
Rank 15 has currcell 7
Rank 17 has currcell 7
Rank 21 has currcell 7
Rank 24 has currcell 7
Rank 23 has currcell 7
Rank 0 has currcell 7
Rank 1 has currcell 7
Rank 2 has currcell 7
Rank 3 has currcell 7
Rank 4 has currcell 7
Rank 7 has currcell 12
Rank 18 has currcell 7
Rank 5 has currcell 7
Rank 6 has currcell 7
Rank 19 has currcell 7
Rank 8 has currcell 7
Rank 10 has currcell 7
Rank 9 has currcell 7
Rank 12 has currcell 7
Rank 11 has currcell 7
Rank 14 has currcell 7
Rank 13 has currcell 7
Rank 16 has currcell 7
Rank 22 has currcell 7
Rank 17 has currcell 7
Rank 21 has currcell 7
Rank 20 has currcell 7
Rank 15 has currcell 7
Rank 24 has currcell 7