Browse Source

Add comments

master
Apostolos Fanakis 6 years ago
parent
commit
1bbbefd517
No known key found for this signature in database GPG Key ID: 56CE2DEDE9F1FB78
  1. 13
      serial/coo_sparse_matrix.c
  2. 27
      serial/coo_sparse_matrix.h
  3. 3
      serial/csr_sparse_matrix.c
  4. 23
      serial/csr_sparse_matrix.h
  5. 1
      serial/serial_gs_pagerank.c
  6. BIN
      serial/serial_gs_pagerank.o
  7. 9
      serial/serial_gs_pagerank_functions.c

13
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) { void addElement(CooSparseMatrix *sparseMatrix, double value, int row, int column) {
// Checks if there is enough space allocated
if (sparseMatrix->numberOfNonZeroElements == sparseMatrix->size) { 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"); printf("Number of non zero elements exceeded size of matrix!\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@ -29,6 +28,7 @@ void addElement(CooSparseMatrix *sparseMatrix, double value, int row, int column
newElement->rowIndex = row; newElement->rowIndex = row;
newElement->columnIndex = column; newElement->columnIndex = column;
// Adds the new element to the first empty (NULL) address of the matrix
sparseMatrix->elements[sparseMatrix->numberOfNonZeroElements] = newElement; sparseMatrix->elements[sparseMatrix->numberOfNonZeroElements] = newElement;
sparseMatrix->numberOfNonZeroElements = sparseMatrix->numberOfNonZeroElements + 1; 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, void transformToCSR(CooSparseMatrix initialSparseMatrix,
CsrSparseMatrix *transformedSparseMatrix) { 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) { if (initialSparseMatrix.numberOfNonZeroElements > transformedSparseMatrix->size) {
printf("Transformed CSR matrix does not have enough space!\n"); printf("Transformed CSR matrix does not have enough space!\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
// Calculates the elements per row
for (int i=0; i<initialSparseMatrix.numberOfNonZeroElements; ++i){ for (int i=0; i<initialSparseMatrix.numberOfNonZeroElements; ++i){
int rowIndex = initialSparseMatrix.elements[i]->rowIndex; int rowIndex = initialSparseMatrix.elements[i]->rowIndex;
transformedSparseMatrix->rowCumulativeIndexes[rowIndex] = transformedSparseMatrix->rowCumulativeIndexes[rowIndex] =
@ -63,6 +68,7 @@ void transformToCSR(CooSparseMatrix initialSparseMatrix,
sum += temp; sum += temp;
} }
// Copies the values and columns of the elements
for (int i=0; i<initialSparseMatrix.numberOfNonZeroElements; ++i){ for (int i=0; i<initialSparseMatrix.numberOfNonZeroElements; ++i){
int row = initialSparseMatrix.elements[i]->rowIndex; int row = initialSparseMatrix.elements[i]->rowIndex;
int destinationIndex = transformedSparseMatrix->rowCumulativeIndexes[row]; int destinationIndex = transformedSparseMatrix->rowCumulativeIndexes[row];
@ -73,6 +79,7 @@ void transformToCSR(CooSparseMatrix initialSparseMatrix,
transformedSparseMatrix->rowCumulativeIndexes[row]++; transformedSparseMatrix->rowCumulativeIndexes[row]++;
} }
// Fixes the cumulative sum
for (int i=0, last=0; i<=transformedSparseMatrix->size; i++){ for (int i=0, last=0; i<=transformedSparseMatrix->size; i++){
int temp = transformedSparseMatrix->rowCumulativeIndexes[i]; int temp = transformedSparseMatrix->rowCumulativeIndexes[i];
transformedSparseMatrix->rowCumulativeIndexes[i] = last; transformedSparseMatrix->rowCumulativeIndexes[i] = last;

27
serial/coo_sparse_matrix.h

@ -1,6 +1,8 @@
#ifndef COO_SPARSE_MATRIX_H /* Include guard */ #ifndef COO_SPARSE_MATRIX_H /* Include guard */
#define COO_SPARSE_MATRIX_H #define COO_SPARSE_MATRIX_H
/* ===== INCLUDES ===== */
#include <stdbool.h> #include <stdbool.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
@ -8,26 +10,51 @@
#include "csr_sparse_matrix.h" #include "csr_sparse_matrix.h"
/* ===== STRUCTURES ===== */
// One element of the coordinate formated sparse matrix.
typedef struct cooSparseMatrixElement { typedef struct cooSparseMatrixElement {
double value; double value;
int rowIndex, columnIndex; int rowIndex, columnIndex;
} CooSparseMatrixElement; } CooSparseMatrixElement;
// A sparse matrix in COOrdinate format (aka triplet format).
typedef struct cooSparseMatrix { typedef struct cooSparseMatrix {
int size, numberOfNonZeroElements; int size, numberOfNonZeroElements;
CooSparseMatrixElement **elements; CooSparseMatrixElement **elements;
} CooSparseMatrix; } CooSparseMatrix;
/* ===== FUNCTION DEFINITIONS ===== */
// initCooSparseMatrix creates and initializes the members of a CooSparseMatrix
// structure instance.
CooSparseMatrix initCooSparseMatrix(); CooSparseMatrix initCooSparseMatrix();
//allocMemoryForCoo allocates memory for the elements of the matrix.
void allocMemoryForCoo(CooSparseMatrix *sparseMatrix, int numberOfElements); 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, void addElement(CooSparseMatrix *sparseMatrix, double value, int row,
int column); int column);
// transposeSparseMatrix transposes the matrix.
void transposeSparseMatrix(CooSparseMatrix *sparseMatrix); void transposeSparseMatrix(CooSparseMatrix *sparseMatrix);
// transformToCSR transforms the sparse matrix representation format from COO
// to CSR.
void transformToCSR(CooSparseMatrix initialSparseMatrix, void transformToCSR(CooSparseMatrix initialSparseMatrix,
CsrSparseMatrix *transformedSparseMatrix); CsrSparseMatrix *transformedSparseMatrix);
// cooSparseMatrixVectorMultiplication calculates the product of a
// CooSparseMatrix and a vector.
void cooSparseMatrixVectorMultiplication(CooSparseMatrix sparseMatrix, void cooSparseMatrixVectorMultiplication(CooSparseMatrix sparseMatrix,
double *vector, double **product, int vectorSize); double *vector, double **product, int vectorSize);
// destroyCooSparseMatrix frees all space used by the CooSparseMatrix.
void destroyCooSparseMatrix(CooSparseMatrix *sparseMatrix); void destroyCooSparseMatrix(CooSparseMatrix *sparseMatrix);
// printCooSparseMatrix prints the values of a CooSparseMatrix.
void printCooSparseMatrix(CooSparseMatrix sparseMatrix); void printCooSparseMatrix(CooSparseMatrix sparseMatrix);
#endif // COO_SPARSE_MATRIX_H #endif // COO_SPARSE_MATRIX_H

3
serial/csr_sparse_matrix.c

@ -24,8 +24,8 @@ void allocMemoryForCsr(CsrSparseMatrix *sparseMatrix, int numberOfElements) {
sparseMatrix->size = numberOfElements; sparseMatrix->size = numberOfElements;
} }
// Row indexes start from 0!
void zeroOutRow(CsrSparseMatrix *sparseMatrix, int row) { void zeroOutRow(CsrSparseMatrix *sparseMatrix, int row) {
// Gets start and end indexes of the row's elements
int startIndex = sparseMatrix->rowCumulativeIndexes[row], int startIndex = sparseMatrix->rowCumulativeIndexes[row],
endIndex = sparseMatrix->rowCumulativeIndexes[row+1]; endIndex = sparseMatrix->rowCumulativeIndexes[row+1];
for (int i=startIndex; i<endIndex; ++i) { for (int i=startIndex; i<endIndex; ++i) {
@ -36,7 +36,6 @@ void zeroOutRow(CsrSparseMatrix *sparseMatrix, int row) {
void zeroOutColumn(CsrSparseMatrix *sparseMatrix, int column) { void zeroOutColumn(CsrSparseMatrix *sparseMatrix, int column) {
for (int i=0; i<sparseMatrix->numberOfNonZeroElements; ++i){ for (int i=0; i<sparseMatrix->numberOfNonZeroElements; ++i){
if(sparseMatrix->columnIndexes[i] == column){ if(sparseMatrix->columnIndexes[i] == column){
// Zeros out this element
sparseMatrix->values[i] = 0; sparseMatrix->values[i] = 0;
} }
} }

23
serial/csr_sparse_matrix.h

@ -1,24 +1,47 @@
#ifndef CSR_SPARSE_MATRIX_H /* Include guard */ #ifndef CSR_SPARSE_MATRIX_H /* Include guard */
#define CSR_SPARSE_MATRIX_H #define CSR_SPARSE_MATRIX_H
/* ===== INCLUDES ===== */
#include <stdbool.h> #include <stdbool.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
/* ===== STRUCTURES ===== */
// A sparse matrix in compressed SparseRow format.
typedef struct csrSparseMatrix { typedef struct csrSparseMatrix {
int size, numberOfNonZeroElements; int size, numberOfNonZeroElements;
int *rowCumulativeIndexes, *columnIndexes; int *rowCumulativeIndexes, *columnIndexes;
double *values; double *values;
} CsrSparseMatrix; } CsrSparseMatrix;
/* ===== FUNCTION DEFINITIONS ===== */
// initCsrSparseMatrix creates and initializes the members of a CsrSparseMatrix
// structure instance.
CsrSparseMatrix initCsrSparseMatrix(); CsrSparseMatrix initCsrSparseMatrix();
// allocMemoryForCsr allocates memory for the elements of the matrix.
void allocMemoryForCsr(CsrSparseMatrix *sparseMatrix, int numberOfElements); 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); 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); void zeroOutColumn(CsrSparseMatrix *sparseMatrix, int column);
// csrSparseMatrixVectorMultiplication calculates the product of a
// CsrSparseMatrix and a vector.
void csrSparseMatrixVectorMultiplication(CsrSparseMatrix sparseMatrix, void csrSparseMatrixVectorMultiplication(CsrSparseMatrix sparseMatrix,
double *vector, double **product, int vectorSize); double *vector, double **product, int vectorSize);
// destroyCsrSparseMatrix frees all space used by the CsrSparseMatrix.
void destroyCsrSparseMatrix(CsrSparseMatrix *sparseMatrix); void destroyCsrSparseMatrix(CsrSparseMatrix *sparseMatrix);
// printCsrSparseMatrix prints the values of a CsrSparseMatrix.
void printCsrSparseMatrix(CsrSparseMatrix sparseMatrix); void printCsrSparseMatrix(CsrSparseMatrix sparseMatrix);
#endif // CSR_SPARSE_MATRIX_H #endif // CSR_SPARSE_MATRIX_H

1
serial/serial_gs_pagerank.c

@ -3,7 +3,6 @@
#include "serial_gs_pagerank_functions.h" #include "serial_gs_pagerank_functions.h"
struct timeval startwtime, endwtime; struct timeval startwtime, endwtime;
double seq_time;
int main(int argc, char **argv) { int main(int argc, char **argv) {
CsrSparseMatrix transitionMatrix = initCsrSparseMatrix(); CsrSparseMatrix transitionMatrix = initCsrSparseMatrix();

BIN
serial/serial_gs_pagerank.o

Binary file not shown.

9
serial/serial_gs_pagerank_functions.c

@ -62,7 +62,7 @@ int pagerank(CsrSparseMatrix *transitionMatrix, double **pagerankVector,
do { do {
// Stores previous pagerank vector // Stores previous pagerank vector
memcpy(previousPagerankVector, *pagerankVector, numberOfPages * sizeof(double)); memcpy(previousPagerankVector, *pagerankVector, numberOfPages * sizeof(double));
// Calculates new pagerank vector // Calculates new pagerank vector
calculateNextPagerank(transitionMatrix, previousPagerankVector, calculateNextPagerank(transitionMatrix, previousPagerankVector,
pagerankVector, linksFromConvergedPagesPagerankVector, pagerankVector, linksFromConvergedPagesPagerankVector,
@ -126,8 +126,8 @@ int pagerank(CsrSparseMatrix *transitionMatrix, double **pagerankVector,
} }
} }
// Increases sparsity of the transition matrix by // Increases sparsity of the transition matrix by zeroing
// deleting elements that correspond to converged pages // out elements that correspond to converged pages
zeroOutRow(transitionMatrix, i); zeroOutRow(transitionMatrix, i);
zeroOutColumn(transitionMatrix, i); zeroOutColumn(transitionMatrix, i);
@ -215,8 +215,6 @@ void calculateNextPagerank(CsrSparseMatrix *transitionMatrix,
double *linksFromConvergedPagesPagerankVector, double *linksFromConvergedPagesPagerankVector,
double *convergedPagerankVector, int vectorSize, double dampingFactor) { double *convergedPagerankVector, int vectorSize, double dampingFactor) {
// Calculates the web uniform probability once. // Calculates the web uniform probability once.
double webUniformProbability = 1. / vectorSize; double webUniformProbability = 1. / vectorSize;
csrSparseMatrixVectorMultiplication(*transitionMatrix, previousPagerankVector, csrSparseMatrixVectorMultiplication(*transitionMatrix, previousPagerankVector,
@ -232,7 +230,6 @@ void calculateNextPagerank(CsrSparseMatrix *transitionMatrix,
for (int i=0; i<vectorSize; ++i) { for (int i=0; i<vectorSize; ++i) {
(*pagerankVector)[i] += normDifference * webUniformProbability + (*pagerankVector)[i] += normDifference * webUniformProbability +
linksFromConvergedPagesPagerankVector[i] + convergedPagerankVector[i]; linksFromConvergedPagesPagerankVector[i] + convergedPagerankVector[i];
//(*pagerankVector)[i] += 0.5*normDifference* webUniformProbability +linksFromConvergedPagesPagerankVector[i] + convergedPagerankVector[i];
} }
} }

Loading…
Cancel
Save