ICON Community Interface 0.4.0
Loading...
Searching...
No Matches
yaxt_c_external.c
Go to the documentation of this file.
1/*
2 --------------------------------------------------------------------
3 Example plugin for the ICON Community Interface (ComIn)
4 to demonstrate the usage of YAXT: External process.
5
6 @authors 08/2021 :: ICON Community Interface <icon@dwd.de>
7
8 Note that in order to demonstrate ComIn's language interoperability,
9 a similar plugin has been implemented in Fortran, see the subdirectory
10 "yaxt_fortran".
11 --------------------------------------------------------------------
12 */
13
14#include <stdio.h>
15
16#include <mpi.h>
17#include <mpi_handshake.h>
18#include <yaxt.h>
19
20double mean(double* data, int size) {
21 double sum = 0;
22 for (int i = 0; i < size; ++i)
23 sum += data[i];
24 return sum / size;
25}
26
27int main(int argc, char** argv) {
28
29 MPI_Init(&argc, &argv);
30
31 const char* group_names[] = {"comin", "yaxt_c_external"};
32 MPI_Comm group_comms[2];
33 mpi_handshake(group_names, group_comms, 2, MPI_COMM_WORLD);
34
35 MPI_Comm plugin_comm;
36 const char* plugin_comm_name = "yaxt_c";
37 mpi_handshake(&plugin_comm_name, &plugin_comm, 1, group_comms[0]);
38
39 xt_initialize(plugin_comm);
40
41 int ncells = atoi(argv[1]);
42 struct Xt_stripe stripe = {.start = 1, .stride = 1, .nstrides = ncells};
43 Xt_idxlist tgt_idxlist = xt_idxstripes_new(&stripe, 1);
44 Xt_idxlist empty_idxlist = xt_idxempty_new();
45
46 Xt_xmap xmap = xt_xmap_all2all_new(empty_idxlist, tgt_idxlist, plugin_comm);
47
48 Xt_redist yaxt_redist = xt_redist_p2p_new(xmap, MPI_DOUBLE);
49 xt_xmap_delete(xmap);
50 xt_idxlist_delete(empty_idxlist);
51 xt_idxlist_delete(tgt_idxlist);
52
53 double* buffer = malloc(ncells * sizeof(*buffer));
54 int ntimesteps = atoi(argv[2]);
55 for (int i = 0; i < ntimesteps; ++i) {
56 xt_redist_s_exchange1(yaxt_redist, NULL, buffer);
57 // now we have the whole field available and can compute things
58 printf("mean: %.2lf\n", mean(buffer, ncells));
59 }
60 MPI_Finalize();
61 return 0;
62}
void mpi_handshake(char const **group_names, MPI_Comm *group_comms, size_t n, MPI_Comm comm)
double mean(double *data, int size)
int main(int argc, char **argv)
MPI_Comm plugin_comm
double * buffer
int ncells
Xt_redist yaxt_redist