C++ Library Extensions 2022.12.09
To help learn modern C++ programming
derivatives.cpp
Go to the documentation of this file.
1// dpcpp -Qstd=c++20 /EHsc derivatives.cpp -o d.exe
2// g++ -std=c++20 derivatives.cpp -ltbb12 -o g.exe
3// clang++ -std=c++20 derivatives.cpp -ltbb12 -o c.exe
4// cl /EHsc /std:c++20 derivatives.cpp /Fe: m.exe
5
6#define CPG_INCLUDE_SYCL
7
9#include <cpg/calculus.hpp>
10
12namespace cpt = cpg::types;
13
15{
16 std::cout<<"1. Single Variable Derivatives with TBB" << cpg::endL;
17
19
20 auto f = [](auto x)
21 {
22 return std::sin(x);
23 };
24
25 auto fx = [](auto x)
26 {
27 return std::cos(x);
28 };
29
30 auto fxx = [](auto x)
31 {
32 return -std::sin(x);
33 };
34
35 auto fxxx = [](auto x)
36 {
37 return -std::cos(x);
38 };
39
40 auto fxxxx = [](auto x)
41 {
42 return std::sin(x);
43 };
44
45 auto pi_2 = std::numbers::pi_v<double> * 2.0;
46 auto bound = std::array{0.0, pi_2};
47
48 auto nr_1 = cna::differentiate(dx_1, f, 5, bound);
49 auto ar_1 = cna::evaluate(fx, 5, bound);
50
51 std::cout <<"nr_1 = " << nr_1 << cpg::endl;
52 std::cout <<"ar_1 = " << ar_1 << cpg::endL;
53
54 auto nr_2 = cna::differentiate(dx_2, f, 5, bound);
55 auto ar_2 = cna::evaluate(fxx, 5, bound);
56
57 std::cout <<"nr_2 = " << nr_2 << cpg::endl;
58 std::cout <<"ar_2 = " << ar_2 << cpg::endL;
59
60 auto nr_3 = cna::differentiate(dx_3, f, 5, bound);
61 auto ar_3 = cna::evaluate(fxxx, 5, bound);
62
63 std::cout <<"nr_3 = " << nr_3 << cpg::endl;
64 std::cout <<"ar_3 = " << ar_3 << cpg::endL;
65
66 auto nr_4 = cna::differentiate(dx_4, f, 5, bound);
67 auto ar_4 = cna::evaluate(fxxxx, 5, bound);
68
69 std::cout <<"nr_4 = " << nr_4 << cpg::endl;
70 std::cout <<"ar_4 = " << ar_4 << cpg::endL;
71}
72
74{
75 std::cout<<"2. Multivariable Derivatives with TBB" << cpg::endL;
76
78
79 auto pi_2 = std::numbers::pi_v<double> * 2.0;
80 auto bound = std::array{0.0, pi_2};
81
82 auto f =[](auto x, auto y)
83 {
84 return std::sin(x) * std::cos(y);
85 };
86
87 auto fx =[](auto x, auto y)
88 {
89 return std::cos(x) * std::cos(y);
90 };
91
92 auto fxy =[](auto x, auto y)
93 {
94 return -std::cos(x) * std::sin(y);
95 };
96
97 auto fxxy =[](auto x, auto y)
98 {
99 return std::sin(x) * std::sin(y);
100 };
101
102 auto cmd_dx = cna::create_command(dx_1);
103
104 auto nr_dx_1 = cna::differentiate<0>(cmd_dx, f, 5, bound, 1.0, 1.0);
105 auto ar_dx_1 = cna::evaluate<0>(fx, 5, bound, 1.0, 1.0);
106
107 std::cout <<"nr_dx_1 = " << nr_dx_1 << cpg::endl;
108 std::cout <<"ar_dx_1 = " << ar_dx_1 << cpg::endL;
109
110 auto cmd_dxdy = cna::create_command(dx_1, dy_1);
111
112 auto nr_dxdy_1 = cna::differentiate<0>(cmd_dxdy, f, 5, bound, 1.0, 1.0);
113 auto ar_dxdy_1 = cna::evaluate<0>(fxy, 5, bound, 1.0, 1.0);
114
115 std::cout <<"nr_dxdy_1 = " << nr_dxdy_1 << cpg::endl;
116 std::cout <<"ar_dxdy_1 = " << ar_dxdy_1 << cpg::endL;
117
118 auto cmd_dxdxdy = cna::create_command(dx_2, dy_1);
119 auto cmd_dxdydx = cna::create_command(dx_1, dy_1, dx_1);
120
121 auto nr_dxdxdy_1 = cna::differentiate<0>(cmd_dxdxdy, f, 5, bound, 1.0, 1.0);
122 auto nr_dxdydx_1 = cna::differentiate<0>(cmd_dxdydx, f, 5, bound, 1.0, 1.0);
123 auto ar_dxdxdy_1 = cna::evaluate<0>(fxxy, 5, bound, 1.0, 1.0);
124
125 std::cout <<"nr_dxdxdy_1 = " << nr_dxdxdy_1 << cpg::endl;
126 std::cout <<"nr_dxdydx_1 = " << nr_dxdydx_1 << cpg::endl;
127 std::cout <<"ar_dxdxdy_1 = " << ar_dxdxdy_1 << cpg::endL;
128}
129
131{
132 std::cout<<"3. Single Variable Derivatives with SYCL" << cpg::endL;
133
134 using namespace cpg::numerical_analysis::commands;
135
136 auto f = [](auto x)
137 {
138 return std::sin(x);
139 };
140
141 auto fx = [](auto x)
142 {
143 return std::cos(x);
144 };
145
146 auto fxx = [](auto x)
147 {
148 return -std::sin(x);
149 };
150
151 auto fxxx = [](auto x)
152 {
153 return -std::cos(x);
154 };
155
156 auto fxxxx = [](auto x)
157 {
158 return std::sin(x);
159 };
160
161 sycl::queue queue;
162
163 auto pi_2 = std::numbers::pi_v<double> * 2.0;
164 auto bound = std::array{0.0, pi_2};
165
166 auto nr_1 = cna::differentiate(queue, dx_1, f, 5, bound);
167 auto ar_1 = cna::evaluate(queue, fx, 5, bound);
168
169 std::cout <<"nr_1 = " << nr_1 << cpg::endl;
170 std::cout <<"ar_1 = " << ar_1 << cpg::endL;
171
172 auto nr_2 = cna::differentiate(queue, dx_2, f, 5, bound);
173 auto ar_2 = cna::evaluate(queue, fxx, 5, bound);
174
175 std::cout <<"nr_2 = " << nr_2 << cpg::endl;
176 std::cout <<"ar_2 = " << ar_2 << cpg::endL;
177
178 auto nr_3 = cna::differentiate(queue, dx_3, f, 5, bound);
179 auto ar_3 = cna::evaluate(queue, fxxx, 5, bound);
180
181 std::cout <<"nr_3 = " << nr_3 << cpg::endl;
182 std::cout <<"ar_3 = " << ar_3 << cpg::endL;
183
184 auto nr_4 = cna::differentiate(queue, dx_4, f, 5, bound);
185 auto ar_4 = cna::evaluate(queue, fxxxx, 5, bound);
186
187 std::cout <<"nr_4 = " << nr_4 << cpg::endl;
188 std::cout <<"ar_4 = " << ar_4 << cpg::endL;
189}
190
192{
193 std::cout<<"4. Multivariable Derivatives with SYCL" << cpg::endL;
194
195 using namespace cpg::numerical_analysis::commands;
196
197 sycl::queue queue;
198
199 auto pi_2 = std::numbers::pi_v<double> * 2.0;
200 auto bound = std::array{0.0, pi_2};
201
202 auto f =[](auto x, auto y)
203 {
204 return std::sin(x) * std::cos(y);
205 };
206
207 auto fx =[](auto x, auto y)
208 {
209 return std::cos(x) * std::cos(y);
210 };
211
212 auto fxy =[](auto x, auto y)
213 {
214 return -std::cos(x) * std::sin(y);
215 };
216
217 auto fxxy =[](auto x, auto y)
218 {
219 return std::sin(x) * std::sin(y);
220 };
221
222 auto cmd_dx = cna::create_command(dx_1);
223
224 auto nr_dx_1 = cna::differentiate<0>(queue, cmd_dx, f, 5, bound, 1.0, 1.0);
225 auto ar_dx_1 = cna::evaluate<0>(queue, fx, 5, bound, 1.0, 1.0);
226
227 std::cout <<"nr_dx_1 = " << nr_dx_1 << cpg::endl;
228 std::cout <<"ar_dx_1 = " << ar_dx_1 << cpg::endL;
229
230 auto cmd_dxdy = cna::create_command(dx_1, dy_1);
231
232 auto nr_dxdy_1 = cna::differentiate<0>(queue, cmd_dxdy, f, 5, bound, 1.0, 1.0);
233 auto ar_dxdy_1 = cna::evaluate<0>(queue, fxy, 5, bound, 1.0, 1.0);
234
235 std::cout <<"nr_dxdy_1 = " << nr_dxdy_1 << cpg::endl;
236 std::cout <<"ar_dxdy_1 = " << ar_dxdy_1 << cpg::endL;
237
238 auto cmd_dxdxdy = cna::create_command(dx_2, dy_1);
239 auto cmd_dxdydx = cna::create_command(dx_1, dy_1, dx_1);
240
241 auto nr_dxdxdy_1 = cna::differentiate<0>(queue, cmd_dxdxdy, f, 5, bound, 1.0, 1.0);
242 auto nr_dxdydx_1 = cna::differentiate<0>(queue, cmd_dxdydx, f, 5, bound, 1.0, 1.0);
243 auto ar_dxdxdy_1 = cna::evaluate<0>(queue, fxxy, 5, bound, 1.0, 1.0);
244
245 std::cout <<"nr_dxdxdy_1 = " << nr_dxdxdy_1 << cpg::endl;
246 std::cout <<"nr_dxdydx_1 = " << nr_dxdydx_1 << cpg::endl;
247 std::cout <<"ar_dxdxdy_1 = " << ar_dxdxdy_1 << cpg::endL;
248
249}
250
251int main()
252{
253 // std::cout.precision(10);
254
256
258
260
262}
auto & cout
void test_calculus_single_variable_tbb()
Definition: derivatives.cpp:14
void test_calculus_multivariabl_tbb()
Definition: derivatives.cpp:73
void test_calculus_single_variable_gpu()
void test_calculus_multivariabl_gpu()
int main()
auto differentiate(cpt::sequence< VarIndex, DerivativeOrder >, FuncType &&func, std::size_t N, std::array< BoundType, 2 > bound)
constexpr auto create_command(SeqType, SeqTypes...) noexcept
auto evaluate(FuncType &&f, ArgType arg1)
constexpr auto endl
Definition: cpg_types.hpp:213
constexpr auto endL
Definition: cpg_types.hpp:214