Arcane  v4.1.4.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
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 "arccore/base/ForLoopRanges.h"
14
15#include <iostream>
16
17using namespace Arcane;
18
19struct XYZ
20{
21 Int32 x;
22 Int32 y;
23 Int32 z;
24 Int32 yz = y * z;
25
26 XYZ(Int32 x_, Int32 y_, Int32 z_)
27 : x(x_)
28 , y(y_)
29 , z(z_)
30 {
31 yz = y * z;
32 }
33 std::array<Int32, 3> getIndices1(Int32 index)
34 {
35 Int32 i2 = impl::fastmod(index, z);
36 Int32 fac = z;
37 Int32 i1 = impl::fastmod(index / fac, y);
38 fac *= y;
39 Int32 i0 = index / fac;
40 return { i0, i1, i2 };
41 }
42
43 std::array<Int32, 3> getIndices2(Int32 index)
44 {
45 Int32 i = index / (y * z);
46 index %= (y * z);
47 Int32 j = index / z;
48 Int32 k = index % z;
49 return { i, j, k };
50 }
51 std::array<Int32, 3> getIndices3(Int32 index)
52 {
53 Int32 i = index / static_cast<unsigned int>(y * z);
54 index %= y * z;
55 Int32 j = index / static_cast<unsigned int>(z);
56 Int32 k = index % z;
57 return { i, j, k };
58 }
59 std::array<Int32, 3> getIndices4(Int32 index)
60 {
61 UInt32 uz = static_cast<unsigned int>(z);
62 Int32 i = index / yz;
63 index %= yz;
64 Int32 j = index / uz;
65 Int32 k = index % uz;
66 return { i, j, k };
67 }
68};
69
70TEST(GetIndices, Versions)
71{
72 Int32 n0 = 150;
73 Int32 n1 = 120;
74 Int32 n2 = 50;
75 Int32 total_size = n0 * n1 * n2;
77 XYZ xv2(n0, n1, n2);
78 for (Int32 i = 0; i < total_size; ++i) {
79 auto x = xv2.getIndices4(i);
80 auto y = xv.getIndices(i);
81 ASSERT_EQ(x[0], y[0]);
82 ASSERT_EQ(x[1], y[1]);
83 ASSERT_EQ(x[2], y[2]);
84 }
85 Int32 nb_loop = 500;
86 if (arccoreIsDebug())
87 nb_loop = 10;
88 double x = Platform::getRealTime();
89 Int64 total0 = 0;
90 for (Int32 k = 0; k < nb_loop; ++k) {
91 for (Int32 i = 0; i < total_size; ++i) {
92 auto x = xv.getIndices(i);
93 total0 += x[0] + x[1] + x[2];
94 }
95 }
96 double t0 = Platform::getRealTime() - x;
97 std::cerr << "TOTAL_0=" << total0 << " time0=" << t0 << "\n";
98
100 Int64 total1 = 0;
101 for (Int32 k = 0; k < nb_loop; ++k) {
102 for (Int32 i = 0; i < total_size; ++i) {
103 auto x = xv2.getIndices1(i);
104 total1 += x[0] + x[1] + x[2];
105 }
106 }
107 ASSERT_EQ(total0, total1);
108 double t1 = Platform::getRealTime() - x;
109 std::cerr << "TOTAL_1=" << total1 << " time1=" << t1 << "\n";
110
112 Int64 total2 = 0;
113 for (Int32 k = 0; k < nb_loop; ++k) {
114 for (Int32 i = 0; i < total_size; ++i) {
115 auto x = xv2.getIndices2(i);
116 total2 += x[0] + x[1] + x[2];
117 }
118 }
119 ASSERT_EQ(total0, total2);
120 double t2 = Platform::getRealTime() - x;
121 std::cerr << "TOTAL_2=" << total2 << " time2=" << t2 << "\n";
122
124 Int64 total3 = 0;
125 for (Int32 k = 0; k < nb_loop; ++k) {
126 for (Int32 i = 0; i < total_size; ++i) {
127 auto x = xv2.getIndices3(i);
128 total3 += x[0] + x[1] + x[2];
129 }
130 }
131 ASSERT_EQ(total0, total3);
132 double t3 = Platform::getRealTime() - x;
133 std::cerr << "TOTAL_3=" << total3 << " time3=" << t3 << "\n";
134
136 Int64 total4 = 0;
137 for (Int32 k = 0; k < nb_loop; ++k) {
138 for (Int32 i = 0; i < total_size; ++i) {
139 auto x = xv2.getIndices4(i);
140 total4 += x[0] + x[1] + x[2];
141 }
142 }
143 ASSERT_EQ(total0, total4);
144 double t4 = Platform::getRealTime() - x;
145 std::cerr << "TOTAL_4=" << total4 << " time3=" << t4 << "\n";
146}
ARCCORE_BASE_EXPORT Real getRealTime()
Temps Real utilisé en secondes.
-*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
std::uint32_t UInt32
Type entier non signé sur 32 bits.
ARCCORE_BASE_EXPORT bool arccoreIsDebug()
Vrai si la macro ARCCORE_DEBUG est définie.
std::int32_t Int32
Type entier signé sur 32 bits.