Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions .vscode/settings.json

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

normally we do not commit .vscode files because they are system dependent. Let's add this to the .gitignore.

Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"files.associations": {
"array": "cpp",
"atomic": "cpp",
"bit": "cpp",
"*.tcc": "cpp",
"cctype": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"compare": "cpp",
"concepts": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"deque": "cpp",
"string": "cpp",
"unordered_map": "cpp",
"vector": "cpp",
"exception": "cpp",
"algorithm": "cpp",
"functional": "cpp",
"iterator": "cpp",
"memory": "cpp",
"memory_resource": "cpp",
"numeric": "cpp",
"optional": "cpp",
"random": "cpp",
"string_view": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"utility": "cpp",
"fstream": "cpp",
"initializer_list": "cpp",
"iosfwd": "cpp",
"iostream": "cpp",
"istream": "cpp",
"limits": "cpp",
"new": "cpp",
"numbers": "cpp",
"ostream": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"typeinfo": "cpp"
}
}
28 changes: 28 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
Binary file added MMformat.ps

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing the variables in this file and following the provided instruction does not change the result in the result.mm file. Maybe the instruction is not clear for that.

Binary file not shown.
48 changes: 47 additions & 1 deletion README.md

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The assignment asks you to implement a separate driver file and library that contains the functions to compute the two inner products. This needs to be updated. Having instructions to build these on the command line rather than CMake or a makefile is fine for assignment 0.

Original file line number Diff line number Diff line change
@@ -1 +1,47 @@
# cpp
# Matrix Computation Toolkit for Matrix Market format
This repository contains the necessary toolkit for Matrix Market format.

## Testing

The repository contains
```bash
mat_mat.cpp
mat_vec.cpp
mmf_product.cpp
```

_mat_vec.cpp_ contains the necessary code to run dense matrix and vector product.
_mat_mat.cpp_ contains the necessary code to run dense matrix matrix product.
_mmf_product.cpp_ contains the master code for the aforementioned operations.

You can take a look at either the individual code or test out sample cases by just double clicking the executables. With the same filename as the cpp files without any extension.

Please note that executing the _mmf_product_ file requires you to specify the computation type:


0. Matrix vector product
1. Matrix matrix product

Both CMake and the linkage between the cpp files have not been implemented yet.

## Using your own input file
You can test the code with your own input file by simply editing the paths for the two matrices/matrix-vector:
```bash
matA = "MMformat.ps";
matB = "MMformat.ps";
```

or uncomment the couts,
```bash
std::cout<<"First Matrix filename is: ";
std::cin >> matA;
std::cout << "Second Matrix filename is : ";
std::cin >> matB;
```
and compile the _mmf_product.cpp_ by simply typing:
```bash
g++ mmf_product.cpp -o mmf_product
```
The above line will update the executable file. Once you run the executable you will see the promps where you can type the input path. The results will be stored in the _results.mm_ file.


24 changes: 24 additions & 0 deletions assignment0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
## Assignment 0
#(a)
I tested my code by creating an executable and running on the command prompt. The matrices used for testing are synthetically generated.

#(b)
The limitations of the matrix market is that it is widely supported for sparse matrices. However, it is not efficient and have limited data types. It does not scale with larger datasets.

#(c)
Right now, my code reads the size of the matrix when it is inputed. However, if I already know the size of the matrices, I would use static arrays, which is more efficient/

#(d)
The matrix is row-major order. This is default for cpp application. If I use column-major it would negatively impact the performance because CPU can cache rows more efficiently.

#(e)
I used in-line comments to describe the functionality of the code.

#(f)
The code may not be best at handling all possible edge cases. For example, when the data does not follow the MMTX format and possibly have strings in the data sequence, the proposed algorithm would not be able to handle the information.

#(g)
Current implementation of the code has different functions defined for the vector and matrix reading. In the future, this could be improved, by reading the user input and automatically switching between vector and matrix within a function. Moreover, the functions are pasted into one single cpp file, which could be improved by using hpp (header) files and importing from another .cpp file for better readability. In addition, there are limitation from the format, where dense matrices cause higher memory consumption.

#(h)
The approach I would take to optimize the code would be using headers.
Binary file added mat_mat
Binary file not shown.
48 changes: 48 additions & 0 deletions mat_mat.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <iostream>
#include <vector>

std::vector<std::vector<double>> mat_mat(const std::vector<std::vector<double>>&mat1,const std::vector<std::vector<double>>&mat2) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using an alias type to avoid writing vector<vector<double>> everywhere. i.e., using Matrix = std::vector<std::vector<double>>

It is fine for this assignment, however vector<vector>> will not tend to give good performance since each column is an independent allocation.

size_t mat1row = mat1.size();
size_t mat1col = mat1[0].size();
size_t mat2row = mat2.size();
size_t mat2col = mat2[0].size();

if (mat1col != mat2row){
throw std::invalid_argument("Matrix dimensions do not match. Abort");
}

std::vector<std::vector<double>> result(mat1row,std::vector<double>(mat2col,0.0));

for (size_t i = 0; i < mat1row; i++){
for (size_t j = 0; j < mat2col; j++){
for (size_t k = 0; k < mat1col; k++){
result[i][j] += mat1[i][k] * mat2[k][j];
}
}
}

return result;
}

void printmat(const std::vector<std::vector<double>> & mat){

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed that the first function was using snake_case? Do you have a style guide? We should make this consistent.

for (const std::vector<double> & row : mat){

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consider using auto& for range based for loop.

for (double val: row){
std::cout << val << " ";

}
std::cout << std::endl;
}
}


int main() {
std::vector<std::vector<double>> matrix1 = {{1.0,2.0,3.0}, {1.0,2.0,3.0},{1.0,2.0,3.0}};
std::vector<std::vector<double>> matrix2 = {{1.0,2.0,3.0}, {1.0,2.0,3.0},{1.0,2.0,3.0}};
//std::vector<double> vector = {1.0,2.0};

std::vector<std::vector<double>> result = mat_mat(matrix1,matrix2);
std::cout << "The product is: "<< std::endl;
printmat(result);
return 0;
}

Binary file added mat_vec

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make sure you are not commiting any binaries. You may need to add them to your .gitignore

Binary file not shown.
35 changes: 35 additions & 0 deletions mat_vec.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <iostream>
#include <vector>

std::vector<double> mat_vec(const std::vector<std::vector<double>>& mat, const std::vector<double>& vec) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The naming of this function is not ideal. If you did not have the context of the assignment, you would not know what this function does without reading the code. Consider renaming it to matrix_vector_product, or inner_product, the latter you would use the same name for matrix-vector and matrix-matrix product but take advantage of function overloading.


size_t matcol = mat[0].size();
size_t vecsize = vec.size();

if(matcol != vecsize) {
throw std::invalid_argument("Matrix and vector dimensions do not match.");
}

std::vector<double> result(mat.size(), 0.0);

for(size_t i = 0; i < mat.size(); i++) {
for(size_t j = 0; j < matcol; j++) {
result[i] += mat[i][j] * vec[j];
}
}

return result;
}

int main() {
std::vector<std::vector<double>> matrix = {{1.0,2.0,3.0}, {1.0,2.0,3.0},{1.0,2.0,3.0}};
std::vector<double> vector = {1.3,2.0,3.0};
//std::vector<double> vector = {1.0,2.0};

std::vector<double> result = mat_vec(matrix,vector);
std::cout << "The product is: ";
for (double val:result) {
std::cout<<val << " ";
}
return 0;
}
Binary file added mmf_product
Binary file not shown.
Loading