YAC 3.21.0
Yet Another Coupler
Loading...
Searching...
No Matches
test_interpolation_parallel5.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 <stdlib.h>
6#include <stdio.h>
7#include <math.h>
8#include <unistd.h>
9#include <string.h>
10
11#include <mpi.h>
12#include <yaxt.h>
13
14#include "tests.h"
15#include "test_common.h"
16#include "weight_file_common.h"
17#include "dist_grid_utils.h"
18#include "grids/dist_grid.h"
19#include "geometry.h"
25
31#define YAC_RAD (0.01745329251994329576923690768489) // M_PI / 180
32
33int main(int argc, char *argv[]) {
34
35 if (argc != 2) {
36 PUT_ERR("wrong number of arguments\n");
37 return TEST_EXIT_CODE;
38 }
39
40 enum yac_interp_weights_reorder_type reorder_type =
41 (strcmp(argv[1], "src") == 0)?YAC_MAPPING_ON_SRC:YAC_MAPPING_ON_TGT;
42
43 if ((reorder_type != YAC_MAPPING_ON_SRC) && strcmp(argv[1], "tgt")) {
44 PUT_ERR("invalid argument (has to be either \"src\" or \"tgt\")\n");
45 return TEST_EXIT_CODE;
46 }
47
48 MPI_Init(NULL, NULL);
49
50 xt_initialize(MPI_COMM_WORLD);
51
52 int comm_rank, comm_size;
53 MPI_Comm_rank(MPI_COMM_WORLD, &comm_rank);
54 MPI_Comm_size(MPI_COMM_WORLD, &comm_size);
55 MPI_Barrier(MPI_COMM_WORLD);
56
57 if (comm_size != 3) {
58 PUT_ERR("ERROR: wrong number of processes");
59 xt_finalize();
60 MPI_Finalize();
61 return TEST_EXIT_CODE;
62 }
63
64 unsigned is_source = comm_rank > 0;
65 unsigned is_target = comm_rank < 2;
66
67 /*
68 * The source grid is distributed among 2 processes.
69 *
70 * The global source grid has 5x4 cells:
71 *
72 * 24--44--25--45--26--46--27--47--28--48--29
73 * | | | | | |
74 * 34 15 36 16 38 17 40 18 42 19 43
75 * | | | | | |
76 * 18--33--19--35--20--37--21--39--22--41--23
77 * | | | | | |
78 * 23 10 25 11 27 12 29 13 31 14 32
79 * | | | | | |
80 * 12--22--13--24--14--26--15--28--16--30--17
81 * | | | | | |
82 * 12 05 14 06 16 07 18 08 20 09 21
83 * | | | | | |
84 * 06--11--07--13--08--15--09--17--10--19--11
85 * | | | | | |
86 * 01 00 03 01 05 02 07 03 09 04 10
87 * | | | | | |
88 * 00--01--01--02--02--04--03--06--04--08--05
89 */
90 /*
91 * The target grid is distributed among 2 processes
92 *
93 * The global target grid has 6x3 cells:
94 *
95 * 21--39--22--40--23--41--24--42--25--43--26--44--27
96 * | | | | | | |
97 * 27 12 29 13 31 14 33 15 35 16 37 17 38
98 * | | | | | | |
99 * 14--26--15--28--16--30--17--32--18--34--19--36--20
100 * | | | | | | |
101 * 14 06 16 07 18 08 20 09 22 10 24 11 25
102 * | | | | | | |
103 * 07--13--08--15--09--17--10--19--11--21--12--23--13
104 * | | | | | | |
105 * 01 00 03 01 05 02 07 03 09 04 11 05 12
106 * | | | | | | |
107 * 00--00--01--02--02--04--03--06--04--08--05--10--06
108 */
109
110 char const source_grid_name[] = "source_grid";
111 char const target_grid_name[] = "target_grid";
112
113 double src_coordinates_x[] = {0,1,2,3,4,5};
114 double src_coordinates_y[] = {0,1,2,3,4};
115 size_t src_global_num_cells[] = {5,4};
116 int src_with_halo = 1;
117 size_t src_local_start[2][2] = {{0,0}, {0,2}};
118 size_t src_local_count[2] = {5,2};
119
120 for (size_t i = 0; i <= src_global_num_cells[0]; ++i)
121 src_coordinates_x[i] *= YAC_RAD;
122 for (size_t i = 0; i <= src_global_num_cells[1]; ++i)
123 src_coordinates_y[i] *= YAC_RAD;
124
125 struct yac_basic_grid * src_grid =
126 (is_source)?
128 source_grid_name,
129 utest_generate_basic_grid_data_reg2d(
130 src_coordinates_x, src_coordinates_y, src_global_num_cells,
131 src_local_start[comm_rank-1], src_local_count, src_with_halo)):
132 yac_basic_grid_empty_new(source_grid_name);
133
134 double tgt_coordinates_x[] = {0.7,1.7,2.7,3.7,4.7,5.7,6.7};
135 double tgt_coordinates_y[] = {0.7,1.7,2.7,3.7};
136 size_t tgt_global_num_cells[] = {6,3};
137 int tgt_with_halo = 1;
138 size_t tgt_local_start[2][2] = {{3,0}, {0,0}};
139 size_t tgt_local_count[2] = {3,3};
140
141 for (size_t i = 0; i <= tgt_global_num_cells[0]; ++i)
142 tgt_coordinates_x[i] *= YAC_RAD;
143 for (size_t i = 0; i <= tgt_global_num_cells[1]; ++i)
144 tgt_coordinates_y[i] *= YAC_RAD;
145
146 struct yac_basic_grid * tgt_grid =
147 (is_target)?
149 target_grid_name,
150 utest_generate_basic_grid_data_reg2d(
151 tgt_coordinates_x, tgt_coordinates_y, tgt_global_num_cells,
152 tgt_local_start[comm_rank], tgt_local_count, tgt_with_halo)):
153 yac_basic_grid_empty_new(target_grid_name);
154
155 struct yac_dist_grid_pair * grid_pair =
156 yac_dist_grid_pair_new(src_grid, tgt_grid, MPI_COMM_WORLD);
157
158 { // test interpolation
159
160 struct yac_interp_field src_fields[] =
161 {{.location = YAC_LOC_CORNER, .coordinates_idx = SIZE_MAX, .masks_idx = SIZE_MAX}};
162 size_t num_src_fields = sizeof(src_fields) / sizeof(src_fields[0]);
163 struct yac_interp_field tgt_field =
164 {.location = YAC_LOC_CORNER, .coordinates_idx = SIZE_MAX, .masks_idx = SIZE_MAX};
165
166 struct yac_interp_grid * interp_grid =
167 yac_interp_grid_new(grid_pair, source_grid_name, target_grid_name,
169
170 // generate a weight file in which all weights are one
171 char const * sum_weight_file_name =
172 "test_interpolation_parallel5_weight_file.nc";
173 if (comm_rank == 0) {
174 int src_indices[4*5*4] =
175 {0,1,6,7, 1,2,7,8, 2,3,8,9, 3,4,9,10, 4,5,10,11,
176 6,7,12,13, 7,8,13,14, 8,9,14,15, 9,10,15,16, 10,11,16,17,
177 12,13,18,19, 13,14,19,20, 14,15,20,21, 15,16,21,22, 16,17,22,23,
178 18,19,24,25, 19,20,25,26, 20,21,26,27, 21,22,27,28, 22,23,28,29};
179 int tgt_indices[4*5*4] =
180 {0,0,0,0, 1,1,1,1, 2,2,2,2, 3,3,3,3, 4,4,4,4,
181 7,7,7,7, 8,8,8,8, 9,9,9,9, 10,10,10,10, 11,11,11,11,
182 14,14,14,14, 15,15,15,15, 16,16,16,16, 17,17,17,17, 18,18,18,18,
183 21,21,21,21, 22,22,22,22, 23,23,23,23, 24,24,24,24, 25,25,25,25};
184 double weights[4*5*4];
185 for (size_t i = 0; i < 4*5*4; ++i) weights[i] = 1.0;
186 size_t num_links = 4*5*4;
187 enum yac_location src_locations[1] = {YAC_LOC_CORNER};
188 enum yac_location tgt_location = YAC_LOC_CORNER;
189 unsigned num_src_fields = 1;
190 int num_links_per_field[1] = {num_links};
191 int * tgt_id_fixed = NULL;
192 size_t num_fixed_tgt = 0;
193 double * fixed_values = NULL;
194 int * num_tgt_per_fixed_value = NULL;
195 size_t num_fixed_values = 0;
196
197 utest_write_weight_file(
198 sum_weight_file_name, src_indices, tgt_indices, weights, num_links,
199 src_locations, num_src_fields, num_links_per_field, tgt_id_fixed,
200 num_fixed_tgt, fixed_values, num_tgt_per_fixed_value,
201 num_fixed_values, tgt_location,
202 source_grid_name, target_grid_name);
203 }
204 MPI_Barrier(MPI_COMM_WORLD);
205
206 size_t const num_method_stacks = 3;
207 struct interp_method * method_stacks[3][3] =
210 NULL},
213 .type = YAC_INTERP_NNN_AVG, .n = 1,
215 .max_search_distance =
218 NULL},
220 sum_weight_file_name, YAC_INTERP_FILE_ON_MISSING_FILE_DEFAULT,
223 NULL}};
224
225 for (size_t method_stack_index = 0;
226 method_stack_index < num_method_stacks; ++method_stack_index) {
227
228 struct yac_interp_weights * weights =
230 method_stacks[method_stack_index], interp_grid);
231
232 yac_interp_method_delete(method_stacks[method_stack_index]);
233
234 struct yac_interpolation * interpolation =
236 weights, reorder_type, 1,
237 YAC_FRAC_MASK_NO_VALUE, 1.0, 0.0, NULL, 1, 1);
238
239 char const weight_file_out[] =
240 "test_interpolation_parallel5_weight_file_out.nc";
242 weights, weight_file_out, source_grid_name, target_grid_name, 0, 0,
244
245 //---------------------
246 // do the interpolation
247 //---------------------
248
249 struct yac_basic_grid_data * src_grid_data =
250 is_source?yac_basic_grid_get_data(src_grid):NULL;
251 double * source_data_field =
252 is_source?
253 xmalloc(src_grid_data->num_vertices * sizeof(*source_data_field)):NULL;
254 double * source_data_pointset[1] = {source_data_field}; // num_pointset == 1
255 double ** source_data[1] = {source_data_pointset}; // collection_size == 1
256
257 struct yac_basic_grid_data * tgt_grid_data =
258 is_target?yac_basic_grid_get_data(tgt_grid):NULL;
259 double * target_data_field =
260 is_target?
261 xmalloc(tgt_grid_data->num_vertices * sizeof(*target_data_field)):NULL;
262 double * target_data[1] = {target_data_field}; // collection_size == 1
263
264 if (is_source) {
265
266 // source_data dimensions [collection_idx]
267 // [pointset_idx]
268 // [local_idx]
269 for (size_t i = 0; i < src_grid_data->num_vertices; ++i)
270 source_data_field[i] =
271 (src_grid_data->core_vertex_mask[i])?
272 ((double)(src_grid_data->vertex_ids[i])):(-1.0);
273 }
274
275 for (int use_put_get = 0; use_put_get < 2; ++use_put_get) {
276
277 if (is_target) {
278
279 // target_data dimensions [collection_idx]
280 // [local_idx]
281 for (size_t i = 0; i < tgt_grid_data->num_vertices; ++i)
282 target_data_field[i] = -1.0;
283 }
284
285 if (use_put_get) {
286 if (is_source)
287 yac_interpolation_execute_put(interpolation, source_data);
288 if (is_target)
289 yac_interpolation_execute_get(interpolation, target_data);
290 } else {
291 yac_interpolation_execute(interpolation, source_data, target_data);
292 }
293
294 if (is_target) {
295
296 //----------------------------
297 // check interpolation results
298 //----------------------------
299
300 double ref_target_data[3][4*7] =
301 {{3.5,4.5,5.5,6.5,7.5,1337,1337,
302 9.5,10.5,11.5,12.5,13.5,1337,1337,
303 15.5,16.5,17.5,18.5,19.5,1337,1337,
304 21.5,22.5,23.5,24.5,25.5,1337,1337},
305 {7,8,9,10,11,11,11,
306 13,14,15,16,17,17,17,
307 19,20,21,22,23,23,23,
308 25,26,27,28,29,29,29},
309 {14,18,22,26,30,1337,1337,
310 38,42,46,50,54,1337,1337,
311 62,66,70,74,78,1337,1337,
312 86,90,94,98,102,1337,1337}};
313
314 for (size_t i = 0; i < tgt_grid_data->num_vertices; ++i) {
315 if (tgt_grid_data->core_vertex_mask[i]) {
316 if (utest_double_are_unequal(
317 target_data[0][i],
318 ref_target_data
319 [method_stack_index][tgt_grid_data->vertex_ids[i]]))
320 PUT_ERR("error in interpolated data on target side\n");
321 } else {
322 if (target_data[0][i] != -1.0)
323 PUT_ERR("error in interpolated data on target side\n");
324 }
325 }
326 }
327 }
328
329 if (is_target) free(target_data_field);
330 if (is_source) free(source_data_field);
331
332 //--------
333 // cleanup
334 //--------
335
336 if (is_source || is_target) yac_interpolation_delete(interpolation);
338
339 if (comm_rank == 0) unlink(weight_file_out);
340 }
341
342 if (comm_rank == 0) unlink(sum_weight_file_name);
343
344 yac_interp_grid_delete(interp_grid);
345 }
346
347 //--------
348 // cleanup
349 //--------
350
351 yac_dist_grid_pair_delete(grid_pair);
352 yac_basic_grid_delete(src_grid);
353 yac_basic_grid_delete(tgt_grid);
354
355 xt_finalize();
356 MPI_Finalize();
357
358 return TEST_EXIT_CODE;
359}
360
struct yac_basic_grid * yac_basic_grid_new(char const *name, struct yac_basic_grid_data grid_data)
Definition basic_grid.c:57
struct yac_basic_grid_data * yac_basic_grid_get_data(struct yac_basic_grid *grid)
Definition basic_grid.c:137
struct yac_basic_grid * yac_basic_grid_empty_new(char const *name)
Definition basic_grid.c:70
void yac_basic_grid_delete(struct yac_basic_grid *grid)
Definition basic_grid.c:77
void yac_dist_grid_pair_delete(struct yac_dist_grid_pair *grid_pair)
Definition dist_grid.c:2377
struct yac_dist_grid_pair * yac_dist_grid_pair_new(struct yac_basic_grid *grid_a, struct yac_basic_grid *grid_b, MPI_Comm comm)
Definition dist_grid.c:2089
void yac_interp_grid_delete(struct yac_interp_grid *interp_grid)
struct yac_interp_grid * yac_interp_grid_new(struct yac_dist_grid_pair *grid_pair, char const *src_grid_name, char const *tgt_grid_name, size_t num_src_fields, struct yac_interp_field const *src_fields, struct yac_interp_field const tgt_field)
Definition interp_grid.c:31
void yac_interp_method_delete(struct interp_method **method)
Delete an interpolation stack and free its resources (but not the pointer array).
struct yac_interp_weights * yac_interp_method_do_search(struct interp_method **method, struct yac_interp_grid *interp_grid)
Perform weight computation using given interpolation stack and grid.
struct interp_method * yac_interp_method_avg_new(enum yac_interp_avg_weight_type weight_type, int partial_coverage)
@ YAC_INTERP_AVG_ARITHMETIC
struct interp_method * yac_interp_method_file_new(char const *weight_file_name, enum yac_interp_file_on_missing_file on_missing_file, enum yac_interp_file_on_success on_success)
#define YAC_INTERP_FILE_ON_SUCCESS_DEFAULT
#define YAC_INTERP_FILE_ON_MISSING_FILE_DEFAULT
struct interp_method * yac_interp_method_fixed_new(double value)
struct interp_method * yac_interp_method_nnn_new(struct yac_interp_method_nnn_config config)
@ YAC_INTERP_NNN_AVG
average of n source points
#define YAC_INTERP_NNN_MAX_SEARCH_DISTANCE_DEFAULT
#define YAC_INTERP_NNN_N_MIN_DEFAULT
a value of 0 is interpreted as "use the value of n"
struct yac_interpolation * yac_interp_weights_get_interpolation(struct yac_interp_weights *weights, enum yac_interp_weights_reorder_type reorder, size_t collection_size, double frac_mask_fallback_value, double scaling_factor, double scaling_summand, char const *yaxt_exchanger_name, int is_source, int is_target)
void yac_interp_weights_delete(struct yac_interp_weights *weights)
void yac_interp_weights_write_to_file(struct yac_interp_weights *weights, char const *filename, char const *src_grid_name, char const *tgt_grid_name, size_t src_grid_size, size_t tgt_grid_size, enum yac_weight_file_on_existing on_existing)
yac_interp_weights_reorder_type
@ YAC_MAPPING_ON_TGT
weights will be applied at target processes
@ YAC_MAPPING_ON_SRC
weights will be applied at source processes
@ YAC_WEIGHT_FILE_ERROR
error when weight file existis already
void yac_interpolation_execute(struct yac_interpolation *interp, double ***src_fields, double **tgt_field)
Execute interpolation synchronously and write results to the target field.
void yac_interpolation_delete(struct yac_interpolation *interp)
Free an interpolation object and release all resources.
void yac_interpolation_execute_get(struct yac_interpolation *interp, double **tgt_field)
Complete interpolation and write results to the target field (get phase).
void yac_interpolation_execute_put(struct yac_interpolation *interp, double ***src_fields)
Provide source field data and start asynchronous execution of interpolation (put phase).
double const YAC_FRAC_MASK_NO_VALUE
yac_location
Definition location.h:12
@ YAC_LOC_CORNER
Definition location.h:15
#define xmalloc(size)
Definition ppm_xfuncs.h:66
enum yac_location location
Definition basic_grid.h:16
struct yac_interp_field tgt_field
Definition interp_grid.c:26
size_t num_src_fields
Definition interp_grid.c:27
struct yac_dist_grid_pair * grid_pair
Definition interp_grid.c:25
struct yac_interp_field src_fields[]
Definition interp_grid.c:28
char const weight_file_out[]
#define TEST_EXIT_CODE
Definition tests.h:15
#define PUT_ERR(string)
Definition tests.h:10