Arcane  4.1.12.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-2025 */
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#include <iostream>
20
21// 'assert' is necessary for accelerator code
22#include <assert.h>
23
24/*---------------------------------------------------------------------------*/
25/*---------------------------------------------------------------------------*/
26
27namespace Arcane::impl
28{
29
30/*---------------------------------------------------------------------------*/
31/*---------------------------------------------------------------------------*/
32
34template <typename ViewType> ARCCORE_HOST_DEVICE auto subViewInterval(ViewType view,
35 typename ViewType::size_type index,
36 typename ViewType::size_type nb_interval) -> ViewType
37{
38 using size_type = typename ViewType::size_type;
39 if (nb_interval <= 0)
40 return ViewType();
41 if (index < 0 || index >= nb_interval)
42 return ViewType();
43 size_type n = view.size();
44 size_type isize = n / nb_interval;
45 size_type ibegin = index * isize;
46 // For the last interval, take the remaining elements
47 if ((index + 1) == nb_interval)
48 isize = n - ibegin;
49 return ViewType::create(view.data() + ibegin, isize);
50}
51
52/*---------------------------------------------------------------------------*/
53/*---------------------------------------------------------------------------*/
54
62template <typename ViewType> inline void
63dumpArray(std::ostream& o, ViewType val, int max_print)
64{
65 using size_type = typename ViewType::size_type;
66 size_type n = val.size();
67 if (max_print > 0 && n > max_print) {
68 // Only displays the first (max_print/2) and the last (max_print/2)
69 // otherwise if the array is very large it can generate enormous
70 // output listings.
71 size_type z = (max_print / 2);
72 size_type z2 = n - z;
73 o << "[0]=\"" << val[0] << '"';
74 for (size_type i = 1; i < z; ++i)
75 o << " [" << i << "]=\"" << val[i] << '"';
76 o << " ... ... (skipping indexes " << z << " to " << z2 << " ) ... ... ";
77 for (size_type i = (z2 + 1); i < n; ++i)
78 o << " [" << i << "]=\"" << val[i] << '"';
79 }
80 else {
81 for (size_type i = 0; i < n; ++i) {
82 if (i != 0)
83 o << ' ';
84 o << "[" << i << "]=\"" << val[i] << '"';
85 }
86 }
87}
88
89/*---------------------------------------------------------------------------*/
90/*---------------------------------------------------------------------------*/
91
93template <typename ViewType> inline bool
94areEqual(ViewType rhs, ViewType lhs)
95{
96 using size_type = typename ViewType::size_type;
97 if (rhs.size() != lhs.size())
98 return false;
99 size_type s = rhs.size();
100 for (size_type i = 0; i < s; ++i) {
101 if (rhs[i] != lhs[i])
102 return false;
103 }
104 return true;
105}
106
107/*---------------------------------------------------------------------------*/
108/*---------------------------------------------------------------------------*/
109
111template <typename View2DType> inline bool
112areEqual2D(View2DType rhs, View2DType lhs)
113{
114 using size_type = typename View2DType::size_type;
115 const size_type dim1_size = rhs.dim1Size();
116 const size_type dim2_size = rhs.dim2Size();
117 if (dim1_size != lhs.dim1Size())
118 return false;
119 if (dim2_size != lhs.dim2Size())
120 return false;
121 for (size_type i = 0; i < dim1_size; ++i) {
122 for (size_type j = 0; j < dim2_size; ++j) {
123 if (rhs(i, j) != lhs(i, j))
124 return false;
125 }
126 }
127 return true;
128}
129
130/*---------------------------------------------------------------------------*/
131/*---------------------------------------------------------------------------*/
132
134extern "C++" ARCCORE_BASE_EXPORT void
135arccoreThrowTooBigInteger [[noreturn]] (std::size_t size);
136
137extern "C++" ARCCORE_BASE_EXPORT void
138arccoreThrowTooBigInt64 [[noreturn]] (std::size_t size);
139
140extern "C++" ARCCORE_BASE_EXPORT void
141arccoreThrowNegativeSize [[noreturn]] (Int64 size);
142
143/*---------------------------------------------------------------------------*/
144/*---------------------------------------------------------------------------*/
145
147inline constexpr ARCCORE_HOST_DEVICE void
148arccoreCheckIsPositive(Int64 size)
149{
150 if (size < 0) {
151#ifdef ARCCORE_DEVICE_CODE
152 assert("'size' is negative");
153#else
154 impl::arccoreThrowNegativeSize(size);
155#endif
156 }
157}
158
160inline constexpr ARCCORE_HOST_DEVICE void
161arccoreCheckIsValidInteger(Int64 size)
162{
163 if (size >= ARCCORE_INTEGER_MAX) {
164#ifdef ARCCORE_DEVICE_CODE
165 assert("'size' is bigger than ARCCORE_INTEGER_MAX");
166#else
167 impl::arccoreThrowTooBigInteger(size);
168#endif
169 }
170}
171
173inline constexpr ARCCORE_HOST_DEVICE void
174arccoreCheckIsValidInt64(size_t size)
175{
176 if (size >= ARCCORE_INT64_MAX) {
177#ifdef ARCCORE_DEVICE_CODE
178 assert("'size' is bigger than ARCCORE_INT64_MAX");
179#else
180 impl::arccoreThrowTooBigInt64(size);
181#endif
182 }
183}
184
185/*---------------------------------------------------------------------------*/
186/*---------------------------------------------------------------------------*/
187
188} // namespace Arcane::impl
189
190namespace Arcane
191{
192
193/*---------------------------------------------------------------------------*/
194/*---------------------------------------------------------------------------*/
195
202inline constexpr ARCCORE_HOST_DEVICE Integer
203arccoreCheckArraySize(unsigned long long size)
204{
205 impl::arccoreCheckIsValidInteger(size);
206 return static_cast<Integer>(size);
207}
208
215inline constexpr Integer
217{
218 impl::arccoreCheckIsValidInteger(size);
219 impl::arccoreCheckIsPositive(size);
220 return static_cast<Integer>(size);
221}
222
229inline constexpr ARCCORE_BASE_EXPORT Integer
230arccoreCheckArraySize(unsigned long size)
231{
232 impl::arccoreCheckIsValidInteger(size);
233 return static_cast<Integer>(size);
234}
235
243inline constexpr ARCCORE_HOST_DEVICE Integer
245{
246 impl::arccoreCheckIsValidInteger(size);
247 impl::arccoreCheckIsPositive(size);
248 return static_cast<Integer>(size);
249}
250
257inline constexpr ARCCORE_HOST_DEVICE Integer
258arccoreCheckArraySize(unsigned int size)
259{
260 impl::arccoreCheckIsValidInteger(size);
261 return static_cast<Integer>(size);
262}
263
270inline constexpr ARCCORE_HOST_DEVICE Integer
272{
273 impl::arccoreCheckIsValidInteger(size);
274 impl::arccoreCheckIsPositive(size);
275 return static_cast<Integer>(size);
276}
277
285inline constexpr ARCCORE_HOST_DEVICE Int64
287{
288 impl::arccoreCheckIsValidInt64(size);
289 return static_cast<Int64>(size);
290}
291
292/*---------------------------------------------------------------------------*/
293/*---------------------------------------------------------------------------*/
294
295template <typename IntType> class ArraySizeChecker;
296
298template <>
300{
301 public:
302
303 template <typename SizeType> ARCCORE_HOST_DEVICE static Int32 check(SizeType size)
304 {
305 return arccoreCheckArraySize(size);
306 }
307};
308
310template <>
312{
313 public:
314
315 static ARCCORE_HOST_DEVICE Int64 check(std::size_t size)
316 {
317 return arccoreCheckLargeArraySize(size);
318 }
319};
320
321/*---------------------------------------------------------------------------*/
322/*---------------------------------------------------------------------------*/
323
324} // namespace Arcane
325
326/*---------------------------------------------------------------------------*/
327/*---------------------------------------------------------------------------*/
328
329namespace Arccore::impl
330{
331using Arcane::impl::arccoreCheckIsPositive;
332using Arcane::impl::arccoreCheckIsValidInt64;
333using Arcane::impl::arccoreCheckIsValidInteger;
334using Arcane::impl::arccoreThrowNegativeSize;
335using Arcane::impl::arccoreThrowTooBigInt64;
336using Arcane::impl::arccoreThrowTooBigInteger;
337using Arcane::impl::areEqual;
338using Arcane::impl::areEqual2D;
339using Arcane::impl::dumpArray;
340using Arcane::impl::subViewInterval;
341} // namespace Arccore::impl
342
343namespace Arccore
344{
347} // namespace Arccore
348
349/*---------------------------------------------------------------------------*/
350/*---------------------------------------------------------------------------*/
351
352#endif
#define ARCCORE_INTEGER_MAX
Macro indicating the maximum value that the Integer type can take.
-- 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.
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.