Arcane  v3.14.10.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
TestMemory.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2024 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
8#include <gtest/gtest.h>
9
10#include "arcane/utils/MemoryView.h"
11#include "arcane/utils/UniqueArray.h"
12#include "arcane/utils/Exception.h"
13#include "arcane/utils/MemoryUtils.h"
14#include "arcane/utils/NumericTypes.h"
15
16#include <random>
17
18/*---------------------------------------------------------------------------*/
19/*---------------------------------------------------------------------------*/
20
21using namespace Arcane;
22
23/*---------------------------------------------------------------------------*/
24/*---------------------------------------------------------------------------*/
25
26template <typename DataType>
28{
29 char _initValue(Int32 v, char*)
30 {
31 auto x = static_cast<char>(v + 5);
32 return x;
33 }
34 Int16 _initValue(Int32 v, Int16*)
35 {
36 auto x = static_cast<Int16>(v + 5);
37 return x;
38 }
39 Int32 _initValue(Int32 v, Int32*)
40 {
41 auto x = static_cast<Int32>(v + 5);
42 return x;
43 }
44 Int64 _initValue(Int32 v, Int64*)
45 {
46 auto x = static_cast<Int64>(v + 5);
47 return x;
48 }
49 Real _initValue(Int32 v, Real*)
50 {
51 auto x = static_cast<Real>(v + 5);
52 return x;
53 }
54 Real3 _initValue(Int32 v, Real3*)
55 {
56 Real x = static_cast<Real>(v + 5);
57 return Real3(x, x / 2.0, x + 1.5);
58 }
59 Real2x2 _initValue(Int32 v, Real2x2*)
60 {
61 Real x = static_cast<Real>(v + 5);
62 Real2 a(x, x / 2.0);
63 Real2 b(x - 7.9, x * 2.0);
64 return { a, b };
65 }
66 Real3x3 _initValue(Int32 v, Real3x3*)
67 {
68 Real x = static_cast<Real>(v + 5);
69 Real3 a(x, x / 2.0, x + 1.5);
70 Real3 b(x - 7.9, x * 2.0, x / 1.5);
71 Real3 c(x + 3.2, x + 4.7, x + 2.5);
72 return { a, b, c };
73 }
74
75 public:
76
77 void apply()
78 {
79 Int32 nb_value = 1000;
80 DataType* dummy = nullptr;
82 for (Int32 i = 0; i < nb_value; ++i) {
83 DataType x = _initValue(i, dummy);
84 array1[i] = x;
85 }
86
87 // Teste MutableMemoryView::copyHost()
88 {
91 ConstMemoryView from(array1.span());
92 to.copyHost(from);
94 }
95
96 // Liste des indexs qu'on veut copier.
97 // Cette liste est générée aléatoirement
99 unsigned int seed0 = 942244294;
100 std::mt19937 mt1(seed0);
101 auto diff_2 = (mt1.max() - mt1.min()) / 2;
102 for (Int32 i = 0; i < nb_value; ++i) {
103 auto r = mt1() - mt1.min();
104 if (r > diff_2)
105 copy_indexes.add(i);
106 }
107 Int32 nb_index = copy_indexes.size();
108 std::cout << "NB_COPY=" << nb_index << "\n";
109
110 // Teste MutableMemoryView::copyFromIndexesHost()
111 {
112 // array2 contient la référence à laquelle
113 // il faudra comparer l'opération de recopie
114 // avec index
116 for (Int32 i = 0; i < nb_index; ++i)
117 array2[i] = array1[copy_indexes[i]];
118
121 ConstMemoryView from(array1.span());
122 to.copyFromIndexesHost(from, copy_indexes);
124 ConstMemoryView view2(array1.view());
125 ASSERT_EQ(view2.bytes(), asBytes(array1));
126 ConstMemoryView view3(array1.view(), 1);
127 ASSERT_EQ(view3.bytes(), asBytes(array1));
128 }
129
130 // Teste MutableMemoryView::copyToIndexesHost()
131 {
132 // array2 contient la référence à laquelle
133 // il faudra comparer l'opération de recopie
134 // avec index
137 for (Int32 i = 0; i < nb_value; ++i) {
138 DataType x = _initValue(i + 27, dummy);
139 array2[i] = x;
140 array3[i] = x;
141 }
142
143 for (Int32 i = 0; i < nb_index; ++i)
144 array2[copy_indexes[i]] = array1[i];
145
147 ConstMemoryView from(array1.span());
148 from.copyToIndexesHost(to, copy_indexes);
150 }
151 }
152};
153
154/*---------------------------------------------------------------------------*/
155/*---------------------------------------------------------------------------*/
156
157TEST(Memory, Basic)
158{
159 // TODO: Tester NumVector et NumMatrix
160 try {
161 MemoryTester<char>{}.apply();
162 MemoryTester<Real>{}.apply();
163 MemoryTester<Real3>{}.apply();
164 MemoryTester<Int16>{}.apply();
165 MemoryTester<Int32>{}.apply();
166 MemoryTester<Int64>{}.apply();
167 MemoryTester<Real2x2>{}.apply();
168 MemoryTester<Real3x3>{}.apply();
169 }
170 catch (const Exception& ex) {
171 std::cerr << "ERROR=" << ex << "\n";
172 throw;
173 }
174}
175
176/*---------------------------------------------------------------------------*/
177/*---------------------------------------------------------------------------*/
178
179TEST(Memory, Copy)
180{
181 Int32 n = 2500;
183 for (Int32 i = 0; i < n; ++i)
184 r1[i] = i + 1;
185
186 {
190 MemoryUtils::copy(r2_view, r1_view);
191 ASSERT_EQ(r1, r2);
192 }
193
194 {
198 MemoryUtils::copy(r3_view, r1_view);
199 ASSERT_EQ(r1, r3);
200 }
201}
202
203/*---------------------------------------------------------------------------*/
204/*---------------------------------------------------------------------------*/
Lecteur des fichiers de maillage via la bibliothèque LIMA.
Definition Lima.cc:120
Classe gérant un vecteur de réel de dimension 2.
Definition Real2.h:121
Classe gérant une matrice de réel de dimension 2x2.
Definition Real2x2.h:53
Classe gérant un vecteur de réel de dimension 3.
Definition Real3.h:132
Classe gérant une matrice de réel de dimension 3x3.
Definition Real3x3.h:66
Classe de base d'une exception.
-*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
std::int64_t Int64
Type entier signé sur 64 bits.
std::int32_t Int32
Type entier signé sur 32 bits.
std::int16_t Int16
Type entier signé sur 16 bits.