YAC 3.21.0
Yet Another Coupler
Loading...
Searching...
No Matches
test_proc_sphere_part_parallel.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 <mpi.h>
7#include <limits.h>
8
9#include "tests.h"
10#include "test_common.h"
11#include "geometry.h"
12#include "proc_sphere_part.h"
13#include "yac_mpi.h"
14
20int main(void) {
21
22 MPI_Init(NULL, NULL);
23
24 int comm_rank, comm_size;
25 MPI_Comm_rank(MPI_COMM_WORLD, &comm_rank);
26 MPI_Comm_size(MPI_COMM_WORLD, &comm_size);
27
28 YAC_ASSERT(comm_size == 5, "ERROR wrong number of processes (has to be 5)")
29
30 {
31 // one process has no data
32 double x_vertices[18] = {0,20,40,60,80,
33 100,120,140,160,180,
34 200,220,240,260,280,
35 300,320,340};
36 double y_vertices[9] = {-80,-60,-40,-20,0,20,40,60,80};
37 size_t local_start[5][2] = {{0,0},{7,0},{0,0},{0,3},{7,3}};
38 size_t local_count[5][2] = {{10,6},{10,6},{0,0},{10,6},{10,6}};
39 size_t num_vertices_a =
40 local_count[comm_rank][0] * local_count[comm_rank][1];
41 yac_coordinate_pointer vertices_a =
42 xmalloc(num_vertices_a * sizeof(*vertices_a));
43
44 for (size_t i = 0, k = 0; i < local_count[comm_rank][1]; ++i)
45 for (size_t j = 0; j < local_count[comm_rank][0]; ++j, ++k)
47 x_vertices[local_start[comm_rank][0]+j],
48 y_vertices[local_start[comm_rank][1]+i],
49 &(vertices_a[k][0]));
50
51 yac_coordinate_pointer vertices[2] = {vertices_a, NULL};
52 size_t num_vertices[2] = {num_vertices_a, 0};
53 yac_int * global_ids_a = NULL, * global_ids_b = NULL;
54 yac_int **global_vertex_ids[2] = {&global_ids_a, &global_ids_b};
55 int * vertex_ranks_a, * vertex_ranks_b;
56 int **vertex_ranks[2] = {&vertex_ranks_a, &vertex_ranks_b};
57 struct proc_sphere_part_node * proc_sphere_part;
59 vertices, num_vertices, &proc_sphere_part, global_vertex_ids,
60 vertex_ranks, MPI_COMM_WORLD);
61
62 free(vertex_ranks_a);
63 free(vertex_ranks_b);
64 free(global_ids_a);
65 free(global_ids_b);
66 free(vertices_a);
67
68 yac_proc_sphere_part_node_delete(proc_sphere_part);
69 }
70
71 {
72 double x_vertices[] = { 0, 10, 20, 30, 40, 50, 60, 70, 80, 90,
73 100,110,120,130,140,150,160,170,180,190,
74 200,210,220,230,240,250,260,270,280,290,
75 300,310,320,330,340,350};
76 double y_vertices[] =
77 {-90,-80,-70,-60,-50,-40,-30,-20,-10,0,10,20,30,40,50,60,70,80,90};
78 size_t local_start[5][2] = {{0,0},{0,5},{12,5},{24,5},{0,14}};
79 size_t local_count[5][2] = {{36,5},{12,9},{12,9},{12,9},{36,5}};
80 size_t num_vertices_a =
81 local_count[comm_rank][0] * local_count[comm_rank][1];
82 yac_coordinate_pointer vertices_a =
83 xmalloc(num_vertices_a * sizeof(*vertices_a));
84
85 for (size_t i = 0, k = 0; i < local_count[comm_rank][1]; ++i)
86 for (size_t j = 0; j < local_count[comm_rank][0]; ++j, ++k)
88 x_vertices[local_start[comm_rank][0]+j],
89 y_vertices[local_start[comm_rank][1]+i],
90 vertices_a[k]);
91
92 yac_coordinate_pointer vertices[2] = {vertices_a, NULL};
93 size_t num_vertices[2] = {num_vertices_a, 0};
94 yac_int * global_ids_a = NULL, * global_ids_b = NULL;
95 yac_int **global_vertex_ids[2] = {&global_ids_a, &global_ids_b};
96 int * vertex_ranks_a, * vertex_ranks_b;
97 int **vertex_ranks[2] = {&vertex_ranks_a, &vertex_ranks_b};
98 struct proc_sphere_part_node * proc_sphere_part;
100 vertices, num_vertices, &proc_sphere_part, global_vertex_ids,
101 vertex_ranks, MPI_COMM_WORLD);
102
103 free(vertex_ranks_a);
104 free(vertex_ranks_b);
105 free(global_ids_a);
106 free(global_ids_b);
107 free(vertices_a);
108
109 double x_search = 45.0, y_search = 80.0;
110 struct bounding_circle bnd_circle;
111 LLtoXYZ_deg(x_search, y_search, bnd_circle.base_vector);
112 bnd_circle.inc_angle.sin = sin(3.13);
113 bnd_circle.inc_angle.cos = cos(3.13);
114
115 int search_ranks[5], rank_count;
117 proc_sphere_part, bnd_circle, search_ranks, &rank_count);
118
119 if (rank_count != 5)
120 PUT_ERR("error in yac_proc_sphere_part_do_bnd_circle_search");
121
122 yac_proc_sphere_part_node_delete(proc_sphere_part);
123 }
124
125 {
126 double coords[5][2][3] =
127 {{{1,0,0},{-1,0,0}},
128 {{0,1,0}},
129 {{0,-1,0}},
130 {{0,0,1}},
131 {{0,0,-1}}};
132 size_t num_vertices_a[5] = {2,1,1,1,1};
133 yac_coordinate_pointer vertices_a =
134 xmalloc(num_vertices_a[comm_rank] * sizeof(*vertices_a));
135
136 for (size_t i = 0; i < num_vertices_a[comm_rank]; ++i)
137 for (int j = 0; j < 3; ++j)
138 vertices_a[i][j] = coords[comm_rank][i][j];
139
140 yac_coordinate_pointer vertices[2] = {vertices_a, NULL};
141 size_t num_vertices[2] = {num_vertices_a[comm_rank], 0};
142 yac_int * global_ids_a = NULL, * global_ids_b = NULL;
143 yac_int **global_vertex_ids[2] = {&global_ids_a, &global_ids_b};
144 int * vertex_ranks_a = NULL, * vertex_ranks_b = NULL;
145 int **vertex_ranks[2] = {&vertex_ranks_a, &vertex_ranks_b};
146 struct proc_sphere_part_node * proc_sphere_part;
148 vertices, num_vertices, &proc_sphere_part, global_vertex_ids,
149 vertex_ranks, MPI_COMM_WORLD);
150
151 free(vertex_ranks_a);
152 free(vertex_ranks_b);
153 free(global_ids_a);
154 free(global_ids_b);
155 free(vertices_a);
156
157 yac_proc_sphere_part_node_delete(proc_sphere_part);
158 }
159
160 {
161 yac_coordinate_pointer vertices[2] = {NULL, NULL};
162 size_t num_vertices[2] = {0, 0};
163 yac_int * global_ids_a = NULL, * global_ids_b = NULL;
164 yac_int **global_vertex_ids[2] = {&global_ids_a, &global_ids_b};
165 int * vertex_ranks_a = NULL, * vertex_ranks_b = NULL;
166 int **vertex_ranks[2] = {&vertex_ranks_a, &vertex_ranks_b};
167 struct proc_sphere_part_node * proc_sphere_part;
169 vertices, num_vertices, &proc_sphere_part, global_vertex_ids,
170 vertex_ranks, MPI_COMM_WORLD);
171
172 free(vertex_ranks_a);
173 free(vertex_ranks_b);
174 free(global_ids_a);
175 free(global_ids_b);
176
177 yac_proc_sphere_part_node_delete(proc_sphere_part);
178 }
179
180 {
181 double x_vertices[] = { 0, 30, 60, 90, 120, 150, 180};
182 double y_vertices[] = {-90,-60,-30, 0, 30, 60, 90};
183
184 size_t local_start[2][5][2] = {{{0,0},{0,3},{3,3},{0,0},{0,0}},
185 {{0,0},{0,0},{0,0},{3,0},{0,3}}};
186 size_t local_count[2][5][2] = {{{7,4},{4,4},{4,4},{0,0},{0,0}},
187 {{0,0},{0,0},{4,4},{4,4},{7,4}}};
188 size_t num_vertices_[2];
189
190 yac_coordinate_pointer vertices_[2];
191 yac_int *global_vertex_ids_in[2];
192
193 for (int grid_idx = 0; grid_idx < 2; ++grid_idx) {
194
195 num_vertices_[grid_idx] =
196 local_count[grid_idx][comm_rank][0] *
197 local_count[grid_idx][comm_rank][1];
198
199 vertices_[grid_idx] =
200 xmalloc(num_vertices_[grid_idx] * sizeof(*(vertices_[0])));
201 global_vertex_ids_in[grid_idx] =
202 xmalloc(
203 num_vertices_[grid_idx] * sizeof(*(global_vertex_ids_in[0])));
204
205 for (size_t i = 0, k = 0; i < local_count[grid_idx][comm_rank][1]; ++i) {
206 for (size_t j = 0; j < local_count[grid_idx][comm_rank][0]; ++j, ++k) {
208 x_vertices[local_start[grid_idx][comm_rank][0]+j],
209 y_vertices[local_start[grid_idx][comm_rank][1]+i],
210 vertices_[grid_idx][k]);
211 global_vertex_ids_in[grid_idx][k] =
212 (local_start[grid_idx][comm_rank][1]+i) * 7 +
213 local_start[grid_idx][comm_rank][0]+j;
214 }
215 }
216 }
217
218 int with_grid[2], with_global_ids[2];
219
220 for (with_grid[0] = 0; with_grid[0] < 2; ++with_grid[0]) {
221
222 for (with_global_ids[0] = 0; with_global_ids[0] < 2;
223 with_global_ids[0]++) {
224
225 for (with_grid[1] = 0; with_grid[1] < 2; ++with_grid[1]) {
226
227 for (with_global_ids[1] = 0; with_global_ids[1] < 2;
228 with_global_ids[1]++) {
229
230 int * vertex_ranks_out[2] = {NULL, NULL};
231 yac_int * global_vertex_ids_out[2] = {NULL,NULL};
232
233 int **vertex_ranks[2];
234 yac_int **global_vertex_ids[2];
235 yac_coordinate_pointer vertices[2];
236 size_t num_vertices[2];
237
238 for (int grid_idx = 0; grid_idx < 2; ++grid_idx){
239 vertices[grid_idx] =
240 (with_grid[grid_idx] && (num_vertices_[grid_idx] > 0))?
241 vertices_[grid_idx]:NULL;
242 num_vertices[grid_idx] =
243 (with_grid[grid_idx]&&(num_vertices_[grid_idx] > 0))?
244 num_vertices_[grid_idx]:0;
245 global_vertex_ids[grid_idx] =
246 (with_global_ids[grid_idx])?
247 &(global_vertex_ids_in[grid_idx]):
248 &(global_vertex_ids_out[grid_idx]);
249 vertex_ranks[grid_idx] = &(vertex_ranks_out[grid_idx]);
250 }
251
252 struct proc_sphere_part_node * proc_sphere_part;
254 vertices, num_vertices, &proc_sphere_part, global_vertex_ids,
255 vertex_ranks, MPI_COMM_WORLD);
256
257 // check results
258 for (int grid_idx = 0; grid_idx < 2; ++grid_idx) {
259
260 int * search_ranks =
261 xmalloc(num_vertices[grid_idx] * sizeof(*search_ranks));
263 proc_sphere_part, vertices[grid_idx], num_vertices[grid_idx],
264 search_ranks);
265 for (size_t i = 0; i < num_vertices[grid_idx]; ++i)
266 if (search_ranks[i] != vertex_ranks_out[grid_idx][i])
267 PUT_ERR("error in yac_proc_sphere_part_do_point_search");
268 free(search_ranks);
269
270 if (with_grid[grid_idx] && !with_global_ids[grid_idx]) {
271
272 int ref_num_global_ids = 37;
273 int min_global_id = INT_MAX, max_global_id = INT_MIN;
274 int global_id_count[ref_num_global_ids];
275
276 for (int i = 0; i < ref_num_global_ids; ++i)
277 global_id_count[i] = 0;
278
279 for (size_t i = 0; i < num_vertices_[grid_idx]; ++i) {
280 if ((int)(global_vertex_ids_out[grid_idx][i]) < min_global_id)
281 min_global_id = (int)(global_vertex_ids_out[grid_idx][i]);
282 if ((int)(global_vertex_ids_out[grid_idx][i]) > max_global_id)
283 max_global_id = (int)(global_vertex_ids_out[grid_idx][i]);
284 global_id_count[global_vertex_ids_out[grid_idx][i]]++;
285 }
286
287 MPI_Allreduce(
288 MPI_IN_PLACE, &min_global_id, 1, MPI_INT, MPI_MIN,
289 MPI_COMM_WORLD);
290 MPI_Allreduce(
291 MPI_IN_PLACE, &max_global_id, 1, MPI_INT, MPI_MAX,
292 MPI_COMM_WORLD);
293 MPI_Allreduce(
294 MPI_IN_PLACE, global_id_count, ref_num_global_ids,
295 MPI_INT, MPI_SUM, MPI_COMM_WORLD);
296
297 if (min_global_id != 0)
298 PUT_ERR("error in yac_proc_sphere_part_new (min_global_id)");
299 if (max_global_id != ref_num_global_ids-1)
300 PUT_ERR("error in yac_proc_sphere_part_new (min_global_id)");
301
302 int ref_num_owners_per_vertex[2][5][7*4] =
303 {{{7,7,7,7,7,7,7,
304 1,1,1,1,1,1,1,
305 1,1,1,1,1,1,1,
306 2,2,2,3,2,2,2},
307 {2,2,2,3, 1,1,1,2, 1,1,1,2, 8,8,8,8},
308 {3,2,2,2, 2,1,1,1, 2,1,1,1, 8,8,8,8},
309 {-1},{-1}},
310 {{-1},{-1},
311 {8,8,8,8, 1,1,1,2, 1,1,1,2, 2,2,2,3},
312 {8,8,8,8, 2,1,1,1, 2,1,1,1, 3,2,2,2},
313 {2,2,2,3,2,2,2,
314 1,1,1,1,1,1,1,
315 1,1,1,1,1,1,1,
316 7,7,7,7,7,7,7}}};
317 for (size_t i = 0; i < num_vertices_[grid_idx]; ++i)
318 if (ref_num_owners_per_vertex[grid_idx][comm_rank][i] !=
319 global_id_count[global_vertex_ids_out[grid_idx][i]])
320 PUT_ERR(
321 "error in yac_proc_sphere_part_new (global_id count)");
322 }
323 }
324
325 for (int grid_idx = 0; grid_idx < 2; ++grid_idx) {
326 free(vertex_ranks_out[grid_idx]);
327 if (!with_global_ids[grid_idx])
328 free(global_vertex_ids_out[grid_idx]);
329 }
330
331 yac_proc_sphere_part_node_delete(proc_sphere_part);
332
333 } // with_global_ids_b
334 } // with_grid_b
335 } // with_global_ids_a
336 } // with_grid_a
337
338 for (int grid_idx = 0; grid_idx < 2; ++grid_idx) {
339 free(vertices_[grid_idx]);
340 free(global_vertex_ids_in[grid_idx]);
341 }
342 }
343
344 MPI_Finalize();
345
346 return TEST_EXIT_CODE;
347}
unsigned grid_idx[3]
#define YAC_ASSERT(exp, msg)
static void LLtoXYZ_deg(double lon, double lat, double p_out[])
Definition geometry.h:278
void yac_proc_sphere_part_do_bnd_circle_search(struct proc_sphere_part_node *node, struct bounding_circle bnd_circle, int *ranks, int *rank_count)
void yac_proc_sphere_part_do_point_search(struct proc_sphere_part_node *node, yac_coordinate_pointer search_coords, size_t count, int *ranks)
void yac_proc_sphere_part_node_delete(struct proc_sphere_part_node *node)
void yac_proc_sphere_part_new(yac_coordinate_pointer vertex_coordinates[2], size_t *num_vertices, struct proc_sphere_part_node **proc_sphere_part, yac_int **global_vertex_ids_[2], int **vertex_ranks[2], MPI_Comm comm)
#define xmalloc(size)
Definition ppm_xfuncs.h:66
Inner node of the process sphere partition tree.
#define TEST_EXIT_CODE
Definition tests.h:15
#define PUT_ERR(string)
Definition tests.h:10
YAC_INT yac_int
Definition yac_types.h:15
double(* yac_coordinate_pointer)[3]
Definition yac_types.h:21