Skip to content
TheChatArchiveThe Chat Archive
gpt-4.1-mini-2025-04-14· OpenAIHugging Face LMArena Human Preference 140K

Write a MPI program to read a 3 X 3 matrix. Enter an element to be searched in the root…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Qwenqwen-max-2025-01-25’s answer.
  1. User

    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.

  2. Assistant

    Certainly! Below is an example MPI program in C that:

    • Reads a 3x3 matrix in the root process (rank 0).
    • Reads an element to be searched in the root process.
    • Distributes the matrix rows among 3 processes.
    • Each process counts the occurrences of the element in its assigned rows.
    • The root process collects the counts and prints the total occurrences.
    • Includes basic MPI error handling.

    MPI Program: Search element occurrences in a 3x3 matrix using 3 processes

    #include <mpi.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    #define MATRIX_SIZE 3
    
    // Error handling function
    void handle_mpi_error(int errcode, const char *msg) {
        if (errcode != MPI_SUCCESS) {
            char err_string[MPI_MAX_ERROR_STRING];
            int err_len;
            MPI_Error_string(errcode, err_string, &err_len);
            fprintf(stderr, "MPI Error in %s: %s\n", msg, err_string);
            MPI_Abort(MPI_COMM_WORLD, errcode);
            exit(EXIT_FAILURE);
        }
    }
    
    int main(int argc, char *argv[]) {
        int rank, size, errcode;
        int matrix[MATRIX_SIZE][MATRIX_SIZE];
        int element, local_count = 0, total_count = 0;
    
        errcode = MPI_Init(&argc, &argv);
        handle_mpi_error(errcode, "MPI_Init");
    
        errcode = MPI_Comm_rank(MPI_COMM_WORLD, &rank);
        handle_mpi_error(errcode, "MPI_Comm_rank");
    
        errcode = MPI_Comm_size(MPI_COMM_WORLD, &size);
        handle_mpi_error(errcode, "MPI_Comm_size");
    
        if (size != 3) {
            if (rank == 0) {
                fprintf(stderr, "Error: This program requires exactly 3 processes.\n");
            }
            MPI_Finalize();
            return EXIT_FAILURE;
        }
    
        if (rank == 0) {
            // Read the 3x3 matrix
            printf("Enter the 3x3 matrix elements:\n");
            for (int i = 0; i < MATRIX_SIZE; i++) {
                for (int j = 0; j < MATRIX_SIZE; j++) {
                    if (scanf("%d", &matrix[i][j]) != 1) {
                        fprintf(stderr, "Invalid input for matrix element.\n");
                        MPI_Abort(MPI_COMM_WORLD, EXIT_FAILURE);
                    }
                }
            }
    
            // Read the element to be searched
            printf("Enter the element to be searched: ");
            if (scanf("%d", &element) != 1) {
                fprintf(stderr, "Invalid input for element.\n");
                MPI_Abort(MPI_COMM_WORLD, EXIT_FAILURE);
            }
        }
    
        // Broadcast the element to all processes
        errcode = MPI_Bcast(&element, 1, MPI_INT, 0, MPI_COMM_WORLD);
        handle_mpi_error(errcode, "MPI_Bcast");
    
        // Scatter the matrix rows to each process
        // Each process gets one row (3 elements)
        int local_row[MATRIX_SIZE];
        errcode = MPI_Scatter(matrix, MATRIX_SIZE, MPI_INT, local_row, MATRIX_SIZE, MPI_INT, 0, MPI_COMM_WORLD);
        handle_mpi_error(errcode, "MPI_Scatter");
    
        // Each process counts occurrences of element in its row
        local_count = 0;
        for (int i = 0; i < MATRIX_SIZE; i++) {
            if (local_row[i] == element) {
                local_count++;
            }
        }
    
        // Reduce counts to root process
        errcode = MPI_Reduce(&local_count, &total_count, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
        handle_mpi_error(errcode, "MPI_Reduce");
    
        if (rank == 0) {
            printf("Total occurrences of element %d in the matrix: %d\n", element, total_count);
        }
    
        MPI_Finalize();
        return 0;
    }
    

    How to compile and run

    mpicc -o mpi_search_matrix mpi_search_matrix.c
    mpirun -np 3 ./mpi_search_matrix
    

    Explanation

    • The root process reads the matrix and the element.
    • The element is broadcast to all processes.
    • The matrix rows are scattered so each process gets one row.
    • Each process counts the occurrences of the element in its row.
    • The counts are reduced (summed) at the root process.
    • Basic error handling is done after each MPI call.
    • The program checks that exactly 3 processes are used.

    If you want me to add more sophisticated error handling or extend the program, just ask!

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.