YAC 3.12.0
Yet Another Coupler
Loading...
Searching...
No Matches
test_restart2.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#include <mpi.h>
6
7#include <stdlib.h>
8#include <stdio.h>
9#include <string.h>
10#include <unistd.h>
11
12#include "yac.h"
13#include "utils_common.h"
14#include "geometry.h"
16#include "test_function.h"
17
24#define DUMMY_VALUE (-1337.0)
25
26static void utest_run_setup(char local_comp_id, char comp_groups[2][2]);
27
28int main (void) {
29
30 // This test checks the restarting of YAC using varying MPI communicators.
31 // This setup could also be executed with a single YAC initilisation.
32
33 MPI_Init(NULL, NULL);
34
35 int comm_rank, comm_size;
36 MPI_Comm_rank(MPI_COMM_WORLD, &comm_rank);
37 MPI_Comm_size(MPI_COMM_WORLD, &comm_size);
38
39 if (comm_size != 15) {
40 fputs("wrong number of processes (has to be 15)\n", stderr);
41 exit(EXIT_FAILURE);
42 }
43
44 char local_comp_id;
45 if (comm_rank < 6) local_comp_id = 'A';
46 else if (comm_rank < 10) local_comp_id = 'B';
47 else if (comm_rank < 13) local_comp_id = 'C';
48 else local_comp_id = 'D';
49
50 // couple A-B and C-D (each in their own world comm)
51 utest_run_setup(local_comp_id, (char[2][2]){{'A','B'},{'C','D'}});
52
53 // couple A-C and B-D (each in their own world comm)
54 utest_run_setup(local_comp_id, (char[2][2]){{'A','C'},{'B','D'}});
55
56 // couple A-D and B-C (each in their own world comm)
57 utest_run_setup(local_comp_id, (char[2][2]){{'A','D'},{'B','C'}});
58
59 MPI_Finalize();
60
61 exit(EXIT_SUCCESS);
62}
63
64static void def_grid(
65 char const * grid_name, int comm_rank, int comm_size,
66 int * grid_id, int * cell_point_id, int local_test_func_idx,
67 int remote_test_func_idx, double ** field_data_out,
68 double ** ref_field_data_in, size_t * field_data_size) {
69
70 unsigned n = 50;
71
72 unsigned nbr_vertices;
73 unsigned nbr_cells;
74 unsigned * num_vertices_per_cell;
75 unsigned * cell_to_vertex;
76 double * x_vertices;
77 double * y_vertices;
78 double * x_cells;
79 double * y_cells;
80
81 int * cell_core_mask;
82 int * corner_core_mask;
83 int * global_cell_id;
84 int * global_corner_id;
85
87 n, &nbr_vertices, &nbr_cells, &num_vertices_per_cell, &cell_to_vertex,
88 &x_vertices, &y_vertices, &x_cells, &y_cells, &global_cell_id,
89 &cell_core_mask, &global_corner_id, &corner_core_mask,
90 comm_rank, comm_size);
91
93 grid_name, nbr_vertices, nbr_cells, (int*)num_vertices_per_cell,
94 x_vertices, y_vertices, (int*)cell_to_vertex, grid_id);
95
98
100 *grid_id, nbr_cells, YAC_LOCATION_CELL, x_cells, y_cells,
102
103 double (*test_func[4])(double, double) =
108
109 *field_data_out = xmalloc(nbr_cells * sizeof(**field_data_out));
110 for (unsigned i = 0; i < nbr_cells; ++i)
111 (*field_data_out)[i] =
112 (cell_core_mask[i])?
113 (*(test_func[local_test_func_idx]))(x_cells[i], y_cells[i]):
115 *ref_field_data_in = xmalloc(nbr_cells * sizeof(**ref_field_data_in));
116 for (unsigned i = 0; i < nbr_cells; ++i)
117 (*ref_field_data_in)[i] =
118 (cell_core_mask[i])?
119 (*(test_func[remote_test_func_idx]))(x_cells[i], y_cells[i]):
121 *field_data_size = nbr_cells;
122 free(cell_core_mask);
123 free(corner_core_mask);
124 free(global_cell_id);
125 free(global_corner_id);
126 free(x_vertices);
127 free(y_vertices);
128 free(x_cells);
129 free(y_cells);
130 free(num_vertices_per_cell);
131 free(cell_to_vertex);
132}
133
134static void run_component(
135 char local_comp_id, char remote_comp_id, MPI_Comm comm) {
136
137 int comm_rank, comm_size;
138 MPI_Comm_rank(comm, &comm_rank);
139 MPI_Comm_size(comm, &comm_size);
140
141 // init YAC
142 yac_cinit_comm(comm);
144
145 // add configuration (one out-going field)
146 yac_cdef_datetime("2008-03-09T16:05:07", "2008-03-10T16:05:07");
147 char local_comp_name[16], local_grid_name[16];
148 char remote_comp_name[16], remote_grid_name[16];
149 char field_out_name[16], field_in_name[16];
150 sprintf(local_comp_name, "comp%c", local_comp_id);
151 sprintf(local_grid_name, "grid%c", local_comp_id);
152 sprintf(remote_comp_name, "comp%c", remote_comp_id);
153 sprintf(remote_grid_name, "grid%c", remote_comp_id);
154 sprintf(field_out_name, "field_%c_to_%c", local_comp_id, remote_comp_id);
155 sprintf(field_in_name, "field_%c_to_%c", remote_comp_id, local_comp_id);
156 int interp_stack_config_id;
157 yac_cget_interp_stack_config(&interp_stack_config_id);
159 interp_stack_config_id, YAC_NNN_AVG, 1, 0.0, -1.0);
161 local_comp_name, local_grid_name, field_out_name,
162 remote_comp_name, remote_grid_name, field_out_name,
164 interp_stack_config_id, 0, 0);
165 yac_cfree_interp_stack_config(interp_stack_config_id);
166
167 // define component
168 int comp_id;
169 yac_cdef_comp(local_comp_name, &comp_id);
170
171 MPI_Comm comp_comm;
172 int comp_rank, comp_size;
173 yac_cget_comp_comm(comp_id, &comp_comm);
174 MPI_Comm_rank(comp_comm, &comp_rank);
175 MPI_Comm_size(comp_comm, &comp_size);
176 MPI_Comm_free(&comp_comm);
177
178 // define grid
180 double * field_data_out, * ref_field_data_in;
181 size_t field_data_size;
182 def_grid(local_grid_name, comp_rank, comp_size, &grid_id, &cell_point_id,
183 (int)(local_comp_id - 'A'), (int)(remote_comp_id - 'A'),
184 &field_data_out, &ref_field_data_in, &field_data_size);
185
186 // define fields
187 int field_out_id, field_in_id;
189 field_out_name, comp_id, &cell_point_id, 1, 1, "1",
190 YAC_TIME_UNIT_SECOND, &field_out_id);
192 field_in_name, comp_id, &cell_point_id, 1, 1, "1",
193 YAC_TIME_UNIT_SECOND, &field_in_id);
194
195 yac_cenddef();
196
197 double * field_data_in = xmalloc(field_data_size * sizeof(*field_data_in));
198
199 // do some ping-pongs
200 for (int t = 0; t < 10; ++t) {
201
202 {
203 int info, err;
204 double *point_set_data[1];
205 double **collection_data[1] = {point_set_data};
206 point_set_data[0] = field_data_out;
207 yac_cput(field_out_id, 1, collection_data, &info, &err);
208 }
209
210 {
211 for (size_t i = 0; i < field_data_size; ++i)
212 field_data_in[i] = DUMMY_VALUE;
213
214 int info, err;
215 double *collection_data[1] = {field_data_in};
216 yac_cget(field_in_id, 1, collection_data, &info, &err);
217
218 for (size_t i = 0; i < field_data_size; ++i) {
219 if(fabs(field_data_in[i] - ref_field_data_in[i]) > 1e-3) {
220 fputs("data data_mismatch\n", stderr);
221 exit(EXIT_FAILURE);
222 }
223 }
224 }
225 }
226 free(field_data_in);
227 free(field_data_out);
228 free(ref_field_data_in);
229
231
232 MPI_Barrier(comm);
233}
234
235
236static void utest_run_setup(char local_comp_id, char comp_groups[2][2]) {
237
238 int group_idx = (local_comp_id == comp_groups[1][0]) ||
239 (local_comp_id == comp_groups[1][1]);
240
241 MPI_Comm group_comm;
242 MPI_Comm_split(MPI_COMM_WORLD, group_idx, 0, &group_comm);
243
244 char remote_comp_id =
245 comp_groups[group_idx][comp_groups[group_idx][0] == local_comp_id];
246
247 run_component(local_comp_id, remote_comp_id, group_comm);
248
249 MPI_Comm_free(&group_comm);
250}
void yac_generate_part_cube_grid_information(unsigned n, unsigned *nbr_vertices, unsigned *nbr_cells, unsigned **num_vertices_per_cell, unsigned **cell_to_vertex, double **x_vertices, double **y_vertices, double **x_cells, double **y_cells, int **global_cell_id, int **cell_core_mask, int **global_corner_id, int **corner_core_mask, int rank, int size)
#define xmalloc(size)
Definition ppm_xfuncs.h:66
int * cell_to_vertex
double yac_test_ana_fcos(double lon, double lat)
double yac_test_harmonic(double lon, double lat)
double yac_test_vortex(double lon, double lat)
double yac_test_gulfstream(double lon, double lat)
static void def_grid(char const *grid_name, int comm_rank, int comm_size, int *grid_id, int *cell_point_id, int local_test_func_idx, int remote_test_func_idx, double **field_data_out, double **ref_field_data_in, size_t *field_data_size)
#define DUMMY_VALUE
static void run_component(char local_comp_id, char remote_comp_id, MPI_Comm comm)
size_t field_data_size
int info
int * cell_core_mask
int grid_id
int cell_point_id
int comp_id
void yac_cenddef(void)
Definition yac.c:4363
void yac_cset_global_index(int const *global_index, int location, int grid_id)
Definition yac.c:5012
void yac_cdef_datetime(const char *start_datetime, const char *end_datetime)
Definition yac.c:738
int const YAC_LOCATION_CELL
Definition yac.c:31
void yac_cfinalize()
Finalises YAC.
Definition yac.c:717
void yac_cget_comp_comm(int comp_id, MPI_Comm *comp_comm)
Definition yac.c:833
void yac_cget(int const field_id, int collection_size, double **recv_field, int *info, int *ierr)
Definition yac.c:2729
int const YAC_TIME_UNIT_SECOND
Definition yac.c:56
void yac_cdef_grid_unstruct(const char *grid_name, int nbr_vertices, int nbr_cells, int *num_vertices_per_cell, double *x_vertices, double *y_vertices, int *cell_to_vertex, int *grid_id)
Definition yac.c:4830
void yac_cdef_points_unstruct(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:1158
void yac_cput(int const field_id, int const collection_size, double ***const send_field, int *info, int *ierr)
Definition yac.c:3684
void yac_cdef_calendar(int calendar)
Definition yac.c:746
void yac_cset_core_mask(int const *is_core, int location, int grid_id)
Definition yac.c:5028
int const YAC_PROLEPTIC_GREGORIAN
Definition yac.c:65
void yac_cfree_interp_stack_config(int interp_stack_config_id)
Definition yac.c:5114
int const YAC_NNN_AVG
Definition yac.c:76
void yac_cadd_interp_stack_config_nnn(int interp_stack_config_id, int type, size_t n, double max_search_distance, double scale)
Definition yac.c:5171
void yac_cget_interp_stack_config(int *interp_stack_config_id)
Definition yac.c:5100
void yac_cdef_comp(char const *comp_name, int *comp_id)
Definition yac.c:990
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:1373
void yac_cdef_couple(char const *src_comp_name, char const *src_grid_name, char const *src_field_name, char const *tgt_comp_name, char const *tgt_grid_name, char const *tgt_field_name, char const *coupling_timestep, int time_unit, int time_reduction, int interp_stack_config_id, int src_lag, int tgt_lag)
Definition yac.c:1928
int const YAC_REDUCTION_TIME_NONE
Definition yac.c:49
void yac_cinit_comm(MPI_Comm comm)
Definition yac.c:462