OpenLB 1.7
Loading...
Searching...
No Matches
platform.h
Go to the documentation of this file.
1/* This file is part of the OpenLB library
2 *
3 * Copyright (C) 2021 Adrian Kummerlaender
4 *
5 * E-mail contact: info@openlb.net
6 * The most recent release of OpenLB can be downloaded at
7 * <http://www.openlb.net/>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public
20 * License along with this program; if not, write to the Free
21 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
23*/
24
25#ifndef PLATFORM_H
26#define PLATFORM_H
27
28#include <stdexcept>
29
30#include "core/meta.h"
31
33namespace olb {
34
36enum struct Platform : std::uint8_t {
37 CPU_SISD,
38 CPU_SIMD,
39 GPU_CUDA,
40};
41
43template <Platform PLATFORM>
45
47
55enum struct ProcessingContext {
58};
59
60namespace stage {
61
62template <ProcessingContext CONTEXT>
64
65}
66
68#ifdef __CUDACC__
69 #define any_platform __device__ __host__
70 #ifdef __CUDA_ARCH__
71 #define platform_constant constexpr __constant__
72 #define platform_constant_definition constexpr __constant__
73 #else
74 #define platform_constant constexpr
75 #define platform_constant_definition constexpr
76 #endif
77#else
78 #define any_platform
79 #define platform_constant constexpr
80 #define platform_constant_definition constexpr
81#endif
82
84
87template <typename CONCRETIZABLE, typename F>
88inline auto callUsingConcretePlatform(Platform platform, typename CONCRETIZABLE::base_t* ptr, F f)
89{
90 switch (platform) {
91#ifdef PLATFORM_CPU_SISD
93 return f(static_cast<typename CONCRETIZABLE::template type<Platform::CPU_SISD>*>(ptr));
94#endif
95#ifdef PLATFORM_CPU_SIMD
97 return f(static_cast<typename CONCRETIZABLE::template type<Platform::CPU_SIMD>*>(ptr));
98#endif
99#ifdef PLATFORM_GPU_CUDA
101 return f(static_cast<typename CONCRETIZABLE::template type<Platform::GPU_CUDA>*>(ptr));
102#endif
103 default:
104 throw std::invalid_argument("Invalid PLATFORM");
105 }
106}
107
108template <typename F>
109inline void callUsingConcretePlatform(Platform platform, F f)
110{
111 switch (platform) {
112#ifdef PLATFORM_CPU_SISD
115 break;
116#endif
117#ifdef PLATFORM_CPU_SIMD
120 break;
121#endif
122#ifdef PLATFORM_GPU_CUDA
125 break;
126#endif
127 default:
128 throw std::invalid_argument("Invalid PLATFORM");
129 }
130}
131
132template <typename CONCRETIZABLE, typename... ARGS>
133typename CONCRETIZABLE::base_t* constructUsingConcretePlatform(Platform platform, ARGS&&... args)
134{
135 switch (platform) {
136#ifdef PLATFORM_CPU_SISD
138 return new typename CONCRETIZABLE::template type<Platform::CPU_SISD>(std::forward<decltype(args)>(args)...);
139#endif
140#ifdef PLATFORM_CPU_SIMD
142 return new typename CONCRETIZABLE::template type<Platform::CPU_SIMD>(std::forward<decltype(args)>(args)...);
143#endif
144#ifdef PLATFORM_GPU_CUDA
146 return new typename CONCRETIZABLE::template type<Platform::GPU_CUDA>(std::forward<decltype(args)>(args)...);
147#endif
148 default:
149 throw std::invalid_argument("Invalid PLATFORM");
150 }
151}
152
154constexpr bool isPlatformCPU(Platform platform) {
155 switch (platform) {
156#ifdef PLATFORM_CPU_SISD
158 return true;
159#endif
160#ifdef PLATFORM_CPU_SIMD
162 return true;
163#endif
164#ifdef PLATFORM_GPU_CUDA
166 return false;
167#endif
168 default:
169 throw std::invalid_argument("Invalid PLATFORM");
170 }
171}
172
173}
174
175#endif
typename std::integral_constant< TYPE, VALUE >::type value
Identity type to wrap non-type template arguments.
Definition meta.h:96
Top level namespace for all of OpenLB.
auto callUsingConcretePlatform(Platform platform, typename CONCRETIZABLE::base_t *ptr, F f)
Dispatcher for concrete platform access.
Definition platform.h:88
ProcessingContext
OpenLB processing contexts.
Definition platform.h:55
@ Simulation
Data available on host for e.g. functor evaluation.
Platform
OpenLB execution targets.
Definition platform.h:36
@ CPU_SIMD
Basic scalar CPU.
@ GPU_CUDA
Vector CPU (AVX2 / AVX-512 collision)
void checkPlatform()
Verifies requirements for using PLATFORM.
constexpr bool isPlatformCPU(Platform platform)
Returns true if platform is equal to Platform::CPU_*.
Definition platform.h:154
CONCRETIZABLE::base_t * constructUsingConcretePlatform(Platform platform, ARGS &&... args)
Definition platform.h:133