forked from BOINC/boinc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoproc_sched.cpp
378 lines (350 loc) · 11.3 KB
/
coproc_sched.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
// This file is part of BOINC.
// http://boinc.berkeley.edu
// Copyright (C) 2014 University of California
//
// BOINC is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation,
// either version 3 of the License, or (at your option) any later version.
//
// BOINC is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with BOINC. If not, see <http://www.gnu.org/licenses/>.
#include <vector>
#include "client_msgs.h"
#include "client_state.h"
#include "client_types.h"
#include "coproc.h"
#include "result.h"
#include "coproc_sched.h"
using std::vector;
#if 0
#define COPROC_DEBUG(x) x
#else
#define COPROC_DEBUG(X)
#endif
////////// Coprocessor scheduling ////////////////
//
// theory of operation:
//
// Jobs can use one or more integral instances, or a fractional instance
//
// RESULT::coproc_indices
// for a running job, the coprocessor instances it's using
// COPROC::pending_usage[]: for each instance, its usage by running jobs
// Note: "running" includes jobs suspended due to CPU throttling.
// That's the only kind of suspended GPU job.
// CORPOC::usage[]: for each instance, its usage
//
// enforce_run_list() calls assign_coprocs(),
// which assigns coproc instances to scheduled jobs,
// and prunes jobs for which we can't make an assignment
// (the job list is in order of decreasing priority)
//
// assign_coprocs():
// clear usage and pending_usage of all instances
// for each running/suspended job J
// increment pending_usage for the instances assigned to J
// for each scheduled job J
// if J is running
// if J's assignment fits
// confirm assignment: dec pending_usage, inc usage
// else
// prune J
// else
// if J.usage is fractional
// look for an instance that's already fractionally assigned
// if that fails, look for a free instance
// if that fails, prune J
// else
// if there are enough instances with usage=0
// assign instances with pending_usage = usage = 0
// (avoid preempting running jobs)
// if need more, assign instances with usage = 0
// else
// prune J
// can the given task use this GPU instance? Enforce
// - GPU exclusions
// - OpenCL availability (relevant if use_all_gpus set)
//
static inline bool can_use_gpu(RESULT* rp, COPROC* cp, int i) {
if (gpu_excluded(rp->app, *cp, i)) {
COPROC_DEBUG(msg_printf(rp->project, MSG_INFO, "GPU %d is excluded for %s", i, rp->name));
return false;
}
if (rp->avp->is_opencl()) {
if (!cp->instance_has_opencl[i]) {
COPROC_DEBUG(msg_printf(rp->project, MSG_INFO, "GPU %d can't do OpenCL for %s", i, rp->name));
return false;
}
}
return true;
}
static inline void increment_pending_usage(
RESULT* rp, double usage, COPROC* cp
) {
double x = (usage<1)?usage:1;
for (int i=0; i<usage; i++) {
int j = rp->coproc_indices[i];
cp->pending_usage[j] += x;
if (log_flags.coproc_debug) {
msg_printf(rp->project, MSG_INFO,
"[coproc] %s instance %d; %f pending for %s", cp->type, i, x, rp->name
);
if (cp->pending_usage[j] > 1) {
msg_printf(rp->project, MSG_INFO,
"[coproc] huh? %s %d %s pending usage > 1",
cp->type, i, rp->name
);
}
}
}
}
// check the GPU assignment for a currently-running app.
// Note: don't check available RAM.
// It may not be known (e.g. NVIDIA) and in any case,
// if the app is still running, it has enough RAM
//
static inline bool current_assignment_ok(
RESULT* rp, double usage, COPROC* cp
) {
double x = (usage<1)?usage:1;
for (int i=0; i<usage; i++) {
int j = rp->coproc_indices[i];
if (cp->usage[j] + x > 1) {
if (log_flags.coproc_debug) {
msg_printf(rp->project, MSG_INFO,
"[coproc] %s %f instance of device %d already assigned to task %s",
cp->type, x, j, rp->name
);
}
return false;
}
}
return true;
}
static inline void confirm_current_assignment(
RESULT* rp, double usage, COPROC* cp
) {
double x = (usage<1)?usage:1;
for (int i=0; i<usage; i++) {
int j = rp->coproc_indices[i];
cp->usage[j] +=x;
cp->pending_usage[j] -=x;
if (log_flags.coproc_debug) {
msg_printf(rp->project, MSG_INFO,
"[coproc] %s instance %d: confirming %f instance for %s",
cp->type, j, x, rp->name
);
}
}
}
static inline bool get_fractional_assignment(RESULT* rp, double usage, COPROC* cp) {
int i;
// try to assign an instance that's already fractionally assigned
//
for (i=0; i<cp->count; i++) {
if (!can_use_gpu(rp, cp, i)) {
continue;
}
if ((cp->usage[i] || cp->pending_usage[i])
&& (cp->usage[i] + cp->pending_usage[i] + usage <= 1)
) {
rp->coproc_indices[0] = i;
cp->usage[i] += usage;
if (log_flags.coproc_debug) {
msg_printf(rp->project, MSG_INFO,
"[coproc] Assigning %f of %s instance %d to %s",
usage, cp->type, i, rp->name
);
}
return true;
}
}
// failing that, assign an unreserved instance
//
for (i=0; i<cp->count; i++) {
if (!can_use_gpu(rp, cp, i)) {
continue;
}
if (!cp->usage[i]) {
rp->coproc_indices[0] = i;
cp->usage[i] += usage;
if (log_flags.coproc_debug) {
msg_printf(rp->project, MSG_INFO,
"[coproc] Assigning %f of %s free instance %d to %s",
usage, cp->type, i, rp->name
);
}
return true;
}
}
if (log_flags.coproc_debug) {
msg_printf(rp->project, MSG_INFO,
"[coproc] Insufficient %s for %s: need %f",
cp->type, rp->name, usage
);
}
return false;
}
static inline bool get_integer_assignment(
RESULT* rp, double usage, COPROC* cp
) {
int i;
// make sure we have enough free instances
//
int nfree = 0;
for (i=0; i<cp->count; i++) {
if (!can_use_gpu(rp, cp, i)) {
continue;
}
if (!cp->usage[i]) {
nfree++;
}
}
if (nfree < usage) {
if (log_flags.coproc_debug) {
msg_printf(rp->project, MSG_INFO,
"[coproc] Insufficient %s for %s; need %d, available %d",
cp->type, rp->name, (int)usage, nfree
);
}
return false;
}
int n = 0;
// assign non-pending instances first
for (i=0; i<cp->count; i++) {
if (!can_use_gpu(rp, cp, i)) {
continue;
}
if (!cp->usage[i] && !cp->pending_usage[i]) {
cp->usage[i] = 1;
rp->coproc_indices[n++] = i;
if (log_flags.coproc_debug) {
msg_printf(rp->project, MSG_INFO,
"[coproc] Assigning %s instance %d to %s",
cp->type, i, rp->name
);
}
if (n == usage) return true;
}
}
// if needed, assign pending instances
for (i=0; i<cp->count; i++) {
if (!can_use_gpu(rp, cp, i)) {
continue;
}
if (!cp->usage[i]) {
cp->usage[i] = 1;
rp->coproc_indices[n++] = i;
if (log_flags.coproc_debug) {
msg_printf(rp->project, MSG_INFO,
"[coproc] Assigning %s pending instance %d to %s",
cp->type, i, rp->name
);
}
if (n == usage) return true;
}
}
if (log_flags.coproc_debug) {
msg_printf(rp->project, MSG_INFO,
"[coproc] huh??? ran out of %s instances for %s",
cp->type, rp->name
);
}
return false;
}
void assign_coprocs(vector<RESULT*>& jobs) {
unsigned int i;
COPROC* cp;
double usage;
coprocs.clear_usage();
// fill in pending usage
//
for (i=0; i<jobs.size(); i++) {
RESULT* rp = jobs[i];
APP_VERSION* avp = rp->avp;
int rt = avp->gpu_usage.rsc_type;
if (rt) {
usage = avp->gpu_usage.usage;
cp = &coprocs.coprocs[rt];
} else {
continue;
}
ACTIVE_TASK* atp = gstate.lookup_active_task_by_result(rp);
if (!atp) continue;
if (atp->is_gpu_task_running()) {
increment_pending_usage(rp, usage, cp);
}
}
vector<RESULT*>::iterator job_iter;
job_iter = jobs.begin();
while (job_iter != jobs.end()) {
RESULT* rp = *job_iter;
APP_VERSION* avp = rp->avp;
int rt = avp->gpu_usage.rsc_type;
if (rt) {
usage = avp->gpu_usage.usage;
cp = &coprocs.coprocs[rt];
} else {
++job_iter;
continue;
}
ACTIVE_TASK* atp = gstate.lookup_active_task_by_result(rp);
if (atp && atp->is_gpu_task_running()) {
if (current_assignment_ok(rp, usage, cp)) {
confirm_current_assignment(rp, usage, cp);
++job_iter;
} else {
job_iter = jobs.erase(job_iter);
}
} else {
if (usage < 1) {
if (get_fractional_assignment(rp, usage, cp)) {
++job_iter;
} else {
job_iter = jobs.erase(job_iter);
}
} else {
if (get_integer_assignment(rp, usage, cp)) {
++job_iter;
} else {
job_iter = jobs.erase(job_iter);
}
}
}
}
#if 0
// enforce "don't use GPUs while active" pref in NVIDIA case;
// it applies only to GPUs running a graphics app
//
if (gstate.host_info.coprocs.nvidia.count && gstate.user_active && !gstate.global_prefs.run_gpu_if_user_active) {
job_iter = jobs.begin();
while (job_iter != jobs.end()) {
RESULT* rp = *job_iter;
if (!rp->avp->ncudas) {
job_iter++;
continue;
}
ACTIVE_TASK* atp = gstate.lookup_active_task_by_result(rp);
bool some_gpu_busy = false;
for (i=0; i<rp->avp->ncudas; i++) {
int dev = atp->coproc_indices[i];
if (gstate.host_info.coprocs.cuda.running_graphics_app[dev]) {
some_gpu_busy = true;
break;
}
}
if (some_gpu_busy) {
job_iter = jobs.erase(job_iter);
} else {
job_iter++;
}
}
}
#endif
}