8.0
general documentation
cs_matrix_assembler_priv.h
Go to the documentation of this file.
1 #ifndef __CS_MATRIX_ASSEMBLER_PRIV_H__
2 #define __CS_MATRIX_ASSEMBLER_PRIV_H__
3 
4 /*============================================================================
5  * Incremental or general construction of matrix.
6  *============================================================================*/
7 
8 /*
9  This file is part of code_saturne, a general-purpose CFD tool.
10 
11  Copyright (C) 1998-2023 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 #include "cs_defs.h"
31 
32 /*----------------------------------------------------------------------------
33  * Standard C library headers
34  *----------------------------------------------------------------------------*/
35 
36 /*----------------------------------------------------------------------------
37  * Local headers
38  *----------------------------------------------------------------------------*/
39 
40 #include "cs_defs.h"
41 #include "cs_log.h"
42 #include "cs_rank_neighbors.h"
43 #include "cs_timer.h"
44 
45 #include "cs_matrix.h"
46 
47 /*----------------------------------------------------------------------------
48  * Header for the current file
49  *----------------------------------------------------------------------------*/
50 
51 /*----------------------------------------------------------------------------*/
52 
54 
57 /*=============================================================================
58  * Local Macro Definitions
59  *============================================================================*/
60 
61 /*=============================================================================
62  * Local Structure Definitions
63  *============================================================================*/
64 
65 /*----------------------------------------------------------------------------
66  * Structure used to pre-build a matrix
67  *----------------------------------------------------------------------------*/
68 
69 struct _cs_matrix_assembler_t {
70 
71  bool separate_diag; /* is diagonal handled separately ? */
72 
73  int flags; /* sum (bitwise or) of option constants */
74 
75  cs_gnum_t l_range[2]; /* local global row range */
76  cs_gnum_t n_g_rows; /* global number of rows */
77  cs_lnum_t n_rows; /* local number of rows */
78 
79  cs_lnum_t size; /* current insertion array size */
80  cs_lnum_t max_size; /* maximum insertion array size */
81 
82  const cs_lnum_t *r_idx; /* shared main row index (0 to n-1) */
83  const cs_lnum_t *c_id; /* shared main column ids (0 to n-1) */
84 
85  cs_lnum_t *_r_idx; /* private main row index (0 to n-1) */
86  cs_lnum_t *_c_id; /* private main column ids (0 to n-1) */
87 
88  cs_lnum_t *d_r_idx; /* distant row index (0 to n-1) */
89  cs_gnum_t *d_g_c_id; /* distant global column ids (0 to n-1) */
90 
91  cs_gnum_t *g_rc_id; /* global row and column ids
92  (local and distant) */
93 
94 #if defined(HAVE_MPI)
95 
96  /* Distant columns associated with local rows */
97 
98  /* Metadata for exchange of matrix coefficient values with other ranks */
99 
100  int n_coeff_ranks; /* number of MPI ranks with which
101  coefficients are exchanged */
102  int *coeff_rank; /* ranks with which coefficients are
103  exchanged */
104 
105  cs_lnum_t coeff_send_size; /* number of coefficients to send */
106  cs_lnum_t coeff_recv_size; /* number of coefficients to receive */
107 
108  cs_lnum_t coeff_send_n_rows; /* number of matching rows */
109  cs_lnum_t *coeff_send_index; /* index of sent coefficient rows */
110  cs_gnum_t *coeff_send_row_g_id; /* global ids matching rows (ordered) */
111  cs_gnum_t *coeff_send_col_g_id; /* global ids matching columns
112  (ordered) */
113 
114  cs_lnum_t *coeff_rank_send_index; /* index of data to send */
115  cs_lnum_t *coeff_rank_recv_index; /* index of data to receive */
116 
117  cs_lnum_t *coeff_recv_row_id; /* local row ids associated with
118  received data; */
119  cs_lnum_t *coeff_recv_col_idx; /* local column index associated with
120  received data; the column numbering
121  implicitely assumes local terms
122  first, distant ones second */
123  cs_gnum_t *coeff_recv_col_g_id; /* global column id couples
124  associated with received data */
125 
126  /* Associated communicator */
127 
128  MPI_Comm comm; /* associated MPI communicator */
129 
130  /* Statistics */
131 
132  int n_ranks_init[2]; /* Number of ranks for initial exchange
133  for distant rows then columns */
134 #endif /* HAVE_MPI */
135 
136  /* Associated vector ghost element info */
137 
138  const cs_halo_t *halo; /* shared halo for associated vectors */
139  cs_halo_t *_halo; /* private halo for associated vectors */
140 
141  cs_lnum_t n_e_g_ids; /* number of external global ids */
142  cs_gnum_t *e_g_id; /* global ids associated with halo
143  elements (size: n_e_g_ids */
144 
145 };
146 
147 /*----------------------------------------------------------------------------
148  * Structure managing matrix coefficient contributions.
149  *----------------------------------------------------------------------------*/
150 
151 struct _cs_matrix_assembler_values_t {
152 
153  const cs_matrix_assembler_t *ma; /* associated matrix assembler */
154 
155  bool separate_diag; /* is diagonal handled separately ? */
156  bool final_assembly; /* are we ready for final assembly ? */
157 
158  cs_lnum_t db_size; /* Diagonal blocks size */
159  cs_lnum_t eb_size; /* Extradiagonal blocks size */
160 
161  cs_lnum_t *diag_idx; /* Local index of diagonal in each row
162  when conversion beween separate
163  diagonal and included diagonal is
164  required */
165 
166  /* Accumulated contributions to distant rows, indexed as per
167  coeff_send_index of the matching assembler structure */
168 
169 #if defined(HAVE_MPI)
170 
171  cs_real_t *coeff_send;
172 
173 #endif
174 
175  /* Matching structure and function pointers; some function type may not be
176  useful for certain matrix structures or libraries. */
177 
178  void *matrix; /* pointer to
179  matrix structure */
180 
184  cs_matrix_assembler_values_begin_t *assembly_begin; /* optional */
185  cs_matrix_assembler_values_end_t *assembly_end; /* optional */
186 
187 };
188 
189 /*============================================================================
190  * Public inline function definitions
191  *============================================================================*/
192 
193 /*----------------------------------------------------------------------------*/
206 /*----------------------------------------------------------------------------*/
207 
208 static inline cs_lnum_t
209 _l_id_binary_search(cs_lnum_t l_id_array_size,
210  cs_lnum_t l_id,
211  const cs_lnum_t l_id_array[])
212 {
213  if (l_id_array_size < 1)
214  return -1;
215 
216  cs_lnum_t start_id = 0;
217  cs_lnum_t end_id = l_id_array_size - 1;
218  cs_lnum_t mid_id = end_id/2;
219  while (start_id < end_id) {
220  if (l_id_array[mid_id] < l_id)
221  start_id = mid_id + 1;
222  else if (l_id_array[mid_id] > l_id)
223  end_id = mid_id - 1;
224  else
225  break;
226  mid_id = start_id + (end_id - start_id)/2;
227  }
228  if (l_id_array[mid_id] != l_id)
229  mid_id = -1;
230 
231  return mid_id;
232 }
233 
234 /*----------------------------------------------------------------------------*/
247 /*----------------------------------------------------------------------------*/
248 
249 static inline cs_lnum_t
250 _g_id_binary_find(cs_lnum_t g_id_array_size,
251  cs_gnum_t g_id,
252  const cs_gnum_t g_id_array[])
253 {
254  cs_lnum_t start_id = 0;
255  cs_lnum_t end_id = g_id_array_size - 1;
256  cs_lnum_t mid_id = (end_id -start_id) / 2;
257  while (start_id < end_id) {
258  if (g_id_array[mid_id] < g_id)
259  start_id = mid_id + 1;
260  else if (g_id_array[mid_id] > g_id)
261  end_id = mid_id - 1;
262  else
263  break;
264  mid_id = start_id + ((end_id -start_id) / 2);
265  }
266  assert(g_id_array[mid_id] == g_id);
267 
268  return mid_id;
269 }
270 
273 /*----------------------------------------------------------------------------*/
274 
276 
277 #endif /* __CS_MATRIX_ASSEMBLER_PRIV_H__ */
#define BEGIN_C_DECLS
Definition: cs_defs.h:509
double cs_real_t
Floating-point value.
Definition: cs_defs.h:319
unsigned long cs_gnum_t
global mesh entity number
Definition: cs_defs.h:298
#define END_C_DECLS
Definition: cs_defs.h:510
int cs_lnum_t
local mesh entity id
Definition: cs_defs.h:313
void() cs_matrix_assembler_values_add_g_t(void *matrix, cs_lnum_t n, cs_lnum_t stride, const cs_gnum_t row_g_id[], const cs_gnum_t col_g_id[], const cs_real_t vals[])
Function pointer for addition to matrix coefficients using global row ids and column indexes.
Definition: cs_matrix_assembler.h:148
struct _cs_matrix_assembler_t cs_matrix_assembler_t
Definition: cs_matrix_assembler.h:61
void() cs_matrix_assembler_values_add_t(void *matrix, cs_lnum_t n, cs_lnum_t stride, const cs_lnum_t row_id[], const cs_lnum_t col_idx[], const cs_real_t vals[])
Function pointer for addition to matrix coefficients using local row ids and column indexes.
Definition: cs_matrix_assembler.h:116
void() cs_matrix_assembler_values_end_t(void *matrix)
Function pointer to complete the final assembly of matrix coefficients.
Definition: cs_matrix_assembler.h:186
void() cs_matrix_assembler_values_init_t(void *matrix, cs_lnum_t db_size, cs_lnum_t eb_size)
Function pointer for initialization of matrix coefficients using local row ids and column indexes.
Definition: cs_matrix_assembler.h:84
void() cs_matrix_assembler_values_begin_t(void *matrix)
Function pointer to start the final assembly of matrix coefficients.
Definition: cs_matrix_assembler.h:169
void matrix(const int *iconvp, const int *idiffp, const int *ndircp, const int *isym, const cs_real_t *thetap, const int *imucpp, const cs_real_t coefbp[], const cs_real_t cofbfp[], const cs_real_t rovsdt[], const cs_real_t i_massflux[], const cs_real_t b_massflux[], const cs_real_t i_visc[], const cs_real_t b_visc[], const cs_real_t xcpp[], cs_real_t da[], cs_real_t xa[])
Definition: cs_matrix_building.c:111
Definition: cs_halo.h:77