Homework 7 assignment due by 4/28/2026.

Homework 7 assignment due by 4/28/2026.#

MPI

Get a source code for matrix addition/substraction from here:

https://www.sanfoundry.com/c-program-addition-subtraction-trace-matrices/

copy/paste it into file matrix.c

A) Compile it and run for 2 x 2 matrices.

B) Copy matrix.c into matrix_broadcast_mpi.c Implement MPI parallelization in the main:

  • Initialize and finalize MPI environment in main,

  • Have only the process of rank 0 to input the parameters - matrix dimensions, matrix elements, and the option,

  • Call MPI_Bcast to broadcast them to the nodes:

    • MPI_Bcast(&option, 1, MPI_INT, 0, MPI_COMM_WORLD);

    • MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);

    • MPI_Bcast(&m, 1, MPI_INT, 0, MPI_COMM_WORLD);

    • MPI_Bcast(&array1, 100, MPI_INT, 0, MPI_COMM_WORLD);

    • MPI_Bcast(&array2, 100, MPI_INT, 0, MPI_COMM_WORLD);

C) Compile the code with ```mpicc`` and run the executable with 2 processes for 2 x 2 matrices.

D) Copy code matrix_broadcast_mpi.c to matrix_gather_mpi.c. Change the array dimension from 10 to 2, and modify it to give the matrix raws to the processes via MPI_Scatter, compute the raws, then gather them into matrix via MPI_Gather: using calls to MPI_Scatter and MPI_Gather:

  • MPI_Scatter(&array1, n, MPI_INT, &partial_array1, n, MPI_INT, 0, MPI_COMM_WORLD);

  • MPI_Scatter(&array2, n, MPI_INT, &partial_array2, n, MPI_INT, 0, MPI_COMM_WORLD);

  • MPI_Gather(&partial_diff, n, MPI_INT, arraysum, n, MPI_INT, 0, MPI_COMM_WORLD);