I want to write a demo to create a Cartesian topology using user-defined parameters. However when I tried to allow user to specify a custom Cartesian topology grid size, the program throws an error. Is it possible to allow user to input (without using command line arguments) values before creating the grid?
Edit: User input problem is solved; now the program freezes when creating the grid.
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
#define ROOT_PROC 0
#define ITGR_TRUE 1
#define DEF_DIMENS 2
int main(int argc, char *argv[]) {
int dim[] = {0, 0};
int mycoord[] = {0, 0};
int period[] = {0, 0};
double givenvalue;
int rank, size;
int cartrank;
MPI_Comm CART_GRID;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
if (rank == ROOT_PROC) {
printf("Make a*b grid with up to %d processes; Enter height (a):", size);
fflush(stdout);
scanf("%d", &dim[0]);
fflush(stdout);
printf("Make %d*b grid; Enter width (b):", dim[0]);
fflush(stdout);
scanf("%d", &dim[1]);
fflush(stdout);
printf("Enter a number (with/without decimal):");
fflush(stdout);
scanf("%lf", &givenvalue);
fflush(stdout);
if (dim[0] * dim[1] > size) {
printf("Too many processes; expected %d, got %d", size, dim[0] * dim[1]);
}
else if (dim[0] * dim[1] < 2) {
printf("Too little processes; do not create empty/1-dimensional grid");
} else {
printf("Make %d*%d grid\n", dim[0], dim[1]);
MPI_Cart_create(MPI_COMM_WORLD, 2, dim, period, ITGR_TRUE, &CART_GRID);
if (CART_GRID != MPI_COMM_NULL) {
return 0;
}
}
}
MPI_Finalize();
return 0;
}