The errors in the code stem primarily from incorrect usage of MPI functions and misinterpretation of MPI ranks, which lead to logical errors and prevent the code from outputting any log files. Here's a detailed explanation of what's wrong with the code and how to fix it:
1. Incorrect Use of MPI_Topo Functions and MPI Ranks:
-
Issue with MPI_Comm_rank and MPI_Cart_coords:
The code incorrectly uses MPI_Cart_coords with my_rank, which is the rank in MPI_COMM_WORLD. After creating a new communicator with MPI_Cart_create, the ranks might be reordered if reorder is set to 1. Therefore, we need to obtain the rank in the new communicator (comm2D) using MPI_Comm_rank and then get the coordinates using this new rank.
-
Fix:
MPI_Comm_rank(comm2D, &my_cart_rank);
MPI_Cart_coords(comm2D, my_cart_rank, ndims, coord);
2. Misinterpretation of MPI_Cart_shift Outputs:
-
Issue with MPI_Cart_shift Variables:
The variables nbr_i_lo, nbr_i_hi, nbr_j_lo, and nbr_j_hi are misleading. In MPI_Cart_shift, rank_source is the rank of the process from which data is received, and rank_dest is the rank of the process to which data is sent.
-
Fix:
Rename the variables to reflect their roles:
int rank_source_row, rank_dest_row;
int rank_source_col, rank_dest_col;
MPI_Cart_shift(comm2D, SHIFT_ROW, DISP, &rank_source_row, &rank_dest_row);
MPI_Cart_shift(comm2D, SHIFT_COL, DISP, &rank_source_col, &rank_dest_col);
3. Incorrect MPI_Sendrecv Usage:
-
Issue with MPI_Sendrecv:
The code uses the same neighbor rank for both sending and receiving, which can cause deadlocks because both processes are trying to send to and receive from each other simultaneously.
-
Fix:
For each communication direction, send to the rank_dest and receive from the rank_source. Also, use different message tags for different directions to avoid message mismatches:
// Exchange with row neighbors
if (rank_source_row != MPI_PROC_NULL || rank_dest_row != MPI_PROC_NULL) {
MPI_Sendrecv(&my_prime, 1, MPI_INT, rank_dest_row, 1,
&recv_prime_row, 1, MPI_INT, rank_source_row, 1,
comm2D, MPI_STATUS_IGNORE);
// Compare and log matches
if (rank_source_row != MPI_PROC_NULL && recv_prime_row == my_prime) {
// Log
}
}
// Exchange with column neighbors
if (rank_source_col != MPI_PROC_NULL || rank_dest_col != MPI_PROC_NULL) {
MPI_Sendrecv(&my_prime, 1, MPI_INT, rank_dest_col, 2,
&recv_prime_col, 1, MPI_INT, rank_source_col, 2,
comm2D, MPI_STATUS_IGNORE);
// Compare and log matches
if (rank_source_col != MPI_PROC_NULL && recv_prime_col == my_prime) {
// Log
}
}
4. Not Checking for MPI_PROC_NULL:
-
Issue:
When a process is at the boundary and has no neighbor in a direction, MPI_Cart_shift returns MPI_PROC_NULL. Attempting to send or receive from MPI_PROC_NULL is safe, but we should check for it when logging to avoid invalid rank reporting.
-
Fix:
Before logging or performing send/receive operations, check if the neighbor rank is not MPI_PROC_NULL.
5. Logging the Correct Ranks:
-
Issue:
In the logging, the code uses neighbor which doesn't accurately represent the rank from which it received the matching prime.
-
Fix:
Use rank_source_row or rank_source_col when logging, depending on the direction of communication.
Corrected Code:
Below is the corrected code with all the fixes applied:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <mpi.h>
#include <time.h>
#define SHIFT_ROW 0
#define SHIFT_COL 1
#define DISP 1
// Function to check if a number is prime
int is_prime(int num) {
if (num <= 1) return 0;
if (num <= 3) return 1;
if (num % 2 == 0 || num % 3 == 0) return 0;
for (int i = 5; i * i <= num; i += 6) {
if (num % i == 0 || num % (i + 2) == 0) return 0;
}
return 1;
}
int main(int argc, char *argv[]) {
int ndims = 2, size, my_rank, reorder, my_cart_rank, ierr;
int nrows, ncols;
int rank_source_row, rank_dest_row;
int rank_source_col, rank_dest_col;
MPI_Comm comm2D;
int dims[ndims], coord[ndims];
int wrap_around[ndims];
int my_prime, recv_prime_row, recv_prime_col;
char filename[20];
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
// Process command line arguments
if (argc == 3) {
nrows = atoi(argv[1]);
ncols = atoi(argv[2]);
dims[0] = nrows;
dims[1] = ncols;
if ((nrows * ncols) != size) {
if (my_rank == 0)
printf("ERROR: nrows*ncols=%d * %d = %d != %d\n",
nrows, ncols, nrows * ncols, size);
MPI_Finalize();
return 0;
}
} else {
dims[0] = dims[1] = 0;
}
// Create Cartesian topology
wrap_around[0] = wrap_around[1] = 0;
reorder = 1;
ierr = MPI_Dims_create(size, ndims, dims);
if (ierr != MPI_SUCCESS) {
printf("ERROR[%d] computing dims\n", ierr);
MPI_Finalize();
return 0;
}
ierr = MPI_Cart_create(MPI_COMM_WORLD, ndims, dims, wrap_around,
reorder, &comm2D);
if (ierr != MPI_SUCCESS) {
printf("ERROR[%d] creating CART\n", ierr);
MPI_Finalize();
return 0;
}
if (comm2D == MPI_COMM_NULL) {
// Not part of the new communicator
MPI_Finalize();
return 0;
}
MPI_Comm_rank(comm2D, &my_cart_rank);
MPI_Cart_coords(comm2D, my_cart_rank, ndims, coord);
MPI_Cart_shift(comm2D, SHIFT_ROW, DISP, &rank_source_row, &rank_dest_row);
MPI_Cart_shift(comm2D, SHIFT_COL, DISP, &rank_source_col, &rank_dest_col);
// Generate random prime number
srand(time(NULL) + my_cart_rank);
do {
my_prime = rand() % 1000 + 2;
} while (!is_prime(my_prime));
// Exchange and compare prime numbers for 500 iterations
for (int iter = 0; iter < 500; iter++) {
// Exchange with row neighbors
if (rank_source_row != MPI_PROC_NULL || rank_dest_row != MPI_PROC_NULL) {
MPI_Sendrecv(&my_prime, 1, MPI_INT, rank_dest_row, 1,
&recv_prime_row, 1, MPI_INT, rank_source_row, 1,
comm2D, MPI_STATUS_IGNORE);
// Compare and log matches
if (rank_source_row != MPI_PROC_NULL && recv_prime_row == my_prime) {
sprintf(filename, "rank_%d.txt", my_cart_rank);
FILE *fp = fopen(filename, "a");
if (fp != NULL) {
fprintf(fp, "Iteration %d: Prime %d matches with rank %d (row neighbor)\n",
iter, my_prime, rank_source_row);
fclose(fp);
}
}
}
// Exchange with column neighbors
if (rank_source_col != MPI_PROC_NULL || rank_dest_col != MPI_PROC_NULL) {
MPI_Sendrecv(&my_prime, 1, MPI_INT, rank_dest_col, 2,
&recv_prime_col, 1, MPI_INT, rank_source_col, 2,
comm2D, MPI_STATUS_IGNORE);
// Compare and log matches
if (rank_source_col != MPI_PROC_NULL && recv_prime_col == my_prime) {
sprintf(filename, "rank_%d.txt", my_cart_rank);
FILE *fp = fopen(filename, "a");
if (fp != NULL) {
fprintf(fp, "Iteration %d: Prime %d matches with rank %d (column neighbor)\n",
iter, my_prime, rank_source_col);
fclose(fp);
}
}
}
}
MPI_Comm_free(&comm2D);
MPI_Finalize();
return 0;
}
Summary of Fixes:
- Corrected the use of
MPI_Comm_rank and MPI_Cart_coords to obtain the correct rank and coordinates in the new communicator.
- Renamed variables from
nbr_* to rank_source_* and rank_dest_* to accurately represent their roles.
- Used appropriate
MPI_Sendrecv calls to send data to rank_dest_* and receive data from rank_source_*.
- Checked for
MPI_PROC_NULL before performing communications and logging.
- Used different tags (
1 for row and 2 for column) in MPI_Sendrecv calls to prevent message mismatches.
- Updated the logging statements to reflect matches with the correct neighbor ranks.
By applying these fixes, the code should now correctly perform the communications between neighbors, compare primes, and output log files when matches occur.