YAC 3.21.0
Yet Another Coupler
Loading...
Searching...
No Matches
test_yac_pak_mci_component.c
Go to the documentation of this file.
1// Copyright (c) 2026 The YAC Authors
2//
3// SPDX-License-Identifier: BSD-3-Clause
4
5#include <stdlib.h>
6#include <stdio.h>
7#include <string.h>
8#include <mpi.h>
9#include "io_utils.h"
10
11#include "tests.h"
12#include "yac.h"
13#include "test_yac_pak_common.h"
14
37#define YAC_RAD (0.01745329251994329576923690768489)
38
39static int utest_has_line(char const *path, char const *prefix) {
40 FILE *f = fopen(path, "r");
41 if (!f) return 0;
42 size_t const plen = strlen(prefix);
43 char line[1024];
44 int found = 0;
45 while (fgets(line, sizeof(line), f)) {
46 if (strncmp(line, prefix, plen) == 0) { found = 1; break; }
47 }
48 fclose(f);
49 return found;
50}
51
52static void utest_run_round(int rank) {
53 int const is_atm = (rank == 0);
54 char const *comp_name = is_atm ? "atm" : "ocn";
55 char const *grid_name = is_atm ? "atm_grid" : "ocn_grid";
56
57 int yac_id = -1;
58 yac_cinit_comm_instance(MPI_COMM_WORLD, &yac_id);
61 yac_id, "2000-01-01T00:00:00", "2000-01-01T01:00:00");
62
63 int comp_id;
64 yac_cdef_comp_instance(yac_id, comp_name, &comp_id);
65
66 int grid_id;
68 grid_name, (int[2]){3, 3}, (int[2]){0, 0},
69 (double[]){0 * YAC_RAD, 1 * YAC_RAD, 2 * YAC_RAD},
70 (double[]){0 * YAC_RAD, 1 * YAC_RAD, 2 * YAC_RAD},
71 &grid_id);
72
73 int point_id;
75 grid_id, (int[2]){3, 3}, YAC_LOCATION_CORNER,
76 (double[]){0 * YAC_RAD, 1 * YAC_RAD, 2 * YAC_RAD},
77 (double[]){0 * YAC_RAD, 1 * YAC_RAD, 2 * YAC_RAD},
78 &point_id);
79
80 int field_id;
82 "heat_flux", comp_id, &point_id, 1, 1,
84
88}
89
90/* Similar to utest_run_round, but this process defines several components.
91 * The field is defined on the first one, which is not the one the trace
92 * be attributed to. */
93static void utest_run_round_multi(
94 int rank, char const * const *comp_names, int num_comps) {
95
96 int yac_id = -1;
97 yac_cinit_comm_instance(MPI_COMM_WORLD, &yac_id);
100 yac_id, "2000-01-01T00:00:00", "2000-01-01T01:00:00");
101
102 int comp_ids[2] = {-1, -1};
104 yac_id, (char const **)comp_names, num_comps, comp_ids);
105
106 char grid_name[32];
107 snprintf(grid_name, sizeof(grid_name), "grid_r%d", rank);
108 int grid_id;
110 grid_name, (int[2]){3, 3}, (int[2]){0, 0},
111 (double[]){0 * YAC_RAD, 1 * YAC_RAD, 2 * YAC_RAD},
112 (double[]){0 * YAC_RAD, 1 * YAC_RAD, 2 * YAC_RAD},
113 &grid_id);
114
115 int point_id;
117 grid_id, (int[2]){3, 3}, YAC_LOCATION_CORNER,
118 (double[]){0 * YAC_RAD, 1 * YAC_RAD, 2 * YAC_RAD},
119 (double[]){0 * YAC_RAD, 1 * YAC_RAD, 2 * YAC_RAD},
120 &point_id);
121
122 int field_id;
124 "heat_flux", comp_ids[0], &point_id, 1, 1,
126
128 yac_cenddef_instance(yac_id);
130}
131
132/* A rank that has to write, and what its file has to report. */
133typedef struct {
134 char const *name;
135 int stride;
136 int comp_rank;
137 int comp_size;
138} utest_writer_t;
139
140static void utest_check_writer(char const *dir, utest_writer_t const *w) {
141 char path[256];
142 snprintf(path, sizeof(path), "%s/%s.csv", dir, w->name);
143 if (!yac_file_exists(path)) {
144 PUT_ERR(" FAIL: expected trace file missing");
145 return;
146 }
147 char line[64];
148 snprintf(line, sizeof(line), "# trace_stride=%d", w->stride);
149 if (!utest_has_line(path, line))
150 PUT_ERR(" FAIL: wrong trace_stride");
151 snprintf(line, sizeof(line), "# component_rank=%d", w->comp_rank);
152 if (!utest_has_line(path, line))
153 PUT_ERR(" FAIL: wrong component_rank");
154 snprintf(line, sizeof(line), "# component_size=%d", w->comp_size);
155 if (!utest_has_line(path, line))
156 PUT_ERR(" FAIL: wrong component_size");
157 /* every writer records at least the events after yac_cdef_comp, in both
158 * write modes */
159 if (utest_count_data_lines(path) < 1)
160 PUT_ERR(" FAIL: trace file has no events");
161}
162
163/* No file may keep the placeholder name of write mode "instant" and a
164 * clean shutdown leaves no .open file behind. */
165static void utest_check_no_leftovers(char const *dir) {
166 char path[256];
167 for (int r = 0; r < 4; ++r) {
168 snprintf(path, sizeof(path), "%s/yac_pak_unknown_r%d.csv", dir, r);
169 if (yac_file_exists(path))
170 PUT_ERR(" FAIL: component name fell back to the placeholder");
171 for (char const **c = (char const *[]){"atm", "ocn", NULL}; *c; ++c) {
172 snprintf(path, sizeof(path), "%s/%s_r%d.open", dir, *c, r);
173 if (yac_file_exists(path))
174 PUT_ERR(" FAIL: .open file left behind");
175 }
176 }
177}
178
179/* STRIDE=2: "atm" caps it to 1, "ocn" writes its component ranks 0 and 2. */
180static void utest_check_selection(char const *dir) {
181 utest_writer_t const expected[3] =
182 {{"atm_r0", 2, 0, 1}, {"ocn_r1", 2, 0, 3}, {"ocn_r3", 2, 2, 3}};
183 for (int i = 0; i < 3; ++i) utest_check_writer(dir, &expected[i]);
184
185 /* rank 2 is selected in the communicator but not within "ocn" */
186 char path[256];
187 snprintf(path, sizeof(path), "%s/ocn_r2.csv", dir);
188 if (yac_file_exists(path))
189 PUT_ERR(" FAIL: rank not selected by its component wrote");
190
191 utest_check_no_leftovers(dir);
192}
193
194/* All four ranks are attributed to "ocn", whatever else they defined. */
195static void utest_check_attributed_to_ocn(char const *dir, char const *other) {
196 char path[256];
197 for (int r = 0; r < 4; ++r) {
198 char name[32];
199 snprintf(name, sizeof(name), "ocn_r%d", r);
200 utest_writer_t const expected = {name, 1, r, 4};
201 utest_check_writer(dir, &expected);
202
203 snprintf(path, sizeof(path), "%s/%s_r%d.csv", dir, other, r);
204 if (yac_file_exists(path))
205 PUT_ERR(" FAIL: attributed to the wrong component");
206 }
207}
208
209static void utest_check_tie_break(char const *dir) {
210 utest_check_attributed_to_ocn(dir, "atm");
211}
212
213static void utest_check_largest(char const *dir) {
214 utest_check_attributed_to_ocn(dir, "io");
215}
216
217/* The default stride must not filter anything. */
218static void utest_check_all_write(char const *dir) {
219 utest_writer_t const expected[4] =
220 {{"atm_r0", 1, 0, 1}, {"ocn_r1", 1, 0, 3},
221 {"ocn_r2", 1, 1, 3}, {"ocn_r3", 1, 2, 3}};
222 for (int i = 0; i < 4; ++i) utest_check_writer(dir, &expected[i]);
223
224 utest_check_no_leftovers(dir);
225}
226
227static void utest_run_case(
228 int rank, char const *level, char const *stride, char const *write_mode,
229 char const *dir, void (*check)(char const *)) {
230
231 setenv("YAC_PAK_TRACE", "1", 1);
232 setenv("YAC_PAK_TRACE_SETUP", "1", 1);
233 setenv("YAC_PAK_TRACE_LEVEL", level, 1);
234 setenv("YAC_PAK_TRACE_STRIDE", stride, 1);
235 setenv("YAC_PAK_TRACE_WRITE_MODE", write_mode, 1);
236 setenv("YAC_PAK_TRACE_DIR", dir, 1);
237
238 if (yac_file_exists(dir) && (rank == 0)) yac_remove_directory(dir);
239 MPI_Barrier(MPI_COMM_WORLD);
240
241 utest_run_round(rank);
242
243 MPI_Barrier(MPI_COMM_WORLD);
244
245 if (rank == 0) {
246 check(dir);
248 }
249
250 utest_clear_trace_env();
251}
252
253static void utest_run_multi_case(
254 int rank, char const * const *comp_names, int num_comps, char const *dir,
255 void (*check)(char const *)) {
256
257 setenv("YAC_PAK_TRACE", "1", 1);
258 setenv("YAC_PAK_TRACE_SETUP", "1", 1);
259 setenv("YAC_PAK_TRACE_LEVEL", "1", 1);
260 setenv("YAC_PAK_TRACE_DIR", dir, 1);
261
262 if (yac_file_exists(dir) && (rank == 0)) yac_remove_directory(dir);
263 MPI_Barrier(MPI_COMM_WORLD);
264
265 utest_run_round_multi(rank, comp_names, num_comps);
266
267 MPI_Barrier(MPI_COMM_WORLD);
268
269 if (rank == 0) {
270 check(dir);
272 }
273
274 utest_clear_trace_env();
275}
276
277int main(void) {
278 MPI_Init(NULL, NULL);
279
280 int rank, size;
281 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
282 MPI_Comm_size(MPI_COMM_WORLD, &size);
283
284 if (size != 4) {
285 fputs("wrong number of processes (must be 4)\n", stderr);
286 MPI_Finalize();
287 return EXIT_FAILURE;
288 }
289
290 /* Test 1: both write modes have to select the same ranks */
291 utest_run_case(
292 rank, "1", "2", "at_finalize", "comp_stride_at_finalize",
293 utest_check_selection);
294 utest_run_case(
295 rank, "1", "2", "instant", "comp_stride_instant",
296 utest_check_selection);
297
298 /* Test 2 */
299 utest_run_case(
300 rank, "1", "1", "at_finalize", "comp_nostride_at_finalize",
301 utest_check_all_write);
302 utest_run_case(
303 rank, "1", "1", "instant", "comp_nostride_instant",
304 utest_check_all_write);
305
306 /* Test 3: no coupling event, so nothing but yac_cdef_comp can supply the
307 * component name */
308 utest_run_case(
309 rank, "0", "1", "at_finalize", "comp_setup_only",
310 utest_check_all_write);
311
312 /* Test 4: "atm" and "ocn" both hold all four ranks, so the sizes tie and
313 * only the name decides. The definition order differs per rank, all of
314 * them still have to pick the same component. */
315 {
316 char const * const even[2] = {"atm", "ocn"};
317 char const * const odd[2] = {"ocn", "atm"};
318 utest_run_multi_case(
319 rank, (rank % 2) ? odd : even, 2, "comp_tie_break",
320 utest_check_tie_break);
321 }
322
323 /* Test 5: ranks 0 and 1 define a subcomponent "io" next to "ocn". They
324 * belong to the larger "ocn" for tracing. */
325 {
326 char const * const both[2] = {"io", "ocn"};
327 char const * const ocn[1] = {"ocn"};
328 if (rank < 2) utest_run_multi_case(
329 rank, both, 2, "comp_largest", utest_check_largest);
330 else utest_run_multi_case(
331 rank, ocn, 1, "comp_largest", utest_check_largest);
332 }
333
334 MPI_Finalize();
335 return TEST_EXIT_CODE;
336}
void yac_remove_directory(char const *path)
Remove a directory and its contents recursively.
Definition io_utils.c:500
int yac_file_exists(const char *filename)
Check whether a file exists.
Definition io_utils.c:394
int point_id
#define YAC_RAD
#define TEST_EXIT_CODE
Definition tests.h:15
#define PUT_ERR(string)
Definition tests.h:10
int comp_ids[1]
int grid_id
int * field_id
int comp_id
char const * name
Definition toy_scrip.c:114
void yac_cdef_datetime_instance(int yac_instance_id, const char *start_datetime, const char *end_datetime)
Definition of job start and end datetime.
Definition yac.c:1019
void yac_csync_def_instance(int yac_instance_id)
Definition yac.c:4918
void yac_cfinalize_instance(int yac_instance_id)
Finalises YAC.
Definition yac.c:998
void yac_cenddef_instance(int yac_instance_id)
Definition yac.c:4984
int const YAC_LOCATION_CORNER
Definition yac.c:39
void yac_cdef_comp_instance(int yac_instance_id, char const *comp_name, int *comp_id)
Definition yac.c:1339
void yac_cdef_grid_reg2d(const char *grid_name, int nbr_vertices[2], int cyclic[2], double *x_vertices, double *y_vertices, int *grid_id)
Definition yac.c:5511
void yac_cdef_points_reg2d(int const grid_id, int const *nbr_points, int const located, double const *x_points, double const *y_points, int *point_id)
Definition yac.c:1540
void yac_cinit_comm_instance(MPI_Comm comm, int *yac_instance_id)
Definition yac.c:700
int const YAC_TIME_UNIT_SECOND
Definition yac.c:64
void yac_cdef_calendar(int calendar)
Definition yac.c:1036
int const YAC_PROLEPTIC_GREGORIAN
Definition yac.c:73
void yac_cdef_field(char const *name, int const comp_id, int const *point_ids, int const num_pointsets, int collection_size, const char *timestep, int time_unit, int *field_id)
Definition yac.c:1929
void yac_cdef_comps_instance(int yac_instance_id, char const **comp_names, int num_comps, int *comp_ids)
Definition yac.c:1294