YAC 3.14.0
Yet Another Coupler
Loading...
Searching...
No Matches
perf_toy_cube.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// #define VERBOSE
6
7#include <mpi.h>
8#include <yaxt.h>
9#include <stdlib.h>
10#include <stdio.h>
11#include <unistd.h>
12#include <math.h>
13#include "yac.h"
14#include "yac_utils.h"
15
16/* ------------------------------------------------- */
17
18const char * fieldName[] = {"icon_out", "cube_out"};
19
20// redefine YAC assert macros
21#undef YAC_ASSERT
22#define STR_USAGE "Usage: %s -c configFilename -n cube edge length\n"
23#define YAC_ASSERT(exp, msg) \
24 { \
25 if(!((exp))) { \
26 fprintf(stderr, "ERROR: %s\n" STR_USAGE, msg, argv[0]); \
27 exit(EXIT_FAILURE); \
28 } \
29 }
30
31static void parse_arguments(
32 int argc, char ** argv, char const ** configFilename, size_t * cube_n);
33
34int main (int argc, char *argv[]) {
35
36 // Initialisation of MPI
37
38 MPI_Init (NULL, NULL);
39
40 char const * configFilename = "perf_toy.yaml"; // default configuration file
41 size_t cube_n = 405; //default cube edge length
42 parse_arguments(argc, argv, &configFilename, &cube_n);
43
44 xt_initialize(MPI_COMM_WORLD);
45
46 /* The initialisation phase includes the reading of the
47 * coupling configuration */
48#ifdef VERBOSE
49 printf (". main: calling yac_cinit\n");
50#endif
51
52 double tic, toc, time;
53
54 MPI_Barrier(MPI_COMM_WORLD);
55
56 tic=MPI_Wtime();
57
58 // move yaml file to node cache
59 {
60 FILE *f = fopen(configFilename, "rb");
61 fseek(f, 0, SEEK_END);
62 long fsize = ftell(f);
63 fseek(f, 0, SEEK_SET); /* same as rewind(f); */
64
65 char *string = malloc(fsize + 1);
66 size_t dummy = fread(string, 1, fsize, f);
67 (void)(dummy); //UNUSED
68 fclose(f);
69 free(string);
70 }
71
72 yac_cinit ();
73 yac_cread_config_yaml(configFilename);
74
75 /* The usual component definition, here for two sequential components on the
76 same process */
77
78#ifdef VERBOSE
79 printf (". main: calling yac_cdef_comp\n");
80#endif
81
82 int comp_id;
83 char * comp_name = "CUBE";
84
85 yac_cdef_comp ( comp_name, &comp_id );
86#ifdef VERBOSE
87 printf(". main: defined %s with local comp ID %i \n", comp_name, comp_id);
88#endif
89
90 MPI_Comm local_comm;
91
92 yac_cget_comp_comm(comp_id, &local_comm);
93
94 int rank, size;
95
96 MPI_Comm_rank(local_comm,&rank);
97 MPI_Comm_size(local_comm,&size);
98
99 MPI_Comm_free(&local_comm);
100
101
102 int cell_point_id;
103 int corner_point_id;
104
105 int field_ids[2];
106 int grid_id;
107
108 unsigned nbr_vertices;
109 unsigned nbr_cells;
110 unsigned * num_vertices_per_cell;
111 unsigned * cell_to_vertex;
112 double * x_vertices;
113 double * y_vertices;
114 double * x_cells;
115 double * y_cells;
116
117 int * global_cell_id;
118 int * global_cell_id_rank;
119 int * global_corner_id;
120 int * global_corner_id_rank;
121
123 (unsigned)cube_n, &nbr_vertices, &nbr_cells,
124 &num_vertices_per_cell, &cell_to_vertex,
125 &x_vertices, &y_vertices, &x_cells,
126 &y_cells, &global_cell_id,
127 &global_cell_id_rank, &global_corner_id,
128 &global_corner_id_rank, rank, size);
129
130 double * x_points, * y_points;
131
132 x_points = x_vertices;
133 y_points = y_vertices;
134
135 double * x_center, * y_center;
136
137 x_center = x_cells;
138 y_center = y_cells;
139
141 "cube_grid", nbr_vertices, nbr_cells, (int*)num_vertices_per_cell,
142 x_vertices, y_vertices, (int*)cell_to_vertex, &grid_id);
143
144 int * cell_core_mask = malloc(nbr_cells * sizeof(*cell_core_mask));
145 int * corner_core_mask = malloc(nbr_vertices * sizeof(*corner_core_mask));
146 for (unsigned i = 0; i < nbr_cells; ++i)
147 cell_core_mask[i] = global_cell_id_rank[i] == rank;
148 for (unsigned i = 0; i < nbr_vertices; ++i)
149 corner_core_mask[i] = global_corner_id_rank[i] == rank;
150
154
156 grid_id, nbr_cells, YAC_LOCATION_CELL, x_center, y_center, &cell_point_id);
158 grid_id, nbr_vertices, YAC_LOCATION_CORNER, x_points, y_points, &corner_point_id);
159
160 /* Field definition for the component */
161
164 &field_ids[0]);
167 &field_ids[1]);
168
169 toc=MPI_Wtime();
170 time = toc-tic;
171 printf ("CUBE: Time for initialisation %f\n", time );
172
173 /* Search. */
174
175#ifdef VERBOSE
176 printf (". main: calling yac_cenddef\n");
177#endif
178
179 MPI_Barrier(MPI_COMM_WORLD);
180
181 tic=MPI_Wtime();
182
183 yac_cenddef( );
184
185 toc=MPI_Wtime();
186 time = toc-tic;
187 printf("CUBE: Time for search %f\n", time );
188
189 double * conserv_in = malloc(nbr_cells * sizeof(*conserv_in));
190 double * avg_in = malloc(nbr_vertices * sizeof(*avg_in));
191
192 for (unsigned i = 0; i < nbr_cells; ++i)
193 conserv_in[i] = -10;
194 for (unsigned i = 0; i < nbr_vertices; ++i) avg_in[i] = -10;
195
196 double * cell_out = malloc(nbr_cells * sizeof(*cell_out));
197 double * vertex_out = malloc(nbr_vertices * sizeof(*vertex_out));
198
199 int err;
200 int info;
201
202 for (unsigned i = 0; i < nbr_cells; ++i)
203 cell_out[i] = yac_test_func(x_center[i], y_center[i]);
204 for (unsigned i = 0; i < nbr_vertices; ++i)
205 vertex_out[i] = yac_test_func(x_vertices[i], y_vertices[i]);
206
207 MPI_Barrier(MPI_COMM_WORLD);
208
209 tic=MPI_Wtime();
210
211 {
212 double *point_set_data[1];
213 double **collection_data[1] = {point_set_data};
214
215 point_set_data[0] = cell_out;
216 yac_cput(field_ids[1], 1, collection_data, &info, &err);
217 }
218
219 {
220 double *collection_data[1] = {conserv_in};
221
222 yac_cget(field_ids[0], 1, collection_data, &info, &err);
223 }
224
225 toc=MPI_Wtime();
226 time = toc-tic;
227 printf ("CUBE: Time for ping-pong %f\n", time );
228
229#ifdef VTK_OUTPUT
230 //----------------------------------------------------------
231 // write field to vtk output file
232 //----------------------------------------------------------
233
234 char vtk_filename[32];
235
236 sprintf(vtk_filename, "perf_toy_cube_%d.vtk", rank);
237
238 double point_data[nbr_vertices][3];
239 for (int i = 0; i < nbr_vertices; ++i) {
240 LLtoXYZ(x_vertices[i], y_vertices[i], point_data[i]);
241 }
242
243 YAC_VTK_FILE *vtk_file = yac_vtk_open(vtk_filename, "cube_out");
244 yac_vtk_write_point_data(vtk_file, (double *)point_data, nbr_vertices);
245 yac_vtk_write_cell_data(vtk_file, (unsigned *)cell_to_vertex,
246 (unsigned*)num_vertices_per_cell, nbr_cells);
248 vtk_file, corner_core_mask, nbr_vertices, "corner_core_mask");
250 vtk_file, global_corner_id, nbr_vertices, "global_corner_id");
252 vtk_file, cell_core_mask, nbr_cells, "cell_core_mask");
254 vtk_file, global_cell_id, nbr_cells, "global_cell_id");
255
257 vtk_file, conserv_in, nbr_cells, "conserv_in");
259 vtk_file, cell_out, nbr_cells, "cell_out");
261 vtk_file, avg_in, nbr_vertices, "avg_in");
263 vtk_file, vertex_out, nbr_vertices, "vertex_out");
264
265 yac_vtk_close(vtk_file);
266#endif // VTK_OUTPUT
267
268 free(corner_core_mask);
269 free(global_corner_id_rank);
270 free(global_corner_id);
271 free(cell_core_mask);
272 free(global_cell_id_rank);
273 free(global_cell_id);
274 free(x_cells);
275 free(y_cells);
276 free(x_vertices);
277 free(y_vertices);
278 free(num_vertices_per_cell);
279 free(cell_to_vertex);
280
282
283 xt_finalize();
284
285 MPI_Finalize();
286
287 free(conserv_in);
288 free(cell_out);
289 free(avg_in);
290 free(vertex_out);
291
292 return EXIT_SUCCESS;
293}
294
295static void parse_arguments(
296 int argc, char ** argv,
297 char const ** configFilename, size_t * cube_n) {
298
299 int opt;
300 while ((opt = getopt(argc, argv, "c:n:")) != -1) {
301 YAC_ASSERT((opt == 'c') || (opt == 'n'), "invalid command argument")
302 switch (opt) {
303 default:
304 case 'c':
305 *configFilename = optarg;
306 break;
307 case 'n':
308 *cube_n = atoi(optarg);
309 YAC_ASSERT(*cube_n > 0, "invalid cube edge length");
310 break;
311 }
312 }
313}
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)
static void parse_arguments(int argc, char **argv, char const **configFilename, size_t *cube_n)
const char * fieldName[]
#define YAC_ASSERT(exp, msg)
int * cell_to_vertex
double yac_test_func(double lon, double lat)
int info
int * cell_core_mask
int grid_id
int cell_point_id
int comp_id
static void LLtoXYZ(double lon, double lat, double p_out[])
Definition toy_scrip.c:587
void yac_vtk_write_cell_scalars_double(YAC_VTK_FILE *vtk_file, double *scalars, unsigned num_cells, char *name)
Definition vtk_output.c:264
void yac_vtk_write_cell_scalars_int(YAC_VTK_FILE *vtk_file, int *scalars, unsigned num_cells, char *name)
Definition vtk_output.c:250
void yac_vtk_write_point_scalars_double(YAC_VTK_FILE *vtk_file, double *scalars, unsigned num_points, char *name)
Definition vtk_output.c:292
void yac_vtk_write_point_data(YAC_VTK_FILE *vtk_file, double *point_data, unsigned num_points)
Definition vtk_output.c:69
void yac_vtk_write_point_scalars_int(YAC_VTK_FILE *vtk_file, int *scalars, unsigned num_points, char *name)
Definition vtk_output.c:278
void yac_vtk_close(YAC_VTK_FILE *vtk_file)
Definition vtk_output.c:403
void yac_vtk_write_cell_data(YAC_VTK_FILE *vtk_file, unsigned *cell_corners, unsigned *num_points_per_cell, unsigned num_cells)
Definition vtk_output.c:88
YAC_VTK_FILE * yac_vtk_open(const char *filename, const char *title)
Definition vtk_output.c:45
void yac_cenddef(void)
Definition yac.c:4414
void yac_cset_global_index(int const *global_index, int location, int grid_id)
Definition yac.c:5063
int const YAC_LOCATION_CELL
Definition yac.c:34
void yac_cinit(void)
Definition yac.c:514
void yac_cfinalize()
Finalises YAC.
Definition yac.c:740
int const YAC_LOCATION_CORNER
Definition yac.c:35
void yac_cget_comp_comm(int comp_id, MPI_Comm *comp_comm)
Definition yac.c:856
void yac_cget(int const field_id, int collection_size, double **recv_field, int *info, int *ierr)
Definition yac.c:2759
int const YAC_TIME_UNIT_SECOND
Definition yac.c:59
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:4881
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:1181
void yac_cput(int const field_id, int const collection_size, double ***const send_field, int *info, int *ierr)
Definition yac.c:3721
void yac_cread_config_yaml(const char *yaml_filename)
Definition yac.c:595
void yac_cset_core_mask(int const *is_core, int location, int grid_id)
Definition yac.c:5079
void yac_cdef_comp(char const *comp_name, int *comp_id)
Definition yac.c:1013
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:1396