48
48
COMPONENT_NAME = "/Interfaces/API/Job"
49
49
50
50
51
+ class BadJobParameterError (ValueError ):
52
+ pass
53
+
54
+
51
55
class Job (API ):
52
56
"""DIRAC jobs"""
53
57
@@ -517,7 +521,10 @@ def setDestination(self, destination):
517
521
518
522
#############################################################################
519
523
def setNumberOfProcessors (self , numberOfProcessors = None , minNumberOfProcessors = None , maxNumberOfProcessors = None ):
520
- """Helper function.
524
+ """Helper function to set the number of processors required by the job.
525
+
526
+ The DIRAC_JOB_PROCESSORS environment variable can be used by the job to
527
+ determine how many processors are actually assigned.
521
528
522
529
Example usage:
523
530
@@ -551,75 +558,50 @@ def setNumberOfProcessors(self, numberOfProcessors=None, minNumberOfProcessors=N
551
558
552
559
>>> job = Job()
553
560
>>> job.setNumberOfProcessors(numberOfProcessors=3, maxNumberOfProcessors=4)
554
- will lead to ignore the second parameter
561
+ will result in a BadJobParameterError
555
562
556
563
>>> job = Job()
557
564
>>> job.setNumberOfProcessors(numberOfProcessors=3, minNumberOfProcessors=2)
558
- will lead to ignore the second parameter
565
+ will result in a BadJobParameterError
559
566
560
567
:param int processors: number of processors required by the job (exact number, unless a min/max are set)
561
568
:param int minNumberOfProcessors: optional min number of processors the job applications can use
562
569
:param int maxNumberOfProcessors: optional max number of processors the job applications can use
563
570
564
- :return: S_OK/S_ERROR
565
- """
566
- if numberOfProcessors :
567
- if not minNumberOfProcessors :
568
- nProc = numberOfProcessors
569
- else :
570
- nProc = max (numberOfProcessors , minNumberOfProcessors )
571
- if nProc > 1 :
572
- self ._addParameter (
573
- self .workflow , "NumberOfProcessors" , "JDL" , nProc , "Exact number of processors requested"
574
- )
575
- self ._addParameter (
576
- self .workflow ,
577
- "MaxNumberOfProcessors" ,
578
- "JDL" ,
579
- nProc ,
580
- "Max Number of processors the job applications may use" ,
581
- )
582
- return S_OK ()
571
+ :return: S_OK
583
572
584
- if maxNumberOfProcessors and not minNumberOfProcessors :
585
- minNumberOfProcessors = 1
573
+ :raises BadJobParameterError: If the function arguments are not valid.
574
+ """
575
+ # If min and max are the same then that's the same as setting numberOfProcessors
576
+ if minNumberOfProcessors and maxNumberOfProcessors and minNumberOfProcessors == maxNumberOfProcessors :
577
+ numberOfProcessors = minNumberOfProcessors
578
+ minNumberOfProcessors = maxNumberOfProcessors = None
586
579
587
- if minNumberOfProcessors and maxNumberOfProcessors and minNumberOfProcessors >= maxNumberOfProcessors :
588
- minNumberOfProcessors = maxNumberOfProcessors
580
+ if numberOfProcessors is not None :
581
+ if minNumberOfProcessors is not None :
582
+ raise BadJobParameterError ("minNumberOfProcessors cannot be used with numberOfProcessors" )
583
+ if maxNumberOfProcessors is not None :
584
+ raise BadJobParameterError ("maxNumberOfProcessors cannot be used with numberOfProcessors" )
589
585
590
- if (
591
- minNumberOfProcessors
592
- and maxNumberOfProcessors
593
- and minNumberOfProcessors == maxNumberOfProcessors
594
- and minNumberOfProcessors > 1
595
- ):
596
586
self ._addParameter (
597
- self .workflow ,
598
- "NumberOfProcessors" ,
599
- "JDL" ,
600
- minNumberOfProcessors ,
601
- "Exact number of processors requested" ,
587
+ self .workflow , "NumberOfProcessors" , "JDL" , numberOfProcessors , "Exact number of processors requested"
602
588
)
603
589
self ._addParameter (
604
590
self .workflow ,
605
591
"MaxNumberOfProcessors" ,
606
592
"JDL" ,
607
- minNumberOfProcessors ,
593
+ numberOfProcessors ,
608
594
"Max Number of processors the job applications may use" ,
609
595
)
610
596
return S_OK ()
611
597
612
- # By this point there should be a min
613
- self ._addParameter (
614
- self .workflow ,
615
- "MinNumberOfProcessors" ,
616
- "JDL" ,
617
- minNumberOfProcessors ,
618
- "Min Number of processors the job applications may use" ,
619
- )
598
+ if minNumberOfProcessors is None and maxNumberOfProcessors is None :
599
+ return S_OK ()
620
600
621
- # If not set, will be "all"
622
- if maxNumberOfProcessors :
601
+ minNumberOfProcessors = minNumberOfProcessors or 1
602
+ if maxNumberOfProcessors is not None :
603
+ if maxNumberOfProcessors < minNumberOfProcessors :
604
+ raise BadJobParameterError ("minNumberOfProcessors must be less than or equal to maxNumberOfProcessors" )
623
605
self ._addParameter (
624
606
self .workflow ,
625
607
"MaxNumberOfProcessors" ,
@@ -628,6 +610,14 @@ def setNumberOfProcessors(self, numberOfProcessors=None, minNumberOfProcessors=N
628
610
"Max Number of processors the job applications may use" ,
629
611
)
630
612
613
+ self ._addParameter (
614
+ self .workflow ,
615
+ "MinNumberOfProcessors" ,
616
+ "JDL" ,
617
+ minNumberOfProcessors ,
618
+ "Min Number of processors the job applications may use" ,
619
+ )
620
+
631
621
return S_OK ()
632
622
633
623
#############################################################################
0 commit comments