| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /*********************************************************************************/ | ||
| 2 | /* Copyright 2009-2023 Barcelona Supercomputing Center */ | ||
| 3 | /* */ | ||
| 4 | /* This file is part of the DLB library. */ | ||
| 5 | /* */ | ||
| 6 | /* DLB is free software: you can redistribute it and/or modify */ | ||
| 7 | /* it under the terms of the GNU Lesser General Public License as published by */ | ||
| 8 | /* the Free Software Foundation, either version 3 of the License, or */ | ||
| 9 | /* (at your option) any later version. */ | ||
| 10 | /* */ | ||
| 11 | /* DLB is distributed in the hope that it will be useful, */ | ||
| 12 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ | ||
| 13 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ | ||
| 14 | /* GNU Lesser General Public License for more details. */ | ||
| 15 | /* */ | ||
| 16 | /* You should have received a copy of the GNU Lesser General Public License */ | ||
| 17 | /* along with DLB. If not, see <https://www.gnu.org/licenses/>. */ | ||
| 18 | /*********************************************************************************/ | ||
| 19 | |||
| 20 | #include "unique_shmem.h" | ||
| 21 | |||
| 22 | #include "LB_core/node_barrier.h" | ||
| 23 | #include "LB_core/spd.h" | ||
| 24 | #include "LB_core/DLB_kernel.h" | ||
| 25 | #include "LB_comm/shmem_barrier.h" | ||
| 26 | #include "LB_numThreads/numThreads.h" | ||
| 27 | #include "apis/dlb_errors.h" | ||
| 28 | #include "apis/dlb_types.h" | ||
| 29 | #include "support/atomic.h" | ||
| 30 | #include "support/options.h" | ||
| 31 | |||
| 32 | #include <assert.h> | ||
| 33 | #include <pthread.h> | ||
| 34 | #include <stdint.h> | ||
| 35 | #include <stdlib.h> | ||
| 36 | #include <string.h> | ||
| 37 | #include <unistd.h> | ||
| 38 | |||
| 39 | /* Test node_barrier module integration with LeWI */ | ||
| 40 | |||
| 41 | struct pthread_args { | ||
| 42 | subprocess_descriptor_t *spd; | ||
| 43 | barrier_t *barrier; | ||
| 44 | }; | ||
| 45 | |||
| 46 | 26 | void* barrier_fn(void *arg) { | |
| 47 | 26 | struct pthread_args *pargs = arg; | |
| 48 | 26 | spd_enter_dlb(pargs->spd); | |
| 49 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 26 times.
|
26 | assert( node_barrier(pargs->spd, pargs->barrier) == DLB_SUCCESS ); |
| 50 | 26 | return NULL; | |
| 51 | } | ||
| 52 | |||
| 53 | /* Callback to count how many times the LeWI module has invoked it */ | ||
| 54 | static int lewi_ntimes = 0; | ||
| 55 | 14 | static void cb_count(int num_threads, void *arg) { | |
| 56 | 14 | ++lewi_ntimes; | |
| 57 | 14 | } | |
| 58 | |||
| 59 | typedef enum { | ||
| 60 | CHECK_NTIMES_ZERO, | ||
| 61 | CHECK_NTIMES_POSITIVE, | ||
| 62 | } lewi_ntimes_check_t; | ||
| 63 | |||
| 64 | /* Initialize two spds with the given dlb_flags, perform a barrier with the given | ||
| 65 | * barrier and perform a lewi check */ | ||
| 66 | 13 | void test_barrier_lewi(const char *dlb_flags, const char *barrier_name, | |
| 67 | int barrier_flags, lewi_ntimes_check_t check) { | ||
| 68 | subprocess_descriptor_t spd1; | ||
| 69 | subprocess_descriptor_t spd2; | ||
| 70 | char options[128]; | ||
| 71 | 13 | sprintf(options, "--barrier --lewi-affinity=none --lewi %s --shm-key=%s", | |
| 72 | dlb_flags, SHMEM_KEY); | ||
| 73 | |||
| 74 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
|
13 | assert( Initialize(&spd1, 111, 0, NULL, options) == DLB_SUCCESS ); |
| 75 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
|
13 | assert( Initialize(&spd2, 222, 0, NULL, options) == DLB_SUCCESS ); |
| 76 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
|
13 | assert( pm_callback_set(&spd1.pm, dlb_callback_set_num_threads, |
| 77 | (dlb_callback_t)cb_count, NULL) == DLB_SUCCESS ); | ||
| 78 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
|
13 | assert( pm_callback_set(&spd2.pm, dlb_callback_set_num_threads, |
| 79 | (dlb_callback_t)cb_count, NULL) == DLB_SUCCESS ); | ||
| 80 | |||
| 81 | /* Perform a barrier with two threads of the same process*/ | ||
| 82 | 13 | lewi_ntimes = 0; | |
| 83 | pthread_t thread1, thread2; | ||
| 84 | barrier_t *barrier; | ||
| 85 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 8 times.
|
13 | if (barrier_name == NULL) { |
| 86 | 5 | barrier = NULL; | |
| 87 | } else { | ||
| 88 |
1/2✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
|
8 | if (shmem_barrier__get_max_barriers() > 1) { |
| 89 | 8 | barrier = node_barrier_register(&spd1, barrier_name, barrier_flags); | |
| 90 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
|
8 | assert( barrier != NULL ); |
| 91 | 8 | barrier = node_barrier_register(&spd2, barrier_name, barrier_flags); | |
| 92 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
|
8 | assert( barrier != NULL ); |
| 93 | } else { | ||
| 94 | ✗ | goto finish_dlb; | |
| 95 | } | ||
| 96 | } | ||
| 97 | 13 | struct pthread_args pargs1 = {.spd = &spd1, .barrier = barrier }; | |
| 98 | 13 | pthread_create(&thread1, NULL, barrier_fn, &pargs1); | |
| 99 | 13 | struct pthread_args pargs2 = {.spd = &spd2, .barrier = barrier }; | |
| 100 | 13 | pthread_create(&thread2, NULL, barrier_fn, &pargs2); | |
| 101 | 13 | pthread_join(thread1, NULL); | |
| 102 | 13 | pthread_join(thread2, NULL); | |
| 103 |
2/3✓ Branch 0 taken 6 times.
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
|
13 | switch(check) { |
| 104 | 6 | case CHECK_NTIMES_ZERO: | |
| 105 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | assert( lewi_ntimes == 0 ); break; |
| 106 | 7 | case CHECK_NTIMES_POSITIVE: | |
| 107 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
|
7 | assert( lewi_ntimes > 0 ); break; |
| 108 | }; | ||
| 109 | |||
| 110 | 13 | finish_dlb: | |
| 111 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
|
13 | assert( Finish(&spd1) == DLB_SUCCESS ); |
| 112 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
|
13 | assert( Finish(&spd2) == DLB_SUCCESS ); |
| 113 | 13 | } | |
| 114 | |||
| 115 | 1 | int main(int argc, char *argv[]) { | |
| 116 | |||
| 117 | /* Test default barrier with --lewi-barrier */ | ||
| 118 | 1 | test_barrier_lewi("--lewi-barrier=no", NULL, 0, CHECK_NTIMES_ZERO); | |
| 119 | 1 | test_barrier_lewi("--lewi-barrier", NULL, 0, CHECK_NTIMES_POSITIVE); | |
| 120 | |||
| 121 | /* Test default barrier with --lewi-barrier-select overwriting --lewi-barrier */ | ||
| 122 | 1 | test_barrier_lewi("--lewi-barrier=yes --lewi-barrier-select=barrier1,barrier2", | |
| 123 | NULL, 0, CHECK_NTIMES_ZERO); | ||
| 124 | 1 | test_barrier_lewi("--lewi-barrier=no --lewi-barrier-select=default", | |
| 125 | NULL, 0, CHECK_NTIMES_POSITIVE); | ||
| 126 | 1 | test_barrier_lewi("--lewi-barrier=no --lewi-barrier-select=barrier1,default", | |
| 127 | NULL, 0, CHECK_NTIMES_POSITIVE); | ||
| 128 | |||
| 129 | /* Test named barrier with different values of --lewi-barrier and API flags */ | ||
| 130 | 1 | test_barrier_lewi("--lewi-barrier=no", "barrier1", | |
| 131 | DLB_BARRIER_LEWI_OFF, CHECK_NTIMES_ZERO); | ||
| 132 | 1 | test_barrier_lewi("--lewi-barrier=yes", "barrier1", | |
| 133 | DLB_BARRIER_LEWI_OFF, CHECK_NTIMES_ZERO); | ||
| 134 | 1 | test_barrier_lewi("--lewi-barrier=no", "barrier1", | |
| 135 | DLB_BARRIER_LEWI_ON, CHECK_NTIMES_POSITIVE); | ||
| 136 | 1 | test_barrier_lewi("--lewi-barrier=yes", "barrier1", | |
| 137 | DLB_BARRIER_LEWI_ON, CHECK_NTIMES_POSITIVE); | ||
| 138 | |||
| 139 | /* Test named barrier with selective flag */ | ||
| 140 | 1 | test_barrier_lewi("--lewi-barrier-select=barrier1", "barrier1", | |
| 141 | DLB_BARRIER_LEWI_OFF, CHECK_NTIMES_ZERO); | ||
| 142 | 1 | test_barrier_lewi("--lewi-barrier-select=barrier1", "barrier1", | |
| 143 | DLB_BARRIER_LEWI_RUNTIME, CHECK_NTIMES_POSITIVE); | ||
| 144 | 1 | test_barrier_lewi("--lewi-barrier-select=barrier0,barrier1", "barrier1", | |
| 145 | DLB_BARRIER_LEWI_RUNTIME, CHECK_NTIMES_POSITIVE); | ||
| 146 | 1 | test_barrier_lewi("", "barrier1", | |
| 147 | DLB_BARRIER_LEWI_RUNTIME, CHECK_NTIMES_ZERO); | ||
| 148 | |||
| 149 | 1 | return 0; | |
| 150 | } | ||
| 151 |