-
Notifications
You must be signed in to change notification settings - Fork 24
/
jobs.c
executable file
·1863 lines (1578 loc) · 47.5 KB
/
jobs.c
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Task Spooler - a task queue system for the unix user
Copyright (C) 2007-2013 Lluís Batlle i Rossell
Please find the license in the provided COPYING file.
*/
#define _DEFAULT_SOURCE
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <time.h>
#include <sys/socket.h>
#include "main.h"
#include "cjson/cJSON.h"
/* The list will access them */
int busy_slots = 0;
int max_slots = 1;
struct Notify {
int socket;
int jobid;
struct Notify *next;
};
/* Globals */
static struct Job *firstjob = 0;
static struct Job *first_finished_job = 0;
static int jobids = 0;
/* This is used for dependencies from jobs
* already out of the queue */
static int last_errorlevel = 0; /* Before the first job, let's consider
a good previous result */
/* We need this to handle well "-d" after a "-nf" run */
static int last_finished_jobid;
static struct Notify *first_notify = 0;
/* server will access them */
int max_jobs;
static struct Job *get_job(int jobid);
void notify_errorlevel(struct Job *p);
static void shuffle(int *array, size_t n) {
if (n > 1) {
size_t i;
srand(time(NULL));
for (i = 0; i < n - 1; i++) {
size_t j = i + rand() / (RAND_MAX / (n - i) + 1);
int t = array[j];
array[j] = array[i];
array[i] = t;
}
}
}
static void destroy_job(struct Job* p) {
free(p->notify_errorlevel_to);
free(p->command);
free(p->output_filename);
pinfo_free(&p->info);
free(p->depend_on);
free(p->label);
free(p->gpu_ids);
free(p);
}
static void send_list_line(int s, const char *str) {
struct Msg m = default_msg();
/* Message */
m.type = LIST_LINE;
m.u.size = strlen(str) + 1;
send_msg(s, &m);
/* Send the line */
send_bytes(s, str, m.u.size);
}
static void send_urgent_ok(int s) {
struct Msg m = default_msg();
/* Message */
m.type = URGENT_OK;
send_msg(s, &m);
}
static void send_swap_jobs_ok(int s) {
struct Msg m = default_msg();
/* Message */
m.type = SWAP_JOBS_OK;
send_msg(s, &m);
}
static struct Job *find_previous_job(const struct Job *final) {
struct Job *p;
/* Show Queued or Running jobs */
p = firstjob;
while (p != 0) {
if (p->next == final)
return p;
p = p->next;
}
return 0;
}
static struct Job *findjob(int jobid) {
struct Job *p;
/* Show Queued or Running jobs */
p = firstjob;
while (p != 0) {
if (p->jobid == jobid)
return p;
p = p->next;
}
return 0;
}
static struct Job *findjob_holding_client() {
struct Job *p;
/* Show Queued or Running jobs */
p = firstjob;
while (p != 0) {
if (p->state == HOLDING_CLIENT)
return p;
p = p->next;
}
return 0;
}
static struct Job *find_finished_job(int jobid) {
struct Job *p;
/* Show Queued or Running jobs */
p = first_finished_job;
while (p != 0) {
if (p->jobid == jobid)
return p;
p = p->next;
}
return 0;
}
static int count_not_finished_jobs() {
int count = 0;
struct Job *p;
/* Show Queued or Running jobs */
p = firstjob;
while (p != 0) {
++count;
p = p->next;
}
return count;
}
static void add_notify_errorlevel_to(struct Job *job, int jobid) {
int *p;
int newsize = (job->notify_errorlevel_to_size + 1)
* sizeof(int);
p = (int *) realloc(job->notify_errorlevel_to,
newsize);
if (p == 0)
error("Cannot allocate more memory for notify_errorlist_to for jobid %i,"
" having already %i elements",
job->jobid, job->notify_errorlevel_to_size);
job->notify_errorlevel_to = p;
job->notify_errorlevel_to_size += 1;
job->notify_errorlevel_to[job->notify_errorlevel_to_size - 1] = jobid;
}
void s_kill_all_jobs(int s) {
struct Job *p;
s_count_running_jobs(s);
/* send running job PIDs */
p = firstjob;
while (p != 0) {
if (p->state == RUNNING)
send(s, &p->pid, sizeof(int), 0);
p = p->next;
}
}
void s_count_running_jobs(int s) {
int count = 0;
struct Job *p;
struct Msg m = default_msg();
/* Count running jobs */
p = firstjob;
while (p != 0) {
if (p->state == RUNNING)
++count;
p = p->next;
}
/* Message */
m.type = COUNT_RUNNING;
m.u.count_running = count;
send_msg(s, &m);
}
int s_count_allocating_jobs() {
int count = 0;
struct Job *p;
/* Count running jobs */
p = firstjob;
while (p != 0) {
if (p->state == ALLOCATING)
count++;
p = p->next;
}
return count;
}
void s_send_label(int s, int jobid) {
struct Job *p = 0;
char *label;
if (jobid == -1) {
/* Find the last job added */
p = firstjob;
if (p != 0)
while (p->next != 0)
p = p->next;
/* Look in finished jobs if needed */
if (p == 0) {
p = first_finished_job;
if (p != 0)
while (p->next != 0)
p = p->next;
}
} else {
p = get_job(jobid);
}
if (p == 0) {
char tmp[50];
sprintf(tmp, "Job %i not finished or not running.\n", jobid);
send_list_line(s, tmp);
return;
}
if (p->label) {
label = (char *) malloc(strlen(p->label) + 1);
sprintf(label, "%s\n", p->label);
} else
label = "";
send_list_line(s, label);
if (p->label)
free(label);
}
void s_send_cmd(int s, int jobid) {
struct Job *p = 0;
if (jobid == -1) {
/* Find the last job added */
p = firstjob;
if (p != 0)
while (p->next != 0)
p = p->next;
/* Look in finished jobs if needed */
if (p == 0) {
p = first_finished_job;
if (p != 0)
while (p->next != 0)
p = p->next;
}
} else {
p = get_job(jobid);
}
if (p == 0) {
char tmp[50];
sprintf(tmp, "Job %i not found in the queue.", jobid);
send_list_line(s, tmp);
return;
}
send_list_line(s, p->command);
}
void s_mark_job_running(int jobid) {
struct Job *p;
p = findjob(jobid);
if (!p)
error("Cannot mark the jobid %i RUNNING.", jobid);
p->state = RUNNING;
}
/* -1 means nothing awaken, otherwise returns the jobid awaken */
int wake_hold_client() {
struct Job *p;
p = findjob_holding_client();
if (p) {
p->state = (p->num_gpus) ? ALLOCATING : QUEUED;
return p->jobid;
}
return -1;
}
const char *jstate2string(enum Jobstate s) {
const char *jobstate;
switch (s) {
case QUEUED:
jobstate = "queued";
break;
case ALLOCATING:
jobstate = "allocating";
break;
case RUNNING:
jobstate = "running";
break;
case FINISHED:
jobstate = "finished";
break;
case SKIPPED:
jobstate = "skipped";
break;
case HOLDING_CLIENT:
jobstate = "skipped";
break;
}
return jobstate;
}
/* Serialize a job and add it to the JSON array. Returns 1 for success, 0 for failure. */
static int add_job_to_json_array(struct Job *p, cJSON *jobs) {
cJSON *job = cJSON_CreateObject();
if (job == NULL)
{
error("Error initializing JSON object for job %i.", p->jobid);
return 0;
}
cJSON_AddItemToArray(jobs, job);
/* Add fields */
cJSON *field;
/* ID */
field = cJSON_CreateNumber(p->jobid);
if (field == NULL)
{
error("Error initializing JSON object for job %i field ID.", p->jobid);
return 0;
}
cJSON_AddItemToObject(job, "ID", field);
/* State */
const char *state_string = jstate2string(p->state);
field = cJSON_CreateStringReference(state_string);
if (field == NULL)
{
error("Error initializing JSON object for job %i field State (value %d/%s).", p->jobid, p->state, state_string);
return 0;
}
cJSON_AddItemToObject(job, "State", field);
/* Output */
field = cJSON_CreateStringReference(p->output_filename);
if (field == NULL)
{
error("Error initializing JSON object for job %i field Output (value %s).", p->jobid, p->output_filename);
return 0;
}
cJSON_AddItemToObject(job, "Output", field);
/* E-Level */
if (p->state == FINISHED) {
field = cJSON_CreateNumber(p->result.errorlevel);
}
else {
field = cJSON_CreateNull();
}
if (field == NULL)
{
error("Error initializing JSON object for job %i field E-Level.", p->jobid);
return 0;
}
cJSON_AddItemToObject(job, "E-Level", field);
/* Time */
if (p->state == FINISHED) {
field = cJSON_CreateNumber(p->result.real_ms);
if (field == NULL)
{
error("Error initializing JSON object for job %i field Time_ms (value %d).", p->result.real_ms);
return 0;
}
}
else {
field = cJSON_CreateNull();
if (field == NULL)
{
error("Error initializing JSON object for job %i field Time_ms (no result).");
return 0;
}
}
cJSON_AddItemToObject(job, "Time_ms", field);
/* GPUs */
#ifndef CPU
field = cJSON_CreateNumber(p->num_gpus);
if (field == NULL)
{
error("Error initializing JSON object for job %i field GPUs (value %d).", p->num_gpus);
return 0;
}
cJSON_AddItemToObject(job, "GPUs", field);
#endif /*CPU*/
/* Command */
field = cJSON_CreateStringReference(p->command);
if (field == NULL)
{
error("Error initializing JSON object for job %i field Command (value %s).", p->jobid, p->command);
return 0;
}
cJSON_AddItemToObject(job, "Command", field);
return 1;
}
void s_list(int s, enum ListFormat listFormat) {
struct Job *p;
char *buffer = 0;
if (listFormat == DEFAULT) {
/* Times: 0.00/0.00/0.00 - 4+4+4+2 = 14*/
buffer = joblist_headers();
send_list_line(s, buffer);
free(buffer);
/* Show Queued or Running jobs */
p = firstjob;
while (p != 0) {
if (p->state != HOLDING_CLIENT) {
buffer = joblist_line(p);
send_list_line(s, buffer);
free(buffer);
}
p = p->next;
}
p = first_finished_job;
/* Show Finished jobs */
while (p != 0) {
buffer = joblist_line(p);
send_list_line(s, buffer);
free(buffer);
p = p->next;
}
}
else if (listFormat == JSON) {
cJSON *jobs = cJSON_CreateArray();
if (jobs == NULL)
{
error("Error initializing JSON array.");
goto end;
}
/* Serialize Queued or Running jobs */
p = firstjob;
while (p != 0) {
if (p->state != HOLDING_CLIENT) {
int success = add_job_to_json_array(p, jobs);
if (success == 0) {
goto end;
}
}
p = p->next;
}
/* Serialize Finished jobs */
p = first_finished_job;
while (p != 0) {
int success = add_job_to_json_array(p, jobs);
if (success == 0) {
goto end;
}
p = p->next;
}
buffer = cJSON_PrintUnformatted(jobs);
if (buffer == NULL)
{
error("Error converting jobs to JSON.");
goto end;
}
// append newline
size_t buffer_strlen = strlen(buffer);
buffer = realloc(buffer, buffer_strlen+1+1);
strcat(buffer, "\n");
send_list_line(s, buffer);
goto end;
end:
cJSON_Delete(jobs);
free(buffer);
}
else if (listFormat == TAB) {
/* Show Queued or Running jobs */
p = firstjob;
while (p != 0) {
if (p->state != HOLDING_CLIENT) {
buffer = joblist_line_plain(p);
send_list_line(s, buffer);
free(buffer);
}
p = p->next;
}
p = first_finished_job;
/* Show Finished jobs */
while (p != 0) {
buffer = joblist_line_plain(p);
send_list_line(s, buffer);
free(buffer);
p = p->next;
}
}
}
#ifndef CPU
void s_list_gpu(int s) {
struct Job *p = firstjob;
char* buffer;
buffer = jobgpulist_header();
send_list_line(s, buffer);
while (p != 0) {
if (p->state == RUNNING && p->num_gpus) {
buffer = jobgpulist_line(p);
send_list_line(s, buffer);
free(buffer);
}
p = p->next;
}
}
#endif
static void init_job(struct Job *p) {
p->next = 0;
p->output_filename = 0;
p->command = 0;
p->depend_on = 0;
p->depend_on_size = 0;
p->gpu_ids = 0;
p->label = 0;
p->notify_errorlevel_to_size = 0;
p->notify_errorlevel_to = 0;
p->dependency_errorlevel = 0;
pinfo_init(&p->info);
}
static struct Job *newjobptr() {
struct Job *p;
if (firstjob == 0) {
firstjob = (struct Job *) malloc(sizeof(*firstjob));
init_job(firstjob);
return firstjob;
}
p = firstjob;
while (p->next != 0)
p = p->next;
p->next = (struct Job *) malloc(sizeof(*p));
init_job(p->next);
return p->next;
}
/* Returns -1 if no last job id found */
static int find_last_jobid_in_queue(int neglect_jobid) {
struct Job *p;
int last_jobid = -1;
p = firstjob;
while (p != 0) {
if (p->jobid != neglect_jobid &&
p->jobid > last_jobid)
last_jobid = p->jobid;
p = p->next;
}
return last_jobid;
}
/* Returns -1 if no last job id found */
static int find_last_stored_jobid_finished() {
struct Job *p;
int last_jobid = -1;
p = first_finished_job;
while (p != 0) {
if (p->jobid > last_jobid)
last_jobid = p->jobid;
p = p->next;
}
return last_jobid;
}
/* Returns job id or -1 on error */
int s_newjob(int s, struct Msg *m) {
struct Job *p;
int res;
p = newjobptr();
p->jobid = jobids++;
/* GPUs */
p->num_gpus = m->u.newjob.gpus;
if (count_not_finished_jobs() < max_jobs)
p->state = (p->num_gpus) ? ALLOCATING : QUEUED;
else
p->state = HOLDING_CLIENT;
p->wait_free_gpus = m->u.newjob.wait_free_gpus;
if (!p->wait_free_gpus)
p->gpu_ids = recv_ints(s, &p->num_gpus);
else {
p->gpu_ids = (int *) malloc((p->num_gpus + 1) * sizeof(int));
memset(p->gpu_ids, -1, (p->num_gpus + 1) * sizeof(int));
}
p->num_slots = m->u.newjob.num_slots;
p->store_output = m->u.newjob.store_output;
p->should_keep_finished = m->u.newjob.should_keep_finished;
/* this error level here is used internally to decide whether a job should be run or not
* so it only matters whether the error level is 0 or not.
* thus, summing the absolute error levels of all dependencies is sufficient.*/
if (m->u.newjob.depend_on_size) {
int *depend_on;
depend_on = recv_ints(s, &p->depend_on_size);
/* Depend on the last queued job. */
int idx = 0;
for (int i = 0; i < p->depend_on_size; i++) {
/* filter out dependencies that are current jobs */
if (depend_on[i] >= p->jobid)
continue;
p->depend_on = (int*) realloc(p->depend_on, (idx + 1) * sizeof(int));
/* As we already have 'p' in the queue,
* neglect it during the find_last_jobid_in_queue() */
if (depend_on[i] == -1) {
p->depend_on[idx] = find_last_jobid_in_queue(p->jobid);
/* We don't trust the last jobid in the queue (running or queued)
* if it's not the last added job. In that case, let
* the next control flow handle it as if it could not
* do_depend on any still queued job. */
if (last_finished_jobid > p->depend_on[idx])
p->depend_on[idx] = -1;
/* If it's queued still without result, let it know
* its result to p when it finishes. */
if (p->depend_on[idx] != -1) {
struct Job *depended_job;
depended_job = findjob(p->depend_on[idx]);
if (depended_job != 0)
add_notify_errorlevel_to(depended_job, p->jobid);
else
warning("The jobid %i is queued to do_depend on the jobid %i"
" suddenly non existent in the queue", p->jobid,
p->depend_on[idx]);
} else /* Otherwise take the finished job, or the last_errorlevel */
{
if (depend_on[i] == -1) {
int ljobid = find_last_stored_jobid_finished();
p->depend_on[idx] = ljobid;
/* If we have a newer result stored, use it */
/* NOTE:
* Reading this now, I don't know how ljobid can be
* greater than last_finished_jobid */
if (last_finished_jobid < ljobid) {
struct Job *parent;
parent = find_finished_job(ljobid);
if (!parent)
error("jobid %i suddenly disappeared from the finished list",
ljobid);
p->dependency_errorlevel += abs(parent->result.errorlevel);
} else
p->dependency_errorlevel += abs(last_errorlevel);
}
}
} else {
/* The user decided what's the job this new job depends on */
struct Job *depended_job;
p->depend_on[idx] = depend_on[i];
depended_job = findjob(p->depend_on[idx]);
if (depended_job != 0)
add_notify_errorlevel_to(depended_job, p->jobid);
else {
struct Job *parent;
parent = find_finished_job(p->depend_on[idx]);
if (parent) {
p->dependency_errorlevel += abs(parent->result.errorlevel);
} else {
/* We consider as if the job not found
didn't finish well */
p->dependency_errorlevel += 1;
}
}
}
idx++;
}
free(depend_on);
p->depend_on_size = idx;
}
/* if dependency list is empty after removing invalid dependencies, make it independent */
if (p->depend_on_size == 0)
p->depend_on = 0;
pinfo_set_enqueue_time(&p->info);
/* load the command */
p->command = malloc(m->u.newjob.command_size);
if (p->command == 0)
error("Cannot allocate memory in s_newjob command_size (%i)",
m->u.newjob.command_size);
res = recv_bytes(s, p->command, m->u.newjob.command_size);
if (res == -1)
error("wrong bytes received");
/* load the label */
if (m->u.newjob.label_size > 0) {
char *ptr;
ptr = (char *) malloc(m->u.newjob.label_size);
if (ptr == 0)
error("Cannot allocate memory in s_newjob label_size(%i)",
m->u.newjob.label_size);
res = recv_bytes(s, ptr, m->u.newjob.label_size);
if (res == -1)
error("wrong bytes received");
p->label = ptr;
}
/* load the info */
if (m->u.newjob.env_size > 0) {
char *ptr;
ptr = (char *) malloc(m->u.newjob.env_size);
if (ptr == 0)
error("Cannot allocate memory in s_newjob env_size(%i)",
m->u.newjob.env_size);
res = recv_bytes(s, ptr, m->u.newjob.env_size);
if (res == -1)
error("wrong bytes received");
pinfo_addinfo(&p->info, m->u.newjob.env_size + 100,
"Environment:\n%s", ptr);
free(ptr);
}
return p->jobid;
}
/* This assumes the jobid exists */
void s_removejob(int jobid) {
struct Job *p;
struct Job *newnext;
if (firstjob->jobid == jobid) {
struct Job *newfirst;
/* First job is to be removed */
newfirst = firstjob->next;
destroy_job(firstjob);
firstjob = newfirst;
return;
}
p = firstjob;
/* Not first job */
while (p->next != 0) {
if (p->next->jobid == jobid)
break;
p = p->next;
}
if (p->next == 0)
error("Job to be removed not found. jobid=%i", jobid);
newnext = p->next->next;
destroy_job(p->next);
p->next = newnext;
}
/* -1 if no one should be run. */
int next_run_job() {
struct Job *p;
const int free_slots = max_slots - busy_slots;
/* busy_slots may be bigger than the maximum slots,
* if the user was running many jobs, and suddenly
* trimmed the maximum slots down. */
if (free_slots <= 0)
return -1;
/* If there are no jobs to run... */
if (firstjob == 0)
return -1;
#ifndef CPU
/* Query GPUs */
int numFree;
int *freeGpuList = getGpuList(&numFree);
#endif
/* Look for a runnable task */
p = firstjob;
while (p != 0) {
if (p->state == QUEUED || p->state == ALLOCATING) {
#ifndef CPU
if (freeGpuList == NULL) {
p = p->next;
continue;
}
if (p->num_gpus && p->wait_free_gpus) {
if (numFree < p->num_gpus) {
/* if fewer GPUs than required then next */
p = p->next;
continue;
}
shuffle(freeGpuList, numFree);
/* We have to check whether some GPUs are used by other jobs, but their RAMs are still free
* These GPUs should not be used.*/
int i = 0, j = 0;
/* loop until all GPUs required can be found, or there are enough GPUs */
int *gpu_ids = (int*) malloc(p->num_gpus * sizeof(int));
while (i < p->num_gpus && j < numFree) {
/* if the prospective GPUs are in used, select the next one */
if (!isInUse(freeGpuList[j]))
gpu_ids[i++] = freeGpuList[j];
j++;
}
/* some GPUs might already be claimed by other jobs, but the system still reports as free -> skip */
if (i < p->num_gpus) {
p = p->next;
free(gpu_ids);
continue;
}
memcpy(p->gpu_ids, gpu_ids, p->num_gpus * sizeof(int));
free(gpu_ids);
}
#endif
if (p->depend_on_size) {
int ready = 1;
for (int i = 0; i < p->depend_on_size; i++) {
struct Job *do_depend_job = get_job(p->depend_on[i]);
/* We won't try to run any job do_depending on an unfinished
* job */
if (do_depend_job != NULL &&
(do_depend_job->state == QUEUED || do_depend_job->state == RUNNING ||
do_depend_job->state == ALLOCATING)) {
/* Next try */
p = p->next;
ready = 0;
break;
}
}
if (ready != 1)
continue;
}
if (free_slots >= p->num_slots) {
busy_slots = busy_slots + p->num_slots;
#ifndef CPU
if (p->num_gpus)
broadcastUsedGpus(p->num_gpus, p->gpu_ids);
free(freeGpuList);
#endif
return p->jobid;
}
}
p = p->next;
}
#ifndef CPU
free(freeGpuList);
#endif
return -1;
}
/* Returns 1000 if no limit, The limit otherwise. */
static int get_max_finished_jobs() {
char *limit;
limit = getenv("TS_MAXFINISHED");
if (limit == NULL)
return 1000;
return abs(atoi(limit));
}
/* Add the job to the finished queue. */
static void new_finished_job(struct Job *j) {
struct Job *p;
int count, max;
max = get_max_finished_jobs();
count = 0;
if (first_finished_job == 0 && count < max) {
first_finished_job = j;
first_finished_job->next = 0;
return;
}
++count;
p = first_finished_job;
while (p->next != 0) {
p = p->next;
++count;
}
/* If too many jobs, wipe out the first */
if (count >= max) {
struct Job *tmp;
tmp = first_finished_job;
first_finished_job = first_finished_job->next;
destroy_job(tmp);
}
p->next = j;
p->next->next = 0;
}
static int job_is_in_state(int jobid, enum Jobstate state) {
struct Job *p;
p = findjob(jobid);
if (p == 0)
return 0;
if (p->state == state)
return 1;
return 0;
}
int job_is_running(int jobid) {
return job_is_in_state(jobid, RUNNING);
}
int job_is_holding_client(int jobid) {
return job_is_in_state(jobid, HOLDING_CLIENT);
}
static int in_notify_list(int jobid) {
struct Notify *n, *tmp;
n = first_notify;
while (n != 0) {
tmp = n;
n = n->next;
if (tmp->jobid == jobid)
return 1;
}
return 0;