MCPcopy Create free account
hub / github.com/boutproject/BOUT-dev / Arithmetic

Class Arithmetic

examples/performance/arithmetic/arithmetic.cxx:35–104  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

33};
34
35class Arithmetic : public PhysicsModel {
36protected:
37 int init(bool) {
38
39 Field3D a = 1.0;
40 Field3D b = 2.0;
41 Field3D c = 3.0;
42 a.setRegion("RGN_ALL");
43 b.setRegion("RGN_NOBNDRY");
44
45 Field3D result1, result2, result3, result4;
46
47 // Using Field methods (classic operator overloading)
48
49 result1 = 2. * a + b * c;
50#define dur_init {Duration::min(), Duration::max(), Duration::zero(), 0}
51 Durations elapsed1 = dur_init, elapsed2 = dur_init, elapsed3 = dur_init,
52 elapsed4 = dur_init;
53
54 for (int ik = 0; ik < 1e3; ++ik) {
55 TIMEIT(elapsed1, result1 = 2. * a + b * c;);
56
57 // Using C loops
58 result2.allocate();
59 BoutReal* rd = &result2(0, 0, 0);
60 BoutReal* ad = &a(0, 0, 0);
61 BoutReal* bd = &b(0, 0, 0);
62 BoutReal* cd = &c(0, 0, 0);
63 TIMEIT(
64 elapsed2,
65 for (int i = 0, iend = (mesh->LocalNx * mesh->LocalNy * mesh->LocalNz) - 1;
66 i != iend; i++) {
67 *rd = 2. * (*ad) + (*bd) * (*cd);
68 rd++;
69 ad++;
70 bd++;
71 cd++;
72 });
73
74 // Template expressions
75 TIMEIT(elapsed3, result3 = eval3D(add(mul(2, a), mul(b, c))););
76
77 // Range iterator
78 result4.allocate();
79 TIMEIT(elapsed4, for (auto i : result4) result4[i] = 2. * a[i] + b[i] * c[i];);
80 }
81
82 output.enable();
83 output << "TIMING | minimum | mean | maximum\n"
84 << "----------- | ---------- | ---------- | ----------\n";
85 //#define PRINT(str,elapsed) output << str << elapsed.min.count()<<
86 //elapsed.avg.count()<< elapsed.max.count() << endl;
87#define PRINT(str, elapsed) \
88 output.write("{:s} | {:7.3f} us | {:7.3f} us | {:7.3f} us\n", str, \
89 elapsed.min.count(), elapsed.avg.count(), elapsed.max.count())
90 PRINT("Fields: ", elapsed1);
91 PRINT("C loop: ", elapsed2);
92 PRINT("Templates: ", elapsed3);

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected