-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcuda_matmul.hpp
More file actions
53 lines (46 loc) · 2.53 KB
/
Copy pathcuda_matmul.hpp
File metadata and controls
53 lines (46 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// cppml/cuda_matmul.hpp — host-side entry points for the CUDA matmul kernels.
//
// This header is plain C++ (no CUDA types leak out) so any translation unit can
// call it; the implementation lives in cuda_matmul.cu and is only compiled when
// the project is configured with CUDA (CPPML_BUILD_CUDA). Matrices are
// row-major float, same convention as cppml::Matrix.
#pragma once
#include <cstddef>
namespace cppml {
namespace cuda {
// True iff a usable CUDA device was found at runtime. Always safe to call (the
// stub build returns false), so callers can branch without #ifdefs.
bool cuda_available();
// Name of device 0, or "" if none. For benchmark/demo reporting.
const char* device_name();
// C = A * B on the GPU. A is (M x K), B is (K x N), C is (M x N), all row-major
// host pointers. Handles H2D copy, launch, D2H copy. `tiled` selects the
// shared-memory tiled kernel (true) or the naive one-thread-per-output kernel
// (false). Returns the kernel-only time in milliseconds via `kernel_ms` if
// non-null (excludes the host<->device transfers).
void matmul(const float* A, const float* B, float* C,
std::size_t M, std::size_t K, std::size_t N,
bool tiled = true, float* kernel_ms = nullptr);
// Multi-channel 2D convolution on the GPU via im2col + tiled GEMM — the GPU
// version of cppml::conv2d_multi_im2col. An im2col kernel unfolds the input
// into a (C_in*kh*kw, out_h*out_w) column matrix on the device, then the tiled
// matmul produces (C_out, out_h*out_w); bias is added in a fused epilogue.
//
// input : C_in * H * W (CHW, row-major host pointer)
// weights : C_out * C_in * kh * kw (row-major, [oc][ic][ky][kx])
// bias : C_out
// output : C_out * out_h * out_w (CHW; caller pre-sizes it)
// out_h = (H + 2*pad - kh)/stride + 1, likewise out_w.
//
// `relu`: when true, bias add and ReLU are FUSED into the GEMM epilogue (one
// kernel for conv→bias→ReLU) instead of separate passes — the operator-fusion
// trick inference engines use to cut global-memory traffic. When false the
// bias is still applied (no ReLU). `kernel_ms` (optional) gets the device time
// for im2col + GEMM (excludes host<->device copies).
void conv2d(const float* input, std::size_t in_channels, std::size_t H, std::size_t W,
const float* weights, const float* bias,
std::size_t out_channels, std::size_t kh, std::size_t kw,
std::size_t stride, std::size_t pad,
float* output, bool relu = false, float* kernel_ms = nullptr);
} // namespace cuda
} // namespace cppml