From 1bbbefd517654c895c2f713840970f8b97eb79a3 Mon Sep 17 00:00:00 2001 From: Apostolof Date: Tue, 2 Oct 2018 02:01:38 +0300 Subject: [PATCH] Add comments --- serial/coo_sparse_matrix.c | 13 ++++++++++--- serial/coo_sparse_matrix.h | 27 ++++++++++++++++++++++++++ serial/csr_sparse_matrix.c | 3 +-- serial/csr_sparse_matrix.h | 23 ++++++++++++++++++++++ serial/serial_gs_pagerank.c | 1 - serial/serial_gs_pagerank.o | Bin 3696 -> 0 bytes serial/serial_gs_pagerank_functions.c | 9 +++------ 7 files changed, 64 insertions(+), 12 deletions(-) delete mode 100644 serial/serial_gs_pagerank.o diff --git a/serial/coo_sparse_matrix.c b/serial/coo_sparse_matrix.c index 93d0ac9..07d7293 100644 --- a/serial/coo_sparse_matrix.c +++ b/serial/coo_sparse_matrix.c @@ -15,9 +15,8 @@ void allocMemoryForCoo(CooSparseMatrix *sparseMatrix, int numberOfElements) { } void addElement(CooSparseMatrix *sparseMatrix, double value, int row, int column) { + // Checks if there is enough space allocated if (sparseMatrix->numberOfNonZeroElements == sparseMatrix->size) { - printf("%d == %d |||| %d, %d\n", sparseMatrix->numberOfNonZeroElements, - sparseMatrix->size, row, column); printf("Number of non zero elements exceeded size of matrix!\n"); exit(EXIT_FAILURE); } @@ -29,6 +28,7 @@ void addElement(CooSparseMatrix *sparseMatrix, double value, int row, int column newElement->rowIndex = row; newElement->columnIndex = column; + // Adds the new element to the first empty (NULL) address of the matrix sparseMatrix->elements[sparseMatrix->numberOfNonZeroElements] = newElement; sparseMatrix->numberOfNonZeroElements = sparseMatrix->numberOfNonZeroElements + 1; } @@ -42,14 +42,19 @@ void transposeSparseMatrix(CooSparseMatrix *sparseMatrix) { } } +/* + * This function is a port of the one found here: + * https://github.com/scipy/scipy/blob/3b36a57/scipy/sparse/sparsetools/coo.h#L34 +*/ void transformToCSR(CooSparseMatrix initialSparseMatrix, CsrSparseMatrix *transformedSparseMatrix) { - // Taken from here: https://github.com/scipy/scipy/blob/3b36a57/scipy/sparse/sparsetools/coo.h#L34 + // Checks if the sizes of the two matrices fit if (initialSparseMatrix.numberOfNonZeroElements > transformedSparseMatrix->size) { printf("Transformed CSR matrix does not have enough space!\n"); exit(EXIT_FAILURE); } + // Calculates the elements per row for (int i=0; irowIndex; transformedSparseMatrix->rowCumulativeIndexes[rowIndex] = @@ -63,6 +68,7 @@ void transformToCSR(CooSparseMatrix initialSparseMatrix, sum += temp; } + // Copies the values and columns of the elements for (int i=0; irowIndex; int destinationIndex = transformedSparseMatrix->rowCumulativeIndexes[row]; @@ -73,6 +79,7 @@ void transformToCSR(CooSparseMatrix initialSparseMatrix, transformedSparseMatrix->rowCumulativeIndexes[row]++; } + // Fixes the cumulative sum for (int i=0, last=0; i<=transformedSparseMatrix->size; i++){ int temp = transformedSparseMatrix->rowCumulativeIndexes[i]; transformedSparseMatrix->rowCumulativeIndexes[i] = last; diff --git a/serial/coo_sparse_matrix.h b/serial/coo_sparse_matrix.h index dd4c31d..0a34b7e 100644 --- a/serial/coo_sparse_matrix.h +++ b/serial/coo_sparse_matrix.h @@ -1,6 +1,8 @@ #ifndef COO_SPARSE_MATRIX_H /* Include guard */ #define COO_SPARSE_MATRIX_H +/* ===== INCLUDES ===== */ + #include #include #include @@ -8,26 +10,51 @@ #include "csr_sparse_matrix.h" +/* ===== STRUCTURES ===== */ + +// One element of the coordinate formated sparse matrix. typedef struct cooSparseMatrixElement { double value; int rowIndex, columnIndex; } CooSparseMatrixElement; +// A sparse matrix in COOrdinate format (aka triplet format). typedef struct cooSparseMatrix { int size, numberOfNonZeroElements; CooSparseMatrixElement **elements; } CooSparseMatrix; +/* ===== FUNCTION DEFINITIONS ===== */ + +// initCooSparseMatrix creates and initializes the members of a CooSparseMatrix +// structure instance. CooSparseMatrix initCooSparseMatrix(); + +//allocMemoryForCoo allocates memory for the elements of the matrix. void allocMemoryForCoo(CooSparseMatrix *sparseMatrix, int numberOfElements); + +// addElement adds an element representing the triplet passed in the arguments +// to the first empty address of the space allocated for the elements. void addElement(CooSparseMatrix *sparseMatrix, double value, int row, int column); + +// transposeSparseMatrix transposes the matrix. void transposeSparseMatrix(CooSparseMatrix *sparseMatrix); + +// transformToCSR transforms the sparse matrix representation format from COO +// to CSR. void transformToCSR(CooSparseMatrix initialSparseMatrix, CsrSparseMatrix *transformedSparseMatrix); + +// cooSparseMatrixVectorMultiplication calculates the product of a +// CooSparseMatrix and a vector. void cooSparseMatrixVectorMultiplication(CooSparseMatrix sparseMatrix, double *vector, double **product, int vectorSize); + +// destroyCooSparseMatrix frees all space used by the CooSparseMatrix. void destroyCooSparseMatrix(CooSparseMatrix *sparseMatrix); + +// printCooSparseMatrix prints the values of a CooSparseMatrix. void printCooSparseMatrix(CooSparseMatrix sparseMatrix); #endif // COO_SPARSE_MATRIX_H \ No newline at end of file diff --git a/serial/csr_sparse_matrix.c b/serial/csr_sparse_matrix.c index bdf8413..f1f9005 100644 --- a/serial/csr_sparse_matrix.c +++ b/serial/csr_sparse_matrix.c @@ -24,8 +24,8 @@ void allocMemoryForCsr(CsrSparseMatrix *sparseMatrix, int numberOfElements) { sparseMatrix->size = numberOfElements; } -// Row indexes start from 0! void zeroOutRow(CsrSparseMatrix *sparseMatrix, int row) { + // Gets start and end indexes of the row's elements int startIndex = sparseMatrix->rowCumulativeIndexes[row], endIndex = sparseMatrix->rowCumulativeIndexes[row+1]; for (int i=startIndex; inumberOfNonZeroElements; ++i){ if(sparseMatrix->columnIndexes[i] == column){ - // Zeros out this element sparseMatrix->values[i] = 0; } } diff --git a/serial/csr_sparse_matrix.h b/serial/csr_sparse_matrix.h index a964514..dddbe49 100644 --- a/serial/csr_sparse_matrix.h +++ b/serial/csr_sparse_matrix.h @@ -1,24 +1,47 @@ #ifndef CSR_SPARSE_MATRIX_H /* Include guard */ #define CSR_SPARSE_MATRIX_H +/* ===== INCLUDES ===== */ + #include #include #include #include +/* ===== STRUCTURES ===== */ + +// A sparse matrix in compressed SparseRow format. typedef struct csrSparseMatrix { int size, numberOfNonZeroElements; int *rowCumulativeIndexes, *columnIndexes; double *values; } CsrSparseMatrix; +/* ===== FUNCTION DEFINITIONS ===== */ + +// initCsrSparseMatrix creates and initializes the members of a CsrSparseMatrix +// structure instance. CsrSparseMatrix initCsrSparseMatrix(); + +// allocMemoryForCsr allocates memory for the elements of the matrix. void allocMemoryForCsr(CsrSparseMatrix *sparseMatrix, int numberOfElements); + +// zeroOutRow assigns a zero value to all the elements of a row in the matrix. void zeroOutRow(CsrSparseMatrix *sparseMatrix, int row); + +// zeroOutColumn assigns a zero value to all the elements of a column in the +// matrix. void zeroOutColumn(CsrSparseMatrix *sparseMatrix, int column); + +// csrSparseMatrixVectorMultiplication calculates the product of a +// CsrSparseMatrix and a vector. void csrSparseMatrixVectorMultiplication(CsrSparseMatrix sparseMatrix, double *vector, double **product, int vectorSize); + +// destroyCsrSparseMatrix frees all space used by the CsrSparseMatrix. void destroyCsrSparseMatrix(CsrSparseMatrix *sparseMatrix); + +// printCsrSparseMatrix prints the values of a CsrSparseMatrix. void printCsrSparseMatrix(CsrSparseMatrix sparseMatrix); #endif // CSR_SPARSE_MATRIX_H \ No newline at end of file diff --git a/serial/serial_gs_pagerank.c b/serial/serial_gs_pagerank.c index aba86f8..ccf25d1 100644 --- a/serial/serial_gs_pagerank.c +++ b/serial/serial_gs_pagerank.c @@ -3,7 +3,6 @@ #include "serial_gs_pagerank_functions.h" struct timeval startwtime, endwtime; -double seq_time; int main(int argc, char **argv) { CsrSparseMatrix transitionMatrix = initCsrSparseMatrix(); diff --git a/serial/serial_gs_pagerank.o b/serial/serial_gs_pagerank.o deleted file mode 100644 index 2be1860146053b42f6f5e4602e3a5c2180d6d372..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 3696 zcmb`JUuYaf7{KRpsfjV}rA2KtDpR;<(l*<>B&AiU=8`5^7_o z;ML-Jc+M!%d@;8KnF?ts6Yc9Y`{^0OrH*B!GM2fWx>icxq)~L;ff>s-tt`!1UZ!C7 z@29Ti*hZGxxz((dv+^D*jr6*ZEt{pOY$3HmJv(R7BeXXSYm%5t%)VH@J2zAQOI!c7 zq*WH9UCD|WjhWg;Yi}RSMJpeTX5v+?{N2wuv+^CSl8v?}%ReN`w;2IdE8iA&t#Sn2 z;=ePSR+){qZF$jo`2laytwpV0i=96g)kx&c4CA^kfneCN0g}frkTlYXE0Ib%QNT?rUHO!lXTb zOP7V@5hjhZe}vgv$z{n8)gIPwtcNn*IBqZFjq}>e`8<=`uLGRZZB*03zLs=c8M@O;Vc*N9(S1wTLxg`j zUaZiNm#9twJLnl#58vC3L)Xe0D#m~VoSWim z%_HS{;GQbv@PrW+{-Igw?rU+E2F0!15Voi({RmkDeAvSBLV68vem46Ktopl zWgMw_+^f{bKMiKQ!~Sdheu%}KgYd|0gm-?DA?V`16m>iYtXce1+yT$H+(ev^_+3K1 zVg0y+_^yc@;(H_dFM)z*XhZ-mGko*S<3@yrXBEc~_2Zp{?F2kH)Gq8u?K+PW{=y{s zLY@Kx#>a#Mmp6I*zBi?bBsL_McxT}n*T>^9!WGn7EAb6+t44gKe*QZFH;ey<*Z=On G#Qy^z_f)R{ diff --git a/serial/serial_gs_pagerank_functions.c b/serial/serial_gs_pagerank_functions.c index 4461f6f..82a163d 100644 --- a/serial/serial_gs_pagerank_functions.c +++ b/serial/serial_gs_pagerank_functions.c @@ -62,7 +62,7 @@ int pagerank(CsrSparseMatrix *transitionMatrix, double **pagerankVector, do { // Stores previous pagerank vector memcpy(previousPagerankVector, *pagerankVector, numberOfPages * sizeof(double)); - + // Calculates new pagerank vector calculateNextPagerank(transitionMatrix, previousPagerankVector, pagerankVector, linksFromConvergedPagesPagerankVector, @@ -126,8 +126,8 @@ int pagerank(CsrSparseMatrix *transitionMatrix, double **pagerankVector, } } - // Increases sparsity of the transition matrix by - // deleting elements that correspond to converged pages + // Increases sparsity of the transition matrix by zeroing + // out elements that correspond to converged pages zeroOutRow(transitionMatrix, i); zeroOutColumn(transitionMatrix, i); @@ -215,8 +215,6 @@ void calculateNextPagerank(CsrSparseMatrix *transitionMatrix, double *linksFromConvergedPagesPagerankVector, double *convergedPagerankVector, int vectorSize, double dampingFactor) { // Calculates the web uniform probability once. - - double webUniformProbability = 1. / vectorSize; csrSparseMatrixVectorMultiplication(*transitionMatrix, previousPagerankVector, @@ -232,7 +230,6 @@ void calculateNextPagerank(CsrSparseMatrix *transitionMatrix, for (int i=0; i