Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 96 additions & 30 deletions src/linal.c
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,8 @@ mat_scale(const Matrix m, double scalar, Matrix *result)
* dimensions m.cols x m.rows)
* @return 0 on success, -1 if input is invalid or dimensions mismatch
*/
#pragma GCC push_options
#pragma GCC target("avx2,fma")
__attribute__((optimize("O3")))
int
mat_transpose(const Matrix m, Matrix *result)
Expand All @@ -353,41 +355,104 @@ mat_transpose(const Matrix m, Matrix *result)

size_t rows = m.rows;
size_t cols = m.cols;
size_t total = rows * cols;

/* Block size tuned for L1 cache (~32KB). 64×64 doubles ≈ 32KB. */
#define TRANSPOSE_BLOCK 64
/* Block size: 44×44 doubles = 15.5KB. Fits L1, 23.5 blocks/dimension
* for 1024 — fine-grained parallelism with 12 threads. */
#define TRANSPOSE_BLOCK 44

if (rows > TRANSPOSE_BLOCK && cols > TRANSPOSE_BLOCK) {
const double *M = m.data;
double *R = result->data;
/* Buffered block transpose: load block row-by-row (sequential reads),
* then write transposed with sequential dest writes.
* Block size chosen so buffer fits in L1 cache (~32KB for 64x64). */
double buf[TRANSPOSE_BLOCK * TRANSPOSE_BLOCK];
for (size_t ii = 0; ii < rows; ii += TRANSPOSE_BLOCK) {
size_t i_end =
(ii + TRANSPOSE_BLOCK < rows)
? ii + TRANSPOSE_BLOCK : rows;
size_t bh = i_end - ii;
for (size_t jj = 0; jj < cols; jj += TRANSPOSE_BLOCK) {
size_t j_end = (jj + TRANSPOSE_BLOCK < cols)
? jj + TRANSPOSE_BLOCK
: cols;
size_t bw = j_end - jj;

/* Load source block into buffer row-by-row */
for (size_t bi = 0; bi < bh; bi++) {
const double *src = M + (ii + bi) * cols + jj;
double *dst_buf = buf + bi * bw;
memcpy(dst_buf, src, bw * sizeof(double));
}
const double *M = m.data;
double *R = result->data;

/* Write transposed: outer=bj(dest row), inner=bi(dest col)
* This gives sequential writes along each dest row. */
for (size_t bj = 0; bj < bw; bj++) {
double *dst_row = R + (jj + bj) * rows + ii;
#if defined(_OPENMP)
/* Parallel block transpose for large matrices.
* Each thread gets its own stack buffer.
* Threshold: 65536 elements (256×256). */
if (total >= 65536) {
int linal_omp_threads;
if (total < 262144)
linal_omp_threads = 2;
else if (total < 1048576)
linal_omp_threads = 4;
else
linal_omp_threads = 12;
#pragma omp parallel for num_threads(linal_omp_threads) schedule(static)
for (long bii = 0; bii < (long)(rows / TRANSPOSE_BLOCK +
(rows % TRANSPOSE_BLOCK > 0));
bii++) {
size_t ii = (size_t)bii * TRANSPOSE_BLOCK;
size_t i_end = (ii + TRANSPOSE_BLOCK < rows)
? ii + TRANSPOSE_BLOCK
: rows;
size_t bh = i_end - ii;
double buf[TRANSPOSE_BLOCK * TRANSPOSE_BLOCK];

for (size_t jj = 0; jj < cols;
jj += TRANSPOSE_BLOCK) {
size_t j_end =
(jj + TRANSPOSE_BLOCK < cols)
? jj + TRANSPOSE_BLOCK
: cols;
size_t bw = j_end - jj;

/* Load source block row-by-row (sequential) */
for (size_t bi = 0; bi < bh; bi++) {
dst_row[bi] = buf[bi * bw + bj];
memcpy(buf + bi * bw,
M + (ii + bi) * cols + jj,
bw * sizeof(double));
}

/* Write transposed: sequential dest writes.
* Explicit unroll for more ILP in strided buffer reads. */
#pragma GCC unroll 4
for (size_t bj = 0; bj < bw; bj++) {
double *dst_row =
R + (jj + bj) * rows + ii;
for (size_t bi = 0; bi < bh;
bi++) {
dst_row[bi] =
buf[bi * bw + bj];
}
}
}
}
} else
#endif
{
/* Serial: same buffered block transpose */
double buf[TRANSPOSE_BLOCK * TRANSPOSE_BLOCK];
for (size_t ii = 0; ii < rows;
ii += TRANSPOSE_BLOCK) {
size_t i_end =
(ii + TRANSPOSE_BLOCK < rows)
? ii + TRANSPOSE_BLOCK
: rows;
size_t bh = i_end - ii;
for (size_t jj = 0; jj < cols;
jj += TRANSPOSE_BLOCK) {
size_t j_end =
(jj + TRANSPOSE_BLOCK < cols)
? jj + TRANSPOSE_BLOCK
: cols;
size_t bw = j_end - jj;

/* Load source block row-by-row (sequential) */
for (size_t bi = 0; bi < bh; bi++) {
memcpy(buf + bi * bw,
M + (ii + bi) * cols + jj,
bw * sizeof(double));
}

/* Write transposed: sequential dest writes */
for (size_t bj = 0; bj < bw; bj++) {
double *dst_row =
R + (jj + bj) * rows + ii;
for (size_t bi = 0; bi < bh;
bi++) {
dst_row[bi] =
buf[bi * bw + bj];
}
}
}
}
Expand All @@ -405,6 +470,7 @@ mat_transpose(const Matrix m, Matrix *result)

return 0;
}
#pragma GCC pop_options

/**
* @brief Subtract two matrices element-wise (A - B).
Expand Down
8 changes: 8 additions & 0 deletions tests/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,14 @@ test_numerics = executable(
include_directories: [public_headers, generated_headers],
)

# Benchmarks
benchmark_mat_transpose = executable(
'benchmark_mat_transpose',
files('benchmark_mat_transpose.c'),
dependencies: [linal_dep],
include_directories: [public_headers, generated_headers],
)

# Run all tests
test('mat_create', test_mat_create)
test('mat_memory', test_mat_memory)
Expand Down
Loading