Arcane  4.1.15.0
Developer documentation
Loading...
Searching...
No Matches
TestGetIndices.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7#include <gtest/gtest.h>
8
9#include "arccore/base/PlatformUtils.h"
10#include "arccore/base/ArrayExtentsValue.h"
11#include "arccore/base/ArrayExtents.h"
12
13#include <iostream>
14
15using namespace Arcane;
16
17struct XYZ
18{
19 Int32 x;
20 Int32 y;
21 Int32 z;
22 Int32 yz = y * z;
23
24 XYZ(Int32 x_, Int32 y_, Int32 z_)
25 : x(x_)
26 , y(y_)
27 , z(z_)
28 {
29 yz = y * z;
30 }
31 std::array<Int32, 3> getIndices1(Int32 index)
32 {
33 Int32 i2 = Impl::fastmod(index, z);
34 Int32 fac = z;
35 Int32 i1 = Impl::fastmod(index / fac, y);
36 fac *= y;
37 Int32 i0 = index / fac;
38 return { i0, i1, i2 };
39 }
40
41 std::array<Int32, 3> getIndices2(Int32 index)
42 {
43 Int32 i = index / (y * z);
44 index %= (y * z);
45 Int32 j = index / z;
46 Int32 k = index % z;
47 return { i, j, k };
48 }
49 std::array<Int32, 3> getIndices3(Int32 index)
50 {
51 Int32 i = index / static_cast<unsigned int>(y * z);
52 index %= y * z;
53 Int32 j = index / static_cast<unsigned int>(z);
54 Int32 k = index % z;
55 return { i, j, k };
56 }
57 std::array<Int32, 3> getIndices4(Int32 index)
58 {
59 UInt32 uz = static_cast<unsigned int>(z);
60 Int32 i = index / yz;
61 index %= yz;
62 Int32 j = index / uz;
63 Int32 k = index % uz;
64 return { i, j, k };
65 }
66};
67
68TEST(GetIndices, Versions)
69{
70 Int32 n0 = 150;
71 Int32 n1 = 120;
72 Int32 n2 = 50;
73 Int32 total_size = n0 * n1 * n2;
75 XYZ xv2(n0, n1, n2);
76 for (Int32 i = 0; i < total_size; ++i) {
77 auto x = xv2.getIndices4(i);
78 auto y = xv.getIndices(i);
79 ASSERT_EQ(x[0], y[0]);
80 ASSERT_EQ(x[1], y[1]);
81 ASSERT_EQ(x[2], y[2]);
82 }
83 Int32 nb_loop = 500;
84 if (arccoreIsDebug())
85 nb_loop = 10;
86 double x = Platform::getRealTime();
87 Int64 total0 = 0;
88 for (Int32 k = 0; k < nb_loop; ++k) {
89 for (Int32 i = 0; i < total_size; ++i) {
90 auto x = xv.getIndices(i);
91 total0 += x[0] + x[1] + x[2];
92 }
93 }
94 double t0 = Platform::getRealTime() - x;
95 std::cerr << "TOTAL_0=" << total0 << " time0=" << t0 << "\n";
96
98 Int64 total1 = 0;
99 for (Int32 k = 0; k < nb_loop; ++k) {
100 for (Int32 i = 0; i < total_size; ++i) {
101 auto x = xv2.getIndices1(i);
102 total1 += x[0] + x[1] + x[2];
103 }
104 }
105 ASSERT_EQ(total0, total1);
106 double t1 = Platform::getRealTime() - x;
107 std::cerr << "TOTAL_1=" << total1 << " time1=" << t1 << "\n";
108
110 Int64 total2 = 0;
111 for (Int32 k = 0; k < nb_loop; ++k) {
112 for (Int32 i = 0; i < total_size; ++i) {
113 auto x = xv2.getIndices2(i);
114 total2 += x[0] + x[1] + x[2];
115 }
116 }
117 ASSERT_EQ(total0, total2);
118 double t2 = Platform::getRealTime() - x;
119 std::cerr << "TOTAL_2=" << total2 << " time2=" << t2 << "\n";
120
122 Int64 total3 = 0;
123 for (Int32 k = 0; k < nb_loop; ++k) {
124 for (Int32 i = 0; i < total_size; ++i) {
125 auto x = xv2.getIndices3(i);
126 total3 += x[0] + x[1] + x[2];
127 }
128 }
129 ASSERT_EQ(total0, total3);
130 double t3 = Platform::getRealTime() - x;
131 std::cerr << "TOTAL_3=" << total3 << " time3=" << t3 << "\n";
132
134 Int64 total4 = 0;
135 for (Int32 k = 0; k < nb_loop; ++k) {
136 for (Int32 i = 0; i < total_size; ++i) {
137 auto x = xv2.getIndices4(i);
138 total4 += x[0] + x[1] + x[2];
139 }
140 }
141 ASSERT_EQ(total0, total4);
142 double t4 = Platform::getRealTime() - x;
143 std::cerr << "TOTAL_4=" << total4 << " time3=" << t4 << "\n";
144}
Real getRealTime()
Real time used in seconds.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
bool arccoreIsDebug()
True if the ARCCORE_DEBUG macro is defined.
std::uint32_t UInt32
Unsigned integer type of 32 bits.
std::int32_t Int32
Signed integer type of 32 bits.