Due Date: 22/04/2025
Points: 100
Welcome to the first homework assignment of the Parallel Programming course! This is a warm-up exercise designed to
get you comfortable with low-level memory management and basic matrix operations in C++. You will implement a naive
matrix multiplication function (naive_matmul) that computes C = A * B using C-style arrays and manual memory
management. This assignment focuses on file I/O, memory management, and understanding matrix storage in memory,
setting the stage for parallelization topics later in the course.
Note: This assignment does not involve parallel programming. You will run your implementation locally on your machine.
- Implement the
naive_matmulfunction to perform matrix multiplicationC = A * Busing a triple nested loop (naive method). - Input matrices ( A ) and ( B ) will be provided in two text files:
input0.raw(for ( A )) andinput1.raw( for ( B )). - The resulting matrix ( C ) must be written to a text file named
result.raw. - The first row of
input0.rawandinput1.rawcontains matrix dimensions (number of rows and columns).
- Use only C-style arrays with manual memory allocation (
mallocornew) and deallocation (freeordelete). - Do not use smart pointers, STL containers (e.g.,
std::vector), or other dynamic memory libraries.
- Store all matrices as 1D arrays in row-major order.
- Row-Major vs. Column-Major Explanation:
- In row-major order, elements of a matrix are stored row-by-row in memory. For a matrix (
A) with dimensions (M * N), the element (A[i][j]) is accessed at index (i * N + j) in a 1D array. - In contrast, column-major order stores elements column-by-column (used in languages like Fortran). Since C++ uses row-major order by default, we adopt it here.
- For more details, refer to: Row- and Column-Major Order.
- In row-major order, elements of a matrix are stored row-by-row in memory. For a matrix (
- A reference solution file (
output.raw) is provided. - Compare your
result.rawwithoutput.rawto validate correctness. The test passes if they match exactly.
- Use CMake to configure and build your project. A sample
CMakeLists.txtis provided below. - Windows Users:
- Option 1: Use CLion with CMake (recommended). Load the project and build/run.
- Option 2: Use Visual Studio with CMake support. Open the folder and let VS configure it.
- Alternatively, install MinGW and run
cmake -G "MinGW Makefiles"followed bymake.
- Linux/Mac Users:
- Run
cmake .to generate a Makefile, thenmaketo build the executable.
- Run
The input files for this assignment are located in the data folder, which contains 10 test cases organized into
subfolders named 0 through 9. Each subfolder includes an input0.raw file (matrix ( A )), an input1.raw file (
matrix ( B )), and a reference output file output.raw (matrix ( C = A * B )). Your executable should accept a
case number (an integer from 0 to 9) as a command-line argument, use it to locate the corresponding folder in
data, and perform matrix multiplication using the input files in that folder. The resulting matrix should be written
to result.raw within the same subfolder. This setup allows automated testing across all 10 cases.
-
Fork this Repository
-
Clone the Fork
git clone https://github.com/parallelcomputingabo/Homework-1.git cd Homework-1 -
Create a New Branch
git checkout -b student-name
-
Implement Your Solution
-
Commit and Push
git add . git commit -m "student-name: Implemented naive matrix multiplication"
git push origin student-name -
Submit a Pull Request (PR)
- Go to your forked repository on GitHub.
- Click on the "Pull Requests" tab.
- Click on "New Pull Request".
- Select the base repository and branch (e.g.,
main) and compare it with your branch. - Add a title and description, then click "Create Pull Request".
| Subtask | Points |
|---|---|
| File reading and memory allocation | 20 |
| Matrix multiplication correctness | 30 |
| Correct use of row-major indexing | 15 |
| File writing and deallocation | 15 |
| Code clarity and commenting | 10 |
Successful test comparison with output.raw |
10 |
| Total | 100 |
Good luck, and have fun coding!