suanPan
suanPan.h
Go to the documentation of this file.
1 /*******************************************************************************
2  * Copyright (C) 2017-2022 Theodore Chang
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 3 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, see <http://www.gnu.org/licenses/>.
16  ******************************************************************************/
17 
18 #ifndef SUANPAN_H
19 #define SUANPAN_H
20 
21 // SUANPAN_DEBUG
22 // _DEBUG --> MSVC
23 // DEBUG --> GCC
24 #if defined(_DEBUG) || defined(DEBUG) || !defined(NDEBUG)
25 #define SUANPAN_DEBUG
26 #define SUANPAN_EXTRA_DEBUG
27 #else
28 #define ARMA_NO_DEBUG
29 #endif
30 
31 #ifdef SUANPAN_SUPERLUMT
32 #define ARMA_DONT_USE_SUPERLU
33 #else
34 #define ARMA_USE_SUPERLU
35 #endif
36 
37 #ifdef SUANPAN_MKL
38 #define MKL_DIRECT_CALL
39 #endif
40 
41 #ifdef SUANPAN_HDF5
42 #define ARMA_USE_HDF5
43 #endif
44 
45 // SUANPAN_WIN
46 // WIN32 _WIN32 __WIN32 __WIN32__ --> MSVC GCC
47 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32) || defined(__WIN32__)
48 #ifndef SUANPAN_WIN
49 #define SUANPAN_WIN
50 #endif
51 #endif
52 
53 // SUANPAN_WIN
54 #if defined(WIN64) || defined(_WIN64) || defined(__WIN64) || defined(__WIN64__)
55 #ifndef SUANPAN_WIN
56 #define SUANPAN_WIN
57 #endif
58 #endif
59 
60 #ifdef SUANPAN_WIN
61 #ifndef NOMINMAX
62 #define NOMINMAX
63 #endif
64 #endif
65 
66 // SUANPAN_UNIX
67 #if defined(unix) || defined(__unix__) || defined(__linux__) || defined(linux)
68 #ifndef SUANPAN_UNIX
69 #define SUANPAN_UNIX
70 #endif
71 #endif
72 
73 #ifdef SUANPAN_VERSION
74 #undef SUANPAN_VERSION
75 #endif
76 #ifdef SUANPAN_COMPILER
77 #undef SUANPAN_COMPILER
78 #endif
79 
80 // SUANPAN_VERSION SUANPAN_COMPILER
81 // __GNUG__ --> GCC
82 #ifdef __GNUG__
83 #define SUANPAN_VERSION __VERSION__
84 #define SUANPAN_COMPILER "GCC"
85 #define SUANPAN_GCC
86 #endif
87 // _MSC_BUILD --> MSVC
88 #ifdef _MSC_BUILD
89 #define SUANPAN_VERSION _MSC_FULL_VER
90 #define SUANPAN_COMPILER "MSVC"
91 #define SUANPAN_MSVC
92 // cuda unused local function
93 #pragma warning(disable : 4505)
94 #endif
95 // __clang__ --> clang
96 #ifdef __clang__
97 #ifdef SUANPAN_VERSION
98 #undef SUANPAN_VERSION
99 #endif
100 #define SUANPAN_VERSION __VERSION__
101 #ifdef SUANPAN_COMPILER
102 #undef SUANPAN_COMPILER
103 #endif
104 #define SUANPAN_COMPILER "CLANG"
105 #define SUANPAN_CLANG
106 #endif
107 // __ICC --> Intel C++
108 #ifdef __ICC
109 #define SUANPAN_VERSION __ICC
110 #define SUANPAN_COMPILER "INTEL"
111 #define SUANPAN_INTEL
112 #ifdef SUANPAN_WIN
113 #undef SUANPAN_WIN
114 #endif
115 #ifndef SUANPAN_UNIX
116 #define SUANPAN_UNIX
117 #endif
118 #endif
119 // __ICL --> Intel C++
120 #ifdef __ICL
121 #define SUANPAN_VERSION __ICL
122 #define SUANPAN_COMPILER "INTEL"
123 #define SUANPAN_INTEL
124 #ifdef SUANPAN_UNIX
125 #undef SUANPAN_UNIX
126 #endif
127 #ifndef SUANPAN_WIN
128 #define SUANPAN_WIN
129 #endif
130 #endif
131 
132 // _USRDLL --> MSVC
133 #ifdef _USRDLL
134 #ifndef SUANPAN_DLL
135 #define SUANPAN_DLL
136 #endif
137 #endif
138 
139 #ifdef SUANPAN_WIN
140 // WIN MSVC GCC IMPORT
141 #define SUANPAN_IMPORT extern "C" __declspec(dllimport)
142 // WIN MSVC GCC EXPORT
143 #define SUANPAN_EXPORT extern "C" __declspec(dllexport)
144 #elif defined(SUANPAN_UNIX)
145 // UNIX GCC IMPORT
146 #define SUANPAN_IMPORT extern "C"
147 // UNIX GCC EXPORT
148 #define SUANPAN_EXPORT extern "C"
149 #else
150 // EMPTY
151 #define SUANPAN_IMPORT extern "C"
152 #define SUANPAN_EXPORT extern "C"
153 #endif
154 
155 #ifdef SUANPAN_DLL
158 #else
162 #endif
163 
164 constexpr auto SUANPAN_EXIT = 1;
165 constexpr auto SUANPAN_SUCCESS = 0;
166 constexpr auto SUANPAN_FAIL = -1;
167 
168 // TWO IMPLEMENTATIONS
169 #ifndef SUANPAN_WIN
170 #define _strcmpi strcasecmp
171 #endif
172 
173 #ifdef SUANPAN_MT
174 #include <tbb/parallel_sort.h>
175 #include <tbb/parallel_for_each.h>
176 #define suanpan_sort tbb::parallel_sort
177 #define suanpan_for_each tbb::parallel_for_each
178 #else
179 #define suanpan_sort std::sort
180 #define suanpan_for_each std::for_each
181 #endif
182 
183 #include <iostream>
184 inline auto& SUANPAN_COUT = std::cout;
185 inline auto& SUANPAN_CERR = std::cerr;
188 
189 #ifdef SUANPAN_WIN
190 #define FOREGROUND_CYAN (FOREGROUND_BLUE | FOREGROUND_GREEN)
191 #define FOREGROUND_YELLOW (FOREGROUND_RED | FOREGROUND_GREEN)
192 #else
193 #define FOREGROUND_RED "\033[1;31m"
194 #define FOREGROUND_GREEN "\033[1;32m"
195 #define FOREGROUND_YELLOW "\033[1;33m"
196 #define FOREGROUND_BLUE "\033[1;34m"
197 #define FOREGROUND_CYAN "\033[1;36m"
198 #endif
199 
200 #include <Toolbox/print.h>
201 
202 #define ARMA_COUT_STREAM SUANPAN_SYNC_COUT
203 #define ARMA_CERR_STREAM SUANPAN_SYNC_CERR
204 
205 #include <armadillo/armadillo>
206 
207 using namespace arma;
208 
209 #include <filesystem>
210 
211 namespace fs = std::filesystem;
212 
213 #include <memory>
214 
215 using std::shared_ptr;
216 using std::unique_ptr;
217 using std::weak_ptr;
218 
219 using std::make_shared;
220 using std::make_unique;
221 
222 using std::exception;
223 using std::invalid_argument;
224 using std::logic_error;
225 using std::out_of_range;
226 
227 using std::istringstream;
228 using std::ostringstream;
229 using std::string;
230 
231 template<class T> concept sp_d = std::is_floating_point_v<T>;
232 template<class T> concept sp_i = std::is_integral_v<T>;
233 
234 namespace suanpan {
235  template<class IN, class FN> void for_all(IN& from, FN&& func) {
236  suanpan_for_each(from.begin(), from.end(), std::forward<FN>(func));
237  }
238 }
239 
240 #ifdef SUANPAN_CLANG
241 // as of clang 13, ranges support is not complete
242 namespace std::ranges {
243  template<class IN, class OUT, class FN> OUT transform(IN& from, OUT to, FN&& func) { return std::transform(from.begin(), from.end(), to, std::forward<FN>(func)); }
244 
245  template<class IN, class FN> FN for_each(IN& from, FN&& func) { return std::for_each(from.begin(), from.end(), std::forward<FN>(func)); }
246 
247  template<class IN, class OUT> OUT copy(IN& from, OUT to) { return std::copy(from.begin(), from.end(), to); }
248 }
249 #endif
250 
251 #endif
concept sp_i
Definition: suanPan.h:232
void for_all(IN &from, FN &&func)
Definition: suanPan.h:235
auto & SUANPAN_SYNC_COUT
Definition: suanPan.h:186
auto & SUANPAN_SYNC_CERR
Definition: suanPan.h:187
#define SUANPAN_IMPORT
Definition: suanPan.h:151
#define SUANPAN_EXPORT
Definition: suanPan.h:152
#define suanpan_for_each
Definition: suanPan.h:180
Definition: MatrixModifier.hpp:36
constexpr auto SUANPAN_FAIL
Definition: suanPan.h:166
constexpr auto SUANPAN_SUCCESS
Definition: suanPan.h:165
auto & SUANPAN_CERR
Definition: suanPan.h:185
Definition: tensorToolbox.h:93
SUANPAN_EXPORT bool SUANPAN_PRINT
Definition: suanPan.h:159
concept sp_d
Definition: suanPan.h:231
constexpr auto SUANPAN_EXIT
Definition: suanPan.h:164
SUANPAN_EXPORT const char * SUANPAN_EXE
Definition: suanPan.h:161
SUANPAN_EXPORT bool SUANPAN_VERBOSE
Definition: suanPan.h:160
auto & SUANPAN_COUT
Definition: suanPan.h:184