Blender V5.0
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" // IWYU pragma: keep
8#include "util/system.h" // IWYU pragma: keep
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 AVX2 the kernel_avx2 will be used. */
17template<typename FunctionType> class CPUKernelFunction {
18 public:
19 CPUKernelFunction(FunctionType kernel_default, FunctionType kernel_avx2)
20 {
21 kernel_info_ = get_best_kernel_info(kernel_default, kernel_avx2);
22 }
23
24 template<typename... Args> auto operator()(Args... args) const
25 {
26 assert(kernel_info_.kernel);
27
28 return kernel_info_.kernel(args...);
29 }
30
31 const char *get_uarch_name() const
32 {
33 return kernel_info_.uarch_name;
34 }
35
36 protected:
37 /* Helper class which allows to pass human-readable microarchitecture name together with function
38 * pointer. */
39 class KernelInfo {
40 public:
42
43 /* TODO(sergey): Use string view, to have higher-level functionality (i.e. comparison) without
44 * memory allocation. */
45 KernelInfo(const char *uarch_name, FunctionType kernel)
47 {
48 }
49
50 const char *uarch_name;
51 FunctionType kernel;
52 };
53
54 KernelInfo get_best_kernel_info(FunctionType kernel_default, FunctionType kernel_avx2)
55 {
56 /* Silence warnings about unused variables when compiling without some architectures. */
57 (void)kernel_avx2;
58
59#ifdef WITH_CYCLES_OPTIMIZED_KERNEL_AVX2
60 if (DebugFlags().cpu.has_avx2() && system_cpu_support_avx2()) {
61 return KernelInfo("AVX2", kernel_avx2);
62 }
63#endif
64
65 return KernelInfo("default", kernel_default);
66 }
67
68 KernelInfo kernel_info_;
69};
70
KernelInfo(const char *uarch_name, FunctionType kernel)
const char * get_uarch_name() const
auto operator()(Args... args) const
CPUKernelFunction(FunctionType kernel_default, FunctionType kernel_avx2)
KernelInfo get_best_kernel_info(FunctionType kernel_default, FunctionType kernel_avx2)
DebugFlags & DebugFlags()
Definition debug.h:145
#define CCL_NAMESPACE_END
#define assert(assertion)
bool system_cpu_support_avx2()
Definition system.cpp:220