-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdrmaa2.go
1805 lines (1613 loc) · 49.8 KB
/
drmaa2.go
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
/*
Copyright 2014 Daniel Gruber, http://www.gridengine.eu
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Implements the DRMAA2 Go language binding based on top of
// Univa's DRMAA2 C API implementation. Should work also on
// other implementations when available.
// Please consult the DRMAA2 standard documents for more detailed
// information (http://www.ogf.org). More examples will also be
// published on my blog at http://www.gridengine.eu.
package drmaa2
import (
"fmt"
"log"
"time"
"unsafe"
)
/*
#cgo LDFLAGS: -ldrmaa2 -O2 -g
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include "drmaa2.h"
drmaa2_j malloc_job() {
drmaa2_j job = (drmaa2_j) malloc(sizeof(drmaa2_j_s));
job->id = NULL;
job->session_name = NULL;
return job;
}
drmaa2_jarray malloc_array_job() {
drmaa2_jarray ja = (drmaa2_jarray) malloc(sizeof(drmaa2_jarray_s));
ja->id = NULL;
ja->session_name = NULL;
ja->job_list = NULL;
return ja;
}
drmaa2_jtemplate malloc_jtemplate() {
drmaa2_jtemplate jt = (drmaa2_jtemplate) malloc(sizeof(drmaa2_jtemplate_s));
jt->remoteCommand = DRMAA2_UNSET_STRING;
jt->args = DRMAA2_UNSET_LIST;
jt->submitAsHold = DRMAA2_UNSET_BOOL;
jt->rerunnable = DRMAA2_UNSET_BOOL;
jt->jobEnvironment = DRMAA2_UNSET_DICT;
jt->workingDirectory = DRMAA2_UNSET_STRING;
jt->jobCategory = DRMAA2_UNSET_STRING;
jt->email = DRMAA2_UNSET_LIST;
jt->emailOnStarted = DRMAA2_UNSET_BOOL;
jt->emailOnTerminated = DRMAA2_UNSET_BOOL;
jt->jobName = DRMAA2_UNSET_STRING;
jt->inputPath = DRMAA2_UNSET_STRING;
jt->outputPath = DRMAA2_UNSET_STRING;
jt->errorPath = DRMAA2_UNSET_STRING;
jt->joinFiles = DRMAA2_UNSET_BOOL;
jt->reservationId = DRMAA2_UNSET_STRING;
jt->queueName = DRMAA2_UNSET_STRING;
jt->minSlots = DRMAA2_UNSET_NUM;
jt->maxSlots = DRMAA2_UNSET_NUM;
jt->priority = DRMAA2_UNSET_NUM;
jt->candidateMachines = DRMAA2_UNSET_LIST;
jt->minPhysMemory = DRMAA2_UNSET_NUM;
jt->machineOS = DRMAA2_UNSET_ENUM;
jt->machineArch = DRMAA2_UNSET_ENUM;
jt->startTime = DRMAA2_UNSET_TIME;
jt->deadlineTime = DRMAA2_UNSET_TIME;
jt->stageInFiles = DRMAA2_UNSET_DICT;
jt->stageOutFiles = DRMAA2_UNSET_DICT;
jt->resourceLimits = DRMAA2_UNSET_DICT;
jt->accountingId = DRMAA2_UNSET_STRING;
jt->implementationSpecific = DRMAA2_UNSET_STRING;
return jt;
}
*/
import "C"
// Interface definitions
// In order to make extension functions dependend
// from the type of the struct we need to store
// the type somewhere.
type StructType int
const (
JobTemplateType = iota
JobInfoType
ReservationTemplateType
ReservationInfoType
QueueInfoType
MachineInfoType
NotificationType
)
// Extension struct which is embedded in DRMAA2 objects
// which are extensible.
type Extension struct {
SType StructType // Stores the type of the struct
Internal unsafe.Pointer // Enhancmement of C struct
ExtensionList map[string]string // stores the extension requests as string
}
// The Drmaa2Extensible interface lists all functions required for DRMAA2
// extensible data structures (JobTemplate, JobInfo etc.).
type Drmaa2Extensible interface {
// Lists all implementation specific key names for
// a particular DRMAA2 extensible data type
ListExtensions() []string
DescribeExtension(string) string
SetExtension(string) error
GetExtension() string
// points to data structure extension from C struct
}
// A JobTemplate is an extensible structure.
func listExtensions(t StructType) []string {
var clist C.drmaa2_string_list
switch t {
case JobTemplateType:
clist = C.drmaa2_jtemplate_impl_spec()
case JobInfoType:
clist = C.drmaa2_jinfo_impl_spec()
case ReservationTemplateType:
clist = C.drmaa2_rinfo_impl_spec()
// TODO: case ReservationInfo:
case QueueInfoType:
clist = C.drmaa2_queueinfo_impl_spec()
case MachineInfoType:
clist = C.drmaa2_machineinfo_impl_spec()
default:
// TODO error
fmt.Println("Error")
}
clistp := C.drmaa2_list(clist)
defer C.drmaa2_list_free(&clistp)
extensions := convertCStringListToGo(clist)
return extensions
}
// Returns a string list containing all implementation specific
// extensions of the JobTemplate object.
func (structType *JobTemplate) ListExtensions() []string {
return listExtensions(JobTemplateType)
}
// Returns a string list containing all implementation specific
// extensions of the Machine object.
func (structType *Machine) ListExtensions() []string {
return listExtensions(MachineInfoType)
}
// Returns a string list containing all implementation specific
// extensions of the Queue object.
func (structType *Queue) ListExtensions() []string {
return listExtensions(QueueInfoType)
}
// Returns a string list containing all implementation specific
// extensions of the JobInfo object.
func (structType *JobInfo) ListExtensions() []string {
return listExtensions(JobInfoType)
}
func (ext *Extension) describeExtension(t StructType, extensionName string) (string, error) {
if ext.Internal != nil {
cdesc := C.drmaa2_describe_attribute(ext.Internal,
C.CString(extensionName))
if cdesc != nil {
defer C.drmaa2_string_free(&cdesc)
return C.GoString(cdesc), nil
}
return "", makeLastError()
}
// pointer to extension data structure is not set,
// therefore it is allocated then used for the C
// call and then thrown away - don't like it
var description C.drmaa2_string
switch t {
case JobInfoType:
jt := C.drmaa2_jtemplate_create()
description = C.drmaa2_describe_attribute(jt.implementationSpecific,
C.CString(extensionName))
C.drmaa2_jtemplate_free(&jt)
// TODO -> other types
default:
fmt.Println("Unimplemented")
}
if description != nil {
defer C.drmaa2_string_free(&description)
return C.GoString(description), nil
}
return "", makeLastError()
}
// Returns the description of an implementation specific
// JobTemplate extension as a string.
func (jt *JobTemplate) DescribeExtension(extensionName string) (string, error) {
// good candidate for an init function in the session manager
return jt.describeExtension(JobTemplateType, extensionName)
}
// TODO MachineInfo / Queue / JobInfo etc.
// checks if a certain extension exists for a given type
func extensionExists(t StructType, ext string) bool {
// TODO expensive - better store available extensions
// here a DRMAA2 init could be really useful
extensions := listExtensions(t)
for _, e := range extensions {
if e == ext {
return true
}
}
return false
}
// Sets a DRM specific extension to a value
func (ext *Extension) setExtension(t StructType, extension, value string) error {
if extensionExists(t, extension) {
if ext.ExtensionList == nil {
ext.ExtensionList = make(map[string]string)
}
ext.ExtensionList[extension] = value
return nil
}
return makeError("Extension not supported", UnsupportedAttribute)
}
func (jt *JobTemplate) SetExtension(extension, value string) error {
return jt.setExtension(JobTemplateType, extension, value)
}
func (m *Machine) SetExtension(extension, value string) error {
return m.setExtension(MachineInfoType, extension, value)
}
func (ji *JobInfo) SetExtension(extension, value string) error {
return ji.setExtension(JobInfoType, extension, value)
}
func (q *Queue) SetExtension(extension, value string) error {
return q.setExtension(QueueInfoType, extension, value)
}
// TODO the other extensions: notification / reservation info / template
// set the Go extension into the real object
// (for example when running the job)
func setExtensionsIntoCObject(ptr unsafe.Pointer, elist map[string]string) {
for key, value := range elist {
C.drmaa2_set_instance_value(ptr, C.CString(key), C.CString(value))
}
}
// For all types which embedds the Extension struct (JobTemplate etc.)
func (e *Extension) GetExtension(extension string) (string, error) {
// check if any extension is stored in the Go struct
if e.ExtensionList != nil {
if value, ok := e.ExtensionList[extension]; ok == true {
return value, nil
}
return "", makeError("Extension not found", UnsupportedAttribute)
}
return "", makeError("Extension not found", UnsupportedAttribute)
}
type Version struct {
Major string
Minor string
}
func (v *Version) String() string {
return fmt.Sprintf("%s.%s", v.Major, v.Minor)
}
// Special timeout value: Don't wait
const ZeroTime = int64(C.DRMAA2_ZERO_TIME)
// Special timeout value: Wait probably infinitly
const InfiniteTime = int64(C.DRMAA2_INFINITE_TIME)
// Capabilities are optional functionalities defined by
// the DRMAA2 standard.
type Capability int
const (
AdvanceReservation = iota
ReserveSlots
Callback
BulkJobsMaxParallel
JtEmail
JtStaging
JtDeadline
JtMaxSlots
JtAccountingId
RtStartNow
RtDuration
RtMachineOS
RtMachineArch
)
// maybe not needed
var capCMap = map[C.drmaa2_capability]Capability{
C.DRMAA2_ADVANCE_RESERVATION: AdvanceReservation,
C.DRMAA2_RESERVE_SLOTS: ReserveSlots,
C.DRMAA2_CALLBACK: Callback,
C.DRMAA2_BULK_JOBS_MAXPARALLEL: BulkJobsMaxParallel,
C.DRMAA2_JT_EMAIL: JtEmail,
C.DRMAA2_JT_STAGING: JtStaging,
C.DRMAA2_JT_DEADLINE: JtDeadline,
C.DRMAA2_JT_MAXSLOTS: JtMaxSlots,
C.DRMAA2_JT_ACCOUNTINGID: JtAccountingId,
C.DRMAA2_RT_STARTNOW: RtStartNow,
C.DRMAA2_RT_DURATION: RtDuration,
C.DRMAA2_RT_MACHINEOS: RtMachineOS,
C.DRMAA2_RT_MACHINEARCH: RtMachineArch,
}
var capMap = map[Capability]C.drmaa2_capability{
AdvanceReservation: C.DRMAA2_ADVANCE_RESERVATION,
ReserveSlots: C.DRMAA2_RESERVE_SLOTS,
Callback: C.DRMAA2_CALLBACK,
BulkJobsMaxParallel: C.DRMAA2_BULK_JOBS_MAXPARALLEL,
JtEmail: C.DRMAA2_JT_EMAIL,
JtStaging: C.DRMAA2_JT_STAGING,
JtDeadline: C.DRMAA2_JT_DEADLINE,
JtMaxSlots: C.DRMAA2_JT_MAXSLOTS,
JtAccountingId: C.DRMAA2_JT_ACCOUNTINGID,
RtStartNow: C.DRMAA2_RT_STARTNOW,
RtDuration: C.DRMAA2_RT_DURATION,
RtMachineOS: C.DRMAA2_RT_MACHINEOS,
RtMachineArch: C.DRMAA2_RT_MACHINEARCH,
}
// DRMAA2 error ID
type ErrorId int
const (
Success ErrorId = iota
DeniedByDrms
DrmCommunication
TryLater
SessionManagement
Timeout
Internal
InvalidArgument
InvalidSession
InvalidState
OutOfResource
UnsupportedAttribute
UnsupportedOperation
ImplementationSpecific
LastError
)
// Maps a C drmaa2_error type into a Go ErrorId
var errorIdMap = map[C.drmaa2_error]ErrorId{
C.DRMAA2_SUCCESS: Success,
C.DRMAA2_DENIED_BY_DRMS: DeniedByDrms,
C.DRMAA2_DRM_COMMUNICATION: DrmCommunication,
C.DRMAA2_TRY_LATER: TryLater,
C.DRMAA2_SESSION_MANAGEMENT: SessionManagement,
C.DRMAA2_TIMEOUT: Timeout,
C.DRMAA2_INTERNAL: Internal,
C.DRMAA2_INVALID_ARGUMENT: InvalidArgument,
C.DRMAA2_INVALID_SESSION: InvalidSession,
C.DRMAA2_INVALID_STATE: InvalidState,
C.DRMAA2_OUT_OF_RESOURCE: OutOfResource,
C.DRMAA2_UNSUPPORTED_ATTRIBUTE: UnsupportedAttribute,
C.DRMAA2_UNSUPPORTED_OPERATION: UnsupportedOperation,
C.DRMAA2_IMPLEMENTATION_SPECIFIC: ImplementationSpecific,
C.DRMAA2_LASTERROR: LastError,
}
// CPU architecture types
type CPU int
const (
OtherCPU CPU = iota
Alpha
ARM
ARM64
Cell
PA_RISC
PA_RISC64
x86
x64
IA_64
MIPS
MIPS64
PowerPC
PowerPC64
SPARC
SPARC64
)
var cpuMap = map[C.drmaa2_cpu]CPU{
C.DRMAA2_OTHER_CPU: OtherCPU,
C.DRMAA2_ALPHA: Alpha,
C.DRMAA2_ARM: ARM,
C.DRMAA2_ARM64: ARM64,
C.DRMAA2_CELL: Cell,
C.DRMAA2_PARISC: PA_RISC,
C.DRMAA2_PARISC64: PA_RISC64,
C.DRMAA2_X86: x86,
C.DRMAA2_X64: x64,
C.DRMAA2_IA64: IA_64,
C.DRMAA2_MIPS: MIPS,
C.DRMAA2_MIPS64: MIPS64,
C.DRMAA2_PPC: PowerPC,
C.DRMAA2_PPC64: PowerPC64,
C.DRMAA2_SPARC: SPARC,
C.DRMAA2_SPARC64: SPARC64,
}
func (cpu CPU) String() string {
switch cpu {
case OtherCPU:
return "OtherCPU"
case Alpha:
return "Alpha"
case ARM:
return "ARM"
case ARM64:
return "ARM64"
case Cell:
return "Cell"
case PA_RISC:
return "PA_RISC"
case PA_RISC64:
return "PA_RISC64"
case x86:
return "x86"
case x64:
return "x64"
case IA_64:
return "IA_64"
case MIPS:
return "MIPS"
case MIPS64:
return "MIPS64"
case PowerPC:
return "PowerPC"
case SPARC:
return "SPARC"
case SPARC64:
return "SPARC64"
}
return "Unknown"
}
// Operating System type
type OS int
const (
OtherOS OS = iota
AIX
BSD
Linux
HPUX
IRIX
MacOS
SunOS
TRU64
UnixWare
Win
WinNT
)
// An OS struct needs to be printable.
func (os OS) String() string {
switch os {
case OtherOS:
return "OtherOS"
case AIX:
return "AIX"
case BSD:
return "BSD"
case Linux:
return "Linux"
case HPUX:
return "HPUX"
case IRIX:
return "IRIX"
case MacOS:
return "MacOS"
case SunOS:
return "SunOS"
case TRU64:
return "TRU64"
case UnixWare:
return "UnixWare"
case Win:
return "Win"
case WinNT:
return "WinNT"
}
return "Unknown"
}
var osMap = map[C.drmaa2_os]OS{
C.DRMAA2_OTHER_OS: OtherOS,
C.DRMAA2_AIX: AIX,
C.DRMAA2_BSD: BSD,
C.DRMAA2_LINUX: Linux,
C.DRMAA2_HPUX: HPUX,
C.DRMAA2_IRIX: IRIX,
C.DRMAA2_MACOS: MacOS,
C.DRMAA2_SUNOS: SunOS,
C.DRMAA2_TRU64: TRU64,
C.DRMAA2_UNIXWARE: UnixWare,
C.DRMAA2_WIN: Win,
C.DRMAA2_WINNT: WinNT,
}
// Job States
type JobState int
const (
Undetermined JobState = iota
Queued
QueuedHeld
Running
Suspended
Requeued
RequeuedHeld
Done
Failed
)
// Implements the Stringer interface
func (js JobState) String() string {
switch js {
case Undetermined:
return "Undetermined"
case Queued:
return "Queued"
case QueuedHeld:
return "QueuedHeld"
case Running:
return "Running"
case Suspended:
return "Suspended"
case Requeued:
return "Requeued"
case RequeuedHeld:
return "RequeuedHeld"
case Done:
return "Done"
case Failed:
return "Failed"
}
return "Unknown"
}
var jobStateMap = map[C.drmaa2_jstate]JobState{
C.DRMAA2_UNDETERMINED: Undetermined,
C.DRMAA2_QUEUED: Queued,
C.DRMAA2_QUEUED_HELD: QueuedHeld,
C.DRMAA2_RUNNING: Running,
C.DRMAA2_SUSPENDED: Suspended,
C.DRMAA2_REQUEUED_HELD: RequeuedHeld,
C.DRMAA2_DONE: Done,
C.DRMAA2_FAILED: Failed,
}
// DRMAA2 error (implements GO Error interface).
type Error struct {
Message string
Id ErrorId
}
// The DRMAA2 Error implements the Error interface.
func (ce Error) Error() string {
return ce.Message
}
// Implement the Stringer interface for an drmaa2.Error
func (ce Error) String() string {
return ce.Message
}
// Intenal function which creats an GO DRMAA2 error.
func makeError(msg string, id ErrorId) Error {
var ce Error
ce.Message = msg
ce.Id = id
return ce
}
func makeLastError() *Error {
cerr := C.drmaa2_lasterror_text()
defer C.free(unsafe.Pointer(cerr))
msg := C.GoString(cerr)
id := C.drmaa2_lasterror()
err := makeError(msg, errorIdMap[id])
return &err
}
// TODO(dg) A Create Method which initializes the values and
// also does initialization about capabilities,
// versions etc. ?!?
type SessionManager struct {
//drmsName string
//drmsVersion string // type Version
//drmaaName string
//drmaaVersion string // type Version
}
type MonitoringSession struct {
name string // internal
ms C.drmaa2_msession // pointer to C drmaa2 session type
}
type JobSession struct {
Name string // public name of job session
js C.drmaa2_jsession // pointer to C drmaa2 job session type
}
type ReservationSession struct {
name string
rs C.drmaa2_rsession
}
type ReservationInfo struct {
ReservationId string
ReservationName string
ReservationStartTime time.Time
ReservationEndTime time.Time
ACL []string
ReservedSlots int64
ReservedMachines []string
}
type Job struct {
// job is private implementation specific (see struct drmaa2_j_s)
id string
session_name string
}
type JobInfo struct {
// reference to the void* pointer which
// is used for extensions
Extension
Id string
ExitStatus int
TerminatingSignal string
Annotation string
State JobState
SubState string
AllocatedMachines []string
SubmissionMachine string
JobOwner string
Slots int64
QueueName string
WallclockTime time.Duration
CPUTime int64
SubmissionTime time.Time
DispatchTime time.Time
FinishTime time.Time
}
type ArrayJob struct {
// needed for suspend / resume ...
aj C.drmaa2_jarray
id string
jobs []Job
sessionName string
jt JobTemplate
}
type Queue struct {
Extension
Name string
}
type Machine struct {
Extension
Name string
Available bool
Sockets int64
CoresPerSocket int64
ThreadsPerCore int64
Load float64
PhysicalMemory int64
VirtualMemory int64
Architecture CPU
OSVersion Version
OS OS
}
type JobTemplate struct {
Extension
RemoteCommand string
Args []string
SubmitAsHold bool
ReRunnable bool
JobEnvironment map[string]string
WorkingDirectory string
JobCategory string
Email []string
EmailOnStarted bool
EmailOnTerminated bool
JobName string
InputPath string
OutputPath string
ErrorPath string
JoinFiles bool
ReservationId string
QueueName string
MinSlots int64
MaxSlots int64
Priority int64
CandidateMachines []string
MinPhysMemory int64
MachineOs string
MachineArch string
StartTime time.Time
DeadlineTime time.Time
StageInFiles map[string]string
StageOutFiles map[string]string
ResourceLimits map[string]string
AccountingId string
}
type ReservationTemplate struct {
Extension
Name string
StartTime time.Time
EndTime time.Time
Duration time.Duration
MinSlots int64
MaxSlots int64
JobCategory string
UsersACL []string
CandidateMachines []string
MinPhysMemory int64
MachineOs string
MachineArch string
}
type Reservation struct {
SessionName string
Contact string
Template ReservationTemplate
ReservationId string
}
// this is needed since there is a difference between "" and nil
func convertGoStringToC(s string) C.drmaa2_string {
if s != "" {
return C.CString(s)
}
return nil
}
// Converts a JobTemplate in the C DRMAA2 equivalent
// and sets the values.
func convertGoJtemplateToC(jt JobTemplate) C.drmaa2_jtemplate {
cjt := C.malloc_jtemplate()
cjt.remoteCommand = convertGoStringToC(jt.RemoteCommand)
cjt.args = C.drmaa2_string_list(convertGoListToC(jt.Args))
cjt.submitAsHold = convertGoBoolToC(jt.SubmitAsHold)
cjt.rerunnable = convertGoBoolToC(jt.ReRunnable)
cjt.jobEnvironment = convertGoDictToC(jt.JobEnvironment)
cjt.workingDirectory = convertGoStringToC(jt.WorkingDirectory)
cjt.jobCategory = convertGoStringToC(jt.JobCategory)
cjt.email = C.drmaa2_string_list(convertGoListToC(jt.Email))
cjt.emailOnStarted = convertGoBoolToC(jt.EmailOnStarted)
cjt.emailOnTerminated = convertGoBoolToC(jt.EmailOnTerminated)
cjt.jobName = convertGoStringToC(jt.JobName)
cjt.inputPath = convertGoStringToC(jt.InputPath)
cjt.outputPath = convertGoStringToC(jt.OutputPath)
cjt.errorPath = convertGoStringToC(jt.ErrorPath)
cjt.joinFiles = convertGoBoolToC(jt.JoinFiles)
cjt.reservationId = convertGoStringToC(jt.ReservationId)
cjt.queueName = convertGoStringToC(jt.QueueName)
// TODO initialize JobTemplate with UNSET values!
if jt.MinSlots > 0 {
cjt.minSlots = C.longlong(jt.MinSlots)
}
if jt.MaxSlots > 0 {
cjt.maxSlots = C.longlong(jt.MaxSlots)
}
if jt.Priority != 0 {
cjt.priority = C.longlong(jt.Priority)
}
cjt.candidateMachines = C.drmaa2_string_list(convertGoListToC(jt.CandidateMachines))
if jt.MinPhysMemory > 0 {
cjt.minPhysMemory = C.longlong(jt.MinPhysMemory)
}
// machineOs
// machineArch
// startTime
// deadlineTime
cjt.stageInFiles = convertGoDictToC(jt.StageInFiles)
cjt.stageOutFiles = convertGoDictToC(jt.StageOutFiles)
cjt.resourceLimits = convertGoDictToC(jt.ResourceLimits)
cjt.accountingId = convertGoStringToC(jt.AccountingId)
return cjt
}
// Converts a element from a DRMAA2 list into
// the C counterpart and treat it like a void*
// pointer.
func convertListElement(element interface{}) unsafe.Pointer {
switch element.(type) {
case Job:
return unsafe.Pointer(convertGoJobToC(element.(Job)))
case string:
return unsafe.Pointer(C.CString(element.(string)))
default:
// unexpected type
log.Fatal("convertListElement unknown type")
}
return nil
}
// Data Type conversion
func convertCStringListToGo(cl C.drmaa2_string_list) []string {
length := int64(C.drmaa2_list_size(C.drmaa2_list(cl)))
list := make([]string, length, length)
for i := int64(0); i < length; i++ {
element := C.GoString(C.drmaa2_string(C.drmaa2_list_get(C.drmaa2_list(cl), C.long(i))))
list[i] = element
}
return list
}
func convertGoListToC(list interface{}) C.drmaa2_list {
var l C.drmaa2_list
switch list.(type) {
case []Job:
tlist := []Job(list.([]Job))
l = C.drmaa2_list_create(C.DRMAA2_JOBLIST, nil)
for _, e := range tlist {
C.drmaa2_list_add(l, unsafe.Pointer(convertGoJobToC(e)))
}
case []string:
tlist := []string(list.([]string))
l = C.drmaa2_list_create(C.DRMAA2_STRINGLIST, nil)
for _, e := range tlist {
C.drmaa2_list_add(l, unsafe.Pointer(C.CString(e)))
}
default:
// unexpected type
log.Fatal("convertGoListToC: unexpected type")
}
log.Print("now looping over all elements")
// for e := range []interface{}(list.([]interface{})) {
// C.drmaa2_list_add(l, convertListElement(e))
// }
return l
}
func convertGoBoolToC(value bool) C.drmaa2_bool {
if value == true {
return C.DRMAA2_TRUE
}
return C.DRMAA2_FALSE
}
func convertGoDictToC(dict map[string]string) C.drmaa2_dict {
// just initialize it with NULL
if dict == nil || len(dict) <= 0 {
return nil
}
cdict := C.drmaa2_dict_create(nil)
for k, v := range dict {
C.drmaa2_dict_set(C.drmaa2_dict(cdict), C.CString(k), C.CString(v))
}
return cdict
}
/* Helper for array jobs. */
func convertGoArrayJobToC(ja ArrayJob) C.drmaa2_jarray {
caj := C.malloc_array_job()
caj.id = C.CString(ja.id)
caj.session_name = C.CString(ja.sessionName)
return caj
}
func convertCArrayJobToGo(ja C.drmaa2_jarray) ArrayJob {
var aj ArrayJob
//aj.aj = ja
aj.id = C.GoString(ja.id)
aj.sessionName = C.GoString(ja.session_name)
aj.jobs = convertCJobListToGo(ja.job_list)
// add array job
jt := C.drmaa2_jarray_get_job_template(ja)
aj.jt = convertCJtemplateToGo(jt)
return aj
}
/* Methods working on job. */
func convertCJobToGo(cj C.drmaa2_j) Job {
var job Job
job.id = C.GoString(cj.id)
job.session_name = C.GoString(cj.session_name)
return job
}
func convertGoJobToC(job Job) C.drmaa2_j {
cjob := C.malloc_job()
cjob.id = C.CString(job.id)
cjob.session_name = C.CString(job.session_name)
return cjob
}
func (job *Job) GetId() string {
return job.id
}
func (job *Job) GetSessionName() string {
return job.session_name
}
func goBool(v C.drmaa2_bool) bool {
if v == C.DRMAA2_TRUE {
return true
}
return false
}
func goStringList(string_list C.drmaa2_string_list) []string {
strings := make([]string, 0)
if string_list != nil {
size := (int64)(C.drmaa2_list_size((C.drmaa2_list)(string_list)))
for i := (int64)(0); i < size; i++ {
cstr := (*C.char)(C.drmaa2_list_get((C.drmaa2_list)(string_list), C.long(i)))
strings = append(strings, C.GoString(cstr))
}
}
return strings
}
func goOS(os C.drmaa2_os) OS {
return osMap[os]
}
func goVersion(version C.drmaa2_version) (v Version) {
if version == nil {
v.Major = "0"
v.Minor = "0"
return v
}
v.Major = C.GoString(version.major)
v.Minor = C.GoString(version.minor)
return v
}
func goArchitecture(cpu C.drmaa2_cpu) CPU {
return cpuMap[cpu]
}
func goJobState(state C.drmaa2_jstate) JobState {
return jobStateMap[state]
}
// Creates a point in Time out of a C time stamp
func goTime(sec C.time_t) time.Time {
// if time C.DRMAA2_UNSET_TIME
return time.Unix((int64)(sec), (int64)(0))
}
// Creates a Duration out of a C time in seconds
func goDuration(sec C.time_t) time.Duration {
timeInSeconds := fmt.Sprintf("%ds", (int64)(sec))
duration, _ := time.ParseDuration(timeInSeconds)
return duration
}
// helper function for converting c jtemplate to go
func convertCJtemplateToGo(t C.drmaa2_jtemplate) JobTemplate {
var jt JobTemplate
jt.AccountingId = C.GoString(t.accountingId)
jt.Args = goStringList(t.args)
jt.EmailOnStarted = goBool(t.emailOnStarted)