YAC 3.13.0
Yet Another Coupler
Loading...
Searching...
No Matches
test_multithreading.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 <stdio.h>
6#include <stdlib.h>
7#include <pthread.h>
8
9#include <mpi.h>
10
11#include "yac.h"
12
19
20void utest_defs(const char* comp_name){
21
22 yac_cdef_comp(comp_name, &comp_id);
23
24 int nbr_vertices[2] = {3, 3};
25 int cyclic[2] = {0,0};
26 double x_vertices[3] = {-1, 0, 1};
27 double y_vertices[3] = {-1, 0, 1};
28 yac_cdef_grid_reg2d(comp_name, nbr_vertices, cyclic, x_vertices, y_vertices, &grid_id);
29
31 x_vertices, y_vertices, &point_id );
32
33 yac_cdef_field ( "field1",
34 comp_id,
35 &point_id,
36 1,
37 1,
38 "1",
40 &field_id1);
41 yac_cdef_field ( "field2",
42 comp_id,
43 &point_id,
44 1,
45 1,
46 "1",
48 &field_id2);
49
51
54 interp_stack_id, YAC_NNN_AVG, 1, 0.0, 1.0);
55
56 yac_cdef_couple("compA", "compA", "field1",
57 "compB", "compB", "field1",
59 interp_stack_id, 0, 0);
60 yac_cdef_couple("compA", "compA", "field2",
61 "compB", "compB", "field2",
63 interp_stack_id, 0, 0);
65}
66
67void * utest_put_field_loop(void* ptr_field_id_){
68 int info = 0, ierror;
69 double buffer[9];
70 int* ptr_field_id = ptr_field_id_;
71 for(int i = 0; i<60; ++i){
72 printf("put: field_id: %d, i: %d\n", *ptr_field_id, i);
73 buffer[5] = *ptr_field_id + i;
74 yac_cput_(*ptr_field_id, 1, buffer, &info, &ierror);
75 }
76 return NULL;
77}
78
79void utest_compA(){
80 utest_defs("compA");
81 pthread_t thread1, thread2;
82 pthread_create( &thread1, NULL, &utest_put_field_loop, (void*) &field_id1);
83 pthread_create( &thread2, NULL, &utest_put_field_loop, (void*) &field_id2);
84
85 pthread_join( thread1, NULL);
86 pthread_join( thread2, NULL);
87}
88
89void * utest_get_field_loop(void* ptr_field_id_){
90 int info = 0, ierror;
91 double buffer[9];
92 int* ptr_field_id = ptr_field_id_;
93 for(int i = 0; i<60; ++i){
94 printf("get: field_id: %d, i: %d\n", *ptr_field_id, i);
95 yac_cget_(*ptr_field_id, 1, buffer, &info, &ierror);
96 if(buffer[5] != *ptr_field_id + i)
97 exit(1);
98 }
99 return NULL;
100}
101
102void utest_compB(){
103 utest_defs("compB");
104 pthread_t thread1, thread2;
105 pthread_create( &thread1, NULL, &utest_get_field_loop, (void*) &field_id1);
106 pthread_create( &thread2, NULL, &utest_get_field_loop, (void*) &field_id2);
107
108 pthread_join( thread1, NULL);
109 pthread_join( thread2, NULL);
110}
111
112int main(void) {
113 int provided;
114 MPI_Init_thread(NULL, NULL, MPI_THREAD_MULTIPLE, &provided);
115
116 if(provided < MPI_THREAD_MULTIPLE){
117 printf("Skip test due to not compatible MPI (MPI_Query_thread: %d)\n", provided);
118 return 77;
119 }
120
121 yac_cinit();
124 "2023-01-09T14:20:00",
125 "2023-01-09T14:21:00" );
126 int rank;
127 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
128 if(rank%2 == 0)
129 utest_compA();
130 else
131 utest_compB();
132
134 MPI_Finalize();
135 return 0;
136}
unsigned cyclic[2]
int interp_stack_id
int grid_id
int point_id
int field_id2
int field_id1
int comp_id
double * buffer
int ierror
void yac_cenddef(void)
Definition yac.c:4400
void yac_cget_(int const field_id, int const collection_size, double *recv_field, int *info, int *ierr)
Definition yac.c:2741
void yac_cdef_datetime(const char *start_datetime, const char *end_datetime)
Definition yac.c:761
void yac_csync_def(void)
Definition yac.c:4385
void yac_cinit(void)
Definition yac.c:510
void yac_cfinalize()
Finalises YAC.
Definition yac.c:740
int const YAC_LOCATION_CORNER
Definition yac.c:35
void yac_cdef_grid_reg2d(const char *grid_name, int nbr_vertices[2], int cyclic[2], double *x_vertices, double *y_vertices, int *grid_id)
Definition yac.c:4831
void yac_cdef_points_reg2d(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:1103
int const YAC_TIME_UNIT_SECOND
Definition yac.c:59
void yac_cdef_calendar(int calendar)
Definition yac.c:769
void yac_cput_(int const field_id, int const collection_size, double *send_field, int *info, int *ierr)
Definition yac.c:3063
int const YAC_PROLEPTIC_GREGORIAN
Definition yac.c:68
int const YAC_NNN_AVG
Definition yac.c:79
void yac_cadd_interp_stack_config_nnn(int interp_stack_config_id, int type, size_t n, double max_search_distance, double scale)
Definition yac.c:5208
void yac_cget_interp_stack_config(int *interp_stack_config_id)
Definition yac.c:5137
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
void yac_cdef_couple(char const *src_comp_name, char const *src_grid_name, char const *src_field_name, char const *tgt_comp_name, char const *tgt_grid_name, char const *tgt_field_name, char const *coupling_timestep, int time_unit, int time_reduction, int interp_stack_config_id, int src_lag, int tgt_lag)
Definition yac.c:1951
int const YAC_REDUCTION_TIME_NONE
Definition yac.c:52