C++ Library Extensions 2022.12.09
To help learn modern C++ programming
gradient.cpp
Go to the documentation of this file.
1/*
2152- How to Implement Numerical Partial Derivatives
3 https://www.youtube.com/watch?v=V1MpmXGkc2c&list=PL1_C6uWTeBDEjwkxjppnQ0TmMS272eNRx&index=155
4
5151- How to Implement Numerical Derivatives - Seven-Point Stencil
6 https://www.youtube.com/watch?v=uyToBr0ViG0&list=PL1_C6uWTeBDEjwkxjppnQ0TmMS272eNRx&index=154
7
8150- Mathematical Theory Behind Numerical Derivatives, Mathematica, Solve System of Linear Equations
9 https://www.youtube.com/watch?v=tbngy72ePyU&list=PL1_C6uWTeBDEjwkxjppnQ0TmMS272eNRx&index=153
10
11149- Intel DPC++, SYCL Ahead of Time Compilation (AOT) and Response Files
12 https://www.youtube.com/watch?v=ttAZRUmmagA&list=PL1_C6uWTeBDEjwkxjppnQ0TmMS272eNRx&index=152
13
14148- Implement Gradient, Curl, Divergence with SYCL For the First Time
15 https://www.youtube.com/watch?v=WjVpbLmPJJg&list=PL1_C6uWTeBDEjwkxjppnQ0TmMS272eNRx&index=151
16
17147- Gradient, Curl, Divergence, Random Parallel Fill, cast_ref, Multidimensional Arrays in SYCL
18 https://www.youtube.com/watch?v=aYGTBfmsZfo&list=PL1_C6uWTeBDEjwkxjppnQ0TmMS272eNRx&index=150
19
20Intel® oneAPI DPC++/C++ Compiler Developer Guide and Reference
21Development Reference Guides - Ahead of Time Compilation
22 https://tinyurl.com/mvsnudvf
23
24Standard Portable Intermediate Representation
25 https://en.wikipedia.org/wiki/Standard_Portable_Intermediate_Representation
26
27dpcpp -Qstd=c++20 /EHsc gradient.cpp -o d.exe
28
29dpcpp -Qstd=c++20 /EHsc -fsycl-targets=spir64_gen -Xs "-device glk" gradient.cpp -o d-gpu.exe
30
31If it does not work on your machine, you do not have SYCL-capable GPU device on your machine
32dpcpp -Qstd=c++20 /EHsc -fsycl-targets=spir64_gen -Xs "-device skl" gradient.cpp -o d-gpu.exe
33
34This command will succeed on any modern CPU devices, including AMD CPUs
35dpcpp -Qstd=c++20 /EHsc -fsycl-targets=spir64_x86_64 gradient.cpp -o d-cpu.exe
36
37dpcpp -Qstd=c++20 /EHsc -fsycl-targets=spir64_x86_64 -Xs "-march=avx2" gradient.cpp -o d-cpu.exe
38
39If fails, you do not have SYCL-capable graphics device on your machine
40dpcpp @min_gpu.txt gradient.cpp -o min.exe
41
42This command will work on any x86_x64 CPU devices
43dpcpp @any_cpu.txt gradient.cpp -o any.exe
44
45skl - 6th generation Intel® Core™ processor (Skylake with Intel® Processor Graphics Gen9)
46kbl - 7th generation Intel® Core™ processor (Kaby Lake with Intel® Processor Graphics Gen9)
47cfl - 8th generation Intel® Core™ processor (Coffee Lake with Intel® Processor Graphics Gen9)
48glk - Gemini Lake with Intel® Processor Graphics Gen9
49icllp - 10th generation Intel® Core™ processor (Ice Lake with Intel® Processor Graphics Gen11)
50tgllp - 11th generation Intel® Core™ processor (Tiger Lake with Intel® Processor Graphics Gen12)
51dg1 - Intel® Iris® Xe MAX graphics
52Gen9 - Intel® Processor Graphics Gen9
53Gen11 - Intel® Processor Graphics Gen11
54Gen12LP - Intel® Processor Graphics Gen12 (Lower Power)
55adls - 12th generation Intel® Core™ processor (Alder Lake S with Intel® Processor Graphics Gen12.2)
56aldp - 12th generation Intel® Core™ processor (Alder Lake P with Intel® Processor Graphics Gen12.2)
57
58*/
59
60#define CPG_INCLUDE_SYCL
61
63#include <cpg/cpg_calculus.hpp>
64
65namespace cpt = cpg::types;
66
68
70{
71 sycl::queue queue;
72 auto device = queue.get_device();
73
74 std::cout <<"Computing Device: "
75 << device.get_info<sycl::info::device::name>() << cpg::endL;
76
77 std::cout <<"1. Gradient Vectors" << cpg::endL;
78
80
81 auto f =[](auto x, auto y, auto z)
82 {
83 return std::sin(x) * std::cos(y) * z * z;
84 };
85
86 auto bound = cpt::make_array(0.0, 5.0);
87
88 std::cout <<"Gradients with CPU"<< cpg::endL;
89
90 auto cpu_gradients = cna::gradients<1, 5, 5>(f, bound, bound, bound);
91
92 for(auto& g: cpu_gradients)
93 std::cout << g << std::endl;
94
96
98 std::cout <<"Gradients with GPU"<< cpg::endL;
99
100 auto gpu_gradients = cna::gradients<1, 5, 5>(queue, f, bound, bound, bound);
101
102 for(auto& g: gpu_gradients)
103 std::cout << g << std::endl;
104
106}
107
109{
110 sycl::queue queue;
111 auto device = queue.get_device();
112
113 std::cout <<"Computing Device: "
114 << device.get_info<sycl::info::device::name>() << cpg::endL;
115
116 std::cout <<"2. Curl of Vectors" << cpg::endL;
117
118 using namespace cpg::numerical_analysis::commands;
119
120 auto fx =[](auto x, auto y, auto z)
121 {
122 return std::sin(x) * std::cos(y) * z * z;
123 };
124
125 auto fy =[](auto x, auto y, auto z)
126 {
127 return std::sin(x) * y * z;
128 };
129
130 auto fz =[](auto x, auto y, auto z)
131 {
132 return std::cos(x) * std::sin(y) * z;
133 };
134
135 auto bound = cpt::make_array(0.0, 5.0);
136
137 std::cout <<"Curls with CPU" << cpg::endL;
138
139 auto cpu_curls = cna::curls<5, 5, 1>(fx, fy, fz, bound, bound, bound);
140
141 for(auto& c: cpu_curls)
142 std::cout << c << std::endl;
143
145
146 std::cout <<"Curls with GPU" << cpg::endL;
147
148 auto gpu_curls = cna::curls<5, 5, 1>(queue, fx, fy, fz, bound, bound, bound);
149
150 for(auto& c: gpu_curls)
151 std::cout << c << std::endl;
152
154}
155
157{
158 sycl::queue queue;
159 auto device = queue.get_device();
160
161 std::cout <<"Computing Device: "
162 << device.get_info<sycl::info::device::name>() << cpg::endL;
163
164 std::cout <<"3. Divergence of Vectors" << cpg::endL;
165
166 using namespace cpg::numerical_analysis::commands;
167
168 auto fx =[](auto x, auto y, auto z)
169 {
170 return std::sin(x) * std::cos(y) * z * z;
171 };
172
173 auto fy =[](auto x, auto y, auto z)
174 {
175 return std::sin(x) * y * z;
176 };
177
178 auto fz =[](auto x, auto y, auto z)
179 {
180 return std::cos(x) * std::sin(y) * z;
181 };
182
183 auto bound = cpt::make_array(0.0, 5.0);
184
185 std::cout << "Divergence with CPU" << cpg::endL;
186
187 auto cpu_divs = cna::divs<5, 5, 1>(fx, fy, fz, bound, bound, bound);
188
189 for(auto& d: cpu_divs)
190 std::cout << d << std::endl;
191
193
194 std::cout << "Divergence with GPU" << cpg::endL;
195
196 auto gpu_divs = cna::divs<5, 5, 1>(queue, fx, fy, fz, bound, bound, bound);
197
198 for(auto& d: gpu_divs)
199 std::cout << d << std::endl;
200
202}
203
205
207{
208 sycl::queue queue;
209 auto device = queue.get_device();
210
211 std::cout <<"Computing Device: "
212 << device.get_info<sycl::info::device::name>() << cpg::endL;
213
214 std::cout <<"1. Gradient Vectors" << cpg::endL;
215
216 using namespace cpg::numerical_analysis::commands;
217
218 auto f =[](auto x, auto y, auto z)
219 {
220 return std::sin(x) * std::cos(y) * z * z;
221 };
222
223 auto bound_x = cpt::make_array(0.0, 5.0);
224 auto bound_y = cpt::make_array(0.0, 5.0);
225 auto bound_z = cpt::make_array(5.0, 5.0);
226
227 std::cout <<"Gradients with GPU"<< cpg::endL;
228
229 auto gpu_gradients = cna::gradients<100, 100, 1>(queue, f, bound_x, bound_y, bound_z);
230
231 for(int i = 0; i < 10; ++i)
232 std::cout << gpu_gradients[i] <<std::endl;
233
235}
236
238{
239 sycl::queue queue;
240 auto device = queue.get_device();
241
242 std::cout <<"Computing Device: "
243 << device.get_info<sycl::info::device::name>() << cpg::endL;
244
245 std::cout <<"2. Curl of Vectors" << cpg::endL;
246
247 using namespace cpg::numerical_analysis::commands;
248
249 auto fx =[](auto x, auto y, auto z)
250 {
251 return std::sin(x) * std::cos(y) * z * z;
252 };
253
254 auto fy =[](auto x, auto y, auto z)
255 {
256 return std::sin(x) * y * z;
257 };
258
259 auto fz =[](auto x, auto y, auto z)
260 {
261 return std::cos(x) * std::sin(y) * z;
262 };
263
264 auto bound_x = cpt::make_array(0.0, 5.0);
265 auto bound_y = cpt::make_array(0.0, 5.0);
266 auto bound_z = cpt::make_array(5.0, 5.0);
267
268 std::cout <<"Curls with GPU" << cpg::endL;
269
270 auto gpu_curls = cna::curls<100, 100, 1>(queue, fx, fy, fz, bound_x, bound_y, bound_z);
271
272 for(auto& c: gpu_curls)
273 std::cout << c << std::endl;
274
276}
277
279{
280 sycl::queue queue;
281 auto device = queue.get_device();
282
283 std::cout <<"Computing Device: "
284 << device.get_info<sycl::info::device::name>() << cpg::endL;
285
286 std::cout <<"3. Divergence of Vectors" << cpg::endL;
287
288 using namespace cpg::numerical_analysis::commands;
289
290 auto fx =[](auto x, auto y, auto z)
291 {
292 return std::sin(x) * std::cos(y) * z * z;
293 };
294
295 auto fy =[](auto x, auto y, auto z)
296 {
297 return std::sin(x) * y * z;
298 };
299
300 auto fz =[](auto x, auto y, auto z)
301 {
302 return std::cos(x) * std::sin(y) * z;
303 };
304
305 auto bound_x = cpt::make_array(0.0, 5.0);
306 auto bound_y = cpt::make_array(0.0, 5.0);
307 auto bound_z = cpt::make_array(5.0, 5.0);
308
309 std::cout << "Divergence with GPU" << cpg::endL;
310
311 auto gpu_divs = cna::divs<100, 100, 1>(queue, fx, fy, fz, bound_x, bound_y, bound_z);
312
313 for(auto& d: gpu_divs)
314 std::cout << d << std::endl;
315
317}
318
319
321{
322 using namespace cpg::numerical_analysis::commands;
323
324 auto f = [](auto x, auto y, auto z)
325 {
326 return std::sin(x) * std::cos(y)*z*z;
327 };
328
329 double d = 4.5;
330
331 auto point = cpt::make_array(1.0, 2.0, 3.0);
332
333 // std::cout <<"type of point: " << Cpg_GetTypeCategory(point) << cpg::endL;
334 auto dx1 = cna::create_command(dx_1);
335
336 std::array g{ cna::partial_derivative(dx1, f, point),
337 cna::partial_derivative(dx1, f, point),
338 cna::partial_derivative(dx1, f, point) };
339
340 // auto g = cna::gradient(f, point);
341
342 std::cout <<"gardient = " << g << cpg::endl;
343}
344
346{
347 using namespace cpg::numerical_analysis::commands;
348
349 auto fx = [](auto x, auto y, auto z)
350 {
351 return std::sin(x)*z*z;
352 };
353
354 auto fy = [](auto x, auto y, auto z)
355 {
356 return std::sin(x) * std::cos(y);
357 };
358
359 auto fz = [](auto x, auto y, auto z)
360 {
361 return std::cos(y)*z*z;
362 };
363
364 auto args = cpt::make_array(1.0, 2.0, 3.0);
365
366 auto curl = cna::curl(fx, fy, fz, 1.0, 3.0, 2.6);
367
368 std::cout <<"curl = " << curl << cpg::endl;
369}
370
372{
373 using namespace cpg::numerical_analysis::commands;
374
375 auto fx = [](auto x, auto y, auto z)
376 {
377 return std::sin(x)*z*z;
378 };
379
380 auto fy = [](auto x, auto y, auto z)
381 {
382 return std::sin(x) * std::cos(y);
383 };
384
385 auto fz = [](auto x, auto y, auto z)
386 {
387 return std::cos(y)*z*z;
388 };
389
390 auto args = cpt::make_array(1.0, 2.0, 3.0);
391
392 auto div = cna::divergence(fx, fy, fz, args);
393
394 std::cout <<"div = " << div << cpg::endl;
395}
396
397int main()
398{
399 // test_gradient();
400 // test_curl();
401 // test_divergence();
402
406
407 // how_to_gradient();
408 // how_to_curl();
409 // how_to_divergence();
410}
auto & cout
auto & endl
void test_curl()
Definition: gradient.cpp:108
void test_curl_gpu()
Definition: gradient.cpp:237
void how_to_curl()
Definition: gradient.cpp:345
void test_gradient()
Definition: gradient.cpp:69
void how_to_gradient()
Definition: gradient.cpp:320
void test_gradient_gpu()
Definition: gradient.cpp:206
void test_divergence()
Definition: gradient.cpp:156
void how_to_divergence()
Definition: gradient.cpp:371
void test_divergence_gpu()
Definition: gradient.cpp:278
int main()
Definition: gradient.cpp:397
constexpr auto create_command(SeqType, SeqTypes...) noexcept
auto divergence(FuncTypeX &&func_x, FuncTypeY &&func_y, FuncTypeZ &&func_z, ArgTypes... args) noexcept
auto partial_derivative(cpt::sequence< VarIndex, Order >, FuncType &&func, ArgTypes... args) noexcept
auto curl(FuncTypeX &&func_x, FuncTypeY &&func_y, FuncTypeZ &&func_z, ArgTypes... args) noexcept
constexpr auto make_array(ArgTypes &&... args) noexcept
Definition: cpg_types.hpp:2698
constexpr auto endl
Definition: cpg_types.hpp:213
constexpr auto endL
Definition: cpg_types.hpp:214