YAC 3.21.0
Yet Another Coupler
Loading...
Searching...
No Matches
interp_method_check.c
Go to the documentation of this file.
1// Copyright (c) 2024 The YAC Authors
2//
3// SPDX-License-Identifier: BSD-3-Clause
4
5#ifdef HAVE_CONFIG_H
6// Get the definition of the 'restrict' keyword.
7#include "config.h"
8#endif
9
10#include <string.h>
11
14#include "parameter/param_str.h"
16#include "utils_core.h"
17#include "ensure_array_size.h"
19#include "instrument_internal.h"
20#include "yac_mpi_common.h"
21#include "yac_mpi_internal.h"
22
23static size_t do_search_check(struct interp_method * method,
24 struct yac_interp_grid * interp_grid,
25 size_t * tgt_points, size_t count,
26 struct yac_interp_weights * weights,
27 int * interpolation_complete);
28static void delete_check(struct interp_method * method);
29
30static struct interp_method_vtable
34
41
42static size_t do_search_check (struct interp_method * method,
43 struct yac_interp_grid * interp_grid,
44 size_t * tgt_points, size_t count,
45 struct yac_interp_weights * weights,
46 int * interpolation_complete) {
47
48 UNUSED(weights);
49 UNUSED(interpolation_complete);
50
51 YAC_INSTRUMENT_START(YAC_INSTR_INTERP_SEARCH_CHECK);
52
53 struct interp_method_check * method_check =
54 (struct interp_method_check *)method;
55
56 yac_int * tgt_global_ids = xmalloc(count * sizeof(*tgt_global_ids));
57 yac_coordinate_pointer tgt_coordinates =
58 xmalloc(count * sizeof(*tgt_coordinates));
59
61 interp_grid, tgt_points, count, tgt_global_ids);
63 interp_grid, tgt_points, count, tgt_coordinates);
64
65 if (method_check->do_search_callback != NULL)
66 method_check->do_search_callback(
67 tgt_global_ids, (yac_const_coordinate_pointer)tgt_coordinates, count,
68 method_check->do_search_user_data);
69
70 free(tgt_coordinates);
71 free(tgt_global_ids);
72
73 YAC_INSTRUMENT_STOP(YAC_INSTR_INTERP_SEARCH_CHECK);
74
75 return 0;
76}
77
79 func_constructor constructor_callback,
80 void * constructor_user_data,
82 void * do_search_user_data) {
83
84 struct interp_method_check * method = xmalloc(1 * sizeof(*method));
85
89
90 if (constructor_callback != NULL)
91 constructor_callback(constructor_user_data);
92
93 return (struct interp_method*)method;
94}
95
96static void delete_check(struct interp_method * method) {
97 free(method);
98}
99
100//------------------------------------------------------------------------------
101// Structures and routines for callback function pointer handling
102//------------------------------------------------------------------------------
103
114
130
137static struct {
138 struct {
140 void * user_data;
143 char * key;
147
163 char const * key) {
164
165 // ensure that the lookup table does not already contain an entry with the
166 // same key and type
167 for (size_t i = 0; i < callback_lookup_table_size; ++i)
169 strcmp(callback_lookup_table[i].key, key) ||
171 "key \"%s\" has been set for the same callback type (%d)",
172 key, (int)type)
173
176
178 xmalloc((strlen(key)+1) * sizeof(*key));
184}
185
199 char const * key, callback_union * callback, enum callback_type type,
200 void ** user_data) {
201
202 // initialise return values
203 callback->ptr = NULL;
204 *user_data = NULL;
205
206 // for all lookup table entries or until a match was found
207 for (size_t i = 0;
209 (callback->ptr == NULL) && (*user_data == NULL); ++i) {
210
211 // if we found a matching lookup table entry
212 // (matching key and callback type)
213 if ((!strcmp(callback_lookup_table[i].key, key)) &&
215
216 *callback = callback_lookup_table[i].value.callback;
217 *user_data = callback_lookup_table[i].value.user_data;
218 }
219 }
220}
221
223 func_constructor constructor_callback, void * user_data, char const * key) {
224
226 cb.constructor = constructor_callback;
228}
229
231 char const * key, func_constructor * constructor_callback,
232 void ** user_data) {
233
236 *constructor_callback = cb.constructor;
237}
238
246
254
264
265// --- Modular config struct and vtable for CHECK method ---
266
281
284 struct yac_interp_method_config_check *config_check =
286 free(config_check->config.constructor_key);
287 free(config_check->config.do_search_key);
288 free(config);
289}
290
292 const struct yac_interp_method_config *config) {
294 struct yac_interp_method_config_check *copy = xmalloc(sizeof(*copy));
295 copy->base.vtable = config_check->base.vtable;
296 copy->base.root_param_cache = NULL;
297 copy->config.constructor_key = xstrdup(config_check->config.constructor_key);
298 copy->config.do_search_key = xstrdup(config_check->config.do_search_key);
299 return (struct yac_interp_method_config *)copy;
300}
301
303 void const *a_, void const *b_) {
305 // Compare check method configuration:
306 // 1. constructor_key: identifies the constructor callback
307 CHECK_STRING(config.constructor_key);
308 // 2. do_search_key: identifies the do_search callback
309 CHECK_STRING(config.do_search_key);
310 return 0;
311}
312
313// Gets interpolation method typed_config_hcsbb_get_type(
317
319 struct yac_interp_method_config const *config) {
320 const struct yac_interp_method_config_check *config_check =
322 func_constructor constructor_callback = NULL;
323 void *constructor_user_data = NULL;
325 void *do_search_user_data = NULL;
327 config_check->config.constructor_key,
328 &constructor_callback, &constructor_user_data);
330 config_check->config.do_search_key,
331 &do_search_callback, &do_search_user_data);
333 constructor_callback, constructor_user_data,
334 do_search_callback, do_search_user_data);
335}
336
338 struct yac_interp_method_config const *config) {
339 struct yac_interp_method_config_check *config_check =
341
342 enum {
343 CONSTRUCTOR_KEY_ALLOW_NULL = 1,
344 CONSTRUCTOR_KEY_ALLOW_EMPTY = 1,
345 CONSTRUCTOR_KEY_HAS_DEFAULT = 1,
346 CONSTRUCTOR_KEY_IS_DEFINED = 1,
347 DO_SEARCH_KEY_ALLOW_NULL = 1,
348 DO_SEARCH_KEY_ALLOW_EMPTY = 1,
349 DO_SEARCH_KEY_HAS_DEFAULT = 1,
350 DO_SEARCH_KEY_IS_DEFINED = 1,
351 };
352
353 struct yac_param *param_constructor_key =
355 "constructor_key",
356 &config_check->config.constructor_key,
358 CONSTRUCTOR_KEY_ALLOW_NULL, CONSTRUCTOR_KEY_ALLOW_EMPTY,
359 YAC_MAX_ROUTINE_NAME_LENGTH - 1, CONSTRUCTOR_KEY_HAS_DEFAULT,
360 CONSTRUCTOR_KEY_IS_DEFINED);
361
362 struct yac_param *param_do_search_key =
364 "do_search_key",
365 &config_check->config.do_search_key,
367 DO_SEARCH_KEY_ALLOW_NULL, DO_SEARCH_KEY_ALLOW_EMPTY,
368 YAC_MAX_ROUTINE_NAME_LENGTH - 1, DO_SEARCH_KEY_HAS_DEFAULT,
369 DO_SEARCH_KEY_IS_DEFINED);
370
371 struct yac_param *root_param_array[] = {
372 param_constructor_key,
373 param_do_search_key
374 };
375 enum {
376 ROOT_PARAM_ARRAY_SIZE =
377 sizeof(root_param_array) / sizeof(root_param_array[0])
378 };
379
381 "check", root_param_array, ROOT_PARAM_ARRAY_SIZE);
382}
383
392
394 struct yac_interp_method_config_check *config_check =
395 xmalloc(sizeof(*config_check));
397 config_check->base.root_param_cache = NULL;
398 config_check->config.constructor_key =
400 config_check->config.do_search_key =
402 return (struct yac_interp_method_config *)config_check;
403}
#define UNUSED(x)
Definition core.h:72
#define ENSURE_ARRAY_SIZE(arrayp, curr_array_size, req_size)
#define YAC_INSTRUMENT_START(ID)
Enter an instrumented region.
#define YAC_INSTRUMENT_STOP(ID)
Exit an instrumented region entered with YAC_INSTRUMENT_START(ID).
void yac_interp_grid_get_tgt_global_ids(struct yac_interp_grid *interp_grid, size_t *tgt_points, size_t count, yac_int *tgt_global_ids)
void yac_interp_grid_get_tgt_coordinates(struct yac_interp_grid *interp_grid, size_t *tgt_points, size_t count, yac_coordinate_pointer tgt_coordinates)
yac_interpolation_list
@ YAC_CHECK
Diagnostic check method (for testing)
#define YAC_MAX_ROUTINE_NAME_LENGTH
Maximum length for routine names (used as keys for callback registration)
static size_t do_search_callback(struct interp_method *method, struct yac_interp_grid *interp_grid, size_t *tgt_points, size_t count, struct yac_interp_weights *weights, int *interpolation_complete)
static void interp_method_check_add_callback(callback_union callback, enum callback_type type, void *user_data, char const *key)
Register a callback function and user data for the check interpolation method.
static struct yac_interp_method_config_vtable yac_interp_method_config_vtable_check
void * user_data
callback_type
Type of callback function for the check interpolation method.
@ CONSTRUCTOR
static struct interp_method * config_check_generate(struct yac_interp_method_config const *config)
void yac_interp_method_check_get_do_search_callback(char const *key, func_do_search *do_search_callback, void **user_data)
callback_union callback
void yac_interp_method_check_add_constructor_callback(func_constructor constructor_callback, void *user_data, char const *key)
void yac_interp_method_check_get_constructor_callback(char const *key, func_constructor *constructor_callback, void **user_data)
void yac_interp_method_check_add_do_search_callback(func_do_search do_search_callback, void *user_data, char const *key)
static int config_check_compare(void const *a_, void const *b_)
static struct yac_interp_method_config * config_check_copy(const struct yac_interp_method_config *config)
static void config_check_delete(struct yac_interp_method_config *config)
char * key
static enum yac_interpolation_list config_check_get_type()
static void interp_method_get_callback(char const *key, callback_union *callback, enum callback_type type, void **user_data)
Retrieve a registered callback and its user data by key and type.
enum callback_type type
struct interp_method * yac_interp_method_check_new(func_constructor constructor_callback, void *constructor_user_data, func_do_search do_search_callback, void *do_search_user_data)
static void delete_check(struct interp_method *method)
static size_t callback_lookup_table_size
static struct yac_param * config_check_get_param(struct yac_interp_method_config const *config)
static size_t do_search_check(struct interp_method *method, struct yac_interp_grid *interp_grid, size_t *tgt_points, size_t count, struct yac_interp_weights *weights, int *interpolation_complete)
void yac_interp_method_check_buf_free()
static struct interp_method_vtable interp_method_check_vtable
struct @23::@24 value
static size_t callback_lookup_table_array_size
struct yac_interp_method_config * yac_interp_method_config_default_check_new(void)
Creates a check interpolation method configuration with default parameters.
static struct @23 * callback_lookup_table
Callback pointer lookup table.
void(* func_do_search)(yac_int const *global_ids, double const (*coordinates_xyz)[3], size_t count, void *user_data)
#define YAC_INTERP_CHECK_CONSTRUCTOR_KEY_DEFAULT
void(* func_constructor)(void *user_data)
#define YAC_INTERP_CHECK_DO_SEARCH_KEY_DEFAULT
#define DEF_INTERP_METHOD_CONFIG_COMPARE_TYPE(TYPE)
#define DEF_INTERP_METHOD_CONFIG_TYPE(TYPE)
#define CHECK_STRING(STR)
struct yac_param * yac_param_str_new(const char *name, char **value_ptr, const char *default_value, int allow_NULL, int allow_empty, size_t max_str_len, int has_default, int is_defined)
Create a string parameter.
Definition param_str.c:176
struct yac_param * yac_param_struct_new(const char *name, struct yac_param **subparams, size_t subparam_count)
Create a new struct parameter with a given name and subparameters.
#define xstrdup(s)
Definition ppm_xfuncs.h:84
#define xmalloc(size)
Definition ppm_xfuncs.h:66
struct interp_method_vtable * vtable
func_do_search do_search_callback
size_t(* do_search)(struct interp_method *method, struct yac_interp_grid *grid, size_t *tgt_points, size_t count, struct yac_interp_weights *weights, int *interpolation_complete)
Concrete implementation of yac_interp_method_config for the check method.
struct yac_interp_method_config base
‍base config object, must be first entry
struct yac_interp_method_check_config config
‍method-specific configuration
void(* delete)(struct yac_interp_method_config *config)
struct yac_interp_method_config_vtable const * vtable
static struct yac_interp_method_config * config
Union for storing different callback function pointers.
func_do_search do_search
func_constructor constructor
#define YAC_ASSERT_F(exp, format,...)
Definition yac_assert.h:39
double const (* yac_const_coordinate_pointer)[3]
Definition yac_types.h:22
YAC_INT yac_int
Definition yac_types.h:15
double(* yac_coordinate_pointer)[3]
Definition yac_types.h:21