This is the Class for handling Large Matrices. It takes in large Matrices stored as comma-separated values (CSV) files and perform both Multiplication (Strassan's Algorithm) and transpose. It contains all the important functions namely ,matmul function, StrassanMultiply function and other helper functions that are needed for smooth functioning of the library. This class also contains blocks of code that are gated by the SET_LEAF_SIZE flag, for example the set_LEAF_SIZE() function and the set_configerd_Leaf_size() function. When matrix.h is compiled with the SET_LEAF_SIZE flag raised the set_LEAF_SIZE() function is active and set_configerd_Leaf_size() function is inactive. And the vice-versa happens when matrix.h is compiled without the SET_LEAF_SIZE flag.
More...
#include <matrix.h>
|
void | Mat_print (std::string path) |
| Function to print a Matrix from a .csv file. More...
|
|
void | set_LEAF_SIZE (int leaf_size) |
| This Function sets the LEAF_SIZE i.e. the array size at which we shift from Strassan's Algo to normal O(n^3) solution, this prevents the StrassenMultiply recursion function from down to a leaf size/Matrix size of 1. Once we reach a square matrix array of size LEAF_SIZE x LEAF_SIZE or lesser, we perform the multiplication using the naive O(n^3) time complexity solution. More...
|
|
void | set_configerd_Leaf_size () |
| When matrix.h is normally called i.e. without any SET_LEAF_SIZE flag, set_configerd_Leaf_size() function gets activated (set_LEAFT_SIZE() function gets deactivated). The set_configerd_Leaf_size() function searches for the configure.txt file (generated earlier by configure_lib.cpp during the configuration process) and sets the optimal value of LEAF_SIZE. This function is called by the MATOPS::BigMatrix<Data1>::matmul before starting the multiplication process. More...
|
|
void | matmul (std::string file_1, std::string file_2, std::string path, bool print=false) |
| This is the BigMatrix multiplication Function that multiplies two matrices A and B stored in A.csv and B.csv respectively and store the result in C.csv file. Before beginning the multiplication process it calls the MATOPS::BigMatrix<Data1>::set_configerd_Leaf_size() function to parses the configure.txt (generated by configure_lib.cpp during the configuration process) and sets the optimal LEAF_SIZE value. More...
|
|
void | Transpose (std::string path, std::string str_path) |
| This is a function to find the Transpose of a BigMatrix and stores it in a csv file. More...
|
|
void | Transpose (std::string path) |
| This is a function to find the Transpose of a BigMatrix and stores it in the same csv file (In-palce transpose). More...
|
|
template<typename Data1>
class MATOPS::BigMatrix< Data1 >
This is the Class for handling Large Matrices. It takes in large Matrices stored as comma-separated values (CSV) files and perform both Multiplication (Strassan's Algorithm) and transpose. It contains all the important functions namely ,matmul function, StrassanMultiply function and other helper functions that are needed for smooth functioning of the library. This class also contains blocks of code that are gated by the SET_LEAF_SIZE flag, for example the set_LEAF_SIZE() function and the set_configerd_Leaf_size() function. When matrix.h is compiled with the SET_LEAF_SIZE flag raised the set_LEAF_SIZE() function is active and set_configerd_Leaf_size() function is inactive. And the vice-versa happens when matrix.h is compiled without the SET_LEAF_SIZE flag.
- Template Parameters
-
Data1 | = Datatype of the BigMatrix. Eg. int, float, double etc. |
◆ add()
Function to Add 2 square Matrices of size n.
- Parameters
-
M1 | = Pointer pointing to BigMatrix M1 loaded into memory |
M2 | = Pointer pointing to BigMatrix M2 loaded into memory |
n | = Size of the Matrices |
- Returns
- Returns a pointer pointing to a memory location containing the sum of M1 and M2
368 for(
int i=0; i<n; i++)
369 for(
int j=0; j<n; j++)
370 temp[i][j] = M1[i][j] + M2[i][j];
Data1 ** Init_matrix(int n)
Function to dynamically allocate/initialize an n x n matrix in the memory.
Definition: matrix.h:345
◆ Init_matrix()
Function to dynamically allocate/initialize an n x n matrix in the memory.
- Parameters
-
- Returns
- Returns a pointer pointing to a memory location containing 0 matrix of size n x n.
349 M=(Data1 **)calloc(n,
sizeof(Data1*));
352 M[i]=(Data1*)calloc(n,
sizeof(Data1));
◆ load_CSV()
template<typename Data1>
std::vector<std::vector<Data1> > MATOPS::BigMatrix< Data1 >::load_CSV |
( |
const std::string & |
path | ) |
|
|
inlineprivate |
Function to load CSV file. This function is internally called by MATOPS::BigMatrix<Data1>::matmul and MATOPS::BigMatrix< Data1 >::Transpose to load the BigMatrix's to be multiplied or Transposed. It throws an error if path is invalid or CSV doesn't exist.
- Parameters
-
path= | "path to CSV file i.e. to be loaded" |
- Returns
- A pointer to the Matrix loaded in memory.
617 std::ifstream indata;
622 std::cerr<<
"File path: '"<<path<<
"' doesn't exist\n";
626 std::vector<std::vector<Data1>> dataList;
627 std::string line =
"";
629 while(getline(indata,line))
631 std::stringstream lineStream(line);
633 std::vector<Data1> temp;
634 while (std::getline(lineStream,cell,
','))
636 temp.push_back(convert_to<Data1>(cell));
638 dataList.push_back(temp);
◆ Mat_print()
Function to print a Matrix from a .csv file.
- Parameters
-
path | = "path to .csv i.e. to be printed" |
652 std::vector<std::vector<Data1>> MAT=
load_CSV(path);
654 for(std::vector<Data1> a: MAT)
std::vector< std::vector< Data1 > > load_CSV(const std::string &path)
Function to load CSV file. This function is internally called by MATOPS::BigMatrix<Data1>::matmul and...
Definition: matrix.h:615
◆ matmul()
template<typename Data1>
void MATOPS::BigMatrix< Data1 >::matmul |
( |
std::string |
file_1, |
|
|
std::string |
file_2, |
|
|
std::string |
path, |
|
|
bool |
print = false |
|
) |
| |
|
inline |
This is the BigMatrix multiplication Function that multiplies two matrices A and B stored in A.csv and B.csv respectively and store the result in C.csv file. Before beginning the multiplication process it calls the MATOPS::BigMatrix<Data1>::set_configerd_Leaf_size() function to parses the configure.txt (generated by configure_lib.cpp during the configuration process) and sets the optimal LEAF_SIZE value.
- Parameters
-
file_1 | = "path to A.csv" |
file_2 | = "path to B.csv" |
path | = path to a store file (no need to predefine C.csv file in the directory, it gets generated automatically.) |
print | = True, To see all the Matrices i.e. A,B and C in the output terminal/stdio. |
Overall Working: When matmul is called, it first sets the the optimal LEAF_SIZE value. It then loads the two csv files from "path to A.csv" and "path to B.csv" directories into two 2D vectors namely MAT_1 and MAT_2 of type Data1 using MATOPS::BigMatrix<Data1>::load_CSV function. The dimensions of both the matrices are determined from MAT_1 and MAT_2 say m_1,n_1 ad m_2,n_2 respectively. If the inner dimensions of both MAT_1 and MAT_2 don't match an error is thrown and the program is exited. If the inner dimensions match then we proceed for Multiplication. Before calling the MATOPS::BigMatrix<Data1>::StrassenMultiply fuction, we pad the matrices with zeros to make both MAT_1 and MAT_2 square matrices of equal dimension, dim_n. Where dim_n is a power of 2 i.e. greater than or equal to max(m_1,n_1,n_2) or max(m_1,m_2,n_2) (since n_1=m_2). We callocate (by calling MATOPS::BigMatrix<Data1>::Init_matrix(dim_n) two chunk of memory of size dim_n x dim_n pointed by pointers A and B. Finally we build the Zero padded square Matrices by copying the contents from from MAT_1 and MAT_2 into the memory (pointed by A and B) respectively. These Memory addresses and dim_n are then passed on to the MATOPS::BigMatrix<Data1>::StrassenMultiply function to evaluate the product of A and B. The MATOPS::BigMatrix<Data1>::StrassenMultiply() func returns a pointer pointing to a location in the memory where the answer is stored. This memory address is then passed to the MATOPS::BigMatrix<Data1>::store_csv() along with the resultant matrix dimension (i.e. m_1 x n_2) and the storage destination path to store the final result in a csv file.
Finally all the callocated memories are freed up using the free command.
735 #ifndef SET_LEAF_SIZE 742 std::vector<std::vector<Data1>> MAT_1=
load_CSV(file_1);
743 std::vector<std::vector<Data1>> MAT_2=
load_CSV(file_2);
746 int m_1=MAT_1.size(), n_1=MAT_1[0].size();
747 int m_2=MAT_2.size(), n_2=MAT_2[0].size();
752 throw "Matrix Inner Dimensions don't match !!! \n";
754 }
catch (
const char* msg)
756 std::cerr<<msg<<
'\n';
760 int max_n= std::max(std::max(m_1,n_1),n_2);
779 for(
int k=0;k<m_1;k++)
781 for(
int l=0;l<n_1;l++)
783 A[k][l]= MAT_1[k][l];
787 for(
int k=0;k<m_2;k++)
789 for(
int l=0;l<n_2;l++)
791 B[k][l]= MAT_2[k][l];
805 std::cout<<
"\nB: \n";
808 std::cout<<
"\nANSWER: \n";
814 store_csv<Data1>(C, m_1,n_2,path);
817 for(
int i=0;i<dim_n;i++)
std::vector< std::vector< Data1 > > load_CSV(const std::string &path)
Function to load CSV file. This function is internally called by MATOPS::BigMatrix<Data1>::matmul and...
Definition: matrix.h:615
Data1 ** Init_matrix(int n)
Function to dynamically allocate/initialize an n x n matrix in the memory.
Definition: matrix.h:345
void print_Mat(Data1 **C, int m, int n)
This function is called from within the MATOPS::BigMatrix<Data1>::matmul function when print == True...
Definition: matrix.h:596
void set_configerd_Leaf_size()
When matrix.h is normally called i.e. without any SET_LEAF_SIZE flag, set_configerd_Leaf_size() funct...
Definition: matrix.h:696
Data1 ** StrassenMultiply(Data1 **A, Data1 **B, int n)
The main Strassen's Algorithm function implemented using recursion. Takes in square Matrices A and B...
Definition: matrix.h:405
◆ print_Mat()
This function is called from within the MATOPS::BigMatrix<Data1>::matmul function when print == True.
- Parameters
-
C | Pointer to the memory where the array is stored. |
m | No of rows of the Matrix |
n | No of Columns of the Matrix |
602 std::cout<<C[i][j]<<
" ";
◆ set_configerd_Leaf_size()
When matrix.h is normally called i.e. without any SET_LEAF_SIZE flag, set_configerd_Leaf_size() function gets activated (set_LEAFT_SIZE() function gets deactivated). The set_configerd_Leaf_size() function searches for the configure.txt file (generated earlier by configure_lib.cpp during the configuration process) and sets the optimal value of LEAF_SIZE. This function is called by the MATOPS::BigMatrix<Data1>::matmul before starting the multiplication process.
698 std::ifstream indata;
699 indata.open(
"configure.txt");
int LEAF_SIZE
Definition: matrix.h:332
◆ set_LEAF_SIZE()
This Function sets the LEAF_SIZE i.e. the array size at which we shift from Strassan's Algo to normal O(n^3) solution, this prevents the StrassenMultiply recursion function from down to a leaf size/Matrix size of 1. Once we reach a square matrix array of size LEAF_SIZE x LEAF_SIZE or lesser, we perform the multiplication using the naive O(n^3) time complexity solution.
A very high value of LEAF_SIZE leads to lesser recursion calls but ends up giving more weightage to the O(n^3) solution, thus suffer high execution time.
On the other hand a very low LEAF_SIZE value leads to higher number of resursion calls and gives lesser weightage to the O(n^3) solution, which again leads to high execution time.
Hence an optimal LEAF_SIZE value has to be found that gives the minimum execution time by finding a trade off between the O(n^3) solution and recurssion calls. This indirectly implies that the value of LEAF_SIZE vary from one machine to another, thus we need to experimentally find out this value for each machine and configure the matrix.h library accordingly. This experimentation is done by the configure_lib.cpp, which raises the SET_LEAF_SIZE flag (thus activating the set_LEAF_SIZE() function) during execution, as a result of which the "matrix.h" library is called (in its configuration mode) by configure_lib.cpp. This allows configure_lib.cpp to manipulate the LEAF_SIZE (private variable) of the matrix.h file. Hence configure.cpp can test the hardware for different values of LEAF_SIZE and pick its optimal (i.e. the value that gives the lowest execution time) value and finally store the optimal value in the configure.txt file.
int LEAF_SIZE
Definition: matrix.h:332
◆ StrassenMultiply()
template<typename Data1>
Data1** MATOPS::BigMatrix< Data1 >::StrassenMultiply |
( |
Data1 ** |
A, |
|
|
Data1 ** |
B, |
|
|
int |
n |
|
) |
| |
|
inlineprivate |
The main Strassen's Algorithm function implemented using recursion. Takes in square Matrices A and B.
- Parameters
-
- Returns
- Returns a square matrix i.e. the Multiplication result of A and B.
The LEAF_SIZE is set by the MATOPS::BigMatrix<Data1>::set_configerd_Leaf_size() function from witin the MATOPS::BigMatrix<Data1>::matmul() function. The input Matrices A and B are both broken down into 4 blocks, these matrices are used to calculate the 7 Strassen's coeffcient matrices. In order to calculate the Strassen's Coefficent the StrassenMultiply function is recurssively called. Once the matrix sizes becomes equal to or less than LEAF_SIZE, we hit the base condition and perform the Matrix multiplication using the O(n^3) solution. Finally all the callocated memory (allocated by Init_matrix()) is freed up.
427 C[i][j]+=A[i][k]*B[k][j];
453 A12[i][j] = A[i][k+j];
454 A21[i][j] = A[k+i][j];
455 A22[i][j] = A[k+i][k+j];
457 B12[i][j] = B[i][k+j];
458 B21[i][j] = B[k+i][j];
459 B22[i][j] = B[k+i][k+j];
463 Data1** TEMP_B12_B22 =
sub(B12, B22, k);
464 Data1** TEMP_A11_A12 =
add(A11, A12, k);
465 Data1** TEMP_A21_A22 =
add(A21, A22, k);
466 Data1** TEMP_B21_B11 =
sub(B21, B11, k);
474 Data1** TEMP_A11_A22 =
add(A11, A22, k);
475 Data1** TEMP_B11_B22 =
add(B11, B22, k);
476 Data1** TEMP_A12_A22 =
sub(A12, A22, k);
477 Data1** TEMP_B21_B22 =
add(B21, B22, k);
478 Data1** TEMP_A11_A21 =
sub(A11, A21, k);
479 Data1** TEMP_B11_B12 =
add(B11, B12, k);
485 Data1** TEMP_P5_P4 =
add(P5, P4, k);
486 Data1** TEMP_P5_P4_P6 =
add(TEMP_P5_P4, P6, k);
487 Data1** TEMP_P5_P1 =
add(P5, P1, k);
488 Data1** TEMP_P5_P1_P3 =
sub(TEMP_P5_P1, P3, k);
491 Data1** C11 =
sub(TEMP_P5_P4_P6, P2, k);
492 Data1** C12 =
add(P1, P2, k);
493 Data1** C21 =
add(P3, P4, k);
494 Data1** C22 =
sub(TEMP_P5_P1_P3, P7, k);
498 for(
int i=0; i<k; i++)
500 for(
int j=0; j<k; j++)
503 C[i][j+k] = C12[i][j];
504 C[k+i][j] = C21[i][j];
505 C[k+i][k+j] = C22[i][j];
510 for(
int i=0; i<k; i++) {
511 free(TEMP_B12_B22[i]);
512 free(TEMP_A11_A12[i]);
513 free(TEMP_A21_A22[i]);
514 free(TEMP_B21_B11[i]);
516 free(TEMP_A11_A22[i]);
517 free(TEMP_B11_B22[i]);
518 free(TEMP_A12_A22[i]);
519 free(TEMP_B21_B22[i]);
520 free(TEMP_A11_A21[i]);
521 free(TEMP_B11_B12[i]);
524 free(TEMP_P5_P4_P6[i]);
526 free(TEMP_P5_P1_P3[i]);
int LEAF_SIZE
Definition: matrix.h:332
Data1 ** Init_matrix(int n)
Function to dynamically allocate/initialize an n x n matrix in the memory.
Definition: matrix.h:345
Data1 ** sub(Data1 **M1, Data1 **M2, int n)
Function to Subtract 2 square Matrices of size n.
Definition: matrix.h:382
Data1 ** add(Data1 **M1, Data1 **M2, int n)
Function to Add 2 square Matrices of size n.
Definition: matrix.h:365
Data1 ** StrassenMultiply(Data1 **A, Data1 **B, int n)
The main Strassen's Algorithm function implemented using recursion. Takes in square Matrices A and B...
Definition: matrix.h:405
◆ sub()
Function to Subtract 2 square Matrices of size n.
- Parameters
-
M1 | = Pointer pointing to BigMatrix M1 loaded into memory |
M2 | = Pointer pointing to BigMatrix M2 loaded into memory |
n | = Size of the Matrices |
- Returns
- Returns a pointer pointing to a memory location containing the difference of M1 and M2
385 for(
int i=0; i<n; i++)
386 for(
int j=0; j<n; j++)
387 temp[i][j] = M1[i][j] - M2[i][j];
Data1 ** Init_matrix(int n)
Function to dynamically allocate/initialize an n x n matrix in the memory.
Definition: matrix.h:345
◆ Transpose() [1/2]
template<typename Data1>
void MATOPS::BigMatrix< Data1 >::Transpose |
( |
std::string |
path, |
|
|
std::string |
str_path |
|
) |
| |
|
inline |
This is a function to find the Transpose of a BigMatrix and stores it in a csv file.
- Parameters
-
path | = "path/to/A.csv" |
str_path | = path to store the Transpose of BigMatrix A. |
842 std::vector<std::vector<Data1>> MAT=
load_CSV(path);
844 Data1** A= (Data1 **)calloc(MAT[0].size(),
sizeof(Data1*));
846 for(
int i=0;i<MAT[0].size();i++)
848 A[i]=(Data1*)calloc(MAT.size(),
sizeof(Data1));
851 for(
int i=0;i<MAT.size();i++)
853 for(
int j=0;j<MAT[0].size();j++)
859 store_csv<Data1>(A,MAT[0].size(),MAT.size(),str_path);
860 for(
int i=0;i<MAT[0].size();i++)
std::vector< std::vector< Data1 > > load_CSV(const std::string &path)
Function to load CSV file. This function is internally called by MATOPS::BigMatrix<Data1>::matmul and...
Definition: matrix.h:615
◆ Transpose() [2/2]
This is a function to find the Transpose of a BigMatrix and stores it in the same csv file (In-palce transpose).
- Parameters
-
873 std::vector<std::vector<Data1>> MAT=
load_CSV(path);
875 Data1** A= (Data1 **)calloc(MAT[0].size(),
sizeof(Data1*));
877 for(
int i=0;i<MAT[0].size();i++)
879 A[i]=(Data1*)calloc(MAT.size(),
sizeof(Data1));
882 for(
int i=0;i<MAT.size();i++)
884 for(
int j=0;j<MAT[0].size();j++)
890 store_csv<Data1>(A,MAT[0].size(),MAT.size(),path);
891 store_csv<Data1>(A,MAT[0].size(),MAT.size(),path);
893 for(
int i=0;i<MAT[0].size();i++)
std::vector< std::vector< Data1 > > load_CSV(const std::string &path)
Function to load CSV file. This function is internally called by MATOPS::BigMatrix<Data1>::matmul and...
Definition: matrix.h:615
◆ LEAF_SIZE
The documentation for this class was generated from the following file: