working on matrix mul matrix

This commit is contained in:
Youmu Liu
2025-12-11 18:09:35 +08:00
parent 631bc2b6e8
commit 9ff438d3b9
13 changed files with 379 additions and 115 deletions
+27
View File
@@ -1,5 +1,32 @@
#include "sn_intrinsic.h"
DECLARE_PNL_PROC_ANS(float, sse, clear)
{
register __m128 zero = _mm_setzero_ps();
PANEL_COL_VEC((*ans), 0, 0) = zero;
PANEL_COL_VEC((*ans), 1, 0) = zero;
PANEL_COL_VEC((*ans), 2, 0) = zero;
PANEL_COL_VEC((*ans), 3, 0) = zero;
}
DECLARE_PNL_PROC_ANS(float, avx, clear)
{
register __m256 zero = _mm256_setzero_ps();
PANEL_COL_VEC((*ans), 0, 0) = zero;
PANEL_COL_VEC((*ans), 1, 0) = zero;
PANEL_COL_VEC((*ans), 2, 0) = zero;
PANEL_COL_VEC((*ans), 3, 0) = zero;
}
DECLARE_PNL_PROC_ANS(float, 512, clear)
{
register __m512 zero = _mm512_setzero_ps();
PANEL_COL_VEC((*ans), 0, 0) = zero;
PANEL_COL_VEC((*ans), 1, 0) = zero;
PANEL_COL_VEC((*ans), 2, 0) = zero;
PANEL_COL_VEC((*ans), 3, 0) = zero;
}
// Matrix transpose implementations
DECLARE_KER_PROC_RIGHT_ANS(float, sse, transpose)
{
+91
View File
@@ -41,6 +41,51 @@ DECLARE_BUILD_PANEL(avx, float)
}
}
DECLARE_BUILD_PANEL(sse, float)
{
/* Single kernel -> 4 columns, each is one __m128 column vector. */
const MAT_KERNEL(float) *src = matrix + (col_index * row_length + row_index);
for (int pc = 0; pc < 4; ++pc) {
PANEL_COL_VEC((*panel), pc, 0) = src->xmm[pc];
}
}
DECLARE_BUILD_PANEL(512, float)
{
const MAT_KERNEL(float) *const zero = (const MAT_KERNEL(float) *)&SN_ZERO_KERNEL;
const MAT_KERNEL(float) *k[4][4];
for (int kc = 0; kc < 4; ++kc) {
for (int kr = 0; kr < 4; ++kr) {
const int col = col_index + kc;
const int row = row_index + kr;
k[kc][kr] = (col < col_length && row < row_length) ?
(matrix + (col * row_length + row)) :
zero;
}
}
/* Each panel column corresponds to a kernel column (0..3) within a block col (0..3). */
for (int pc = 0; pc < 16; ++pc) {
const int blk_col = pc >> 2; /* which kernel column block (0..3) */
const int kc_col = pc & 3; /* column inside a 4x4 kernel */
const __m128 r0 = k[blk_col][0]->xmm[kc_col];
const __m128 r1 = k[blk_col][1]->xmm[kc_col];
const __m128 r2 = k[blk_col][2]->xmm[kc_col];
const __m128 r3 = k[blk_col][3]->xmm[kc_col];
const __m256 top = _mm256_set_m128(r1, r0);
const __m256 bot = _mm256_set_m128(r3, r2);
__m512 col = _mm512_castps256_ps512(top);
col = _mm512_insertf32x8(col, bot, 1);
PANEL_COL_VEC((*panel), pc, 0) = col;
}
}
DECLARE_STORE_PANEL(avx, float)
{
const int Q = 8; /* fixed for AVX float panel */
@@ -96,3 +141,49 @@ DECLARE_STORE_PANEL(avx, float)
k_lb->xmm[kc_col] = bot128;
}
}
DECLARE_STORE_PANEL(sse, float)
{
MAT_KERNEL(float) *dst = matrix + (col_index * row_length + row_index);
for (int pc = 0; pc < 4; ++pc) {
const __m128 col = PANEL_COL_VEC((*panel), pc, 0);
dst->xmm[pc] = col;
}
}
DECLARE_STORE_PANEL(512, float)
{
MAT_KERNEL(float) zero;
MAT_KERNEL(float) *k[4][4];
for (int kc = 0; kc < 4; ++kc) {
for (int kr = 0; kr < 4; ++kr) {
const int col = col_index + kc;
const int row = row_index + kr;
k[kc][kr] = (col < col_length && row < row_length) ?
(matrix + (col * row_length + row)) :
&zero;
}
}
for (int pc = 0; pc < 16; ++pc) {
const int blk_col = pc >> 2; /* which kernel column block (0..3) */
const int kc_col = pc & 3; /* column inside a 4x4 kernel */
const __m512 col = PANEL_COL_VEC((*panel), pc, 0);
const __m256 top = _mm512_castps512_ps256(col);
const __m256 bot = _mm512_extractf32x8_ps(col, 1);
const __m128 r0 = _mm256_castps256_ps128(top);
const __m128 r1 = _mm256_extractf128_ps(top, 1);
const __m128 r2 = _mm256_castps256_ps128(bot);
const __m128 r3 = _mm256_extractf128_ps(bot, 1);
k[blk_col][0]->xmm[kc_col] = r0;
k[blk_col][1]->xmm[kc_col] = r1;
k[blk_col][2]->xmm[kc_col] = r2;
k[blk_col][3]->xmm[kc_col] = r3;
}
}
+9 -3
View File
@@ -65,8 +65,10 @@ void load_intrinsic_sse(sn_intrinsic *context)
context->city_hash_simplify = city_hash_simplify_sse;
context->factorial_simd = factorial_simd_sse;
context->index_pair = index_pair_sse;
context->f32_clear_panel = PNL_PROC(float, sse, clear);
context->f32_build_panel = BUILD_PANEL(sse, float);
context->f32_buffer_add = BUF_PROC(float, sse, add);
context->f32_panel_fma = KER_PROC(float, sse, fma);
context->f32_panel_fma = PNL_PROC(float, sse, fma);
context->f32_buffer_fill = BUF_PROC(float, sse, fill);
context->variant = BUF_PROC(float, sse, scalar_mul);
context->f32_buffer_sub = BUF_PROC(float, sse, sub);
@@ -80,8 +82,10 @@ void load_intrinsic_avx(sn_intrinsic *context)
context->city_hash_simplify = city_hash_simplify_sse;
context->factorial_simd = factorial_simd_sse;
context->index_pair = index_pair_sse;
context->f32_clear_panel = PNL_PROC(float, avx, clear);
context->f32_build_panel = BUILD_PANEL(avx, float);
context->f32_buffer_add = BUF_PROC(float, avx, add);
context->f32_panel_fma = KER_PROC(float, sse, fma);
context->f32_panel_fma = PNL_PROC(float, avx, fma);
context->f32_buffer_fill = BUF_PROC(float, avx, fill);
context->variant = BUF_PROC(float, avx, scalar_mul);
context->f32_buffer_sub = BUF_PROC(float, avx, sub);
@@ -95,8 +99,10 @@ void load_intrinsic_512(sn_intrinsic *context)
context->city_hash_simplify = city_hash_simplify_sse;
context->factorial_simd = factorial_simd_sse;
context->index_pair = index_pair_sse;
context->f32_clear_panel = PNL_PROC(float, sse, clear);
context->f32_build_panel = BUILD_PANEL(512, float);
context->f32_buffer_add = BUF_PROC(float, 512, add);
context->f32_panel_fma = KER_PROC(float, sse, fma);
context->f32_panel_fma = PNL_PROC(float, 512, fma);
context->f32_buffer_fill = BUF_PROC(float, 512, fill);
context->variant = BUF_PROC(float, 512, scalar_mul);
context->f32_buffer_sub = BUF_PROC(float, 512, sub);