YetAnotherCoupler 3.2.0_a
Loading...
Searching...
No Matches
test_multithreading.c

This example tests multithreading with YAC.

// Copyright (c) 2024 The YAC Authors
//
// SPDX-License-Identifier: BSD-3-Clause
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <mpi.h>
#include "yac.h"
int comp_id, grid_id, point_id, field_id1, field_id2, interp_stack_id;
void defs(const char* comp_name){
yac_cdef_comp(comp_name, &comp_id);
int nbr_vertices[2] = {3, 3};
int cyclic[2] = {0,0};
double x_vertices[3] = {-1, 0, 1};
double y_vertices[3] = {-1, 0, 1};
yac_cdef_grid_reg2d(comp_name, nbr_vertices, cyclic, x_vertices, y_vertices, &grid_id);
x_vertices, y_vertices, &point_id );
yac_cdef_field ( "field1",
comp_id,
&point_id,
1,
1,
"1",
&field_id1);
yac_cdef_field ( "field2",
comp_id,
&point_id,
1,
1,
"1",
&field_id2);
yac_cget_interp_stack_config(&interp_stack_id);
interp_stack_id, YAC_NNN_AVG, 1, 1.0);
yac_cdef_couple("compA", "compA", "field1",
"compB", "compB", "field1",
interp_stack_id, 0, 0);
yac_cdef_couple("compA", "compA", "field2",
"compB", "compB", "field2",
interp_stack_id, 0, 0);
}
void put_field_loop(int* ptr_field_id){
int info = 0, ierror;
double buffer[9];
for(int i = 0; i<60; ++i){
printf("put: field_id: %d, i: %d\n", *ptr_field_id, i);
buffer[5] = *ptr_field_id + i;
yac_cput_(*ptr_field_id, 1, buffer, &info, &ierror);
}
}
void compA(){
defs("compA");
pthread_t thread1, thread2;
pthread_create( &thread1, NULL, (void*(*)(void*)) put_field_loop, (void*) &field_id1);
pthread_create( &thread2, NULL, (void*(*)(void*)) put_field_loop, (void*) &field_id2);
pthread_join( thread1, NULL);
pthread_join( thread2, NULL);
}
void get_field_loop(int* ptr_field_id){
int info = 0, ierror;
double buffer[9];
for(int i = 0; i<60; ++i){
printf("get: field_id: %d, i: %d\n", *ptr_field_id, i);
yac_cget_(*ptr_field_id, 1, buffer, &info, &ierror);
if(buffer[5] != *ptr_field_id + i)
exit(1);
}
}
void compB(){
defs("compB");
pthread_t thread1, thread2;
pthread_create( &thread1, NULL, (void*(*)(void*)) get_field_loop, (void*) &field_id1);
pthread_create( &thread2, NULL, (void*(*)(void*)) get_field_loop, (void*) &field_id2);
pthread_join( thread1, NULL);
pthread_join( thread2, NULL);
}
int main(void) {
int provided;
MPI_Init_thread(NULL, NULL, MPI_THREAD_MULTIPLE, &provided);
if(provided < MPI_THREAD_MULTIPLE){
printf("Skip test due to not compatible MPI (MPI_Query_thread: %d)\n", provided);
return 77;
}
"2023-01-09T14:20:00",
"2023-01-09T14:21:00" );
int rank;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if(rank%2 == 0)
compA();
else
compB();
MPI_Finalize();
return 0;
}
int main(int argc, char **argv)
void yac_cenddef(void)
Definition yac.c:2840
void yac_cget_(int const field_id, int const collection_size, double *recv_field, int *info, int *ierr)
Definition yac.c:1832
void yac_cdef_datetime(const char *start_datetime, const char *end_datetime)
Definition yac.c:554
void yac_csync_def(void)
Definition yac.c:2826
void yac_cinit(void)
Definition yac.c:402
void yac_cfinalize()
Definition yac.c:533
int const YAC_LOCATION_CORNER
Definition yac.c:28
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:3172
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:845
void yac_cadd_interp_stack_config_nnn(int interp_stack_config_id, int type, size_t n, double scale)
Definition yac.c:3419
int const YAC_TIME_UNIT_SECOND
Definition yac.c:52
void yac_cdef_calendar(int calendar)
Definition yac.c:562
void yac_cput_(int const field_id, int const collection_size, double *send_field, int *info, int *ierr)
Definition yac.c:1917
int const YAC_PROLEPTIC_GREGORIAN
Definition yac.c:61
int const YAC_NNN_AVG
Definition yac.c:72
void yac_cget_interp_stack_config(int *interp_stack_config_id)
Definition yac.c:3348
void yac_cdef_comp(char const *comp_name, int *comp_id)
Definition yac.c:795
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:1085
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:1476
int const YAC_REDUCTION_TIME_NONE
Definition yac.c:45