9.2
general documentation
cs_algorithm.h
Go to the documentation of this file.
1#pragma once
2
3/*============================================================================
4 * Various base algorithms.
5 *============================================================================*/
6
7/*
8 This file is part of code_saturne, a general-purpose CFD tool.
9
10 Copyright (C) 1998-2026 EDF S.A.
11
12 This program is free software; you can redistribute it and/or modify it under
13 the terms of the GNU General Public License as published by the Free Software
14 Foundation; either version 2 of the License, or (at your option) any later
15 version.
16
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
20 details.
21
22 You should have received a copy of the GNU General Public License along with
23 this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
24 Street, Fifth Floor, Boston, MA 02110-1301, USA.
25*/
26
27/*----------------------------------------------------------------------------*/
28
29/*----------------------------------------------------------------------------
30 * Local headers
31 *----------------------------------------------------------------------------*/
32
33#include "base/cs_base.h"
34
35#include "base/cs_dispatch.h"
36#include "base/cs_halo.h"
37#include "alge/cs_matrix.h"
38
39/*============================================================================
40 * Macro definitions
41 *============================================================================*/
42
43/*============================================================================
44 * Type definitions
45 *============================================================================*/
46
47/*============================================================================
48 * Global variables
49 *============================================================================*/
50
51namespace cs {
52namespace algorithm {
53
54/*=============================================================================
55 * Public inline functions
56 *============================================================================*/
57
58/*----------------------------------------------------------------------------*/
69/*----------------------------------------------------------------------------*/
70
72l_binary_search(cs_lnum_t l_id_array_size,
73 cs_lnum_t l_id,
74 const cs_lnum_t l_id_array[])
75{
76 if (l_id_array_size < 1)
77 return -1;
78
79 cs_lnum_t start_id = 0;
80 cs_lnum_t end_id = l_id_array_size - 1;
81 cs_lnum_t mid_id = (end_id -start_id) / 2;
82 while (start_id < end_id) {
83 if (l_id_array[mid_id] < l_id)
84 start_id = mid_id + 1;
85 else if (l_id_array[mid_id] > l_id)
86 end_id = mid_id - 1;
87 else
88 break;
89 mid_id = start_id + ((end_id -start_id) / 2);
90 }
91 if (l_id_array[mid_id] != l_id)
92 mid_id = -1;
93
94 return mid_id;
95}
96
97/*=============================================================================
98 * Public function prototypes
99 *============================================================================*/
100
101/*--------------------------------------------------------------------------*/
102/*
103 * Transform a count to an index in-place.
104 *
105 * For n input elements, the array size should be size n+1, to account
106 * for the past-the-end count.
107 *
108 * If temporary storage is provided by the caller, it will be used
109 * it its size is sufficient, avoiding the overhead of local
110 * memory allocation. This is useful only for device code.
111 *
112 * \param[in] ctx associated dispatch context
113 * \param[in] n number of elements
114 * \param[in, out] a count in, index out (size: n+1)
115 * \param tmp_size optional temporary memory size
116 * \param tmp_storage optional temporary memory
117 */
118/*--------------------------------------------------------------------------*/
119
120void
122 cs_lnum_t n,
123 cs_lnum_t a[],
124 size_t tmp_size = 0,
125 void *tmp_storage = nullptr);
126
127/*--------------------------------------------------------------------------*/
137/*--------------------------------------------------------------------------*/
138
141 cs_lnum_t n,
142 short counter[]);
143
144/*----------------------------------------------------------------------------*/
145
146} // namespace algorithm
147} // namespace cs
Definition: cs_dispatch.h:2288
#define CS_F_HOST_DEVICE
Definition: cs_defs.h:555
unsigned cs_gnum_t
global mesh entity number
Definition: cs_defs.h:317
int cs_lnum_t
local mesh entity id
Definition: cs_defs.h:325
CS_F_HOST_DEVICE cs_lnum_t l_binary_search(cs_lnum_t l_id_array_size, cs_lnum_t l_id, const cs_lnum_t l_id_array[])
Binary search for a given local id in a given array of ordered local ids, when the id might not be pr...
Definition: cs_algorithm.h:72
void count_to_index(cs_dispatch_context &ctx, cs_lnum_t n, cs_lnum_t a[], size_t tmp_size=0, void *tmp_storage=nullptr)
cs_gnum_t count_reduce_sum(cs_dispatch_context &ctx, cs_lnum_t n, short counter[])
Sum a local counter.
Definition: cs_algorithm.h:51