diff options
Diffstat (limited to 'SpMV/include')
-rw-r--r-- | SpMV/include/common.h | 24 | ||||
-rw-r--r-- | SpMV/include/dfatool_host.ah | 31 | ||||
-rw-r--r-- | SpMV/include/matrix.h | 138 | ||||
-rw-r--r-- | SpMV/include/params.h | 51 | ||||
-rw-r--r-- | SpMV/include/timer.h | 54 | ||||
-rw-r--r-- | SpMV/include/utils.h | 10 |
6 files changed, 308 insertions, 0 deletions
diff --git a/SpMV/include/common.h b/SpMV/include/common.h new file mode 100644 index 0000000..6118814 --- /dev/null +++ b/SpMV/include/common.h @@ -0,0 +1,24 @@ + +/* 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/include/dfatool_host.ah b/SpMV/include/dfatool_host.ah new file mode 100644 index 0000000..91d44bd --- /dev/null +++ b/SpMV/include/dfatool_host.ah @@ -0,0 +1,31 @@ +#pragma once + +#include <sys/time.h> +#include "dfatool_host_dpu.ah" + +aspect DfatoolHostTiming : public DfatoolHostDPUTiming { + unsigned long n_rows, n_cols, n_nonzero; + unsigned int element_size; + + virtual int getKernel() { return 1; } + + DfatoolHostTiming() { + element_size = sizeof(float); + } + + advice call("% input_params(...)"): after() { + printf("[>>] SpMV | n_dpus=%u\n", NR_DPUS); + } + + advice call("% readCOOMatrix(...)") : after() { + struct COOMatrix* c = tjp->result(); + n_rows = c->numRows; + n_cols = c->numCols; + n_nonzero = c->numNonzeros; + printf("[--] SpMV | n_dpus=%u n_rows=%lu n_cols=%lu n_nonzero=%lu\n", NR_DPUS, n_rows, n_cols, n_nonzero); + } + + advice execution("% main(...)") : after() { + printf("[<<] SpMV | n_dpus=%u n_rows=%lu n_cols=%lu n_nonzero=%lu\n", NR_DPUS, n_rows, n_cols, n_nonzero); + } +}; diff --git a/SpMV/include/matrix.h b/SpMV/include/matrix.h new file mode 100644 index 0000000..ce8745e --- /dev/null +++ b/SpMV/include/matrix.h @@ -0,0 +1,138 @@ + +#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/include/params.h b/SpMV/include/params.h new file mode 100644 index 0000000..bf60e79 --- /dev/null +++ b/SpMV/include/params.h @@ -0,0 +1,51 @@ + +#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/include/timer.h b/SpMV/include/timer.h new file mode 100644 index 0000000..cb513cb --- /dev/null +++ b/SpMV/include/timer.h @@ -0,0 +1,54 @@ +#pragma once + +#include <stdio.h> +#include <sys/time.h> + +#if DFATOOL_TIMING + +#define dfatool_printf(fmt, ...) do { printf(fmt, __VA_ARGS__); } while (0) + +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 double getElapsedTime(Timer timer) +{ + return ((double)((timer.endTime.tv_sec - timer.startTime.tv_sec) + + (timer.endTime.tv_usec - + timer.startTime.tv_usec) / 1.0e6)); +} + +#else + +#define dfatool_printf(fmt, ...) do {} while (0) + +typedef int Timer; + +static void startTimer(Timer* timer) +{ + (void)timer; +} + +static void stopTimer(Timer* timer) +{ + (void)timer; +} + +static double getElapsedTime(Timer timer) +{ + (void)timer; + return 0.0; +} + +#endif diff --git a/SpMV/include/utils.h b/SpMV/include/utils.h new file mode 100644 index 0000000..ccd8fbd --- /dev/null +++ b/SpMV/include/utils.h @@ -0,0 +1,10 @@ + +#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 |