YAC 3.21.0
Yet Another Coupler
Loading...
Searching...
No Matches
couple_config_grid.c
Go to the documentation of this file.
1// Copyright (c) 2026 The YAC Authors
2//
3// SPDX-License-Identifier: BSD-3-Clause
4
8
9#include "yac_mpi_common.h"
10#include "yac_mpi_internal.h"
11#include "dist_merge.h"
12#include "utils_common.h"
13
21 struct yac_couple_config_grid * grid, char const * name) {
22
24 (name != NULL) && (name[0] != '\0'),
25 "invalid name (NULL or empty string is not allowed)")
26
27 grid->name = yac_string_dup(name);
28 grid->output_filename = NULL;
29 grid->metadata = NULL;
30}
31
33 struct yac_couple_config_grid ** grids, size_t * num_grids,
34 char const * name) {
35
36 YAC_ASSERT((grids != NULL) && (num_grids != NULL), "invalid argument")
37
38 YAC_ASSERT(name != NULL, "invalid name (NULL is not allowed)")
39
40 for (size_t i = 0; i < *num_grids; ++i)
41 if (!strcmp((*grids)[i].name, name)) return i;
42
43 size_t grid_idx = *num_grids;
44 (*num_grids)++;
45 *grids = xrealloc(*grids, (*num_grids) * sizeof(**grids));
47
48 return grid_idx;
49}
50
52 struct yac_couple_config_grid * grid,
53 char const * output_filename) {
54
56 (output_filename != NULL) && (output_filename[0] != '\0'),
57 "invalid output filename for grid \"%s\" "
58 "(NULL or empty string is not allowed)",
59 grid->name);
61 (grid->output_filename == NULL) ||
62 (!strcmp(grid->output_filename, output_filename)),
63 "output file name for grid \"%s\" has already been set "
64 "(\"%s\" != \"%s\")",
65 grid->name, grid->output_filename, output_filename);
66
67 if (grid->output_filename == NULL)
68 grid->output_filename = xstrdup(output_filename);
69}
70
72 struct yac_couple_config_grid * grid,
73 char const * metadata) {
74
76 metadata != NULL,
77 "invalid metadata for grid \"%s\" (NULL is not allowed)",
78 grid->name);
80 (grid->metadata == NULL) || (!strcmp(grid->metadata, metadata)),
81 "metadata for grid \"%s\" has already been set "
82 "(\"%s\" != \"%s\")",
83 grid->name, grid->metadata, metadata);
84
85 if (grid->metadata == NULL)
86 grid->metadata = xstrdup(metadata);
87}
88
90 void * grid_, MPI_Comm comm) {
91
92 struct yac_couple_config_grid * grid = grid_;
93
94 return
96 "yac_couple_config_get_grid_pack_size", grid->name, comm, 1);
97}
98
100 void * grid_, void * buffer, int buffer_size, int * position, MPI_Comm comm) {
101
102 struct yac_couple_config_grid * grid =
103 (struct yac_couple_config_grid *)grid_;
105 "yac_couple_config_pack_grid", grid->name, buffer, buffer_size,
106 position, comm, 1);
107}
108
110 void * buffer, int buffer_size, int * position, void * grid_,
111 MPI_Comm comm) {
112
113 struct yac_couple_config_grid * grid =
114 (struct yac_couple_config_grid *)grid_;
115
116 char * name = yac_string_unpack(buffer, buffer_size, position, comm);
117 // yac_couple_config_grid_init duplicates the name
119 free(name);
120}
121
122static int yac_couple_config_grid_compare(void const * a, void const * b) {
123 YAC_ASSERT(((struct yac_couple_config_grid const *)a)->name != NULL &&
124 ((struct yac_couple_config_grid const *)b)->name != NULL,
125 "invalid name (NULL is not allowed)");
126 return strcmp(((struct yac_couple_config_grid const *)a)->name,
127 ((struct yac_couple_config_grid const *)b)->name);
128}
129
131 void * a_, void * b_, MPI_Comm comm) {
132
133 struct yac_couple_config_grid * a = a_;
134 struct yac_couple_config_grid * b = b_;
135
136 if (b!= NULL) {
138 b->output_filename = NULL;
139 a->metadata = b->metadata;
140 b->metadata = NULL;
141 }
142
144 "grid output_filename", (char **)&(a->output_filename), comm);
145 yac_couple_config_sync_string("grid metadata", (char **)&(a->metadata), comm);
146}
147
149 free((void*)grid->name);
150 grid->name = NULL;
151 free((void*)grid->output_filename);
152 grid->output_filename = NULL;
153 free(grid->metadata);
154 grid->metadata = NULL;
155}
156
157static struct yac_dist_merge_vtable
165
167 struct yac_couple_config_grid ** grids,
168 size_t * num_grids,
169 struct yac_couple_config_component * components,
170 size_t num_components,
171 MPI_Comm comm) {
172
173 YAC_ASSERT((grids != NULL) && (num_grids != NULL), "invalid argument")
174
175 size_t * old_to_new_idx;
176 void * p_grids = *grids;
178 num_grids, &p_grids, sizeof((*grids)[0]),
179 comm, &dist_merge_vtable_grid, &old_to_new_idx);
180 *grids = p_grids;
181
182 for (size_t comp_idx = 0; comp_idx < num_components; ++comp_idx) {
184 components + comp_idx, old_to_new_idx);
185 }
186
187 free(old_to_new_idx);
188}
189
191 struct yac_couple_config_grid ** grids, size_t * num_grids) {
192
193 YAC_ASSERT((grids != NULL) && (num_grids != NULL), "invalid argument")
194
195 for (size_t i = 0; i < *num_grids; ++i)
196 yac_couple_config_grid_free((*grids) + i);
197
198 free(*grids);
199 *grids = NULL;
200 *num_grids = 0;
201}
unsigned num_components
unsigned grid_idx[3]
#define YAC_ASSERT(exp, msg)
void yac_couple_config_component_update_all_grid_idx(struct yac_couple_config_component *component, size_t const *old_to_new_idx)
static void yac_couple_config_grid_merge(void *a_, void *b_, MPI_Comm comm)
void yac_couple_config_grids_free(struct yac_couple_config_grid **grids, size_t *num_grids)
static int yac_couple_config_grid_compare(void const *a, void const *b)
static size_t yac_couple_config_get_grid_pack_size(void *grid_, MPI_Comm comm)
static struct yac_dist_merge_vtable dist_merge_vtable_grid
void yac_couple_config_grid_free(struct yac_couple_config_grid *grid)
void yac_couple_config_set_grid_metadata_value(struct yac_couple_config_grid *grid, char const *metadata)
static void yac_couple_config_unpack_grid(void *buffer, int buffer_size, int *position, void *grid_, MPI_Comm comm)
size_t yac_couple_config_grid_append(struct yac_couple_config_grid **grids, size_t *num_grids, char const *name)
void yac_couple_config_set_grid_output_filename_value(struct yac_couple_config_grid *grid, char const *output_filename)
static void yac_couple_config_pack_grid(void *grid_, void *buffer, int buffer_size, int *position, MPI_Comm comm)
static void yac_couple_config_grid_init(struct yac_couple_config_grid *grid, char const *name)
void yac_couple_config_grids_merge(struct yac_couple_config_grid **grids, size_t *num_grids, struct yac_couple_config_component *components, size_t num_components, MPI_Comm comm)
void yac_couple_config_sync_string(char const *string_name, char **string, MPI_Comm comm)
void yac_dist_merge(size_t *count, void **array, size_t element_size, MPI_Comm comm, struct yac_dist_merge_vtable *vtable, size_t **idx_old_to_new)
Definition dist_merge.c:67
#define xstrdup(s)
Definition ppm_xfuncs.h:84
#define xrealloc(ptr, size)
Definition ppm_xfuncs.h:67
size_t(* get_pack_size)(void *element, MPI_Comm comm)
Definition dist_merge.h:20
double * buffer
char const * name
Definition toy_scrip.c:114
char * yac_string_dup(char const *string)
Definition utils_core.c:33
#define YAC_ASSERT_F(exp, format,...)
Definition yac_assert.h:39
size_t yac_string_get_pack_size(char const *caller, char const *string, MPI_Comm comm, int allow_null)
Compute number of bytes required to pack a string for MPI transport.
Definition yac_mpi.c:651
char * yac_string_unpack(void const *buffer, int buffer_size, int *position, MPI_Comm comm)
Unpack a C string from a buffer packed with yac_string_pack.
Definition yac_mpi.c:701
void yac_string_pack(char const *caller, char const *string, void *buffer, int buffer_size, int *position, MPI_Comm comm, int allow_null)
Pack a C string into a provided buffer using MPI_Pack semantics.
Definition yac_mpi.c:671