Arcane  4.2.1.0
Developer documentation
Loading...
Searching...
No Matches
ArrayViewCommon.h
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/* ArrayViewCommon.h (C) 2000-2026 */
9/* */
10/* Common declarations for the ArrayView, ConstArrayView, and Span classes. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCCORE_BASE_ARRAYVIEWCOMMON_H
13#define ARCCORE_BASE_ARRAYVIEWCOMMON_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
17#include "arccore/base/ArrayIterator.h"
18
19// This header file is only required when we want to print values of the view.
20// We can remove it but in this case all user code has to explicitly include
21// it so it may break source compatibility. So at the moment (July 2026)
22// we include it.
23#include "arccore/base/ArrayViewDumper.h"
24
25#include <iosfwd>
26// 'assert' is necessary for accelerator code
27#include <assert.h>
28
29#ifndef ARCCORE_COMPILING_FRAMEWORK
30// This header is not needed and will be removed in July 2027
31#include <iostream>
32#endif
33
34/*---------------------------------------------------------------------------*/
35/*---------------------------------------------------------------------------*/
36
37namespace Arcane::Impl
38{
39
40/*---------------------------------------------------------------------------*/
41/*---------------------------------------------------------------------------*/
42
43template <typename ViewType>
44class ArrayViewDumper;
45
46/*---------------------------------------------------------------------------*/
47/*---------------------------------------------------------------------------*/
48
50template <typename ViewType> ARCCORE_HOST_DEVICE auto
51subViewInterval(ViewType view,
52 typename ViewType::size_type index,
53 typename ViewType::size_type nb_interval) -> ViewType
54{
55 using size_type = typename ViewType::size_type;
56 if (nb_interval <= 0)
57 return ViewType();
58 if (index < 0 || index >= nb_interval)
59 return ViewType();
60 size_type n = view.size();
61 size_type isize = n / nb_interval;
62 size_type ibegin = index * isize;
63 // For the last interval, take the remaining elements
64 if ((index + 1) == nb_interval)
65 isize = n - ibegin;
66 return ViewType::create(view.data() + ibegin, isize);
67}
68
69/*---------------------------------------------------------------------------*/
70/*---------------------------------------------------------------------------*/
78template <typename ViewType> inline void
79dumpArray(std::ostream& o, ViewType val, int max_print)
80{
81 ArrayViewDumper<ViewType>::dumpArray(o, val, max_print);
82}
83
84/*---------------------------------------------------------------------------*/
85/*---------------------------------------------------------------------------*/
86
88template <typename ViewType> inline bool
89areEqual(ViewType rhs, ViewType lhs)
90{
91 using size_type = typename ViewType::size_type;
92 if (rhs.size() != lhs.size())
93 return false;
94 size_type s = rhs.size();
95 for (size_type i = 0; i < s; ++i) {
96 if (rhs[i] != lhs[i])
97 return false;
98 }
99 return true;
100}
101
102/*---------------------------------------------------------------------------*/
103/*---------------------------------------------------------------------------*/
104
106template <typename View2DType> inline bool
107areEqual2D(View2DType rhs, View2DType lhs)
108{
109 using size_type = typename View2DType::size_type;
110 const size_type dim1_size = rhs.dim1Size();
111 const size_type dim2_size = rhs.dim2Size();
112 if (dim1_size != lhs.dim1Size())
113 return false;
114 if (dim2_size != lhs.dim2Size())
115 return false;
116 for (size_type i = 0; i < dim1_size; ++i) {
117 for (size_type j = 0; j < dim2_size; ++j) {
118 if (rhs(i, j) != lhs(i, j))
119 return false;
120 }
121 }
122 return true;
123}
124
125/*---------------------------------------------------------------------------*/
126/*---------------------------------------------------------------------------*/
127
129extern "C++" ARCCORE_BASE_EXPORT void
130arccoreThrowTooBigInteger [[noreturn]] (std::size_t size);
131
132extern "C++" ARCCORE_BASE_EXPORT void
133arccoreThrowTooBigInt64 [[noreturn]] (std::size_t size);
134
135extern "C++" ARCCORE_BASE_EXPORT void
136arccoreThrowNegativeSize [[noreturn]] (Int64 size);
137
138/*---------------------------------------------------------------------------*/
139/*---------------------------------------------------------------------------*/
140
142inline constexpr ARCCORE_HOST_DEVICE void
143arccoreCheckIsPositive(Int64 size)
144{
145 if (size < 0) {
146#ifdef ARCCORE_DEVICE_CODE
147 assert("'size' is negative");
148#else
149 Arcane::Impl::arccoreThrowNegativeSize(size);
150#endif
151 }
152}
153
155inline constexpr ARCCORE_HOST_DEVICE void
156arccoreCheckIsValidInteger(Int64 size)
157{
158 if (size >= ARCCORE_INTEGER_MAX) {
159#ifdef ARCCORE_DEVICE_CODE
160 assert("'size' is bigger than ARCCORE_INTEGER_MAX");
161#else
162 Arcane::Impl::arccoreThrowTooBigInteger(size);
163#endif
164 }
165}
166
168inline constexpr ARCCORE_HOST_DEVICE void
169arccoreCheckIsValidInt64(size_t size)
170{
171 if (size >= ARCCORE_INT64_MAX) {
172#ifdef ARCCORE_DEVICE_CODE
173 assert("'size' is bigger than ARCCORE_INT64_MAX");
174#else
175 Arcane::Impl::arccoreThrowTooBigInt64(size);
176#endif
177 }
178}
179
180/*---------------------------------------------------------------------------*/
181/*---------------------------------------------------------------------------*/
182
183} // namespace Arcane::impl
184
185namespace Arcane
186{
187
188/*---------------------------------------------------------------------------*/
189/*---------------------------------------------------------------------------*/
190
197inline constexpr ARCCORE_HOST_DEVICE Integer
198arccoreCheckArraySize(unsigned long long size)
199{
200 Arcane::Impl::arccoreCheckIsValidInteger(size);
201 return static_cast<Integer>(size);
202}
203
210inline constexpr Integer
212{
213 Arcane::Impl::arccoreCheckIsValidInteger(size);
214 Arcane::Impl::arccoreCheckIsPositive(size);
215 return static_cast<Integer>(size);
216}
217
224inline constexpr ARCCORE_BASE_EXPORT Integer
225arccoreCheckArraySize(unsigned long size)
226{
227 Arcane::Impl::arccoreCheckIsValidInteger(size);
228 return static_cast<Integer>(size);
229}
230
238inline constexpr ARCCORE_HOST_DEVICE Integer
240{
241 Arcane::Impl::arccoreCheckIsValidInteger(size);
242 Arcane::Impl::arccoreCheckIsPositive(size);
243 return static_cast<Integer>(size);
244}
245
252inline constexpr ARCCORE_HOST_DEVICE Integer
253arccoreCheckArraySize(unsigned int size)
254{
255 Arcane::Impl::arccoreCheckIsValidInteger(size);
256 return static_cast<Integer>(size);
257}
258
265inline constexpr ARCCORE_HOST_DEVICE Integer
267{
268 Arcane::Impl::arccoreCheckIsValidInteger(size);
269 Arcane::Impl::arccoreCheckIsPositive(size);
270 return static_cast<Integer>(size);
271}
272
280inline constexpr ARCCORE_HOST_DEVICE Int64
282{
283 Arcane::Impl::arccoreCheckIsValidInt64(size);
284 return static_cast<Int64>(size);
285}
286
287/*---------------------------------------------------------------------------*/
288/*---------------------------------------------------------------------------*/
289
290template <typename IntType> class ArraySizeChecker;
291
293template <>
295{
296 public:
297
298 template <typename SizeType> ARCCORE_HOST_DEVICE static Int32 check(SizeType size)
299 {
300 return arccoreCheckArraySize(size);
301 }
302};
303
305template <>
307{
308 public:
309
310 static ARCCORE_HOST_DEVICE Int64 check(std::size_t size)
311 {
312 return arccoreCheckLargeArraySize(size);
313 }
314};
315
316/*---------------------------------------------------------------------------*/
317/*---------------------------------------------------------------------------*/
318
319} // namespace Arcane
320
321/*---------------------------------------------------------------------------*/
322/*---------------------------------------------------------------------------*/
323
324namespace Arccore::impl
325{
326using Arcane::Impl::arccoreCheckIsPositive;
327using Arcane::Impl::arccoreCheckIsValidInt64;
328using Arcane::Impl::arccoreCheckIsValidInteger;
329using Arcane::Impl::arccoreThrowNegativeSize;
330using Arcane::Impl::arccoreThrowTooBigInt64;
331using Arcane::Impl::arccoreThrowTooBigInteger;
332using Arcane::Impl::areEqual;
333using Arcane::Impl::areEqual2D;
334using Arcane::Impl::dumpArray;
335using Arcane::Impl::subViewInterval;
336} // namespace Arccore::impl
337
338namespace Arccore
339{
342} // namespace Arccore
343
344/*---------------------------------------------------------------------------*/
345/*---------------------------------------------------------------------------*/
346
347#endif
#define ARCCORE_INTEGER_MAX
Macro indicating the maximum value that the Integer type can take.
Helper class to dump an array view on a stream.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
constexpr __host__ __device__ Integer arccoreCheckArraySize(unsigned long long size)
Checks that size can be converted into an 'Integer' to serve as an array size. If possible,...
std::int64_t Int64
Signed integer type of 64 bits.
Int32 Integer
Type representing an integer.
void dumpArray(std::ostream &o, ConstArrayView< T > val, int max_print)
Displays the values of array val to the stream o.
constexpr __host__ __device__ Int64 arccoreCheckLargeArraySize(size_t size)
Checks that size can be converted into an 'Int64' to serve as an array size.
std::int32_t Int32
Signed integer type of 32 bits.
Namespace of Arccore.