chundoong-lab-ta/SHPC2023/hw2_answer/matmul/matmul.c

63 lines
1.5 KiB
C

#define _GNU_SOURCE
#include "util.h"
#include <immintrin.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
// Xeon Silver 4216 - Intel
// 16 cores
// L1: 16x32 KB -> TILE: (64 x 64)?? NO tile, but vectorization
// L2: 16x1.0 MB -> TILE: (256 x 256) (TILING)
// L3: 22 MB -> Backups
//
#define ITILE3 (512)
#define JTILE3 (512)
#define ITILE2 (8)
#define JTILE2 (128)
#define KTILE2 (2) // CACHELINE SIZE
void _matmul2(
const float *A, const float *B, float *C,
int M, int N, int K,
int lda, int ldb, int ldc,
int num_threads) {
//#pragma omp parallel for num_threads(num_threads)
for (int ii = 0; ii < M; ii += ITILE2) {
for (int jj = 0; jj < N; jj += JTILE2) {
for (int kk = 0; kk < K; kk += KTILE2) {
for (int k = kk; k < kk + KTILE2; ++k) {
for (int i = ii; i < ii + ITILE2; ++i) {
for (int j = jj; j < jj + JTILE2; ++j) {
C[i * ldc + j] += A[i * lda + k] * B[k * ldb + j];
}
}
}
}
}
}
}
void _matmul3(
const float *A, const float *B, float *C,
int M, int N, int K,
int lda, int ldb, int ldc,
int num_threads) {
for (int ii = 0; ii < M; ii += ITILE3) {
for (int jj = 0; jj < N; jj += JTILE3) {
_matmul2(&A[ii * lda], &B[jj], &C[ii * ldc + jj], ITILE3, JTILE3, K, lda, ldb, ldc, num_threads);
}
}
}
void matmul(const float *A, const float *B, float *C, int M, int N, int K, int num_threads) {
_matmul3(A, B, C, M, N, K, K, N, N, num_threads);
}