Optimized Belief Propagation (CPU and GPU)
RunCPUSettings.h
Go to the documentation of this file.
1 /*
2 Copyright (C) 2024 Scott Grauer-Gray
3 
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 
27 #ifndef RUNCPUSETTINGS_H_
28 #define RUNCPUSETTINGS_H_
29 
30 #include <vector>
31 #include <array>
32 #include <thread>
33 #include <set>
34 
35 //check if running on ARM architecture
36 #if defined(COMPILING_FOR_ARM)
37 #include <arm_neon.h> //needed for float16_t type
38 #endif
39 
40 //define and set CPU vectorization options using preprocessor
41 //needed to determine what code gets compiled to support vectorization
42 #define AVX_256_DEFINE 0
43 #define AVX_256_F16_DEFINE 1
44 #define AVX_512_DEFINE 2
45 #define AVX_512_F16_DEFINE 3
46 #define NEON_DEFINE 4
47 #define NO_VECTORIZATION 5
48 #if defined(COMPILING_FOR_ARM) //NEON supported on ARM but AVX is not
49 #define CPU_VECTORIZATION_DEFINE NEON_DEFINE
50 #else
51 //by default CPU vectorization during compilation via Makefile
52 //use AVX 512 if not set during compilation
53 #if defined(AVX_512_VECTORIZATION)
54 #define CPU_VECTORIZATION_DEFINE AVX_512_DEFINE
55 #elif defined(AVX_512_F16_VECTORIZATION)
56 #define CPU_VECTORIZATION_DEFINE AVX_512_F16_DEFINE
57 #elif defined(AVX_256_VECTORIZATION)
58 #define CPU_VECTORIZATION_DEFINE AVX_256_DEFINE
59 #elif defined(AVX_256_F16_VECTORIZATION)
60 #define CPU_VECTORIZATION_DEFINE AVX_256_F16_DEFINE
61 #else
62 #define CPU_VECTORIZATION_DEFINE AVX_512_DEFINE
63 #endif //defined(AVX_512_VECTORIZATION)
64 #endif //COMPILING_FOR_ARM
65 
69 namespace run_cpu {
70 
73 constexpr std::string_view kSimulateSingleCPU{"SimulateSingleCPU"};
74 
76 const unsigned int kNumThreadsCPU{std::thread::hardware_concurrency()};
77 
80 const unsigned int kMinNumThreadsRun{std::min(kNumThreadsCPU, 4u)};
81 
83 const std::array<unsigned int, 2> kParallelParamsDefault{
84  {kNumThreadsCPU, 1}};
85 
86 #if defined(DEFAULT_PARALLEL_PARAMS_ONLY)
87 
90 const std::set<std::array<unsigned int, 2>> kParallelParameterAltOptions{};
91 
92 #elif defined(LIMITED_ALT_PARALLEL_PARAMS)
93 
96 const std::set<std::array<unsigned int, 2>> kParallelParameterAltOptions{
97  { kNumThreadsCPU, 1},
98  { kNumThreadsCPU / 2, 1}};
99 
100 #else
101 
105 const std::set<std::array<unsigned int, 2>> kParallelParameterAltOptions{
106  { kNumThreadsCPU, 1},
107  { (3 * kNumThreadsCPU) / 4 , 1},
108  { kNumThreadsCPU / 2, 1},
109  { kNumThreadsCPU / 4, 1},
110  { kNumThreadsCPU / 8, 1}};
111 
112 #endif
113 
114 };
115 
116 #endif /* RUNCPUSETTINGS_H_ */
Namespace with CPU run defaults and constants.
constexpr std::string_view kSimulateSingleCPU
Constant that specifies that run is simulating single CPU on a dual-CPU system.
const unsigned int kNumThreadsCPU
Constant corresponding to number of threads on CPU.
const std::array< unsigned int, 2 > kParallelParamsDefault
Default parallel parameters setting on CPU.
const unsigned int kMinNumThreadsRun
Minimum number of threads to allow for any parallel parameters setting on CPU.
const std::set< std::array< unsigned int, 2 > > kParallelParameterAltOptions
Parallel parameters options that are tested in order to find optimized configuration in run....