This example tests multithreading with YAC.
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <mpi.h>
int comp_id, grid_id, point_id, field_id1, field_id2, interp_stack_id;
void defs(const char* comp_name){
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};
x_vertices, y_vertices, &point_id );
comp_id,
&point_id,
1,
1,
"1",
&field_id1);
comp_id,
&point_id,
1,
1,
"1",
&field_id2);
"compB", "compB", "field1",
interp_stack_id, 0, 0);
"compB", "compB", "field2",
interp_stack_id, 0, 0);
}
void * put_field_loop(void* ptr_field_id_){
int info = 0, ierror;
double buffer[9];
int* ptr_field_id = ptr_field_id_;
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);
}
return NULL;
}
void compA(){
defs("compA");
pthread_t thread1, thread2;
pthread_create( &thread1, NULL, &put_field_loop, (void*) &field_id1);
pthread_create( &thread2, NULL, &put_field_loop, (void*) &field_id2);
pthread_join( thread1, NULL);
pthread_join( thread2, NULL);
}
void * get_field_loop(void* ptr_field_id_){
int info = 0, ierror;
double buffer[9];
int* ptr_field_id = ptr_field_id_;
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);
}
return NULL;
}
void compB(){
defs("compB");
pthread_t thread1, thread2;
pthread_create( &thread1, NULL, &get_field_loop, (void*) &field_id1);
pthread_create( &thread2, NULL, &get_field_loop, (void*) &field_id2);
pthread_join( thread1, NULL);
pthread_join( thread2, NULL);
}
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_cget_(int const field_id, int const collection_size, double *recv_field, int *info, int *ierr)
void yac_cdef_datetime(const char *start_datetime, const char *end_datetime)
int const YAC_LOCATION_CORNER
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)
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)
int const YAC_TIME_UNIT_SECOND
void yac_cdef_calendar(int calendar)
void yac_cput_(int const field_id, int const collection_size, double *send_field, int *info, int *ierr)
int const YAC_PROLEPTIC_GREGORIAN
void yac_cadd_interp_stack_config_nnn(int interp_stack_config_id, int type, size_t n, double max_search_distance, double scale)
void yac_cget_interp_stack_config(int *interp_stack_config_id)
void yac_cdef_comp(char const *comp_name, int *comp_id)
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)
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)
int const YAC_REDUCTION_TIME_NONE