YAC 3.18.0
Yet Another Coupler
Loading...
Searching...
No Matches
test_mpi_handshake_c.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 <mpi.h>
6
7#include <stdlib.h>
8#include <stdio.h>
9#include <string.h>
10
11#include <yaxt.h>
12
13#include "yac.h"
14#include "tests.h"
15
21int main (void) {
22
23 MPI_Init(NULL, NULL);
24 xt_initialize(MPI_COMM_WORLD);
25
26 int global_rank, global_size;
27 MPI_Comm_rank(MPI_COMM_WORLD, &global_rank);
28 MPI_Comm_size(MPI_COMM_WORLD, &global_size);
29
30 if (global_size != 6) {
31 fprintf(stderr, "Wrong number of processes (should be 6)\n");
32 exit(EXIT_FAILURE);
33 }
34
35 {
36 int color = MPI_UNDEFINED;
37 MPI_Comm group_comm = MPI_COMM_NULL, main_comm = MPI_COMM_NULL;
38 char const * groupMain = "main";
39 char const * groups[2];
40 MPI_Comm comms_out[2];
41 switch (global_rank) {
42 default:
43 case (0):
44 case (1):
45 // first two ranks are part of group A
46 groups[0] = groupMain;
47 groups[1] = "group a";
48 yac_cmpi_handshake(MPI_COMM_WORLD, 2, groups, comms_out);
49 main_comm = comms_out[0];
50 group_comm = comms_out[1];
51 color = 0;
52 break;
53 case (2):
54 case (3):
55 // next two ranks are part of group B
56 groups[0] = "group b";
57 groups[1] = groupMain;
58 yac_cmpi_handshake(MPI_COMM_WORLD, 2, groups, comms_out);
59 group_comm = comms_out[0];
60 main_comm = comms_out[1];
61 color = 1;
62 break;
63 case (4):
64 // rank four does only provide the main group
65 yac_cmpi_handshake(MPI_COMM_WORLD, 1, &groupMain, &main_comm);
66 break;
67 case (5):
68 // rank five does a dummy initialisation
69 yac_cmpi_handshake(MPI_COMM_WORLD, 0, NULL, NULL);
70 break;
71 }
72
73 MPI_Comm ref_group_comm, ref_main_comm;
74 MPI_Comm_split(MPI_COMM_WORLD, color, 0, &ref_group_comm);
75 MPI_Comm_split(MPI_COMM_WORLD, (global_rank == 5)?MPI_UNDEFINED:0, 0,
76 &ref_main_comm);
77
78 if (ref_group_comm != MPI_COMM_NULL) {
79
80 int result;
81 MPI_Comm_compare(ref_group_comm, group_comm, &result);
82
83 if ((result != MPI_CONGRUENT) && (result != MPI_IDENT))
84 PUT_ERR("ERROR in yac_cmpi_handshake");
85
86 MPI_Comm_free(&ref_group_comm);
87 MPI_Comm_free(&group_comm);
88
89 } else if (group_comm != MPI_COMM_NULL) {
90 PUT_ERR("ERROR in yac_cmpi_handshake");
91 }
92
93 if (ref_main_comm != MPI_COMM_NULL) {
94
95 int result;
96 MPI_Comm_compare(ref_main_comm, main_comm, &result);
97
98 if ((result != MPI_CONGRUENT) && (result != MPI_IDENT))
99 PUT_ERR("ERROR in yac_cmpi_handshake");
100
101 MPI_Comm_free(&ref_main_comm);
102 MPI_Comm_free(&main_comm);
103
104 } else if (group_comm != MPI_COMM_NULL) {
105 PUT_ERR("ERROR in yac_cmpi_handshake");
106 }
107 }
108
109 xt_finalize();
110 MPI_Finalize();
111
112 return TEST_EXIT_CODE;
113}
#define TEST_EXIT_CODE
Definition tests.h:15
#define PUT_ERR(string)
Definition tests.h:10
void yac_cmpi_handshake(MPI_Comm comm, size_t n, char const **group_names, MPI_Comm *group_comms)
Definition yac.c:669