嗨,我一直在努力处理这段代码。它有点混乱,但是我最终要清理它,所以请多多包涵。该代码应该模拟一艘船的运动,但我无法使其正常工作。我在一个循环中使用了集体函数Broadcast,但是它仅适用于for循环的前两次迭代。Ill给出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};
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,coords);
if(rank == PORT) enviromentType = LAND;
else enviromentType = SEA;
MPI_Cart_shift(cartcomm,1,&nbrs[UP],&nbrs[DOWN] );
MPI_Cart_shift(cartcomm,&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,MPI_INT,tag,MPI_COMM_WORLD,&reqs[i]);
MPI_Irecv(&inbuf[i],&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};
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_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",buffArr[1]);
}
MPI_Finalize();
return 0;
}
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