【发布时间】:2021-03-26 09:41:57
【问题描述】:
我有这段代码:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "mpi.h"
struct fields{
int hostNumber;
int *numberArray;
};
struct fields *start(int);
struct fields *gatherData(int);
int main(int argc, char** argv) {
int rank, size, count, *tmpArray, tmpNumOfArray;
struct fields *myFields;
setbuf(stdout, NULL);
MPI_Init(&argc, &argv);
MPI_Status status;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
while(1){
if (rank == 0){
//printf("I am parent-process with rank = %d, size = %d\n", rank, size);
myFields = start(rank);
//for (int i = 0 ; i < myFields->hostNumber ; i++) printf("%d\n",((myFields->numberArray)[i]));
if ((myFields->hostNumber) < size){
printf("Error!!! Number of Processes is more than number of Elements.\n");
MPI_Abort(MPI_COMM_WORLD, 0);
}
if ((myFields->hostNumber) < (2 * size)){
printf("Error!!! The Host of Numbers should be at least double of the number of Processes!!!\n");
MPI_Abort(MPI_COMM_WORLD, 0);
}
tmpNumOfArray = myFields->hostNumber;
MPI_Bcast(&tmpNumOfArray, 1, MPI_INT, 0, MPI_COMM_WORLD);
}
MPI_Barrier(MPI_COMM_WORLD);
printf("process = %d, tmpNumOfArray = %d\n", rank, tmpNumOfArray);
sleep(5);
}
MPI_Finalize();
return 0;
}
struct fields *start(int rank){
int input;
struct fields *myFields;
system("clear");
printf("1) Type 1 For Execution\n");
printf("2) Type 2 For Exit\n");
printf("Give your choice:");
scanf("%d",&input);
switch(input){
case 1:
myFields = gatherData(rank);
break;
case 2:
default:
MPI_Abort(MPI_COMM_WORLD, 0);
}
return myFields;
}
struct fields *gatherData(int rank){
int host;
struct fields *myFields;
myFields = (struct fields *)malloc(sizeof(struct fields));
if (myFields == NULL){
printf("Cannot allocate memory for myFields struct!\n");
MPI_Abort(MPI_COMM_WORLD, 0);
}
if (rank == 0){
printf("Give the host of the numbers to be statically checked: ");
scanf("%d",&host);
int *nmbArray = (int *)malloc(sizeof(nmbArray) * host);
for (int i = 0; i < host; i++){
printf("Give the %d number:", i);
scanf("%d", (&(nmbArray[i])));
}
myFields->hostNumber = host;
myFields->numberArray = &nmbArray[0];
}
return myFields;
}
当我运行它时,我得到以下信息:
process = 2, tmpNumOfArray = 0
process = 0, tmpNumOfArray = 10
process = 1, tmpNumOfArray = 0
process = 3, tmpNumOfArray = 0
我是什么:
process = 2, tmpNumOfArray = 10
process = 0, tmpNumOfArray = 10
process = 1, tmpNumOfArray = 10
process = 3, tmpNumOfArray = 10
有什么建议吗?不要专注于 gatherData() 和 start() 函数,因为它们运行完美,问题在于 main() 到 MPI_Bcast() 函数。我也试过 MPI_COMM_SPAWN 但我认为它不能从父母广播给孩子。
先谢谢了!!!
【问题讨论】:
标签: c performance parallel-processing mpi hpc