YAC 3.21.0
Yet Another Coupler
Loading...
Searching...
No Matches
perf_toy_common.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 <string.h>
8#include <stdarg.h>
9
10#include "yac.h"
11#include "yac_utils.h"
12#include "perf_toy_common.h"
13
14struct timer_data {
15 char * user_string;
16 MPI_Comm comm;
17 double tic;
18};
19
20void * start_timer(MPI_Comm comm, char const * fmt, ...) {
21
22 // generate user string for timer label
23 va_list args1;
24 va_start(args1, fmt);
25 va_list args2;
26 va_copy(args2, args1);
27 char buf[1 + vsnprintf(NULL, 0, fmt, args1)];
28 va_end(args1);
29 vsnprintf(buf, sizeof buf, fmt, args2);
30 va_end(args2);
31
32 // wait for all processes to reach this point before starting the timer
33 MPI_Barrier(comm);
34
35 // allocate and initialize timer data
36 struct timer_data * timer = malloc(1 * sizeof(*timer));
37 timer->user_string = strdup(buf);
38 timer->comm = comm;
39 timer->tic = MPI_Wtime();
40
41 return (void*)timer;
42}
43
44void stop_timer(void * timer_) {
45
46 struct timer_data * timer = (struct timer_data*)timer_;
47
48 // Measure the elapsed time since the timer was started
49 int rank, size;
50 MPI_Comm_rank(timer->comm, &rank);
51 MPI_Comm_size(timer->comm, &size);
52 struct {
53 double value;
54 int rank;
55 } time, time_min, time_max;
56 double time_sum;
57 time.value = MPI_Wtime() - timer->tic;
58 time.rank = rank;
59
60 // Reduce the timing results to find the minimum, maximum, and average times
61 // across all processes
62 // (also acts as a barrier to ensure all processes have finished timing)
63 MPI_Reduce(&time, &time_min, 1, MPI_DOUBLE_INT, MPI_MINLOC, 0, timer->comm);
64 MPI_Reduce(&time, &time_max, 1, MPI_DOUBLE_INT, MPI_MAXLOC, 0, timer->comm);
65 MPI_Reduce(&time.value, &time_sum, 1, MPI_DOUBLE, MPI_SUM, 0, timer->comm);
66
67 // Print the timing results on the root process
68 if (!rank) {
69 fprintf(
70 stderr, "%s;%e;%e;%e\n",
71 timer->user_string, time_sum / (double)size,
72 time_min.value, time_max.value);
73 }
74
75 // cleanup
76 free(timer->user_string);
77 free(timer);
78}
79
81 int comp_id, char const * grid_name, char const * grid_filename,
82 int * cell_point_id, int * corner_point_id) {
83
84 // get component communicator
85 MPI_Comm comp_comm;
86 yac_cget_comp_comm(comp_id, &comp_comm);
87
88 // get basic grid data
89 int num_vertices;
90 int num_cells;
91 int * num_vertices_per_cell;
92 int * cell_to_vertex;
93 double * x_vertices;
94 double * y_vertices;
95 double * x_cells;
96 double * y_cells;
97
98 int * cell_mask;
99 int * global_cell_ids;
100 int * cell_core_mask;
101 int * global_vertex_ids;
102 int * vertex_core_mask;
103
104 // read the grid information in parallel
105 // (global grid is automatically evenly distributed among all component
106 // processes based on the global index of the cells and vertices)
107 void * read_timer =
109 comp_comm, "%s;%s", "read_grid", grid_filename);
111 grid_filename, comp_comm, &num_vertices, &num_cells,
112 &num_vertices_per_cell, &cell_to_vertex, &global_cell_ids,
113 &cell_core_mask, &global_vertex_ids, &vertex_core_mask,
114 &x_vertices, &y_vertices, &x_cells, &y_cells, &cell_mask);
115 stop_timer(read_timer);
116
117 // returned cell core mask arrays contain -1 for all locally owned cells
118 // and the rank of the owner for all halo cells. Convert them to
119 // boolean arrays with 1 for locally owned cells and 0 for halo.
120 for (int i = 0; i < num_cells; ++i)
121 cell_core_mask[i] = cell_core_mask[i] == -1;
122
123 // mark all vertices as core vertices that are used by at least one core cell
124 memset(vertex_core_mask, 0, num_vertices * sizeof(*vertex_core_mask));
125 for (int i = 0, offset = 0; i < num_cells; ++i) {
126 if (cell_core_mask[i]) {
127 for (int j = 0; j < num_vertices_per_cell[i]; ++j) {
128 int vertex_idx = cell_to_vertex[offset + j];
129 vertex_core_mask[vertex_idx] = 1;
130 }
131 }
132 offset += num_vertices_per_cell[i];
133 }
134
135 // define grid
136 int grid_id;
138 grid_name, num_vertices, num_cells, num_vertices_per_cell,
139 x_vertices, y_vertices, cell_to_vertex, &grid_id);
140
141 // set global ids and core mask
146
147 // define points (cell centers and vertices)
150 x_cells, y_cells, cell_point_id);
152 grid_id, num_vertices, YAC_LOCATION_CORNER,
153 x_vertices, y_vertices, corner_point_id);
154
155 // cleanup
156 free(num_vertices_per_cell);
157 free(cell_to_vertex);
158 free(x_cells);
159 free(y_cells);
160 free(x_vertices);
161 free(y_vertices);
162 free(cell_mask);
163 free(global_cell_ids);
164 free(cell_core_mask);
165 free(global_vertex_ids);
166 free(vertex_core_mask);
167
168 MPI_Comm_free(&comp_comm);
169}
struct @23::@24 value
void * start_timer(MPI_Comm comm, char const *fmt,...)
Starts a timer and returns a pointer to the timer data.
void init_grid(int comp_id, char const *grid_name, char const *grid_filename, int *cell_point_id, int *corner_point_id)
Initializes grid data and registers it with YAC.
void stop_timer(void *timer_)
Stops the timer and prints the timing results.
void yac_read_icon_grid_information_parallel(const char *filename, MPI_Comm comm, int *nbr_vertices, int *nbr_cells, int **num_vertices_per_cell, int **cell_to_vertex, int **global_cell_ids, int **cell_owner, int **global_vertex_ids, int **vertex_owner, double **x_vertices, double **y_vertices, double **x_cells, double **y_cells, int **cell_msk)
char * user_string
user string for timer label
double tic
start time (MPI_Wtime)
MPI_Comm comm
MPI communicator.
int * cell_to_vertex
int * cell_mask
size_t num_cells[2]
int * cell_core_mask
int grid_id
int cell_point_id
int comp_id
void yac_cset_global_index(int const *global_index, int location, int grid_id)
Definition yac.c:5726
int const YAC_LOCATION_CELL
Definition yac.c:38
int const YAC_LOCATION_CORNER
Definition yac.c:39
void yac_cget_comp_comm(int comp_id, MPI_Comm *comp_comm)
Definition yac.c:1132
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:5547
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:1639
void yac_cset_core_mask(int const *is_core, int location, int grid_id)
Definition yac.c:5742