diff options
Diffstat (limited to 'SpMV/support')
-rw-r--r-- | SpMV/support/common.h | 25 | ||||
-rw-r--r-- | SpMV/support/matrix.h | 119 | ||||
-rw-r--r-- | SpMV/support/params.h | 46 | ||||
-rw-r--r-- | SpMV/support/timer.h | 27 | ||||
-rw-r--r-- | SpMV/support/utils.h | 11 |
5 files changed, 228 insertions, 0 deletions
diff --git a/SpMV/support/common.h b/SpMV/support/common.h new file mode 100644 index 0000000..58fede8 --- /dev/null +++ b/SpMV/support/common.h @@ -0,0 +1,25 @@ + +/* Common data structures between host and DPUs */ + +#ifndef _COMMON_H_ +#define _COMMON_H_ + +#define ROUND_UP_TO_MULTIPLE_OF_2(x) ((((x) + 1)/2)*2) +#define ROUND_UP_TO_MULTIPLE_OF_8(x) ((((x) + 7)/8)*8) + +struct DPUParams { + uint32_t dpuNumRows; /* Number of rows assigned to the DPU */ + uint32_t dpuRowPtrsOffset; /* Offset of the row pointers */ + uint32_t dpuRowPtrs_m; + uint32_t dpuNonzeros_m; + uint32_t dpuInVector_m; + uint32_t dpuOutVector_m; +}; + +struct Nonzero { + uint32_t col; + float value; +}; + +#endif + diff --git a/SpMV/support/matrix.h b/SpMV/support/matrix.h new file mode 100644 index 0000000..d25da1b --- /dev/null +++ b/SpMV/support/matrix.h @@ -0,0 +1,119 @@ + +#ifndef _MATRIX_H_ +#define _MATRIX_H_ + +#include <assert.h> +#include <stdio.h> + +#include "common.h" +#include "utils.h" + +struct COOMatrix { + uint32_t numRows; + uint32_t numCols; + uint32_t numNonzeros; + uint32_t* rowIdxs; + struct Nonzero* nonzeros; +}; + +struct CSRMatrix { + uint32_t numRows; + uint32_t numCols; + uint32_t numNonzeros; + uint32_t* rowPtrs; + struct Nonzero* nonzeros; +}; + +static struct COOMatrix readCOOMatrix(const char* fileName) { + + struct COOMatrix cooMatrix; + + // Initialize fields + FILE* fp = fopen(fileName, "r"); + assert(fscanf(fp, "%u", &cooMatrix.numRows)); + if(cooMatrix.numRows%2 == 1) { + PRINT_WARNING("Reading matrix %s: number of rows must be even. Padding with an extra row.", fileName); + cooMatrix.numRows++; + } + assert(fscanf(fp, "%u", &cooMatrix.numCols)); + assert(fscanf(fp, "%u", &cooMatrix.numNonzeros)); + cooMatrix.rowIdxs = (uint32_t*) malloc(ROUND_UP_TO_MULTIPLE_OF_8(cooMatrix.numNonzeros*sizeof(uint32_t))); + cooMatrix.nonzeros = (struct Nonzero*) malloc(ROUND_UP_TO_MULTIPLE_OF_8(cooMatrix.numNonzeros*sizeof(struct Nonzero))); + + // Read the nonzeros + for(uint32_t i = 0; i < cooMatrix.numNonzeros; ++i) { + uint32_t rowIdx; + assert(fscanf(fp, "%u", &rowIdx)); + cooMatrix.rowIdxs[i] = rowIdx - 1; // File format indexes begin at 1 + uint32_t colIdx; + assert(fscanf(fp, "%u", &colIdx)); + cooMatrix.nonzeros[i].col = colIdx - 1; // File format indexes begin at 1 + cooMatrix.nonzeros[i].value = 1.0f; + } + + return cooMatrix; + +} + +static void freeCOOMatrix(struct COOMatrix cooMatrix) { + free(cooMatrix.rowIdxs); + free(cooMatrix.nonzeros); +} + +static struct CSRMatrix coo2csr(struct COOMatrix cooMatrix) { + + struct CSRMatrix csrMatrix; + + // Initialize fields + csrMatrix.numRows = cooMatrix.numRows; + csrMatrix.numCols = cooMatrix.numCols; + csrMatrix.numNonzeros = cooMatrix.numNonzeros; + csrMatrix.rowPtrs = (uint32_t*) malloc(ROUND_UP_TO_MULTIPLE_OF_8((csrMatrix.numRows + 1)*sizeof(uint32_t))); + csrMatrix.nonzeros = (struct Nonzero*) malloc(ROUND_UP_TO_MULTIPLE_OF_8(csrMatrix.numNonzeros*sizeof(struct Nonzero))); + + // Histogram rowIdxs + memset(csrMatrix.rowPtrs, 0, (csrMatrix.numRows + 1)*sizeof(uint32_t)); + for(uint32_t i = 0; i < cooMatrix.numNonzeros; ++i) { + uint32_t rowIdx = cooMatrix.rowIdxs[i]; + csrMatrix.rowPtrs[rowIdx]++; + } + + // Prefix sum rowPtrs + uint32_t sumBeforeNextRow = 0; + for(uint32_t rowIdx = 0; rowIdx < csrMatrix.numRows; ++rowIdx) { + uint32_t sumBeforeRow = sumBeforeNextRow; + sumBeforeNextRow += csrMatrix.rowPtrs[rowIdx]; + csrMatrix.rowPtrs[rowIdx] = sumBeforeRow; + } + csrMatrix.rowPtrs[csrMatrix.numRows] = sumBeforeNextRow; + + // Bin the nonzeros + for(uint32_t i = 0; i < cooMatrix.numNonzeros; ++i) { + uint32_t rowIdx = cooMatrix.rowIdxs[i]; + uint32_t nnzIdx = csrMatrix.rowPtrs[rowIdx]++; + csrMatrix.nonzeros[nnzIdx] = cooMatrix.nonzeros[i]; + } + + // Restore rowPtrs + for(uint32_t rowIdx = csrMatrix.numRows - 1; rowIdx > 0; --rowIdx) { + csrMatrix.rowPtrs[rowIdx] = csrMatrix.rowPtrs[rowIdx - 1]; + } + csrMatrix.rowPtrs[0] = 0; + + return csrMatrix; + +} + +static void freeCSRMatrix(struct CSRMatrix csrMatrix) { + free(csrMatrix.rowPtrs); + free(csrMatrix.nonzeros); +} + +static void initVector(float* vec, uint32_t size) { + for(uint32_t i = 0; i < size; ++i) { + vec[i] = 1.0f; + } +} + +#endif + diff --git a/SpMV/support/params.h b/SpMV/support/params.h new file mode 100644 index 0000000..b4b696c --- /dev/null +++ b/SpMV/support/params.h @@ -0,0 +1,46 @@ + +#ifndef _PARAMS_H_ +#define _PARAMS_H_ + +#include "common.h" +#include "utils.h" + +static void usage() { + PRINT( "\nUsage: ./program [options]" + "\n" + "\nBenchmark-specific options:" + "\n -f <F> input matrix file name (default=data/bcsstk30.mtx)" + "\n" + "\nGeneral options:" + "\n -v <V> verbosity" + "\n -h help" + "\n\n"); +} + +typedef struct Params { + const char* fileName; + unsigned int verbosity; +} Params; + +static struct Params input_params(int argc, char **argv) { + struct Params p; + p.fileName = "data/bcsstk30.mtx"; + p.verbosity = 1; + int opt; + while((opt = getopt(argc, argv, "f:v:h")) >= 0) { + switch(opt) { + case 'f': p.fileName = optarg; break; + case 'v': p.verbosity = atoi(optarg); break; + case 'h': usage(); exit(0); + default: + PRINT_ERROR("Unrecognized option!"); + usage(); + exit(0); + } + } + + return p; +} + +#endif + diff --git a/SpMV/support/timer.h b/SpMV/support/timer.h new file mode 100644 index 0000000..f8cd5fc --- /dev/null +++ b/SpMV/support/timer.h @@ -0,0 +1,27 @@ + +#ifndef _TIMER_H_ +#define _TIMER_H_ + +#include <stdio.h> +#include <sys/time.h> + +typedef struct Timer { + struct timeval startTime; + struct timeval endTime; +} Timer; + +static void startTimer(Timer* timer) { + gettimeofday(&(timer->startTime), NULL); +} + +static void stopTimer(Timer* timer) { + gettimeofday(&(timer->endTime), NULL); +} + +static float getElapsedTime(Timer timer) { + return ((float) ((timer.endTime.tv_sec - timer.startTime.tv_sec) + + (timer.endTime.tv_usec - timer.startTime.tv_usec)/1.0e6)); +} + +#endif + diff --git a/SpMV/support/utils.h b/SpMV/support/utils.h new file mode 100644 index 0000000..ddb1e2c --- /dev/null +++ b/SpMV/support/utils.h @@ -0,0 +1,11 @@ + +#ifndef _UTILS_H_ +#define _UTILS_H_ + +#define PRINT_ERROR(fmt, ...) fprintf(stderr, "\033[0;31mERROR:\033[0m " fmt "\n", ##__VA_ARGS__) +#define PRINT_WARNING(fmt, ...) fprintf(stderr, "\033[0;35mWARNING:\033[0m " fmt "\n", ##__VA_ARGS__) +#define PRINT_INFO(cond, fmt, ...) if(cond) printf("\033[0;32mINFO:\033[0m " fmt "\n", ##__VA_ARGS__); +#define PRINT(fmt, ...) printf(fmt "\n", ##__VA_ARGS__) + +#endif + |