{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "4529d73c-a404-4814-87b7-5c286d64eff2",
   "metadata": {},
   "source": [
    "###  Homework 7 assignment due by 4/28/2026.\n",
    "MPI\n",
    "\n",
    "\n",
    "Get a source code for matrix addition/substraction from here:\n",
    "\n",
    "<TT>https://www.sanfoundry.com/c-program-addition-subtraction-trace-matrices/<TT>\n",
    "\n",
    "copy/paste it into file ```matrix.c```\n",
    "\n",
    "A) Compile it and run for 2 x 2 matrices.\n",
    "\n",
    "B) Copy ```matrix.c``` into ```matrix_broadcast_mpi.c```\n",
    "  Implement MPI parallelization in the ```main```:\n",
    "  - Initialize and finalize MPI environment in ```main```,\n",
    "  - Have only the process of rank 0 to input the parameters - matrix dimensions, matrix elements, and the option,\n",
    "  - Call MPI_Bcast to broadcast them to the nodes:\n",
    "     - MPI_Bcast(&option, 1, MPI_INT, 0, MPI_COMM_WORLD);\n",
    "     - MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);\n",
    "     - MPI_Bcast(&m, 1, MPI_INT, 0, MPI_COMM_WORLD);\n",
    "     - MPI_Bcast(&array1, 100, MPI_INT, 0, MPI_COMM_WORLD);\n",
    "     - MPI_Bcast(&array2, 100, MPI_INT, 0, MPI_COMM_WORLD);\n",
    "\n",
    "C) Compile the code with ```mpicc`` and run the executable with 2 processes for 2 x 2 matrices.\n",
    "\n",
    "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:\n",
    "  using calls to ```MPI_Scatter``` and ```MPI_Gather```:\n",
    "  - MPI_Scatter(&array1, n, MPI_INT, &partial_array1, n, MPI_INT, 0, MPI_COMM_WORLD);\n",
    "  - MPI_Scatter(&array2, n, MPI_INT, &partial_array2, n, MPI_INT,  0, MPI_COMM_WORLD);\n",
    "  - MPI_Gather(&partial_diff, n, MPI_INT, arraysum, n, MPI_INT, 0, MPI_COMM_WORLD);"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
