Skip to content

Commit 8c0890f

Browse files
committed
Add missing matmul kernel C files
1 parent 4bedf3d commit 8c0890f

2 files changed

Lines changed: 144 additions & 0 deletions

File tree

src/blosc2/matmul_kernels.c

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* Copyright (c) 2019-present, Blosc Development Team <blosc@blosc.org>
3+
* All rights reserved.
4+
*
5+
* SPDX-License-Identifier: BSD-3-Clause
6+
*/
7+
8+
/* This module hosts the backend-selection and accelerator-specific GEMM
9+
* wrappers used by the matmul fast path. The portable naive kernel stays in
10+
* blosc2_ext.pyx, and external BLAS-style integrations live here.
11+
*/
12+
13+
#include "matmul_kernels.h"
14+
static int g_b2_matmul_backend = B2_MATMUL_BACKEND_AUTO;
15+
16+
int b2_has_accelerate(void) {
17+
#if defined(__APPLE__)
18+
return 1;
19+
#else
20+
return 0;
21+
#endif
22+
}
23+
24+
void b2_set_matmul_backend(int backend) {
25+
switch (backend) {
26+
case B2_MATMUL_BACKEND_AUTO:
27+
case B2_MATMUL_BACKEND_NAIVE:
28+
case B2_MATMUL_BACKEND_ACCELERATE:
29+
g_b2_matmul_backend = backend;
30+
break;
31+
default:
32+
g_b2_matmul_backend = B2_MATMUL_BACKEND_AUTO;
33+
break;
34+
}
35+
}
36+
37+
int b2_get_matmul_backend(void) {
38+
return g_b2_matmul_backend;
39+
}
40+
41+
int b2_get_selected_matmul_backend(void) {
42+
if (g_b2_matmul_backend == B2_MATMUL_BACKEND_ACCELERATE && !b2_has_accelerate()) {
43+
return B2_MATMUL_BACKEND_NAIVE;
44+
}
45+
if (g_b2_matmul_backend == B2_MATMUL_BACKEND_AUTO) {
46+
return b2_has_accelerate() ? B2_MATMUL_BACKEND_ACCELERATE : B2_MATMUL_BACKEND_NAIVE;
47+
}
48+
return g_b2_matmul_backend;
49+
}
50+
51+
const char *b2_get_matmul_backend_name(void) {
52+
switch (g_b2_matmul_backend) {
53+
case B2_MATMUL_BACKEND_NAIVE:
54+
return "naive";
55+
case B2_MATMUL_BACKEND_ACCELERATE:
56+
return "accelerate";
57+
case B2_MATMUL_BACKEND_AUTO:
58+
default:
59+
return "auto";
60+
}
61+
}
62+
63+
const char *b2_get_selected_matmul_backend_name(void) {
64+
switch (b2_get_selected_matmul_backend()) {
65+
case B2_MATMUL_BACKEND_ACCELERATE:
66+
return "accelerate";
67+
case B2_MATMUL_BACKEND_NAIVE:
68+
default:
69+
return "naive";
70+
}
71+
}
72+
73+
#if defined(__APPLE__)
74+
#include <Accelerate/Accelerate.h>
75+
76+
int b2_gemm_accelerate_f32(const float *A, const float *B, float *C, int M, int K, int N) {
77+
cblas_sgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans, M, N, K, 1.0f, A, K, B, N, 1.0f, C, N);
78+
return 0;
79+
}
80+
81+
int b2_gemm_accelerate_f64(const double *A, const double *B, double *C, int M, int K, int N) {
82+
cblas_dgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans, M, N, K, 1.0, A, K, B, N, 1.0, C, N);
83+
return 0;
84+
}
85+
#else
86+
int b2_gemm_accelerate_f32(const float *A, const float *B, float *C, int M, int K, int N) {
87+
(void)A;
88+
(void)B;
89+
(void)C;
90+
(void)M;
91+
(void)K;
92+
(void)N;
93+
return -1;
94+
}
95+
96+
int b2_gemm_accelerate_f64(const double *A, const double *B, double *C, int M, int K, int N) {
97+
(void)A;
98+
(void)B;
99+
(void)C;
100+
(void)M;
101+
(void)K;
102+
(void)N;
103+
return -1;
104+
}
105+
#endif

src/blosc2/matmul_kernels.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright (c) 2019-present, Blosc Development Team <blosc@blosc.org>
3+
* All rights reserved.
4+
*
5+
* SPDX-License-Identifier: BSD-3-Clause
6+
*
7+
* This header declares the small backend-selection layer for matmul block
8+
* acceleration. The portable naive kernel remains in blosc2_ext.pyx, while
9+
* platform BLAS backends such as Accelerate are exposed through this module.
10+
*/
11+
12+
#ifndef PYTHON_BLOSC2_MATMUL_KERNELS_H
13+
#define PYTHON_BLOSC2_MATMUL_KERNELS_H
14+
15+
#ifdef __cplusplus
16+
extern "C" {
17+
#endif
18+
19+
typedef enum b2_matmul_backend {
20+
B2_MATMUL_BACKEND_AUTO = 0,
21+
B2_MATMUL_BACKEND_NAIVE = 1,
22+
B2_MATMUL_BACKEND_ACCELERATE = 2,
23+
} b2_matmul_backend;
24+
25+
int b2_has_accelerate(void);
26+
void b2_set_matmul_backend(int backend);
27+
int b2_get_matmul_backend(void);
28+
int b2_get_selected_matmul_backend(void);
29+
const char *b2_get_matmul_backend_name(void);
30+
const char *b2_get_selected_matmul_backend_name(void);
31+
32+
int b2_gemm_accelerate_f32(const float *A, const float *B, float *C, int M, int K, int N);
33+
int b2_gemm_accelerate_f64(const double *A, const double *B, double *C, int M, int K, int N);
34+
35+
#ifdef __cplusplus
36+
}
37+
#endif
38+
39+
#endif

0 commit comments

Comments
 (0)