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 2be1860..0000000 Binary files a/serial/serial_gs_pagerank.o and /dev/null differ 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