Blender V4.3
kernel_function.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#pragma once
6
7#include "util/debug.h"
8#include "util/system.h"
9
11
12/* A wrapper around per-microarchitecture variant of a kernel function.
13 *
14 * Provides a function-call-like API which gets routed to the most suitable implementation.
15 *
16 * For example, on a computer which only has SSE4.2 the kernel_sse42 will be used. */
17template<typename FunctionType> class CPUKernelFunction {
18 public:
19 CPUKernelFunction(FunctionType kernel_default,
20 FunctionType kernel_sse42,
21 FunctionType kernel_avx2)
22 {
23 kernel_info_ = get_best_kernel_info(kernel_default, kernel_sse42, kernel_avx2);
24 }
25
26 template<typename... Args> inline auto operator()(Args... args) const
27 {
28 assert(kernel_info_.kernel);
29
30 return kernel_info_.kernel(args...);
31 }
32
33 const char *get_uarch_name() const
34 {
36 }
37
38 protected:
39 /* Helper class which allows to pass human-readable microarchitecture name together with function
40 * pointer. */
41 class KernelInfo {
42 public:
43 KernelInfo() : KernelInfo("", nullptr) {}
44
45 /* TODO(sergey): Use string view, to have higher-level functionality (i.e. comparison) without
46 * memory allocation. */
47 KernelInfo(const char *uarch_name, FunctionType kernel)
49 {
50 }
51
52 const char *uarch_name;
53 FunctionType kernel;
54 };
55
56 KernelInfo get_best_kernel_info(FunctionType kernel_default,
57 FunctionType kernel_sse42,
58 FunctionType kernel_avx2)
59 {
60 /* Silence warnings about unused variables when compiling without some architectures. */
61 (void)kernel_sse42;
62 (void)kernel_avx2;
63
64#ifdef WITH_CYCLES_OPTIMIZED_KERNEL_AVX2
66 return KernelInfo("AVX2", kernel_avx2);
67 }
68#endif
69
70#ifdef WITH_CYCLES_OPTIMIZED_KERNEL_SSE42
71 if (DebugFlags().cpu.has_sse42() && system_cpu_support_sse42()) {
72 return KernelInfo("SSE4.2", kernel_sse42);
73 }
74#endif
75
76 return KernelInfo("default", kernel_default);
77 }
78
80};
81
KernelInfo(const char *uarch_name, FunctionType kernel)
const char * get_uarch_name() const
CPUKernelFunction(FunctionType kernel_default, FunctionType kernel_sse42, FunctionType kernel_avx2)
KernelInfo get_best_kernel_info(FunctionType kernel_default, FunctionType kernel_sse42, FunctionType kernel_avx2)
KernelInfo kernel_info_
auto operator()(Args... args) const
CPU cpu
Definition debug.h:117
DebugFlags & DebugFlags()
Definition debug.h:142
#define CCL_NAMESPACE_END
bool has_avx2()
Definition debug.h:34
bool system_cpu_support_avx2()
Definition system.cpp:227
bool system_cpu_support_sse42()
Definition system.cpp:222