我正在嘗試做的練習要求我們構建一個行程環,其中每個行程將數字 x 傳遞給以下行程,只有 rank=0 的行程會減小 x。當 x 等于 0 時,程式結束。作業代碼如下:
#include <stdio.h>
#include <stddef.h>
#include <string.h>
#include "mpi.h"
int main(int argc, char** argv) {
int x = 0;
int tag = 99;
MPI_Status status;
MPI_Init(&argc, &argv);
int myRank;
MPI_Comm_rank(MPI_COMM_WORLD, &myRank);
int size;
MPI_Comm_size(MPI_COMM_WORLD, &size);
if (myRank == 0) {
printf("Process %d enter a value: \n", myRank);
fflush(stdout);
scanf_s("%d", &x);
while (x>0) {
MPI_Send(&x, 1, MPI_INT, 1, tag, MPI_COMM_WORLD);
MPI_Recv(&x, 1, MPI_INT, MPI_ANY_SOURCE, tag, MPI_COMM_WORLD, &status);
printf("Process %d : lap number %d \n", myRank, x);
x--;
}
}
else {
do {
MPI_Recv(&x, 1, MPI_INT, MPI_ANY_SOURCE, tag, MPI_COMM_WORLD, &status);
printf("Process %d : lap number %d \n", myRank, x);
MPI_Send(&x, 1, MPI_INT, (myRank 1) % size, tag, MPI_COMM_WORLD);
} while (x > 1);
}
MPI_Finalize();
return 0;
}
但是如果我像這樣更改最后一個 do while 回圈:
#include <stdio.h>
#include <stddef.h>
#include <string.h>
#include "mpi.h"
int main(int argc, char** argv) {
int x = 0;
int tag = 99;
MPI_Status status;
MPI_Init(&argc, &argv);
int myRank;
MPI_Comm_rank(MPI_COMM_WORLD, &myRank);
int size;
MPI_Comm_size(MPI_COMM_WORLD, &size);
if (myRank == 0) {
printf("Process %d enter a value: \n", myRank);
fflush(stdout);
scanf_s("%d", &x);
while (x>0) {
MPI_Send(&x, 1, MPI_INT, 1, tag, MPI_COMM_WORLD);
MPI_Recv(&x, 1, MPI_INT, MPI_ANY_SOURCE, tag, MPI_COMM_WORLD, &status);
printf("Processo %d : giro numero %d \n", myRank, x);
x--;
}
}
else {
while (x>0) {
MPI_Recv(&x, 1, MPI_INT, MPI_ANY_SOURCE, tag, MPI_COMM_WORLD, &status);
printf("Processo %d : giro numero %d \n", myRank, x);
MPI_Send(&x, 1, MPI_INT, (myRank 1) % size, tag, MPI_COMM_WORLD);
}
}
MPI_Finalize();
return 0;
}
我收到此錯誤:
job aborted:
[ranks] message
[0] fatal error
Fatal error in MPI_Send: Other MPI error, error stack:
MPI_Send(buf=0x000000F6100FF514, count=1, MPI_INT, dest=1, tag=99, MPI_COMM_WORLD) failed
failed to attach to a bootstrap queue - 10616:296
[1-2] terminated
但我不明白為什么......這兩個代碼不應該是等價的嗎?
uj5u.com熱心網友回復:
您設定x為零,然后除零之外的所有行程都執行while (x>0) { stuff }。所以他們什么都不做。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/383644.html
上一篇:如何在代碼中設定斷點來除錯PHP
