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. 7
      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) {
// 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; i<initialSparseMatrix.numberOfNonZeroElements; ++i){
int rowIndex = initialSparseMatrix.elements[i]->rowIndex;
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; i<initialSparseMatrix.numberOfNonZeroElements; ++i){
int row = initialSparseMatrix.elements[i]->rowIndex;
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;

27
serial/coo_sparse_matrix.h

@ -1,6 +1,8 @@
#ifndef COO_SPARSE_MATRIX_H /* Include guard */
#define COO_SPARSE_MATRIX_H
/* ===== INCLUDES ===== */
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
@ -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

3
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; i<endIndex; ++i) {
@ -36,7 +36,6 @@ void zeroOutRow(CsrSparseMatrix *sparseMatrix, int row) {
void zeroOutColumn(CsrSparseMatrix *sparseMatrix, int column) {
for (int i=0; i<sparseMatrix->numberOfNonZeroElements; ++i){
if(sparseMatrix->columnIndexes[i] == column){
// Zeros out this element
sparseMatrix->values[i] = 0;
}
}

23
serial/csr_sparse_matrix.h

@ -1,24 +1,47 @@
#ifndef CSR_SPARSE_MATRIX_H /* Include guard */
#define CSR_SPARSE_MATRIX_H
/* ===== INCLUDES ===== */
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdlib.h>
/* ===== 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

1
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();

BIN
serial/serial_gs_pagerank.o

Binary file not shown.

7
serial/serial_gs_pagerank_functions.c

@ -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<vectorSize; ++i) {
(*pagerankVector)[i] += normDifference * webUniformProbability +
linksFromConvergedPagesPagerankVector[i] + convergedPagerankVector[i];
//(*pagerankVector)[i] += 0.5*normDifference* webUniformProbability +linksFromConvergedPagesPagerankVector[i] + convergedPagerankVector[i];
}
}

Loading…
Cancel
Save