9.2
general documentation
cs_array.h
Go to the documentation of this file.
1#ifndef CS_ARRAY_H
2#define CS_ARRAY_H
3
4/*============================================================================
5 * Array handling utilities.
6 *============================================================================*/
7
8/*
9 This file is part of code_saturne, a general-purpose CFD tool.
10
11 Copyright (C) 1998-2026 EDF S.A.
12
13 This program is free software; you can redistribute it and/or modify it under
14 the terms of the GNU General Public License as published by the Free Software
15 Foundation; either version 2 of the License, or (at your option) any later
16 version.
17
18 This program is distributed in the hope that it will be useful, but WITHOUT
19 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
20 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
21 details.
22
23 You should have received a copy of the GNU General Public License along with
24 this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
25 Street, Fifth Floor, Boston, MA 02110-1301, USA.
26*/
27
28/*----------------------------------------------------------------------------*/
29
30/*----------------------------------------------------------------------------
31 * Standard C and C++ library headers
32 *----------------------------------------------------------------------------*/
33
34#include <string.h>
35
36/*----------------------------------------------------------------------------
37 * Local headers
38 *----------------------------------------------------------------------------*/
39
40#include "base/cs_defs.h"
41#include "base/cs_base_accel.h"
42#if defined (__CUDACC__)
43#include "base/cs_array_cuda.h"
44#include "base/cs_base_cuda.h"
45#endif
46#if defined (__HIPCC__)
47#include "base/cs_array_hip.h"
48#include "base/cs_base_hip.h"
49#endif
50
51#include "base/cs_dispatch.h"
52#include "base/cs_mdspan.h"
53#include "base/cs_parall.h"
54
55#if defined(__cplusplus)
56#include <limits>
57#endif
58/*----------------------------------------------------------------------------*/
59
60/*=============================================================================
61 * Macro definitions
62 *============================================================================*/
63
64/* Define the way to apply a subset. In case of a copy, the subset is related
65 to the reference (input array) and/or the destination array (output
66 array) */
67
68#define CS_ARRAY_SUBSET_NULL -1
69#define CS_ARRAY_SUBSET_IN 0
70#define CS_ARRAY_SUBSET_OUT 1
71#define CS_ARRAY_SUBSET_INOUT 2
72
73/*============================================================================
74 * Type definitions
75 *============================================================================*/
76
77/*============================================================================
78 * Global variables
79 *============================================================================*/
80
81/*=============================================================================
82 * Public inline function prototypes
83 *============================================================================*/
84
85/*=============================================================================
86 * Public function prototypes
87 *============================================================================*/
88
89/*----------------------------------------------------------------------------*/
104/*----------------------------------------------------------------------------*/
105
106template <typename T, size_t stride, typename... Arrays>
107void
109 const T *ref_val,
110 Arrays&&... arrays)
111{
112 /* Expand the parameter pack */
113 T* array_ptrs[] = {arrays ... };
114
115#if defined (__CUDACC__) || defined(__HIPCC__)
116 bool is_available_on_device = cs_check_device_ptr(ref_val);
117 for (T* array : array_ptrs)
118 is_available_on_device = is_available_on_device
119 && (cs_check_device_ptr(array)
121
122 if (is_available_on_device) {
123#if defined (__CUDACC__)
124 cudaStream_t stream_ = cs_cuda_get_stream(0);
125#else
126 hipStream_t stream_ = cs_hip_get_stream(0);
127#endif
128 cs_arrays_set_value<T, stride>(stream_,
129 false,
130 n_elts,
131 ref_val,
132 arrays...);
133 return;
134 }
135#endif
136
137 auto set_value = [=](cs_lnum_t i_elt) {
138 for (T* array : array_ptrs)
139 memcpy(array + i_elt*stride, ref_val, stride*sizeof(T));
140 };
141
142 /* Loop through each index and assign values */
143# ifdef _OPENMP
144 int n_threads = cs_parall_n_threads(n_elts, CS_THR_MIN);
145# pragma omp parallel for num_threads(n_threads)
146# endif
147 for (cs_lnum_t i = 0; i < n_elts; i++)
148 set_value(i);
149}
150
151/*----------------------------------------------------------------------------*/
166/*----------------------------------------------------------------------------*/
167
168template <typename T, size_t stride, typename... Arrays>
169void
171 const T ref_val,
172 Arrays&&... arrays)
173{
174 /* Expand the parameter pack */
175 T* array_ptrs[] = {arrays ... };
176
177#if defined (__CUDACC__) || defined (__HIPCC__)
178 bool is_available_on_device = true;
179 for (T* array : array_ptrs)
180 is_available_on_device = is_available_on_device
181 && (cs_check_device_ptr(array)
183
184 if (is_available_on_device) {
185#if defined (__CUDACC__)
186 cudaStream_t stream_ = cs_cuda_get_stream(0);
187#else
188 hipStream_t stream_ = cs_hip_get_stream(0);
189#endif
190 cs_arrays_set_value<T, stride>(stream_,
191 false,
192 n_elts,
193 ref_val,
194 arrays...);
195 return;
196 }
197#endif
198
199 auto set_value = [=](cs_lnum_t i_elt) {
200 for (T* array : array_ptrs)
201 for (size_t k = 0; k < stride; k++) {
202 array[i_elt*stride + k] = ref_val;
203 }
204 };
205
206 /* Loop through each index and assign values */
207# ifdef _OPENMP
208 int n_threads = cs_parall_n_threads(n_elts, CS_THR_MIN);
209# pragma omp parallel for num_threads(n_threads)
210# endif
211 for (cs_lnum_t i = 0; i < n_elts; i++)
212 set_value(i);
213}
214
215/*----------------------------------------------------------------------------*/
234/*----------------------------------------------------------------------------*/
235
236template <typename T, size_t stride, typename... Arrays>
237void
239 const cs_lnum_t n_elts,
240 const T *ref_val,
241 Arrays&&... arrays)
242{
243 /* Expand the parameter pack */
244 T* array_ptrs[] = {arrays ... };
245
246#if defined (__CUDACC__) || defined (__HIPCC__)
247 if (ctx.use_gpu()) {
248 cs_stream_t stream_ = ctx.stream();
249 cs_arrays_set_value<T, stride>(stream_,
250 true,
251 n_elts,
252 ref_val,
253 arrays...);
254 return;
255 }
256#endif
257
258 auto set_value = [=](cs_lnum_t i_elt) {
259 for (T* array : array_ptrs)
260 memcpy(array + i_elt*stride, ref_val, stride*sizeof(T));
261 };
262
263 /* Loop through each index and assign values */
264# ifdef _OPENMP
265 int n_threads = ctx.n_cpu_threads();
266 if (n_threads < 0)
267 n_threads = cs_parall_n_threads(n_elts, CS_THR_MIN);
268# pragma omp parallel for num_threads(n_threads)
269# endif
270 for (cs_lnum_t i = 0; i < n_elts; i++)
271 set_value(i);
272}
273
274/*----------------------------------------------------------------------------*/
293/*----------------------------------------------------------------------------*/
294
295template <typename T, size_t stride, typename... Arrays>
296void
298 const cs_lnum_t n_elts,
299 const T ref_val,
300 Arrays&&... arrays)
301{
302 /* Expand the parameter pack */
303 T* array_ptrs[] = {arrays ... };
304
305#if defined (__CUDACC__) || defined (__HIPCC__)
306 if (ctx.use_gpu()) {
307 cs_stream_t stream_ = ctx.stream();
308 cs_arrays_set_value<T, stride>(stream_,
309 true,
310 n_elts,
311 ref_val,
312 arrays...);
313 return;
314 }
315#endif
316
317 auto set_value = [=](cs_lnum_t i_elt) {
318 for (T* array : array_ptrs)
319 for (size_t k = 0; k < stride; k++) {
320 array[i_elt*stride + k] = ref_val;
321 }
322 };
323
324 /* Loop through each index and assign values */
325# ifdef _OPENMP
326 int n_threads = ctx.n_cpu_threads();
327 if (n_threads < 0)
328 n_threads = cs_parall_n_threads(n_elts, CS_THR_MIN);
329# pragma omp parallel for num_threads(n_threads)
330# endif
331 for (cs_lnum_t i = 0; i < n_elts; i++)
332 set_value(i);
333}
334
335/*----------------------------------------------------------------------------*/
352/*----------------------------------------------------------------------------*/
353
354template <typename T, size_t stride, typename... Arrays>
355void
357 const cs_lnum_t n_elts,
358 Arrays&&... arrays)
359{
360 /* Expand the parameter pack */
361 T* array_ptrs[] = {arrays ... };
362
363#if defined (__CUDACC__) || defined (__HIPCC__)
364 if (ctx.use_gpu()) {
365 cs_stream_t stream_ = ctx.stream();
366 cs_arrays_set_zero<T, stride>(stream_,
367 true,
368 n_elts,
369 arrays...);
370 return;
371 }
372#endif
373
374 constexpr T c = static_cast<T>(0);
375
376 cs_lnum_t n_vals = n_elts*stride;
377
378# ifdef _OPENMP
379 int n_threads = ctx.n_cpu_threads();
380 if (n_threads < 0)
381 n_threads = cs_parall_n_threads(n_vals, CS_THR_MIN);
382# pragma omp parallel num_threads(n_threads)
383# endif
384 {
385 for (T* array : array_ptrs) {
386# ifdef _OPENMP
387# pragma omp for nowait
388# endif
389 for (cs_lnum_t i = 0; i < n_vals; i++)
390 array[i] = c;
391 }
392 };
393}
394
395/*----------------------------------------------------------------------------*/
411/*----------------------------------------------------------------------------*/
412
413template <typename T, size_t stride, typename... Arrays>
414void
416 const cs_lnum_t *elt_ids,
417 const T *ref_val,
418 Arrays&&... arrays)
419{
420 if (n_elts < 1)
421 return;
422
423 if (elt_ids == NULL)
424 cs_arrays_set_value<T, stride>(n_elts, ref_val, arrays...);
425 else {
426 /* Expand the parameter pack */
427 T* array_ptrs[] = {arrays ... };
428
429 auto set_value = [=](cs_lnum_t i_elt) {
430 for (T* array : array_ptrs)
431 memcpy(array + i_elt*stride, ref_val, stride*sizeof(T));
432 };
433
434 /* Loop through each index on the subset and assign values */
435# ifdef _OPENMP
436 int n_threads = cs_parall_n_threads(n_elts, CS_THR_MIN);
437# pragma omp parallel for num_threads(n_threads)
438# endif
439 for (cs_lnum_t i = 0; i < n_elts; i++)
440 set_value(elt_ids[i]);
441 }
442}
443
444/*----------------------------------------------------------------------------*/
460/*----------------------------------------------------------------------------*/
461
462template <typename T, size_t stride, typename... Arrays>
463void
465 const cs_lnum_t *elt_ids,
466 const T ref_val,
467 Arrays&&... arrays)
468{
469 if (n_elts < 1)
470 return;
471
472 if (elt_ids == NULL)
473 cs_arrays_set_value<T, stride>(n_elts, ref_val, arrays...);
474 else {
475
476 /* Expand the parameter pack */
477 T* array_ptrs[] = {arrays ... };
478
479 auto set_value = [=](cs_lnum_t i_elt) {
480 for (T* array : array_ptrs)
481 for (size_t k = 0; k < stride; k++) {
482 array[i_elt*stride + k] = ref_val;
483 }
484 };
485
486 /* Loop through each index on the subset and assign values */
487# ifdef _OPENMP
488 int n_threads = cs_parall_n_threads(n_elts, CS_THR_MIN);
489# pragma omp parallel for num_threads(n_threads)
490# endif
491 for (cs_lnum_t i = 0; i < n_elts; i++)
492 set_value(elt_ids[i]);
493 }
494}
495
496/*----------------------------------------------------------------------------*/
507/*----------------------------------------------------------------------------*/
508
509template <typename T>
510void
512 const T* src,
513 T* dest)
514{
515#if defined (__CUDACC__) || defined (__HIPCC__)
516 bool is_available_on_device = cs_check_device_ptr(src)
517 && cs_check_device_ptr(dest);
518
519 if (is_available_on_device) {
520#if defined (__CUDACC__)
521 cudaStream_t stream_ = cs_cuda_get_stream(0);
522#else
523 hipStream_t stream_ = cs_hip_get_stream(0);
524#endif
525 cs_array_copy<T>(stream_, false, size, src, dest);
526 return;
527 }
528#endif
529
530# ifdef _OPENMP
531 int n_threads = cs_parall_n_threads(size, CS_THR_MIN);
532# pragma omp parallel for num_threads(n_threads)
533# endif
534 for (cs_lnum_t ii = 0; ii < size; ii++)
535 dest[ii] = src[ii];
536}
537
538/*----------------------------------------------------------------------------*/
552/*----------------------------------------------------------------------------*/
553
554template <typename T>
555void
557 const T *x,
558 const T *y,
559 T *diff)
560{
561 cs_array_copy(size, x, diff);
562
563# ifdef _OPENMP
564 int n_threads = cs_parall_n_threads(size, CS_THR_MIN);
565# pragma omp parallel for num_threads(n_threads)
566# endif
567 for (cs_lnum_t ii = 0; ii < size; ii++)
568 diff[ii] -= y[ii];
569}
570
571/*----------------------------------------------------------------------------*/
572/*
573 * \brief Assign true to all elements of an array. Case of an array of booleans
574 *
575 * \param[in] size total number of elements to set
576 * \param[in, out] a array to set
577 */
578/*----------------------------------------------------------------------------*/
579
580void
582 bool a[]);
583
584/*----------------------------------------------------------------------------*/
585/*
586 * \brief Assign false to all elements of an array. Case of an array of booleans
587 *
588 * \param[in] size total number of elements to set
589 * \param[in, out] a array to set
590 */
591/*----------------------------------------------------------------------------*/
592
593void
595 bool a[]);
596
597/*----------------------------------------------------------------------------*/
598/*
599 * \brief Assign zero to all elements of an array. Case of a cs_flag_t array.
600 *
601 * \param[in] size total number of elements to set to zero
602 * \param[in, out] a array of flags to set
603 */
604/*----------------------------------------------------------------------------*/
605
606void
608 cs_flag_t a[]);
609
610/*----------------------------------------------------------------------------*/
611/*
612 * \brief Assign zero to all elements of an array. Case of a cs_lnum_t array.
613 *
614 * \param[in] size total number of elements to set to zero
615 * \param[in, out] a array to set
616 */
617/*----------------------------------------------------------------------------*/
618
619void
621 cs_lnum_t a[]);
622
623/*----------------------------------------------------------------------------*/
624/*
625 * \brief Copy values from an array of cs_lnum_t type to another of the same
626 * dimensions.
627 *
628 * \param[in] size number of elements * dimension
629 * \param[in] src source array values (size: n_elts*dim)
630 * \param[out] dest destination array values (size: n_elts*dim)
631 */
632/*----------------------------------------------------------------------------*/
633
634void
636 const cs_lnum_t src[],
637 cs_lnum_t dest[]);
638
639/*----------------------------------------------------------------------------*/
640/*
641 * \brief Assign the value "num" to all elements of an array. Case of a
642 * cs_lnum_t array.
643 *
644 * \param[in] size total number of elements to set
645 * \param[in] num value to set
646 * \param[in, out] a array to set
647 */
648/*----------------------------------------------------------------------------*/
649
650void
652 cs_lnum_t num,
653 cs_lnum_t a[]);
654
655/*----------------------------------------------------------------------------*/
656/*
657 * \brief Assign the value "num" to an array on a selected subset of elements.
658 * if elt_ids is null, then one recovers the function
659 * \ref cs_array_lnum_set_value
660 *
661 * \param[in] n_elts number of elements
662 * \param[in] elt_ids list of ids in the subset or null (size: n_elts)
663 * \param[in] num value to set
664 * \param[in, out] a array to set
665 */
666/*----------------------------------------------------------------------------*/
667
668void
670 const cs_lnum_t elt_ids[],
671 cs_lnum_t num,
672 cs_lnum_t a[]);
673/*----------------------------------------------------------------------------*/
674/*
675 * \brief Assign zero to all elements of an array. Case of a int array.
676 *
677 * \param[in] size total number of elements to set to zero
678 * \param[in, out] a array to set
679 */
680/*----------------------------------------------------------------------------*/
681
682void
684 int a[]);
685
686/*----------------------------------------------------------------------------*/
687/*
688 * \brief Assign the value "num" to all elements of an array. Case of a
689 * int array.
690 *
691 * \param[in] size total number of elements to set
692 * \param[in] num value to set
693 * \param[in, out] a array to set
694 */
695/*----------------------------------------------------------------------------*/
696
697void
699 int num,
700 int a[]);
701
702/*----------------------------------------------------------------------------*/
703/*
704 * \brief Assign the value "num" to an array on a selected subset of elements.
705 * if elt_ids is null, then one recovers the function
706 * \ref cs_array_int_set_value
707 *
708 * \param[in] n_elts number of elements
709 * \param[in] elt_ids list of ids in the subset or null (size: n_elts)
710 * \param[in] num value to set
711 * \param[in, out] a array to set
712 */
713/*----------------------------------------------------------------------------*/
714
715void
717 const cs_lnum_t elt_ids[],
718 int num,
719 int a[]);
720
721/*----------------------------------------------------------------------------*/
722/*
723 * \brief Copy an array ("ref") into another array ("dest") on possibly only a
724 * part of the array(s). Array with stride > 1 are assumed to be
725 * interlaced. The subset of elements on which working is defined by
726 * "elt_ids". The way to apply the subset is defined with the parameter
727 * "mode" as follows:
728 * - Only the "ref" array if mode = 0 (CS_ARRAY_SUBSET_IN)
729 * - Only the "dest" array if mode = 1 (CS_ARRAY_SUBSET_OUT)
730 * - Both "ref" and "dest" arrays if mode = 2 (CS_ARRAY_SUBSET_INOUT)
731 *
732 * It elt_ids is null or mode < 0 (CS_ARRAY_SUBSET_NULL), then the
733 * behavior is as \ref cs_array_real_copy
734 *
735 * One assumes that all arrays are allocated with a correct size.
736 *
737 * \param[in] n_elts number of elements in the array
738 * \param[in] stride number of values for each element
739 * \param[in] mode apply the subset ids to which array(s)
740 * \param[in] elt_ids list of ids in the subset or null (size: n_elts)
741 * \param[in] ref reference values to copy
742 * \param[in, out] dest array storing values after applying the indirection
743 */
744/*----------------------------------------------------------------------------*/
745
746void
748 int stride,
749 const cs_lnum_t elt_ids[],
750 int mode,
751 const cs_real_t ref[],
752 cs_real_t dest[]);
753
754/*----------------------------------------------------------------------------*/
755/*
756 * \brief Copy real values from an array to another of the same dimensions.
757 *
758 * \param[in] size number of elements * dimension
759 * \param[in] src source array values (size: n_elts*dim)
760 * \param[out] dest destination array values (size: n_elts*dim)
761 */
762/*----------------------------------------------------------------------------*/
763
764void
766 const cs_real_t src[],
767 cs_real_t dest[]);
768
769/*----------------------------------------------------------------------------*/
770/*
771 * \brief Multiply each value by a scaling factor s.t. dest *= scaling_factor
772 * If elt_ids is non-null, one applies an indirection.
773 * A stride can also be applied. One assumes an interlaced array.
774 *
775 * \param[in] n_elts number of elements
776 * \param[in] stride number of values for each element
777 * \param[in] elt_ids list of ids in the subset or null (size: n_elts)
778 * \param[in] scaling_factor value of the scaling factor
779 * \param[out] dest destination array values
780 */
781/*----------------------------------------------------------------------------*/
782
783void
785 int stride,
786 const cs_lnum_t *elt_ids,
787 cs_real_t scaling_factor,
788 cs_real_t dest[]);
789
790/*----------------------------------------------------------------------------*/
791/*
792 * \brief Add in place an array s.t. r += l_add
793 *
794 * \param[in] n_elts number of elements
795 * \param[in] l_add array to add
796 * \param[out] r destination array values
797 */
798/*----------------------------------------------------------------------------*/
799
800void
802 const cs_real_t l_add[],
803 cs_real_t r[]);
804
805/*----------------------------------------------------------------------------*/
806/*
807 * \brief Assign a constant value of dim "stride" to an interlaced array
808 * sharing the same stride
809 *
810 * \param[in] n_elts number of elements
811 * \param[in] stride number of values for each element
812 * \param[in] ref_val list of values to assign (size: stride)
813 * \param[in, out] a array to set (size: n_elts*stride)
814 */
815/*----------------------------------------------------------------------------*/
816
817void
819 int stride,
820 const cs_real_t ref_val[],
821 cs_real_t a[]);
822
823/*----------------------------------------------------------------------------*/
824/*
825 * \brief Assign a weighted constant value of dim "stride" to an interlaced
826 * array sharing the same stride. Apply a weight for each element. This
827 * weight is constant for each component of an element.
828 *
829 * \param[in] n_elts number of elements
830 * \param[in] stride number of values for each element
831 * \param[in] ref_val list of values to assign (size: stride)
832 * \param[in] weight values of the weight to apply (size: n_elts)
833 * \param[in, out] a array to set (size: n_elts*stride)
834 */
835/*----------------------------------------------------------------------------*/
836
837void
839 int stride,
840 const cs_real_t ref_val[],
841 const cs_real_t weight[],
842 cs_real_t a[]);
843
844/*----------------------------------------------------------------------------*/
845/*
846 * \brief Assign a constant value of dim "stride" to an interlaced array
847 * sharing the same stride. Only a subset of elements are considered.
848 * If elt_ids is null, then one recovers the function
849 * \ref cs_array_real_set_value
850 *
851 * \param[in] n_elts number of elements
852 * \param[in] stride number of values for each element
853 * \param[in] elt_ids list of ids in the subset or null (size: n_elts)
854 * \param[in] ref_val list of values to assign (size: stride)
855 * \param[in, out] a array to set (size >= n_elts * stride)
856 */
857/*----------------------------------------------------------------------------*/
858
859void
861 int stride,
862 const cs_lnum_t elt_ids[],
863 const cs_real_t ref_val[],
864 cs_real_t a[]);
865
866/*----------------------------------------------------------------------------*/
867/*
868 * \brief Assign a weighted constant value of dim "stride" to an interlaced
869 * array sharing the same stride. Only a subset of elements are
870 * considered. If elt_ids is null, then one recovers the function \ref
871 * cs_array_real_set_wvalue Apply a weight for each element. This
872 * weight is constant for each component of an element.
873 *
874 * \param[in] n_elts number of elements
875 * \param[in] stride number of values for each element
876 * \param[in] elt_ids list of ids in the subset or null (size: n_elts)
877 * \param[in] ref_val list of values to assign (size: stride)
878 * \param[in] weight values of the weight to apply (size >= n_elts)
879 * \param[in, out] a array to set (size >= n_elts*stride)
880 */
881/*----------------------------------------------------------------------------*/
882
883void
885 int stride,
886 const cs_lnum_t elt_ids[],
887 const cs_real_t ref_val[],
888 const cs_real_t weight[],
889 cs_real_t a[]);
890
891/*----------------------------------------------------------------------------*/
892/*
893 * \brief Assign a constant scalar value to an array.
894 *
895 * \param[in] n_elts number of elements
896 * \param[in] ref_val value to assign
897 * \param[in, out] a array to set
898 */
899/*----------------------------------------------------------------------------*/
900
901void
903 cs_real_t ref_val,
904 cs_real_t a[]);
905
906/*----------------------------------------------------------------------------*/
907/*
908 * \brief Assign a weighted constant scalar value to an array.
909 * The weight array has the same size as the array "a".
910 *
911 * \param[in] n_elts number of elements
912 * \param[in] ref_val value to assign
913 * \param[in] weight values of the weight to apply
914 * \param[in, out] a array to set
915 */
916/*----------------------------------------------------------------------------*/
917
918void
920 cs_real_t ref_val,
921 const cs_real_t weight[],
922 cs_real_t a[]);
923
924/*----------------------------------------------------------------------------*/
925/*
926 * \brief Assign a constant scalar value to an array on a selected subset of
927 * elements. If elt_ids is null, then one recovers the function
928 * cs_array_real_set_scalar
929 *
930 * \param[in] n_elts number of elements
931 * \param[in] elt_ids list of ids in the subset or null (size: n_elts)
932 * \param[in] ref_val value to assign
933 * \param[in, out] a array to set
934 */
935/*----------------------------------------------------------------------------*/
936
937void
939 const cs_lnum_t elt_ids[],
940 cs_real_t ref_val,
941 cs_real_t a[]);
942
943/*----------------------------------------------------------------------------*/
944/*
945 * \brief Assign a weighted constant scalar value to an array on a selected
946 * subset of elements. If elt_ids is null, then one recovers the function
947 * cs_array_real_set_wscalar
948 *
949 * \param[in] n_elts number of elements
950 * \param[in] elt_ids list of ids in the subset or null (size: n_elts)
951 * \param[in] ref_val value to assign
952 * \param[in] weight values of weights to apply
953 * \param[in, out] a array to set
954 */
955/*----------------------------------------------------------------------------*/
956
957void
959 const cs_lnum_t elt_ids[],
960 cs_real_t ref_val,
961 const cs_real_t weight[],
962 cs_real_t a[]);
963
964/*----------------------------------------------------------------------------*/
965/*
966 * \brief Assign a constant vector to an array of stride 3 which is interlaced
967 *
968 * \param[in] n_elts number of elements
969 * \param[in] ref_val vector to assign
970 * \param[in, out] a array to set
971 */
972/*----------------------------------------------------------------------------*/
973
974void
976 const cs_real_t ref_val[3],
977 cs_real_t a[]);
978
979/*----------------------------------------------------------------------------*/
980/*
981 * \brief Assign a weighted constant vector value to an interlaced array (of
982 * stride 3). The array of weights has the same size as the array "a".
983 *
984 * \param[in] n_elts number of elements
985 * \param[in] ref_val vector to assign
986 * \param[in] weight values of the weight to apply
987 * \param[in, out] a array to set
988 */
989/*----------------------------------------------------------------------------*/
990
991void
993 const cs_real_t ref_val[3],
994 const cs_real_t weight[],
995 cs_real_t a[]);
996
997/*----------------------------------------------------------------------------*/
998/*
999 * \brief Assign a constant vector to an interlaced array (of stride 3) on a
1000 * selected subset of elements. If elt_ids is null, then one recovers the
1001 * function cs_array_real_set_vector
1002 *
1003 * \param[in] n_elts number of elements
1004 * \param[in] elt_ids list of ids in the subset or null (size: n_elts)
1005 * \param[in] ref_val vector to assign
1006 * \param[in, out] a array to set
1007 */
1008/*----------------------------------------------------------------------------*/
1009
1010void
1012 const cs_lnum_t elt_ids[],
1013 const cs_real_t ref_val[3],
1014 cs_real_t a[]);
1015
1016/*----------------------------------------------------------------------------*/
1017/*
1018 * \brief Assign a weighted constant vector value to an interlaced array (of
1019 * stride 3). The subset selection is given by elt_ids. If null, then
1020 * one recovers the function \ref cs_array_real_set_wvector
1021 * The array of weights has the same size as the array "a".
1022 *
1023 * \param[in] n_elts number of elements
1024 * \param[in] elt_ids list of ids in the subset or null (size: n_elts)
1025 * \param[in] ref_val vector to assign
1026 * \param[in] weight values of the weight to apply
1027 * \param[in, out] a array to set
1028 */
1029/*----------------------------------------------------------------------------*/
1030
1031void
1033 const cs_lnum_t elt_ids[],
1034 const cs_real_t ref_val[3],
1035 const cs_real_t weight[],
1036 cs_real_t a[]);
1037
1038/*----------------------------------------------------------------------------*/
1039/*
1040 * \brief Assign a constant 3x3 tensor to an array (of stride 9) which is
1041 * interlaced
1042 *
1043 * \param[in] n_elts number of elements
1044 * \param[in] ref_tens tensor to assign
1045 * \param[in, out] a array to set
1046 */
1047/*----------------------------------------------------------------------------*/
1048
1049void
1051 const cs_real_t ref_tens[3][3],
1052 cs_real_t a[]);
1053
1054/*----------------------------------------------------------------------------*/
1055/*
1056 * \brief Assign a constant 3x3 tensor to an interlaced array (of stride 9) on
1057 * a subset of elements. If elt_ids is null, then one recovers the
1058 * function cs_array_real_set_tensor
1059 *
1060 * \param[in] n_elts number of elements
1061 * \param[in] elt_ids list of ids defining the subset or nullptr
1062 * \param[in] ref_tens tensor to assign
1063 * \param[in, out] a array to set
1064 */
1065/*----------------------------------------------------------------------------*/
1066
1067void
1069 const cs_lnum_t elt_ids[],
1070 const cs_real_t ref_tens[3][3],
1071 cs_real_t a[]);
1072
1073/*----------------------------------------------------------------------------*/
1074/*
1075 * \brief Assign zero to all elements of an array.
1076 *
1077 * \param[in] size total number of elements to set to zero
1078 * \param[in, out] a array to set
1079 */
1080/*----------------------------------------------------------------------------*/
1081
1082void
1084 cs_real_t a[]);
1085
1086/*----------------------------------------------------------------------------*/
1087/*
1088 * \brief Assign a constant value to an array (deprecated function).
1089 *
1090 * \param[in] n_elts number of associated elements
1091 * \param[in] dim associated dimension
1092 * \param[in] v value to assign
1093 * \param[out] a array values (size: n_elts*dim)
1094 */
1095/*----------------------------------------------------------------------------*/
1096
1097void
1099 cs_lnum_t dim,
1100 cs_real_t v,
1101 cs_real_t a[]);
1102
1103/*----------------------------------------------------------------------------*/
1104
1105namespace cs {
1106
1107/*----------------------------------------------------------------------------*/
1115/*----------------------------------------------------------------------------*/
1116
1117template<class T, int N = 1, layout L = layout::right>
1118class array : public mdspan<T, N, L> {
1119
1120private:
1121 using _span = mdspan<T,N,L>;
1122
1123public:
1124 using mdspan<T,N,L>::copy_data;
1125
1126 /*--------------------------------------------------------------------------*/
1130 /*--------------------------------------------------------------------------*/
1131
1132 CS_F_HOST
1133 void *
1134 operator new
1135 (
1136 std::size_t count,
1137#if (defined(__GNUC__) || defined(__clang__)) \
1138 && !defined(CS_AOCC_BUILTIN_WORKAROUND) \
1139 && __has_builtin(__builtin_LINE) && __has_builtin(__builtin_FILE)
1140 const char *file_name = __builtin_FILE(),
1141 const int line_number = __builtin_LINE()
1142#else
1143 const char *file_name = __FILE__,
1144 const int line_number = __LINE__
1145#endif
1146 )
1147 {
1148 return cs_mem_malloc_hd(CS_ALLOC_HOST,
1149 1,
1150 count,
1151 "cs::array",
1152 file_name,
1153 line_number);
1154 }
1155
1156 /*--------------------------------------------------------------------------*/
1160 /*--------------------------------------------------------------------------*/
1161
1162 CS_F_HOST
1163 void *
1164 operator new[]
1165 (
1166 std::size_t count,
1167#if (defined(__GNUC__) || defined(__clang__)) && \
1168 __has_builtin(__builtin_LINE) && \
1169 __has_builtin(__builtin_FILE)
1170 const char *file_name = __builtin_FILE(),
1171 const int line_number = __builtin_LINE()
1172#else
1173 const char *file_name = __FILE__,
1174 const int line_number = __LINE__
1175#endif
1176 )
1177 {
1178 return cs_mem_malloc_hd(CS_ALLOC_HOST,
1179 1,
1180 count,
1181 "cs::array",
1182 file_name,
1183 line_number);
1184 }
1185
1186 /*--------------------------------------------------------------------------*/
1190 /*--------------------------------------------------------------------------*/
1191
1192 CS_F_HOST
1193 void
1194 operator delete(void *ptr)
1195 {
1196 CS_FREE(ptr);
1197 }
1198
1199 /*--------------------------------------------------------------------------*/
1203 /*--------------------------------------------------------------------------*/
1204
1205 CS_F_HOST
1206 void
1207 operator delete[](void *ptr)
1208 {
1209 CS_FREE(ptr);
1210 }
1211
1212 /*--------------------------------------------------------------------------*/
1216 /*--------------------------------------------------------------------------*/
1217
1220 {
1221 _owner = true;
1222#if !defined(__CUDA_ARCH__) && \
1223 !defined(SYCL_LANGUAGE_VERSION) && \
1224 !defined(__HIP_DEVICE_COMPILE__)
1225 _mode = cs_alloc_mode;
1226#else
1227 // use default and avoid compiler warnings
1229#endif
1230 }
1231
1232 /*--------------------------------------------------------------------------*/
1236 /*--------------------------------------------------------------------------*/
1237
1238 CS_F_HOST
1240 (
1241 cs_lnum_t size,
1242 cs_alloc_mode_t alloc_mode = cs_alloc_mode,
1243#if (defined(__GNUC__) || defined(__clang__)) && \
1244 __has_builtin(__builtin_LINE) && \
1245 __has_builtin(__builtin_FILE)
1246 const char *file_name = __builtin_FILE(),
1247 const int line_number = __builtin_LINE()
1248#else
1249 const char *file_name = __FILE__,
1250 const int line_number = __LINE__
1251#endif
1252 )
1253 :
1254 _owner(true),
1255 _mode(alloc_mode)
1256 {
1257 /* Only usable for array */
1258 static_assert(N == 1,
1259 "Instantiation using only total size only possible for "
1260 " cs::array<T,1> or cs::array<T>");
1262 allocate_(file_name, line_number);
1263#if defined(CS_ARRAY_DBG_INIT)
1264 set_to_nan_();
1265#endif
1266 }
1267
1268 /*--------------------------------------------------------------------------*/
1272 /*--------------------------------------------------------------------------*/
1273
1274 CS_F_HOST
1276 (
1277 const cs_lnum_t(&dims)[N],
1278 cs_alloc_mode_t alloc_mode = cs_alloc_mode,
1279#if (defined(__GNUC__) || defined(__clang__)) && \
1280 __has_builtin(__builtin_LINE) && \
1281 __has_builtin(__builtin_FILE)
1282 const char *file_name = __builtin_FILE(),
1283 const int line_number = __builtin_LINE()
1284#else
1285 const char *file_name = __FILE__,
1286 const int line_number = __LINE__
1287#endif
1288 )
1289 :
1290 _owner(true),
1291 _mode(alloc_mode)
1292 {
1293 _span::set_size_(dims);
1294 allocate_(file_name, line_number);
1295#if defined(CS_ARRAY_DBG_INIT)
1296 set_to_nan_();
1297#endif
1298 }
1299
1300 /*--------------------------------------------------------------------------*/
1304 /*--------------------------------------------------------------------------*/
1305
1306 CS_F_HOST
1308 (
1309 cs_lnum_t size1,
1310 cs_lnum_t size2,
1311 cs_alloc_mode_t alloc_mode = cs_alloc_mode,
1312#if (defined(__GNUC__) || defined(__clang__)) && \
1313 __has_builtin(__builtin_LINE) && \
1314 __has_builtin(__builtin_FILE)
1315 const char *file_name = __builtin_FILE(),
1316 const int line_number = __builtin_LINE()
1317#else
1318 const char *file_name = __FILE__,
1319 const int line_number = __LINE__
1320#endif
1321 )
1322 :
1323 _owner(true),
1324 _mode(alloc_mode)
1325 {
1326 /* Only usable for array */
1327 static_assert(N == 2,
1328 "Instantiation using (size1, size2) only possible for "
1329 " cs::array<T,2>");
1330 cs_lnum_t tmp_size[N] = {size1, size2};
1331 _span::set_size_(tmp_size);
1332 allocate_(file_name, line_number);
1333#if defined(CS_ARRAY_DBG_INIT)
1334 set_to_nan_();
1335#endif
1336 }
1337
1338 /*--------------------------------------------------------------------------*/
1342 /*--------------------------------------------------------------------------*/
1343
1344 CS_F_HOST
1346 (
1347 cs_lnum_t size1,
1348 cs_lnum_t size2,
1349 cs_lnum_t size3,
1350 cs_alloc_mode_t alloc_mode = cs_alloc_mode,
1351#if (defined(__GNUC__) || defined(__clang__)) && \
1352 __has_builtin(__builtin_LINE) && \
1353 __has_builtin(__builtin_FILE)
1354 const char *file_name = __builtin_FILE(),
1355 const int line_number = __builtin_LINE()
1356#else
1357 const char *file_name = __FILE__,
1358 const int line_number = __LINE__
1359#endif
1360 )
1361 :
1362 _owner(true),
1363 _mode(alloc_mode)
1364 {
1365 /* Only usable for array */
1366 static_assert(N == 3,
1367 "Instantiation using (size1, size2, size3) only possible "
1368 " for cs::array<T,3>");
1369 cs_lnum_t tmp_size[N] = {size1, size2, size3};
1370 _span::set_size_(tmp_size);
1371 allocate_(file_name, line_number);
1372#if defined(CS_ARRAY_DBG_INIT)
1373 set_to_nan_();
1374#endif
1375 }
1376
1377 /*--------------------------------------------------------------------------*/
1381 /*--------------------------------------------------------------------------*/
1382
1383 CS_F_HOST
1385 (
1386 cs_lnum_t size1,
1387 cs_lnum_t size2,
1388 cs_lnum_t size3,
1389 cs_lnum_t size4,
1390 cs_alloc_mode_t alloc_mode = cs_alloc_mode,
1391#if (defined(__GNUC__) || defined(__clang__)) && \
1392 __has_builtin(__builtin_LINE) && \
1393 __has_builtin(__builtin_FILE)
1394 const char *file_name = __builtin_FILE(),
1395 const int line_number = __builtin_LINE()
1396#else
1397 const char *file_name = __FILE__,
1398 const int line_number = __LINE__
1399#endif
1400 )
1401 :
1402 _owner(true),
1403 _mode(alloc_mode)
1404 {
1405 /* Only usable for array */
1406 static_assert(N == 4,
1407 "Instantiation using (size1, size2, size3, size4) only possible "
1408 " for cs::array<T,4>");
1409 cs_lnum_t tmp_size[N] = {size1, size2, size3, size4};
1410 _span::set_size_(tmp_size);
1411 allocate_(file_name, line_number);
1412#if defined(CS_ARRAY_DBG_INIT)
1413 set_to_nan_();
1414#endif
1415 }
1416
1417 /*--------------------------------------------------------------------------*/
1421 /*--------------------------------------------------------------------------*/
1422
1423 template<typename... Args>
1426 (
1427 T *data,
1428 Args... indices
1429 )
1430 :
1431 _span(data, indices...),
1432 _owner(false)
1433 {
1434#if !defined(__CUDA_ARCH__) && \
1435 !defined(SYCL_LANGUAGE_VERSION) && \
1436 !defined(__HIP_DEVICE_COMPILE__)
1437 _mode = cs_alloc_mode;
1438#else
1439 // use default and avoid compiler warnings
1441#endif
1442 }
1443
1444 /*--------------------------------------------------------------------------*/
1448 /*--------------------------------------------------------------------------*/
1449
1452 (
1453 T *data,
1454 const cs_lnum_t(&dims)[N]
1455 )
1456 :
1457 _span(data, dims),
1458 _owner(false)
1459 {
1460#if !defined(__CUDA_ARCH__) && \
1461 !defined(SYCL_LANGUAGE_VERSION) && \
1462 !defined(__HIP_DEVICE_COMPILE__)
1463 _mode = cs_alloc_mode;
1464#else
1465 // use default and avoid compiler warnings
1467#endif
1468 }
1469
1470 /*--------------------------------------------------------------------------*/
1475 /*--------------------------------------------------------------------------*/
1476
1479 (
1480 const array& other
1481 )
1482 :_span()
1483 {
1485 _mode = other._mode;
1486 _owner = false;
1487 _span::set_data_ptr(other.data());
1488 }
1489
1490 /*--------------------------------------------------------------------------*/
1494 /*--------------------------------------------------------------------------*/
1495
1498 (
1499 array&& other
1500 )
1501 : array()
1502 {
1503 swap(*this, other);
1504 }
1505
1506 /*--------------------------------------------------------------------------*/
1510 /*--------------------------------------------------------------------------*/
1511
1514 {
1515 clear();
1516 }
1517
1518 /*--------------------------------------------------------------------------*/
1522 /*--------------------------------------------------------------------------*/
1523
1525 friend void
1527 (
1528 array& first,
1529 array& second
1530 )
1531 {
1532 /* Swap the different members between the two references. */
1533 cs::swap_objects(first._span::_extent, second._span::_extent);
1534 cs::swap_objects(first._span::_offset, second._span::_offset);
1535 cs::swap_objects(first._span::_size, second._span::_size);
1536 cs::swap_objects(first._span::_data, second._span::_data);
1537 cs::swap_objects(first._owner, second._owner);
1538 cs::swap_objects(first._mode, second._mode);
1539 }
1540
1541 /*--------------------------------------------------------------------------*/
1545 /*--------------------------------------------------------------------------*/
1546
1549 {
1550 swap(*this, other);
1551
1552 return *this;
1553 }
1554
1555 /*--------------------------------------------------------------------------*/
1559 /*--------------------------------------------------------------------------*/
1560
1562 void
1564 {
1565 if (_owner) {
1566 // Avoid compiler warnings. If device, object cannot be owner...
1567#if !defined(__CUDA_ARCH__) && \
1568 !defined(SYCL_LANGUAGE_VERSION) && \
1569 !defined(__HIP_DEVICE_COMPILE__)
1571#endif
1572 }
1573 else {
1574 _span::set_data_ptr(nullptr);
1575 }
1576 _span::_size = 0;
1577 for (int i = 0; i < N; i++) {
1578 _span::_offset[i] = 0;
1579 _span::_extent[i] = 0;
1580 }
1581 }
1582
1583 CS_F_HOST
1586 (
1587#if (defined(__GNUC__) || defined(__clang__)) && \
1588 __has_builtin(__builtin_LINE) && \
1589 __has_builtin(__builtin_FILE)
1590 const char *file_name = __builtin_FILE(),
1591 const int line_number = __builtin_LINE()
1592#else
1593 const char *file_name = __FILE__,
1594 const int line_number = __LINE__
1595#endif
1596 ) const
1597 {
1598 array<T, N, L> new_array(this->_extent,
1599 this->_mode,
1600 file_name,
1601 line_number);
1602 new_array.copy_data(*this);
1603
1604 return new_array;
1605 }
1606
1607 /*--------------------------------------------------------------------------*/
1613 /*--------------------------------------------------------------------------*/
1614
1618 {
1620 }
1621
1622 /*--------------------------------------------------------------------------*/
1630 /*--------------------------------------------------------------------------*/
1631
1632 template<int _N_, layout _L_ = L>
1636 (
1637 const cs_lnum_t(&dims)[_N_]
1638 )
1639 {
1640 /* sanity check for total size */
1641 cs_lnum_t s = 1;
1642 for (int i = 0; i < _N_; i++)
1643 s *= dims[i];
1644
1645 if (s != _span::_size) {
1646#if !defined(__CUDA_ARCH__) && \
1647 !defined(SYCL_LANGUAGE_VERSION) && \
1648 !defined(__HIP_DEVICE_COMPILE__)
1649 bft_error(__FILE__, __LINE__, 0,
1650 _("%s: requested span has total size of %d instead of %d "
1651 "for this array.\n"),
1652 __func__, s, _span::_size);
1653#else
1654 return mdspan<T,_N_,_L_>();
1655#endif
1656 }
1657
1658 return mdspan<T,_N_,_L_>(_span::_data, dims);
1659 }
1660
1661 /*--------------------------------------------------------------------------*/
1669 /*--------------------------------------------------------------------------*/
1670
1671 template<layout _L_ = L, typename... Args>
1673 mdspan<T, sizeof...(Args), _L_>
1675 (
1676 Args... indices
1677 )
1678 {
1679 check_non_empty_args_(indices...);
1680
1681 constexpr int n_vals = sizeof...(Args);
1682 cs_lnum_t tmp_args[n_vals] = {indices...};
1683
1684 return get_mdspan<n_vals, _L_>(tmp_args);
1685 }
1686
1687 /*--------------------------------------------------------------------------*/
1691 /*--------------------------------------------------------------------------*/
1692
1694 void
1696 {
1697 cs_lnum_t tmp[N] = {0};
1698 _span::set_size_(tmp);
1699 _span::set_data_ptr(nullptr);
1700 _owner = false;
1701#if !defined(__CUDA_ARCH__) && \
1702 !defined(SYCL_LANGUAGE_VERSION) && \
1703 !defined(__HIP_DEVICE_COMPILE__)
1704 _mode = cs_alloc_mode;
1705#else
1707#endif
1708 }
1709
1710 /*--------------------------------------------------------------------------*/
1714 /*--------------------------------------------------------------------------*/
1715
1716 CS_F_HOST
1718 (
1719 cs_lnum_t new_size,
1720#if (defined(__GNUC__) || defined(__clang__)) && \
1721 __has_builtin(__builtin_LINE) && \
1722 __has_builtin(__builtin_FILE)
1723 const char *file_name = __builtin_FILE(),
1724 const int line_number = __builtin_LINE()
1725#else
1726 const char *file_name = __FILE__,
1727 const int line_number = __LINE__
1728#endif
1729 )
1730 {
1731 static_assert(N == 1,
1732 "Method reshape(size, ...) only possible for "
1733 " cs::array<T> or cs::array<T,1>");
1734 assert(new_size >= 0);
1735
1736 /* If same dimensions, nothing to do ... */
1737 if (new_size == _span::_size)
1738 return;
1739
1740 if (_owner) {
1741 _span::set_size_(new_size);
1742 reallocate_(file_name, line_number);
1743 }
1744 else
1745 bft_error(__FILE__, __LINE__, 0,
1746 _("%s: array cannot be reshaped if non owner, "
1747 "use mdspan instead.\n"), __func__);
1748
1749 }
1750
1751 /*--------------------------------------------------------------------------*/
1755 /*--------------------------------------------------------------------------*/
1756
1757 CS_F_HOST
1758 void
1760 (
1761 cs_lnum_t new_size,
1762 cs_lnum_t size_to_keep,
1763#if (defined(__GNUC__) || defined(__clang__)) && \
1764 __has_builtin(__builtin_LINE) && \
1765 __has_builtin(__builtin_FILE)
1766 const char *file_name = __builtin_FILE(),
1767 const int line_number = __builtin_LINE()
1768#else
1769 const char *file_name = __FILE__,
1770 const int line_number = __LINE__
1771#endif
1772 )
1773 {
1774 static_assert(N==1,
1775 "Method reshape_and_copy(size, ...) only possible for "
1776 " cs::array<T> or cs::array<T,1>");
1777 assert(new_size >= 0);
1778 assert(size_to_keep <= new_size && size_to_keep <= _span::_size);
1779
1780 /* If same dimensions, nothing to do ... */
1781 if (new_size == _span::_size)
1782 return;
1783
1784 if (!(size_to_keep <= new_size && size_to_keep <= _span::_size))
1785 bft_error(__FILE__, __LINE__, 0,
1786 "%s: Data cannot be saved when new size is smaller "
1787 "than size to keep.\n",
1788 __func__);
1789
1790 if (_owner) {
1791 /* If new size is larger, realloc is sufficient. */
1792 _span::set_size_(new_size);
1793 reallocate_(file_name, line_number);
1794 }
1795 else
1796 bft_error(__FILE__, __LINE__, 0,
1797 _("%s: array cannot be reshaped if non owner, "
1798 "use mdspan instead.\n"), __func__);
1799 }
1800
1801 /*--------------------------------------------------------------------------*/
1805 /*--------------------------------------------------------------------------*/
1806
1807 CS_F_HOST
1809 (
1810 const cs_lnum_t(&dims)[N],
1811#if (defined(__GNUC__) || defined(__clang__)) && \
1812 __has_builtin(__builtin_LINE) && \
1813 __has_builtin(__builtin_FILE)
1814 const char *file_name = __builtin_FILE(),
1815 const int line_number = __builtin_LINE()
1816#else
1817 const char *file_name = __FILE__,
1818 const int line_number = __LINE__
1819#endif
1820 )
1821 {
1822 /* If same dimensions, nothing to do ... */
1823 bool same_dim = true;
1824 for (int i = 0; i < N; i++)
1825 if (dims[i] != _span::_extent[i])
1826 same_dim = false;
1827
1828 if (same_dim)
1829 return;
1830
1831 if (_owner) {
1832 _span::set_size_(dims);
1833 reallocate_(file_name, line_number);
1834 }
1835 else
1836 bft_error(__FILE__, __LINE__, 0,
1837 _("%s: array cannot be reshaped if non owner, "
1838 "use mdspan instead.\n"), __func__);
1839 }
1840
1841 /*--------------------------------------------------------------------------*/
1845 /*--------------------------------------------------------------------------*/
1846
1847 CS_F_HOST
1849 (
1850 const cs_lnum_t(&dims)[N],
1851 cs_lnum_t size_to_keep = -1,
1852#if (defined(__GNUC__) || defined(__clang__)) && \
1853 __has_builtin(__builtin_LINE) && \
1854 __has_builtin(__builtin_FILE)
1855 const char *file_name = __builtin_FILE(),
1856 const int line_number = __builtin_LINE()
1857#else
1858 const char *file_name = __FILE__,
1859 const int line_number = __LINE__
1860#endif
1861 )
1862 {
1863 assert(size_to_keep <= dims[N-1] && size_to_keep <= _span::_extent[N-1]);
1864
1865 /* If same dimensions, nothing to do ... */
1866 bool same_dim = true;
1867 for (int i = 0; i < N; i++)
1868 if (dims[i] != _span::_extent[i])
1869 same_dim = false;
1870
1871 if (same_dim)
1872 return;
1873
1874 if (_owner) {
1875 /* check that size_to_keep is either >0 or == -1 (keep all) */
1876 if (size_to_keep != 0) {
1877 /* Temporary copy */
1878 array<T,N,L> tmp = this->get_deep_copy();
1879
1880 /* Update instance size */
1881 _span::set_size_(dims);
1882 reallocate_(file_name, line_number);
1883
1884 /* Local copies for the context loop */
1885 cs_lnum_t new_o[N], new_e[N], old_o[N], old_e[N], max_e[N];
1886
1887 for (int i = 0; i < N; i++) {
1888 new_o[i] = _span::offset(i);
1889 new_e[i] = _span::extent(i);
1890 old_o[i] = tmp.offset(i);
1891 old_e[i] = tmp.extent(i);
1892
1893 max_e[i] = (new_e[i] > old_e[i]) ? old_e[i] : new_e[i];
1894 }
1895 if (max_e[N-1] > size_to_keep && size_to_keep > 0)
1896 max_e[N-1] = size_to_keep;
1897
1898 T* old_data = tmp.data();
1899 T* new_data = _span::data();
1900
1901 cs_lnum_t loop_size = tmp.size();
1902
1903 /* Loop using dispatch */
1905
1906 ctx.parallel_for(loop_size, CS_LAMBDA (cs_lnum_t e_id) {
1907 cs_lnum_t idx[N];
1908 cs_lnum_t dummy = e_id;
1909
1910 for (int i = N-1; i >= 1; i--) {
1911 idx[i] = dummy % old_e[i];
1912 dummy = (dummy - idx[i]) / old_e[i];
1913 }
1914 idx[0] = dummy;
1915
1916 bool all_ok = true;
1917 for (int i = 0; i < N; i++) {
1918 if (idx[i] >= max_e[i]) {
1919 all_ok = false;
1920 break;
1921 }
1922 }
1923
1924 if (all_ok) {
1925 cs_lnum_t o_id = 0;
1926 cs_lnum_t n_id = 0;
1927
1928 for (int i = 0; i < N; i++) {
1929 o_id += idx[i] * old_o[i];
1930 n_id += idx[i] * new_o[i];
1931 }
1932
1933 new_data[n_id] = old_data[o_id];
1934 }
1935 });
1936
1937 ctx.wait();
1938 }
1939 else {
1940 reshape(dims, file_name, line_number);
1941 }
1942 }
1943 else
1944 bft_error(__FILE__, __LINE__, 0,
1945 _("%s: array cannot be reshaped if non owner, "
1946 "use mdspan instead.\n"), __func__);
1947 }
1948
1949 /*--------------------------------------------------------------------------*/
1953 /*--------------------------------------------------------------------------*/
1954
1955 CS_F_HOST
1956 void
1958 (
1959 const cs_lnum_t size1,
1960 const cs_lnum_t size2,
1961#if (defined(__GNUC__) || defined(__clang__)) && \
1962 __has_builtin(__builtin_LINE) && \
1963 __has_builtin(__builtin_FILE)
1964 const char *file_name = __builtin_FILE(),
1965 const int line_number = __builtin_LINE()
1966#else
1967 const char *file_name = __FILE__,
1968 const int line_number = __LINE__
1969#endif
1970 )
1971 {
1972 static_assert(N == 2,
1973 "Method reshape(size1, size2, ...) only possible for "
1974 " cs::array<T,2>");
1975 cs_lnum_t tmp_size[N] = {size1, size2};
1976
1977 reshape(tmp_size, file_name, line_number);
1978 }
1979
1980 /*--------------------------------------------------------------------------*/
1984 /*--------------------------------------------------------------------------*/
1985
1986 CS_F_HOST
1987 void
1989 (
1990 const cs_lnum_t size1,
1991 const cs_lnum_t size2,
1992 cs_lnum_t size_to_keep,
1993#if (defined(__GNUC__) || defined(__clang__)) && \
1994 __has_builtin(__builtin_LINE) && \
1995 __has_builtin(__builtin_FILE)
1996 const char *file_name = __builtin_FILE(),
1997 const int line_number = __builtin_LINE()
1998#else
1999 const char *file_name = __FILE__,
2000 const int line_number = __LINE__
2001#endif
2002 )
2003 {
2004 static_assert(N == 2,
2005 "Method reshape_and_copy(size1, size2, ...) only possible for "
2006 " cs::array<T,2>");
2007 cs_lnum_t tmp_size[N] = {size1, size2};
2008
2009 reshape_and_copy(tmp_size, size_to_keep, file_name, line_number);
2010 }
2011
2012 /*--------------------------------------------------------------------------*/
2016 /*--------------------------------------------------------------------------*/
2017
2018 CS_F_HOST
2019 void
2021 (
2022 const cs_lnum_t size1,
2023 const cs_lnum_t size2,
2024 const cs_lnum_t size3,
2025#if (defined(__GNUC__) || defined(__clang__)) && \
2026 __has_builtin(__builtin_LINE) && \
2027 __has_builtin(__builtin_FILE)
2028 const char *file_name = __builtin_FILE(),
2029 const int line_number = __builtin_LINE()
2030#else
2031 const char *file_name = __FILE__,
2032 const int line_number = __LINE__
2033#endif
2034 )
2035 {
2036 static_assert(N == 3,
2037 "Method reshape(size1, size2, size3, ...) only possible for "
2038 " cs::array<T,3>");
2039 cs_lnum_t tmp_size[N] = {size1, size2, size3};
2040
2041 reshape(tmp_size, file_name, line_number);
2042 }
2043
2044 /*--------------------------------------------------------------------------*/
2048 /*--------------------------------------------------------------------------*/
2049
2050 CS_F_HOST
2051 void
2053 (
2054 const cs_lnum_t size1,
2055 const cs_lnum_t size2,
2056 const cs_lnum_t size3,
2057 cs_lnum_t size_to_keep,
2058#if (defined(__GNUC__) || defined(__clang__)) && \
2059 __has_builtin(__builtin_LINE) && \
2060 __has_builtin(__builtin_FILE)
2061 const char *file_name = __builtin_FILE(),
2062 const int line_number = __builtin_LINE()
2063#else
2064 const char *file_name = __FILE__,
2065 const int line_number = __LINE__
2066#endif
2067 )
2068 {
2069 static_assert(N == 3,
2070 "Method reshape_and_copy(size1, size2, size3, ...) only "
2071 "possible for cs::array<T,3>");
2072 cs_lnum_t tmp_size[N] = {size1, size2, size3};
2073
2074 reshape_and_copy(tmp_size, size_to_keep, file_name, line_number);
2075 }
2076
2077 /*--------------------------------------------------------------------------*/
2081 /*--------------------------------------------------------------------------*/
2082
2083 CS_F_HOST
2084 void
2086 (
2087 const cs_lnum_t size1,
2088 const cs_lnum_t size2,
2089 const cs_lnum_t size3,
2090 const cs_lnum_t size4,
2091 cs_lnum_t size_to_keep,
2092#if (defined(__GNUC__) || defined(__clang__)) && \
2093 __has_builtin(__builtin_LINE) && \
2094 __has_builtin(__builtin_FILE)
2095 const char *file_name = __builtin_FILE(),
2096 const int line_number = __builtin_LINE()
2097#else
2098 const char *file_name = __FILE__,
2099 const int line_number = __LINE__
2100#endif
2101 )
2102 {
2103 static_assert(N == 4,
2104 "Method reshape_and_copy(size1, size2, size3, size4, ...)"
2105 " only possible for cs::array<T,4>");
2106 cs_lnum_t tmp_size[N] = {size1, size2, size3, size4};
2107
2108 reshape_and_copy(tmp_size, size_to_keep, file_name, line_number);
2109 }
2110
2111 /*--------------------------------------------------------------------------*/
2115 /*--------------------------------------------------------------------------*/
2116
2119 (
2121 )
2122 {
2123 if (_mode != mode) {
2124 /* Since allocation mode is changed, deallocate data */
2125 if (_span::_size != 0)
2126 clear();
2127
2128 _mode = mode;
2129 }
2130 }
2131
2132 /*--------------------------------------------------------------------------*/
2138 /*--------------------------------------------------------------------------*/
2139
2141 inline
2142 bool
2143 owner() const
2144 {
2145 return _owner;
2146 }
2147
2148 /*--------------------------------------------------------------------------*/
2154 /*--------------------------------------------------------------------------*/
2155
2157 inline
2159 mode() const
2160 {
2161 return _mode;
2162 }
2163
2164 /*--------------------------------------------------------------------------*/
2169 /*--------------------------------------------------------------------------*/
2170
2171 CS_F_HOST
2172 inline
2173 void
2175 (
2176 const array& other,
2177 const cs_lnum_t n_vals = -1
2179 )
2180 {
2181 auto& ctx = cs::execution::default_context();
2182 copy_data(ctx, other, n_vals);
2183 ctx.wait();
2184 }
2185
2186 /*--------------------------------------------------------------------------*/
2192 /*--------------------------------------------------------------------------*/
2193
2194 CS_F_HOST
2195 void
2197 (
2198 cs_dispatch_context &ctx,
2199 const array &other,
2200 const cs_lnum_t n_vals = -1
2202 )
2203 {
2204 const cs_lnum_t loop_size = (n_vals == -1) ? _span::_size : n_vals;
2205
2206 assert(loop_size <= _span::size());
2207 assert(loop_size <= other.size());
2208
2209 // Explicit pointer, avoid passing internal member of class to the functor
2210 T* data_ptr = _span::data();
2211 T* o_data_ptr = other.data();
2212
2213 ctx.parallel_for(loop_size, CS_LAMBDA (cs_lnum_t e_id) {
2214 data_ptr[e_id] = o_data_ptr[e_id];
2215 });
2216 }
2217
2218private:
2219
2220 /*--------------------------------------------------------------------------*/
2224 /*--------------------------------------------------------------------------*/
2225
2226 template<typename... Args>
2228 static inline
2229 void
2230 check_non_empty_args_
2231 (
2232 Args...
2233 )
2234 {
2235 static_assert(sizeof...(Args) > 0, "At least one input argument is needed.");
2236 static_assert(cs::are_integral<Args...>::value, "Non integral input arguments.");
2237 }
2238
2239 /*--------------------------------------------------------------------------*/
2243 /*--------------------------------------------------------------------------*/
2244
2245 CS_F_HOST
2246 void
2247 allocate_
2248 (
2249 const char *file_name,
2250 const int line_number
2251 )
2252 {
2253 const char *_ptr_name = "cs::array._data";
2254 _span::_data = static_cast<T *>(cs_mem_malloc_hd(_mode,
2256 sizeof(T),
2257 _ptr_name,
2258 file_name,
2259 line_number));
2260 }
2261
2262 /*--------------------------------------------------------------------------*/
2266 /*--------------------------------------------------------------------------*/
2267
2268 CS_F_HOST
2269 void
2270 reallocate_
2271 (
2272 const char *file_name,
2273 const int line_number
2274 )
2275 {
2276 /* If not owner no-op */
2277 if (_owner) {
2278 const char *_ptr_name = "cs::array._data";
2279 _span::_data = static_cast<T *>(cs_mem_realloc_hd(_span::_data,
2280 _mode,
2282 sizeof(T),
2283 _ptr_name,
2284 file_name,
2285 line_number));
2286 }
2287 };
2288
2289 /*--------------------------------------------------------------------------*/
2293 /*--------------------------------------------------------------------------*/
2294 CS_F_HOST
2295 inline
2296 void
2297 set_to_nan_()
2298 {
2299 auto& ctx = cs::execution::default_context();
2300 constexpr T tmp_nan = std::numeric_limits<T>::signaling_NaN();
2301 _span::set_to_val(ctx, tmp_nan);
2302 ctx.wait();
2303 }
2304 /*===========================================================================
2305 * Private members
2306 *==========================================================================*/
2307
2308 bool _owner;
2309 cs_alloc_mode_t _mode;
2310
2311};
2312
2313} /* namespace cs */
2314
2315/*--------------------------------------------------------------------------*/
2316/* Useful aliases without namespace */
2317/*--------------------------------------------------------------------------*/
2318
2319template<class T>
2321
2322template<class T, cs::layout L = cs::layout::right>
2324
2325template<class T, cs::layout L = cs::layout::right>
2327
2328template<class T, cs::layout L = cs::layout::right>
2330
2331template<class T, int N, cs::layout L = cs::layout::right>
2333
2334template<class T, int N>
2336
2337template<class T, int N>
2339
2340/*----------------------------------------------------------------------------*/
2341
2342#endif /* CS_ARRAY_H */
void bft_error(const char *const file_name, const int line_num, const int sys_error_code, const char *const format,...)
Calls the error handler (set by bft_error_handler_set() or default).
Definition: bft_error.cpp:187
Define a templated array class (owner of data)
Definition: cs_array.h:1118
CS_F_HOST_DEVICE mdspan< T, _N_, _L_ > get_mdspan(const cs_lnum_t(&dims)[_N_])
Get span view of array with a given array of dimensions (extent). If total size of span is different ...
Definition: cs_array.h:1636
CS_F_HOST_DEVICE void set_alloc_mode(cs_alloc_mode_t mode)
Set memory allocation mode.
Definition: cs_array.h:2119
CS_F_HOST array(cs_lnum_t size1, cs_lnum_t size2, cs_alloc_mode_t alloc_mode=cs_alloc_mode, const char *file_name=__FILE__, const int line_number=__LINE__)
Constructor method for 2D array based on sizes.
Definition: cs_array.h:1308
CS_F_HOST_DEVICE cs_alloc_mode_t mode() const
Getter function for allocation mode.
Definition: cs_array.h:2159
CS_F_HOST void copy_data(cs_dispatch_context &ctx, const array &other, const cs_lnum_t n_vals=-1)
Copy data from raw pointer, we suppose that data size is same as that of the array....
Definition: cs_array.h:2197
CS_F_HOST void reshape(cs_lnum_t new_size, const char *file_name=__FILE__, const int line_number=__LINE__)
Resize data array.
Definition: cs_array.h:1718
CS_F_HOST array(cs_lnum_t size, cs_alloc_mode_t alloc_mode=cs_alloc_mode, const char *file_name=__FILE__, const int line_number=__LINE__)
Constructor method using only size.
Definition: cs_array.h:1240
CS_F_HOST void reshape_and_copy(cs_lnum_t new_size, cs_lnum_t size_to_keep, const char *file_name=__FILE__, const int line_number=__LINE__)
Resize data array and copy old data.
Definition: cs_array.h:1760
CS_F_HOST_DEVICE array(const array &other)
Constructor method using copy results in a shallow copy. If a deep copy is needed,...
Definition: cs_array.h:1479
CS_F_HOST_DEVICE mdspan< T, N, L > view()
Get span view of array, same dimensions as array.
Definition: cs_array.h:1617
CS_F_HOST_DEVICE array & operator=(array other)
Assignment operator.
Definition: cs_array.h:1548
CS_F_HOST array(cs_lnum_t size1, cs_lnum_t size2, cs_lnum_t size3, cs_lnum_t size4, cs_alloc_mode_t alloc_mode=cs_alloc_mode, const char *file_name=__FILE__, const int line_number=__LINE__)
Constructor method for 4D array based on sizes.
Definition: cs_array.h:1385
CS_F_HOST_DEVICE array(array &&other)
Move constructor.
Definition: cs_array.h:1498
CS_F_HOST void reshape(const cs_lnum_t(&dims)[N], const char *file_name=__FILE__, const int line_number=__LINE__)
Resize data array and do not keep old data.
Definition: cs_array.h:1809
CS_F_HOST void reshape(const cs_lnum_t size1, const cs_lnum_t size2, const char *file_name=__FILE__, const int line_number=__LINE__)
Resize data array 2D (no explicit copy, only realloc)
Definition: cs_array.h:1958
CS_F_HOST array< T, N, L > get_deep_copy(const char *file_name=__FILE__, const int line_number=__LINE__) const
Definition: cs_array.h:1586
CS_F_HOST void copy_data(const array &other, const cs_lnum_t n_vals=-1)
Copy data from another array, we suppose that data size is same as that of the array....
Definition: cs_array.h:2175
CS_F_HOST_DEVICE array()
Default constructor method leading to "empty container".
Definition: cs_array.h:1219
CS_F_HOST array(cs_lnum_t size1, cs_lnum_t size2, cs_lnum_t size3, cs_alloc_mode_t alloc_mode=cs_alloc_mode, const char *file_name=__FILE__, const int line_number=__LINE__)
Constructor method for 3D array based on sizes.
Definition: cs_array.h:1346
CS_F_HOST_DEVICE void clear()
Clear data (empty container).
Definition: cs_array.h:1563
CS_F_HOST void reshape_and_copy(const cs_lnum_t size1, const cs_lnum_t size2, cs_lnum_t size_to_keep, const char *file_name=__FILE__, const int line_number=__LINE__)
Resize data array and copy old data (2D)
Definition: cs_array.h:1989
CS_F_HOST void reshape(const cs_lnum_t size1, const cs_lnum_t size2, const cs_lnum_t size3, const char *file_name=__FILE__, const int line_number=__LINE__)
Resize data array 3D (no explicit copy, only realloc)
Definition: cs_array.h:2021
CS_F_HOST array(const cs_lnum_t(&dims)[N], cs_alloc_mode_t alloc_mode=cs_alloc_mode, const char *file_name=__FILE__, const int line_number=__LINE__)
Constructor method using dimensions.
Definition: cs_array.h:1276
CS_F_HOST_DEVICE array(T *data, const cs_lnum_t(&dims)[N])
Constructor method for non owner version in multidimensional.
Definition: cs_array.h:1452
CS_F_HOST void reshape_and_copy(const cs_lnum_t(&dims)[N], cs_lnum_t size_to_keep=-1, const char *file_name=__FILE__, const int line_number=__LINE__)
Resize data array and copy old data.
Definition: cs_array.h:1849
CS_F_HOST_DEVICE ~array()
Destructor method.
Definition: cs_array.h:1513
CS_F_HOST void reshape_and_copy(const cs_lnum_t size1, const cs_lnum_t size2, const cs_lnum_t size3, cs_lnum_t size_to_keep, const char *file_name=__FILE__, const int line_number=__LINE__)
Resize data array and copy old data (3D)
Definition: cs_array.h:2053
CS_F_HOST_DEVICE void set_empty()
Initializer method for empty containers.
Definition: cs_array.h:1695
CS_F_HOST_DEVICE bool owner() const
Getter function for owner status.
Definition: cs_array.h:2143
CS_F_HOST_DEVICE array(T *data, Args... indices)
Constructor method for non owner version.
Definition: cs_array.h:1426
CS_F_HOST_DEVICE friend void swap(array &first, array &second)
Class swap operator used for assignment or move.
Definition: cs_array.h:1527
CS_F_HOST void reshape_and_copy(const cs_lnum_t size1, const cs_lnum_t size2, const cs_lnum_t size3, const cs_lnum_t size4, cs_lnum_t size_to_keep, const char *file_name=__FILE__, const int line_number=__LINE__)
Resize data array and copy old data (4D)
Definition: cs_array.h:2086
Define a templated mdspan class (non owner of data)
Definition: cs_mdspan.h:68
CS_F_HOST void set_to_val(T val, const cs_lnum_t n_vals=-1)
Set all values of the data array to a constant value.
Definition: cs_mdspan.h:680
CS_F_HOST_DEVICE void set_size_(const cs_lnum_t(&dims)[N])
Set size of array based on dimensions.
Definition: cs_mdspan.h:1067
T * _data
Definition: cs_mdspan.h:1186
CS_F_HOST_DEVICE cs_lnum_t size() const
Getter for total size.
Definition: cs_mdspan.h:508
CS_F_HOST_DEVICE cs_lnum_t offset(int i) const
Getter for extent along a given dimension.
Definition: cs_mdspan.h:544
cs_lnum_t _size
Definition: cs_mdspan.h:1185
cs_lnum_t _extent[N]
Definition: cs_mdspan.h:1183
cs_lnum_t _offset[N]
Definition: cs_mdspan.h:1184
CS_F_HOST_DEVICE T * data() const
Getter for data raw pointer.
Definition: cs_mdspan.h:561
CS_F_HOST_DEVICE cs_lnum_t extent(int i) const
Getter for extent along a given dimension.
Definition: cs_mdspan.h:525
CS_F_HOST_DEVICE void set_data_ptr(T *data_ptr) const
Set data pointer.
Definition: cs_mdspan.h:454
auto parallel_for(cs_lnum_t n, F &&f, Args &&... args)
Definition: cs_dispatch.h:2146
void wait(void)
Wait (synchronize) until launched computations have finished.
Definition: cs_dispatch.h:2211
Definition: cs_dispatch.h:2288
void cs_array_real_fill_zero(cs_lnum_t size, cs_real_t a[])
Assign zero to all elements of an array.
Definition: cs_array.cpp:1015
void cs_array_real_set_wvalue(cs_lnum_t n_elts, int stride, const cs_real_t ref_val[], const cs_real_t weight[], cs_real_t a[])
Assign a weighted constant value of dim "stride" to an interlaced array sharing the same stride....
Definition: cs_array.cpp:598
void cs_array_real_set_scalar_on_subset(cs_lnum_t n_elts, const cs_lnum_t elt_ids[], cs_real_t ref_val, cs_real_t a[])
Assign a constant scalar value to an array on a selected subset of elements. If elt_ids is null,...
Definition: cs_array.cpp:762
void cs_array_set_value_real(cs_lnum_t n_elts, cs_lnum_t dim, cs_real_t v, cs_real_t a[])
Assign a constant value to an array (deprecated function).
Definition: cs_array.cpp:1041
void cs_array_bool_fill_false(cs_lnum_t size, bool a[])
Assign false to all elements of an array. Case of an array of booleans.
Definition: cs_array.cpp:102
void cs_arrays_set_zero(cs_dispatch_context &ctx, const cs_lnum_t n_elts, Arrays &&... arrays)
Assign zero value to all elements of multiple arrays.
Definition: cs_array.h:356
void cs_array_real_set_tensor(cs_lnum_t n_elts, const cs_real_t ref_tens[3][3], cs_real_t a[])
Assign a constant 3x3 tensor to an array (of stride 9) which is interlaced.
Definition: cs_array.cpp:946
void cs_array_int_fill_zero(cs_lnum_t size, int a[])
Assign zero to all elements of an array. Case of a int array.
Definition: cs_array.cpp:243
void cs_array_real_set_vector(cs_lnum_t n_elts, const cs_real_t ref_val[3], cs_real_t a[])
Assign a constant vector to an array of stride 3 which is interlaced.
Definition: cs_array.cpp:826
void cs_array_flag_fill_zero(cs_lnum_t size, cs_flag_t a[])
Assign zero to all elements of an array. Case of a cs_flag_t array.
Definition: cs_array.cpp:120
void cs_array_real_scale(cs_lnum_t n_elts, int stride, const cs_lnum_t *elt_ids, cs_real_t scaling_factor, cs_real_t dest[])
Multiply each value by a scaling factor s.t. dest *= scaling_factor If elt_ids is non-null,...
Definition: cs_array.cpp:485
void cs_arrays_set_value_on_subset(const cs_lnum_t n_elts, const cs_lnum_t *elt_ids, const T *ref_val, Arrays &&... arrays)
Assign values on a selected subset of elements to multiple arrays. ref_val is input as a pointer or a...
Definition: cs_array.h:415
void cs_array_difference(cs_lnum_t size, const T *x, const T *y, T *diff)
Compute the difference diff = x - y. All arrays have the same dimension.
Definition: cs_array.h:556
void cs_array_real_set_wvector_on_subset(cs_lnum_t n_elts, const cs_lnum_t elt_ids[], const cs_real_t ref_val[3], const cs_real_t weight[], cs_real_t a[])
Assign a weighted constant vector value to an interlaced array (of stride 3). The subset selection is...
Definition: cs_array.cpp:912
void cs_array_copy(const cs_lnum_t size, const T *src, T *dest)
Copy values from an array to another of the same dimensions.
Definition: cs_array.h:511
void cs_array_real_copy_subset(cs_lnum_t n_elts, int stride, const cs_lnum_t elt_ids[], int mode, const cs_real_t ref[], cs_real_t dest[])
Copy an array ("ref") into another array ("dest") on possibly only a part of the array(s)....
Definition: cs_array.cpp:332
void cs_array_lnum_fill_zero(cs_lnum_t size, cs_lnum_t a[])
Assign zero to all elements of an array. Case of a cs_lnum_t array.
Definition: cs_array.cpp:142
void cs_array_lnum_set_value_on_subset(cs_lnum_t n_elts, const cs_lnum_t elt_ids[], cs_lnum_t num, cs_lnum_t a[])
Assign the value "num" to an array on a selected subset of elements. if elt_ids is null,...
Definition: cs_array.cpp:218
void cs_array_real_set_scalar(cs_lnum_t n_elts, cs_real_t ref_val, cs_real_t a[])
Assign a constant scalar value to an array.
Definition: cs_array.cpp:716
void cs_array_bool_fill_true(cs_lnum_t size, bool a[])
Assign true to all elements of an array. Case of an array of booleans.
Definition: cs_array.cpp:84
void cs_array_real_set_vector_on_subset(cs_lnum_t n_elts, const cs_lnum_t elt_ids[], const cs_real_t ref_val[3], cs_real_t a[])
Assign a constant vector to an interlaced array (of stride 3) on a selected subset of elements....
Definition: cs_array.cpp:877
void cs_array_real_set_value_on_subset(cs_lnum_t n_elts, int stride, const cs_lnum_t elt_ids[], const cs_real_t ref_val[], cs_real_t a[])
Assign a constant value of dim "stride" to an interlaced array sharing the same stride....
Definition: cs_array.cpp:637
void cs_array_real_padd(cs_lnum_t n_elts, const cs_real_t l_add[], cs_real_t r[])
Add in place an array s.t. r += l_add.
Definition: cs_array.cpp:537
void cs_array_lnum_copy(cs_lnum_t size, const cs_lnum_t src[], cs_lnum_t dest[])
Copy values from an array of cs_lnum_t type to another of the same dimensions.
Definition: cs_array.cpp:166
void cs_array_real_copy(cs_lnum_t size, const cs_real_t src[], cs_real_t dest[])
Copy real values from an array to another of the same dimensions.
Definition: cs_array.cpp:453
void cs_array_int_set_value(cs_lnum_t size, int num, int a[])
Assign the value "num" to all elements of an array. Case of a int array.
Definition: cs_array.cpp:267
void cs_array_real_set_value(cs_lnum_t n_elts, int stride, const cs_real_t ref_val[], cs_real_t a[])
Assign a constant value of dim "stride" to an interlaced array sharing the same stride.
Definition: cs_array.cpp:563
void cs_arrays_set_value(const cs_lnum_t n_elts, const T *ref_val, Arrays &&... arrays)
Assign values to all elements of multiple arrays. ref_val is input as a pointer or an array.
Definition: cs_array.h:108
void cs_array_real_set_wscalar_on_subset(cs_lnum_t n_elts, const cs_lnum_t elt_ids[], cs_real_t ref_val, const cs_real_t weight[], cs_real_t a[])
Assign a weighted constant scalar value to an array on a selected subset of elements....
Definition: cs_array.cpp:795
void cs_array_real_set_tensor_on_subset(cs_lnum_t n_elts, const cs_lnum_t elt_ids[], const cs_real_t ref_tens[3][3], cs_real_t a[])
Assign a constant 3x3 tensor to an interlaced array (of stride 9) on a subset of elements....
Definition: cs_array.cpp:981
void cs_array_real_set_wvector(cs_lnum_t n_elts, const cs_real_t ref_val[3], const cs_real_t weight[], cs_real_t a[])
Assign a weighted constant vector value to an interlaced array (of stride 3). The array of weights ha...
Definition: cs_array.cpp:850
void cs_array_lnum_set_value(cs_lnum_t size, cs_lnum_t num, cs_lnum_t a[])
Assign the value "num" to all elements of an array. Case of a cs_lnum_t array.
Definition: cs_array.cpp:195
void cs_array_real_set_wvalue_on_subset(cs_lnum_t n_elts, int stride, const cs_lnum_t elt_ids[], const cs_real_t ref_val[], const cs_real_t weight[], cs_real_t a[])
Assign a weighted constant value of dim "stride" to an interlaced array sharing the same stride....
Definition: cs_array.cpp:678
void cs_array_int_set_value_on_subset(cs_lnum_t n_elts, const cs_lnum_t elt_ids[], int num, int a[])
Assign the value "num" to an array on a selected subset of elements. if elt_ids is null,...
Definition: cs_array.cpp:290
void cs_array_real_set_wscalar(cs_lnum_t n_elts, cs_real_t ref_val, const cs_real_t weight[], cs_real_t a[])
Assign a weighted constant scalar value to an array. The weight array has the same size as the array ...
Definition: cs_array.cpp:738
void * cs_stream_t
Definition: cs_defs.h:135
#define CS_F_HOST_DEVICE
Definition: cs_defs.h:555
double cs_real_t
Floating-point value.
Definition: cs_defs.h:332
#define _(String)
Definition: cs_defs.h:61
#define __has_builtin(x)
Definition: cs_defs.h:268
#define CS_LAMBDA
Definition: cs_defs.h:564
#define CS_THR_MIN
Definition: cs_defs.h:483
int cs_lnum_t
local mesh entity id
Definition: cs_defs.h:325
unsigned short int cs_flag_t
Definition: cs_defs.h:334
#define CS_F_HOST
Definition: cs_defs.h:553
@ k
Definition: cs_field_pointer.h:68
#define CS_FREE(_ptr)
Definition: cs_mem.h:151
#define cs_alloc_mode
Definition: cs_mem.h:183
cs_alloc_mode_t cs_check_device_ptr(const void *ptr)
Check if a pointer is associated with a device.
Definition: cs_mem.h:727
cs_alloc_mode_t
Definition: cs_mem.h:46
@ CS_ALLOC_HOST
Definition: cs_mem.h:48
@ CS_ALLOC_HOST_DEVICE_SHARED
Definition: cs_mem.h:53
static int cs_parall_n_threads(cs_lnum_t n_elements, cs_lnum_t min_thread_elements)
Compute recommended number of threads for a section.
Definition: cs_parall.h:549
cs_dispatch_context & default_context(void)
Definition: cs_execution_context.cpp:62
Definition: cs_algorithm.h:51
layout
Definition: cs_mdspan.h:51
CS_F_HOST_DEVICE constexpr std::enable_if_t< std::is_move_constructible< T >::value &&std::is_move_assignable< T >::value > swap_objects(T &obj1, T &obj2)
A swap method which is callable from GPU and not only CPU.
Definition: cs_defs.h:814
Utility template to check if a pack of parameters is made of integral types.
Definition: cs_defs.h:775