Write a MPI program to read a 3 X 3 matrix. Enter an element to be searched in the root process. Find the
number of occurrences of this element in the matrix using three processes. Also, handle different errors
using error handling routines.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Below is an MPI program written in C that reads a 3x3 matrix, searches for a specific element entered by the root process (rank 0), and counts the number of occurrences of that element using three processes. The program also includes error handling to manage potential issues during execution.
Full MPI Program
c
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
#define SIZE 3 // Size of the matrix (3x3)
int main(int argc, char *argv[]) {
int rank, size;
int matrix[SIZE][SIZE]; // 3x3 matrix
int search_element; // Element to be searched
int local_count = 0; // Local count of occurrences for each process
int global_count = 0; // Total count of occurrences across all processes
int i, j;
// Initialize MPI
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
// Error handling: Ensure exactly 3 processes are used
if (size != 3) {
if (rank == 0) {
fprintf(stderr, "Error: This program requires exactly 3 processes.\n");
}
MPI_Finalize();
return EXIT_FAILURE;
}
// Root process (rank 0) reads the matrix and the element to search
if (rank == 0) {
printf("Enter the 3x3 matrix:\n");
for (i = 0; i < SIZE; i++) {
for (j = 0; j < SIZE; j++) {
if (scanf("%d", &matrix[i][j]) != 1) {
fprintf(stderr, "Error: Invalid input for matrix element.\n");
MPI_Abort(MPI_COMM_WORLD, EXIT_FAILURE);
}
}
}
printf("Enter the element to search: ");
if (scanf("%d", &search_element) != 1) {
fprintf(stderr, "Error: Invalid input for search element.\n");
MPI_Abort(MPI_COMM_WORLD, EXIT_FAILURE);
}
}
// Broadcast the search element and the matrix to all processes
if (MPI_Bcast(&search_element, 1, MPI_INT, 0, MPI_COMM_WORLD) != MPI_SUCCESS) {
fprintf(stderr, "Error: Failed to broadcast search element.\n");
MPI_Abort(MPI_COMM_WORLD, EXIT_FAILURE);
}
if (MPI_Bcast(matrix, SIZE * SIZE, MPI_INT, 0, MPI_COMM_WORLD) != MPI_SUCCESS) {
fprintf(stderr, "Error: Failed to broadcast matrix.\n");
MPI_Abort(MPI_COMM_WORLD, EXIT_FAILURE);
}
// Each process searches its assigned rows for the element
int rows_per_process = SIZE / size; // Rows handled by each process
int start_row = rank * rows_per_process;
int end_row = (rank == size - 1) ? SIZE : start_row + rows_per_process;
for (i = start_row; i < end_row; i++) {
for (j = 0; j < SIZE; j++) {
if (matrix[i][j] == search_element) {
local_count++;
}
}
}
// Reduce the local counts to get the global count
if (MPI_Reduce(&local_count, &global_count, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD) != MPI_SUCCESS) {
fprintf(stderr, "Error: Failed to perform reduction.\n");
MPI_Abort(MPI_COMM_WORLD, EXIT_FAILURE);
}
// Root process prints the result
if (rank == 0) {
printf("The element %d occurs %d times in the matrix.\n", search_element, global_count);
}
// Finalize MPI
MPI_Finalize();
return EXIT_SUCCESS;
}
Explanation of the Code
Initialization:
The program initializes MPI and retrieves the rank and size of the communicator.
It ensures that exactly 3 processes are used. If not, it terminates with an error message.
Input Handling:
The root process (rank 0) reads the 3x3 matrix and the element to be searched.
Input validation is performed to ensure correct data entry.
Broadcasting Data:
The search element and the matrix are broadcasted to all processes using MPI_Bcast.
Parallel Search:
Each process is assigned a subset of rows from the matrix based on its rank.
Each process searches its assigned rows for the specified element and counts its occurrences locally.
Reduction:
The local counts from all processes are reduced using MPI_Reduce to compute the total count of occurrences.
Output:
The root process prints the total count of occurrences.
Error Handling:
The program checks the return values of MPI functions and handles errors gracefully using MPI_Abort.
Finalization:
MPI is finalized at the end of the program.
How to Compile and Run
Save the code to a file, e.g., search_matrix.c.
Compile the program using an MPI compiler:
bash
mpicc search_matrix.c -o search_matrix
Run the program with 3 processes:
bash
mpirun -np 3 ./search_matrix
Example Input/Output
Input:
code
Enter the 3x3 matrix:
1 2 3
4 2 6
7 8 2
Enter the element to search: 2
Output:
code
The element 2 occurs 3 times in the matrix.
This program efficiently distributes the workload across three processes, ensuring scalability and correctness.