Commit b8f2bba6 authored by Dubský Jan's avatar Dubský Jan
Browse files

Solution 4.02 (Speed: 21.77)

parent 0c003a0b
...@@ -18,8 +18,9 @@ public: ...@@ -18,8 +18,9 @@ public:
using matrix_element = std::uint16_t; using matrix_element = std::uint16_t;
matrix(std::size_t vs, std::size_t hs) : matrix(std::size_t vs, std::size_t hs) :
VS(vs), HS(hs), ROW_BYTES(calc_row_bytes(vs, hs)), ROW_SIZE(ROW_BYTES / sizeof(matrix_element)) { VS(vs), HS(hs), ROW_BYTES(calc_row_bytes(hs)), COL_BYTES(calc_row_bytes(vs)), ROW_SIZE(ROW_BYTES / sizeof(matrix_element)), COL_SIZE(COL_BYTES / sizeof(matrix_element)) {
mat = (uint16_t*)std::aligned_alloc(CACHE_LINE_SIZE, VS * ROW_BYTES); mat = (uint16_t*)std::aligned_alloc(CACHE_LINE_SIZE, VS * ROW_BYTES);
mat_r = (uint16_t*)std::aligned_alloc(CACHE_LINE_SIZE, HS * COL_BYTES);
} }
std::size_t vsize() const { std::size_t vsize() const {
...@@ -31,10 +32,12 @@ public: ...@@ -31,10 +32,12 @@ public:
} }
void set(std::size_t i, std::size_t j, matrix_element e) { void set(std::size_t i, std::size_t j, matrix_element e) {
addr(i, j) = e; addr(i, j) = e;
addr_r(i, j) = e;
} }
matrix_element get(std::size_t i, std::size_t j) const { matrix_element get(std::size_t i, std::size_t j) const {
assert(addr_r(i, j) == addr(i, j));
return addr(i, j); return addr(i, j);
} }
...@@ -47,18 +50,20 @@ public: ...@@ -47,18 +50,20 @@ public:
for (size_t j = 0; j < HS; ++j) { for (size_t j = 0; j < HS; ++j) {
matrix_element res = std::numeric_limits<matrix_element>::max(); matrix_element res = std::numeric_limits<matrix_element>::max();
for (size_t k = 0; k < a.HS; ++k) { for (size_t k = 0; k < a.HS; ++k) {
res = std::min(res, (matrix_element)(a.get(i, k) + b.get(k, j))); res = std::min(res, (matrix_element)(a.addr(i, k) + b.addr_r(k, j)));
} }
set(i, j, res); addr(i, j) = res;
addr_r(i, j) = res;
} }
} }
} }
private: private:
const size_t VS, HS; const size_t VS, HS;
const size_t ROW_BYTES; const size_t ROW_BYTES, COL_BYTES;
const size_t ROW_SIZE; const size_t ROW_SIZE, COL_SIZE;
uint16_t* mat; uint16_t* mat;
uint16_t* mat_r;
matrix_element& addr(size_t row, size_t col) const { matrix_element& addr(size_t row, size_t col) const {
assert(col < HS); assert(col < HS);
...@@ -66,8 +71,14 @@ private: ...@@ -66,8 +71,14 @@ private:
return *(mat + (row * ROW_SIZE) + col); return *(mat + (row * ROW_SIZE) + col);
} }
static size_t calc_row_bytes(size_t vs, size_t hs) { matrix_element& addr_r(size_t row, size_t col) const {
size_t ret = sizeof(matrix_element) * hs; assert(col < HS);
assert(row < VS);
return *(mat_r + (col * COL_SIZE) + row);
}
static size_t calc_row_bytes(size_t val) {
size_t ret = sizeof(matrix_element) * val;
if (ret % CACHE_LINE_SIZE) { if (ret % CACHE_LINE_SIZE) {
ret += CACHE_LINE_SIZE; ret += CACHE_LINE_SIZE;
ret = (ret / CACHE_LINE_SIZE) * CACHE_LINE_SIZE; ret = (ret / CACHE_LINE_SIZE) * CACHE_LINE_SIZE;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment