novak-99 / MLPP

A library created to revitalize C++ as a machine learning front end. Per aspera ad astra.
MIT License
1.08k stars 155 forks source link

Optimizing matrix multiplication #4

Closed Jonas1312 closed 1 year ago

Jonas1312 commented 2 years ago

Impressive work!

You should swap the two inner loops here: https://github.com/novak-99/MLPP/blob/2a21d259997e44d80552b5c5842f05d4eae1d62a/MLPP/LinAlg/LinAlg.cpp#L80-L86

That is:

 for(int i = 0; i < A.size(); i++){ 
     for(int k = 0; k < B.size(); k++){ 
         for(int j = 0; j < B[0].size(); j++){ 
             C[i][j] += A[i][k] * B[k][j]; 
         } 
     } 
 } 

It won't change the result, but it should speed up the multiplication. Explanations: https://viralinstruction.com/posts/hardware/#15f5c31a-8aef-11eb-3f19-cf0a4e456e7a

Also, std::vector<std::vector<>> is not the best way to store a matrix: https://stackoverflow.com/a/55478808

novak-99 commented 2 years ago

Thanks so much for finding this, it looks to be a great optimization. I just pushed the commit with that change, and will look into better containers/data structures for storing matrices and vectors.

Jokeren commented 2 years ago

Great work Marc!

I can foresee one day you'll become an expert in C++ & ML. Keep practicing!

reddishshoib commented 2 years ago

Hey there I want want to contribute in this project. But I don't know how to Can u gyu's please Help me out