我的mpi_Bcast有问题。我想发送一个数组来计算每个进程到另一个进程的数量,并且随机进程不会返回任何内容并崩溃(进程等级2和最后一个)。每个进程的数量可以在1以上不同。可以有人帮我吗?
#include <stdio.h> // printf
#include <mpi.h>
#include <stdlib.h>
#include <time.h>
#include <iostream>
#include "EasyBMP.h"
using namespace std;
int main(int argc, char **argv) {
MPI_Init(&argc, &argv);
int rank, size;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
int* per_process = new int[size];
for (int i = 0; i < size; i++){
per_process[i] = 0;
}
if (rank == 0){
for (int i = 0; i < size; i++){
int default_for_process = 12 / size;
int rest = 12 % size;
if (i < rest){
default_for_process++;
}
per_process[i] = default_for_process;
}
}
MPI_Bcast(&per_process, size, MPI_INT, 0, MPI_COMM_WORLD);
for (int i = 0; i < size; i++){
cout <<rank<<" "<< per_process[i];
}
cout << endl;
MPI_Finalize();
return 0;
}