I need to divide the processes into several communicators. In one write even, and in others odd. I wrote the code, but when I run, I get errors. Help me figure out what the gap. There is an opinion that it is necessary to use MPI_Comm_split (), but I do not understand how to do it correctly.
#include <stdio.h> #include <mpi.h> #include <iostream> #include <Windows.h> #include <vector> using namespace std; int main(int argc, char *argv[]){ SetConsoleOutputCP(1251); int errCode; if ((errCode = MPI_Init(&argc, &argv)) != 0) { return errCode; } int world_rank, world_size; int *odd, *even; MPI_Comm_rank(MPI_COMM_WORLD, &world_rank); MPI_Comm_size(MPI_COMM_WORLD, &world_size); int oddSize = world_size / 2; int evenSize = world_size - oddSize; odd = new int[oddSize]; even = new int[evenSize]; MPI_Comm odd_comm, even_comm; MPI_Group group11, group12, group13, group21, group22, group31, group32, odd_group, even_group, group_world; MPI_Comm_group(MPI_COMM_WORLD, &group_world); for (int i = 0, j = 0, p = 0; i < world_size; i++) { if (i % 2 == 0) { odd[j] = i; j++; } else { even[p] = i; p++; } } MPI_Group_incl(group_world, oddSize, odd, &odd_group); MPI_Group_incl(group_world, evenSize, even, &even_group); MPI_Comm_create(MPI_COMM_WORLD, odd_group, &odd_comm); int row_rank, row_size, size; MPI_Comm_rank(odd_comm, &row_rank); MPI_Comm_size(odd_comm, &row_size); if (world_rank == 0) cout << "Rank = " << row_rank << " Size = " << row_size << endl;/**/ MPI_Comm_free(&odd_comm); MPI_Finalize(); return 0; } Errors of this type appear.
job aborted: [ranks] message [0] terminated [1] fatal error Fatal error in MPI_Comm_rank: Invalid communicator, error stack: MPI_Comm_rank(MPI_COMM_NULL, rank=0x0111FC70) failed Invalid communicator [2] terminated [3] fatal error Fatal error in MPI_Comm_rank: Invalid communicator, error stack: MPI_Comm_rank(MPI_COMM_NULL, rank=0x009FFDB8) failed Invalid communicator [4] terminated [5] fatal error Fatal error in MPI_Comm_rank: Invalid communicator, error stack: MPI_Comm_rank(MPI_COMM_NULL, rank=0x00BDF7E4) failed Invalid communicator [6-8] terminated [9] fatal error Fatal error in MPI_Comm_rank: Invalid communicator, error stack: MPI_Comm_rank(MPI_COMM_NULL, rank=0x005FF7C0) failed Invalid communicator [10-16] terminated [17] fatal error Fatal error in MPI_Comm_rank: Invalid communicator, error stack: MPI_Comm_rank(MPI_COMM_NULL, rank=0x00DFFB58) failed Invalid communicator [18-29] terminated ---- error analysis ----- [1,3,5,9,17] on DRON mpi has detected a fatal error and aborted Debug\lab2_1.exe