Line | Branch | Exec | Source |
---|---|---|---|
1 | /*********************************************************************************/ | ||
2 | /* Copyright 2009-2021 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 "LB_comm/comm_lend_light.h" | ||
21 | |||
22 | #include "LB_comm/shmem.h" | ||
23 | #include "support/tracing.h" | ||
24 | #include "support/debug.h" | ||
25 | #include "support/types.h" | ||
26 | |||
27 | #include <stdlib.h> | ||
28 | |||
29 | int defaultCPUS; | ||
30 | int greedy; | ||
31 | //pointers to the shared memory structures | ||
32 | struct shdata { | ||
33 | int idleCpus; | ||
34 | int attached_nprocs; | ||
35 | }; | ||
36 | |||
37 | struct shdata *shdata; | ||
38 | static shmem_handler_t *shm_handler = NULL; | ||
39 | static const char *shmem_name = "lewi"; | ||
40 | static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; | ||
41 | static int subprocesses_attached = 0; | ||
42 | |||
43 | 1 | static void cleanup_shmem(void *shdata_ptr, int pid) { | |
44 | 1 | struct shdata *shared_data = shdata_ptr; | |
45 | 1 | __sync_fetch_and_sub(&shared_data->attached_nprocs, 1); | |
46 | 1 | } | |
47 | |||
48 | 16 | static bool is_shmem_empty(void) { | |
49 |
2/4✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
|
16 | return shdata && shdata->attached_nprocs == 0; |
50 | } | ||
51 | |||
52 | 29 | static void open_shmem(const char *shmem_key) { | |
53 | 29 | pthread_mutex_lock(&mutex); | |
54 | { | ||
55 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 13 times.
|
29 | if (shm_handler == NULL) { |
56 | 32 | shm_handler = shmem_init((void**)&shdata, | |
57 | 16 | &(const shmem_props_t) { | |
58 | .size = sizeof(struct shdata), | ||
59 | .name = shmem_name, | ||
60 | .key = shmem_key, | ||
61 | .version = SHMEM_VERSION_IGNORE, | ||
62 | .cleanup_fn = cleanup_shmem, | ||
63 | }); | ||
64 | 16 | subprocesses_attached = 1; | |
65 | } else { | ||
66 | 13 | ++subprocesses_attached; | |
67 | } | ||
68 | } | ||
69 | 29 | pthread_mutex_unlock(&mutex); | |
70 | 29 | } | |
71 | |||
72 | 29 | void ConfigShMem(int defCPUS, int is_greedy, const char *shmem_key) { | |
73 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 28 times.
|
29 | verbose(VB_SHMEM, "LoadCommonConfig"); |
74 | 29 | defaultCPUS=defCPUS; | |
75 | 29 | greedy=is_greedy; | |
76 | |||
77 | // Shared memory creation | ||
78 | 29 | open_shmem(shmem_key); | |
79 | |||
80 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 13 times.
|
29 | if (__sync_fetch_and_add(&shdata->attached_nprocs, 1) == 0) { |
81 | // Initialize shared memory if this is the 1st process attached | ||
82 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 15 times.
|
16 | verbose(VB_SHMEM, "setting values to the shared mem"); |
83 | |||
84 | /* idleCPUS */ | ||
85 | 16 | shdata->idleCpus = 0; | |
86 | add_event(IDLE_CPUS_EVENT, 0); | ||
87 | |||
88 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 15 times.
|
16 | verbose(VB_SHMEM, "Finished setting values to the shared mem"); |
89 | } | ||
90 | 29 | } | |
91 | |||
92 | 29 | static void close_shmem(void) { | |
93 | 29 | pthread_mutex_lock(&mutex); | |
94 | { | ||
95 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 13 times.
|
29 | if (--subprocesses_attached == 0) { |
96 | 16 | shmem_finalize(shm_handler, is_shmem_empty); | |
97 | 16 | shm_handler = NULL; | |
98 | 16 | shdata = NULL; | |
99 | } | ||
100 | } | ||
101 | 29 | pthread_mutex_unlock(&mutex); | |
102 | 29 | } | |
103 | |||
104 | 29 | void finalize_comm() { | |
105 |
1/2✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
|
29 | if (shm_handler) { |
106 | 29 | __sync_fetch_and_sub(&shdata->attached_nprocs, 1); | |
107 | 29 | close_shmem(); | |
108 | } | ||
109 | 29 | } | |
110 | |||
111 | 13 | int releaseCpus(int cpus) { | |
112 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 8 times.
|
13 | verbose(VB_SHMEM, "Releasing CPUS..."); |
113 | |||
114 | 13 | __sync_fetch_and_add (&(shdata->idleCpus), cpus); | |
115 | add_event(IDLE_CPUS_EVENT, shdata->idleCpus); | ||
116 | |||
117 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 8 times.
|
13 | verbose(VB_SHMEM, "DONE Releasing CPUS (idle %d)", shdata->idleCpus); |
118 | |||
119 | 13 | return 0; | |
120 | } | ||
121 | |||
122 | /* | ||
123 | Returns de number of cpus | ||
124 | that are assigned | ||
125 | */ | ||
126 | 12 | int acquireCpus(int current_cpus) { | |
127 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 9 times.
|
12 | verbose(VB_SHMEM, "Acquiring CPUS..."); |
128 | 12 | int cpus = defaultCPUS-current_cpus; | |
129 | |||
130 | //I don't care if there aren't enough cpus | ||
131 | |||
132 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
12 | if((__sync_sub_and_fetch (&(shdata->idleCpus), cpus)>0) && greedy) { |
133 | ✗ | cpus+=__sync_val_compare_and_swap(&(shdata->idleCpus), shdata->idleCpus, 0); | |
134 | } | ||
135 | add_event(IDLE_CPUS_EVENT, shdata->idleCpus); | ||
136 | |||
137 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 9 times.
|
12 | verbose(VB_SHMEM, "Using %d CPUS... %d Idle", cpus, shdata->idleCpus); |
138 | |||
139 | 12 | return cpus+current_cpus; | |
140 | } | ||
141 | |||
142 | /* | ||
143 | Returns de number of cpus | ||
144 | that are assigned | ||
145 | */ | ||
146 | 10 | int checkIdleCpus(int myCpus, int maxResources) { | |
147 |
1/2✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
|
10 | verbose(VB_SHMEM, "Checking idle CPUS... %d", shdata->idleCpus); |
148 | int cpus; | ||
149 | int aux; | ||
150 | //WARNING// | ||
151 | //if more CPUS than the availables are used release some | ||
152 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
10 | if ((shdata->idleCpus < 0) && (myCpus>defaultCPUS) ) { |
153 | ✗ | aux=shdata->idleCpus; | |
154 | ✗ | cpus=min_int(abs(aux), myCpus-defaultCPUS); | |
155 | ✗ | if(__sync_bool_compare_and_swap(&(shdata->idleCpus), aux, aux+cpus)) { | |
156 | ✗ | myCpus-=cpus; | |
157 | } | ||
158 | |||
159 | //if there are idle CPUS use them | ||
160 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 1 times.
|
10 | } else if( shdata->idleCpus > 0) { |
161 | 9 | aux=shdata->idleCpus; | |
162 |
1/2✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
|
9 | if(aux>maxResources) { aux=maxResources; } |
163 | |||
164 |
1/2✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
|
9 | if(__sync_bool_compare_and_swap(&(shdata->idleCpus), shdata->idleCpus, shdata->idleCpus-aux)) { |
165 | 9 | myCpus+=aux; | |
166 | } | ||
167 | } | ||
168 | add_event(IDLE_CPUS_EVENT, shdata->idleCpus); | ||
169 | |||
170 |
1/2✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
|
10 | verbose(VB_SHMEM, "Using %d CPUS... %d Idle", myCpus, shdata->idleCpus); |
171 | 10 | return myCpus; | |
172 | } | ||
173 |