Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
TestMemory.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
8#include <gtest/gtest.h>
9
10#include "arcane/utils/MemoryView.h"
11#include "arcane/utils/UniqueArray.h"
12#include "arcane/utils/Exception.h"
14#include "arcane/utils/NumericTypes.h"
15#include "arccore/common/internal/MemoryUtilsInternal.h"
16
17#include <random>
18
19/*---------------------------------------------------------------------------*/
20/*---------------------------------------------------------------------------*/
21
22using namespace Arcane;
23
24/*---------------------------------------------------------------------------*/
25/*---------------------------------------------------------------------------*/
26
27template <typename DataType>
29{
30 char _initValue(Int32 v, char*)
31 {
32 auto x = static_cast<char>(v + 5);
33 return x;
34 }
35 Int16 _initValue(Int32 v, Int16*)
36 {
37 auto x = static_cast<Int16>(v + 5);
38 return x;
39 }
40 Int32 _initValue(Int32 v, Int32*)
41 {
42 auto x = static_cast<Int32>(v + 5);
43 return x;
44 }
45 Int64 _initValue(Int32 v, Int64*)
46 {
47 auto x = static_cast<Int64>(v + 5);
48 return x;
49 }
50 Real _initValue(Int32 v, Real*)
51 {
52 auto x = static_cast<Real>(v + 5);
53 return x;
54 }
55 Real3 _initValue(Int32 v, Real3*)
56 {
57 Real x = static_cast<Real>(v + 5);
58 return Real3(x, x / 2.0, x + 1.5);
59 }
60 Real2x2 _initValue(Int32 v, Real2x2*)
61 {
62 Real x = static_cast<Real>(v + 5);
63 Real2 a(x, x / 2.0);
64 Real2 b(x - 7.9, x * 2.0);
65 return { a, b };
66 }
67 Real3x3 _initValue(Int32 v, Real3x3*)
68 {
69 Real x = static_cast<Real>(v + 5);
70 Real3 a(x, x / 2.0, x + 1.5);
71 Real3 b(x - 7.9, x * 2.0, x / 1.5);
72 Real3 c(x + 3.2, x + 4.7, x + 2.5);
73 return { a, b, c };
74 }
75
76 public:
77
78 void apply()
79 {
80 Int32 nb_value = 1000;
81 DataType* dummy = nullptr;
82 UniqueArray<DataType> array1(nb_value);
83 for (Int32 i = 0; i < nb_value; ++i) {
84 DataType x = _initValue(i, dummy);
85 array1[i] = x;
86 }
87
88 // Test MutableMemoryView::copyHost()
89 {
90 UniqueArray<DataType> array2(nb_value);
91 MutableMemoryView to(array2.span());
92 ConstMemoryView from(array1.span());
93 MemoryUtils::copyHost(to, from);
94 ASSERT_EQ(array1, array2);
95 }
96
97 // List of indices we want to copy.
98 // This list is generated randomly
99 UniqueArray<Int32> copy_indexes;
100 unsigned int seed0 = 942244294;
101 std::mt19937 mt1(seed0);
102 auto diff_2 = (mt1.max() - mt1.min()) / 2;
103 for (Int32 i = 0; i < nb_value; ++i) {
104 auto r = mt1() - mt1.min();
105 if (r > diff_2)
106 copy_indexes.add(i);
107 }
108 Int32 nb_index = copy_indexes.size();
109 std::cout << "NB_COPY=" << nb_index << "\n";
110
111 // Test MutableMemoryView::copyFromIndexesHost()
112 {
113 // array2 contains the reference to which
114 // the copy operation must be compared
115 // with index
116 UniqueArray<DataType> array2(nb_index);
117 for (Int32 i = 0; i < nb_index; ++i)
118 array2[i] = array1[copy_indexes[i]];
119
120 UniqueArray<DataType> array3(nb_index);
121 MutableMemoryView to(array3.span());
122 ConstMemoryView from(array1.span());
123 MemoryUtils::copyHostWithIndexedDestination(to, from, copy_indexes);
124 ASSERT_EQ(array2, array3);
125 ConstMemoryView view2(array1.view());
126 ASSERT_EQ(view2.bytes(), asBytes(array1));
127 ConstMemoryView view3(array1.view(), 1);
128 ASSERT_EQ(view3.bytes(), asBytes(array1));
129 }
130
131 // Test MutableMemoryView::copyToIndexesHost()
132 {
133 // array2 contains the reference to which
134 // the copy operation must be compared
135 // with index
136 UniqueArray<DataType> array2(nb_value);
137 UniqueArray<DataType> array3(nb_value);
138 for (Int32 i = 0; i < nb_value; ++i) {
139 DataType x = _initValue(i + 27, dummy);
140 array2[i] = x;
141 array3[i] = x;
142 }
143
144 for (Int32 i = 0; i < nb_index; ++i)
145 array2[copy_indexes[i]] = array1[i];
146
147 MutableMemoryView to(array3.span());
148 ConstMemoryView from(array1.span());
149 MemoryUtils::copyHostWithIndexedSource(to, from, copy_indexes);
150 ASSERT_EQ(array2, array3);
151 }
152 }
153};
154
155/*---------------------------------------------------------------------------*/
156/*---------------------------------------------------------------------------*/
157
158TEST(Memory, Basic)
159{
160 // TODO: Test NumVector and NumMatrix
161 try {
162 MemoryTester<char>{}.apply();
163 MemoryTester<Real>{}.apply();
164 MemoryTester<Real3>{}.apply();
165 MemoryTester<Int16>{}.apply();
166 MemoryTester<Int32>{}.apply();
167 MemoryTester<Int64>{}.apply();
168 MemoryTester<Real2x2>{}.apply();
169 MemoryTester<Real3x3>{}.apply();
170 }
171 catch (const Exception& ex) {
172 std::cerr << "ERROR=" << ex << "\n";
173 throw;
174 }
175}
176
177/*---------------------------------------------------------------------------*/
178/*---------------------------------------------------------------------------*/
179namespace
180{
181void _checkSetDataMemoryResource(const String& name, eMemoryResource expected_mem_resource)
182{
184 ASSERT_EQ(v, expected_mem_resource);
187 ASSERT_EQ(v2, expected_mem_resource);
188}
189} // namespace
190
191TEST(Memory, Allocator)
192{
193 _checkSetDataMemoryResource("Device", eMemoryResource::Device);
194 _checkSetDataMemoryResource("HostPinned", eMemoryResource::HostPinned);
195 _checkSetDataMemoryResource("Host", eMemoryResource::Host);
196 _checkSetDataMemoryResource("UnifiedMemory", eMemoryResource::UnifiedMemory);
197}
198
199/*---------------------------------------------------------------------------*/
200/*---------------------------------------------------------------------------*/
201
202TEST(Memory, Copy)
203{
204 Int32 n = 2500;
205 UniqueArray<Int32> r1(n);
206 for (Int32 i = 0; i < n; ++i)
207 r1[i] = i + 1;
208
209 {
210 UniqueArray<Int32> r2(n);
211 Span<Int32> r2_view(r2);
212 Span<const Int32> r1_view(r1);
213 MemoryUtils::copy(r2_view, r1_view);
214 ASSERT_EQ(r1, r2);
215 }
216
217 {
218 UniqueArray<Int32> r3(n);
219 SmallSpan<Int32> r3_view(r3.view());
220 SmallSpan<const Int32> r1_view(r1.view());
221 MemoryUtils::copy(r3_view, r1_view);
222 ASSERT_EQ(r1, r3);
223 }
224}
225
226/*---------------------------------------------------------------------------*/
227/*---------------------------------------------------------------------------*/
Memory and allocator management functions.
Integer size() const
Number of elements in the vector.
Span< const T > span() const
Immutable view of this array.
void add(ConstReferenceType val)
Adds element val to the end of the array.
ArrayView< T > view() const
Mutable view of this array.
Constant view on a contiguous memory region containing fixed-size elements.
constexpr SpanType bytes() const
View in byte form.
Mutable view on a contiguous memory region containing fixed-size elements.
Class managing a 2-dimensional real vector.
Definition Real2.h:122
Class managing a 2x2 matrix of reals.
Definition Real2x2.h:55
Class managing a 3-dimensional real vector.
Definition Real3.h:132
Class managing a 3x3 real matrix.
Definition Real3x3.h:67
View of an array of elements of type T.
Definition Span.h:805
View of an array of elements of type T.
Definition Span.h:635
1D data vector with value semantics (STL style).
Concept for allocating, resizing and freeing memory block.
void setDefaultDataMemoryResource(eMemoryResource mem_resource)
Sets the memory resource used for the data memory allocator.
void copyHostWithIndexedSource(MutableMemoryView destination, ConstMemoryView source, Span< const Int32 > indexes)
Copies data on the host with indirection.
eMemoryResource getMemoryResourceFromName(const String &name)
Returns the memory resource by its name.
void copyHostWithIndexedDestination(MutableMemoryView destination, ConstMemoryView source, Span< const Int32 > indexes)
Copies indexed data from v into the instance.
void copyHost(MutableMemoryView destination, ConstMemoryView source)
Copies the data from source into destination.
eMemoryResource getDefaultDataMemoryResource()
Memory resource used by the default allocator for data.
void copy(MutableMemoryView destination, eMemoryResource destination_mem, ConstMemoryView source, eMemoryResource source_mem, const RunQueue *queue=nullptr)
Copies source to destination using the queue queue.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
std::int64_t Int64
Signed integer type of 64 bits.
std::int16_t Int16
Signed integer type of 16 bits.
double Real
Type representing a real number.
Impl::SpanTypeFromSize< conststd::byte, SizeType >::SpanType asBytes(const SpanImpl< DataType, SizeType, Extent > &s)
Converts the view into an array of non-modifiable bytes.
Definition Span.h:1032
eMemoryResource
List of available memory resources.
@ HostPinned
Allocates on the host.
@ Host
Allocates on the host.
@ UnifiedMemory
Allocates using unified memory.
@ Device
Allocates on the device.
std::int32_t Int32
Signed integer type of 32 bits.