| 1 |
Distributed Resource Management Application API |
| 2 |
=============================================== |
| 3 |
|
| 4 |
This guide is a tutorial for getting started programming with DRMAA. It is |
| 5 |
basically a one to one translation of the original in C for Grid Engine. It |
| 6 |
assumes that you already know what DRMAA is and that you have drmaa-python |
| 7 |
installed. If not, have a look at Installing. The following code segments are |
| 8 |
also included in the repository. |
| 9 |
|
| 10 |
Starting and Stopping a Session |
| 11 |
------------------------------- |
| 12 |
|
| 13 |
The following two code segments show the most basic DRMAA python binding |
| 14 |
program. |
| 15 |
|
| 16 |
.. code-block:: python |
| 17 |
:linenos: |
| 18 |
:caption: example1.py |
| 19 |
:name: example-1 |
| 20 |
|
| 21 |
#!/usr/bin/env python |
| 22 |
|
| 23 |
import drmaa |
| 24 |
|
| 25 |
def main(): |
| 26 |
"""Create a drmaa session and exit""" |
| 27 |
with drmaa.Session() as s: |
| 28 |
print('A session was started successfully') |
| 29 |
|
| 30 |
if __name__=='__main__': |
| 31 |
main() |
| 32 |
|
| 33 |
The first thing to notice is that every call to a DRMAA function will return an |
| 34 |
error code. In this tutorial, we ignore all error codes. |
| 35 |
|
| 36 |
Now let's look at the functions being called. First, on line 7, we initialise a |
| 37 |
``Session`` object by calling ``drmaa.Session()``. The ``Session`` is |
| 38 |
automatically initialized via ``initialize()``, and it creates a session and starts |
| 39 |
an event client listener thread. The session is used for organizing jobs |
| 40 |
submitted through DRMAA, and the thread is used to receive updates from the |
| 41 |
queue master about the state of jobs and the system in general. Once |
| 42 |
``initialize()`` has been called successfully, it is the responsibility of the |
| 43 |
calling application to also call ``exit()`` before terminating, but since we're |
| 44 |
using it as a context manager via the ``with`` statement, that will happene |
| 45 |
automatically. If an application does not call ``exit()`` before terminating, |
| 46 |
session state may be left behind in the user's home directory, and the queue |
| 47 |
master may be left with a dead event client handle, which can decrease queue |
| 48 |
master performance. |
| 49 |
|
| 50 |
At the end of our program, ``exit()`` is called automatically when we leave the |
| 51 |
``with`` block. ``exit()`` cleans up the session and stops the event client |
| 52 |
listener thread. Most other DRMAA functions must be called before ``exit()``. |
| 53 |
Some functions, like ``getContact()``, can be called after ``exit()``, but these |
| 54 |
functions only provide general information. Any function that does work, such as |
| 55 |
``runJob()`` or ``wait()`` must be called before ``exit()`` is called. If such a |
| 56 |
function is called after ``exit()`` is called, it will return an error. |
| 57 |
|
| 58 |
.. code-block:: python |
| 59 |
:linenos: |
| 60 |
:caption: example1.1.py |
| 61 |
:name: example-1-1 |
| 62 |
:emphasize-lines: 13,15,18,19 |
| 63 |
|
| 64 |
#!/usr/bin/env python |
| 65 |
|
| 66 |
import drmaa |
| 67 |
|
| 68 |
def main(): |
| 69 |
""" |
| 70 |
Create a session, show that each session has an ID, use session ID to |
| 71 |
disconnect, then reconnect. Finally, exit. |
| 72 |
""" |
| 73 |
s = drmaa.Session() |
| 74 |
s.initialize() |
| 75 |
print('A session was started successfully') |
| 76 |
response = s.contact |
| 77 |
print('session contact returns: %s' % response) |
| 78 |
s.exit() |
| 79 |
print('Exited from session') |
| 80 |
|
| 81 |
s.initialize(response) |
| 82 |
print('Session was restarted successfullly') |
| 83 |
s.exit() |
| 84 |
|
| 85 |
|
| 86 |
if __name__=='__main__': |
| 87 |
main() |
| 88 |
|
| 89 |
This example is very similar to :ref:`Example 1 <example-1>`. The difference is |
| 90 |
that it uses the Grid Engine feature of reconnectable sessions. The DRMAA |
| 91 |
concept of a session is translated into a session tag in the Grid Engine job |
| 92 |
structure. That means that every job knows to which session it belongs. With |
| 93 |
reconnectable sessions, it's possible to initialize the DRMAA library to a |
| 94 |
previous session, allowing the library access to that session's job list. The |
| 95 |
only limitation, though, is that jobs which end between the calls to ``exit()`` |
| 96 |
and ``init()`` will be lost, as the reconnecting session will no longer see |
| 97 |
these jobs, and so won't know about them. |
| 98 |
|
| 99 |
On line 13, we use the contact attribute to get the contact information for this |
| 100 |
session. On line 15 we then exit the session. On line 18, we use the stored |
| 101 |
contact information to reconnect to the previous session. Had we submitted jobs |
| 102 |
before calling ``exit()``, those jobs would now be available again for |
| 103 |
operations such as ``wait()`` and ``synchronize()``. Finally, on line 19 we exit |
| 104 |
the session a second time. |
| 105 |
|
| 106 |
Running a Job |
| 107 |
------------- |
| 108 |
|
| 109 |
The following code segments show how to use the DRMAA python binding to |
| 110 |
submit a job to Grid Engine. It submits a small shell script (``sleeper.sh``) |
| 111 |
which takes two arguments: |
| 112 |
|
| 113 |
.. code-block:: bash |
| 114 |
|
| 115 |
#!/bin/bash |
| 116 |
echo "Hello world, the answer is $1" |
| 117 |
sleep 3s |
| 118 |
echo "$2 Bye world!" |
| 119 |
|
| 120 |
.. code-block:: python |
| 121 |
:linenos: |
| 122 |
:caption: example2.py |
| 123 |
:name: example-2 |
| 124 |
:emphasize-lines: 12-22 |
| 125 |
|
| 126 |
#!/usr/bin/env python |
| 127 |
|
| 128 |
import drmaa |
| 129 |
import os |
| 130 |
|
| 131 |
def main(): |
| 132 |
""" |
| 133 |
Submit a job. |
| 134 |
Note, need file called sleeper.sh in current directory. |
| 135 |
""" |
| 136 |
with drmaa.Session() as s: |
| 137 |
print('Creating job template') |
| 138 |
jt = s.createJobTemplate() |
| 139 |
jt.remoteCommand = os.path.join(os.getcwd(), 'sleeper.sh') |
| 140 |
jt.args = ['42', 'Simon says:'] |
| 141 |
jt.joinFiles=True |
| 142 |
|
| 143 |
jobid = s.runJob(jt) |
| 144 |
print('Your job has been submitted with ID %s' % jobid) |
| 145 |
|
| 146 |
print('Cleaning up') |
| 147 |
s.deleteJobTemplate(jt) |
| 148 |
|
| 149 |
if __name__=='__main__': |
| 150 |
main() |
| 151 |
|
| 152 |
The beginning and end of this program are the same as the first example. What's |
| 153 |
new is in lines 12--22. On line 12 we ask DRMAA to allocate a job template |
| 154 |
for us. A job template is a structure used to store information about a job to |
| 155 |
be submitted. The same template can be reused for multiple calls to ``runJob()`` |
| 156 |
or ``runBulkJob()``. |
| 157 |
|
| 158 |
On line 13 we set the ``remoteCommand`` attribute. This attribute tells DRMAA |
| 159 |
where to find the program we want to run. Its value is the path to the |
| 160 |
executable. The path can be either relative or absolute. If relative, it is |
| 161 |
relative to the ``workingDirectory`` attribute, which if not set defaults to the |
| 162 |
user's home directory. For more information on DRMAA attributes, please see the |
| 163 |
attributes man page. Note that for this program to work, the script |
| 164 |
``sleeper.sh`` must be in the current directory. |
| 165 |
|
| 166 |
On line 14 we set the ``args`` attribute. This attribute tells DRMAA what |
| 167 |
arguments to pass to the executable. |
| 168 |
|
| 169 |
On line 17 we submit the job with ``runJob()``. DRMAA will place the ID assigned |
| 170 |
to the job into the character array we passed to ``runJob()``. The job is now |
| 171 |
running as though submitted by ``qsub`` or ``bsub``. At this point calling |
| 172 |
``exit()`` and/or terminating the program will have no effect on the job. |
| 173 |
|
| 174 |
To clean things up, we delete the job template on line 21. This frees the memory |
| 175 |
DRMAA set aside for the job template, but has no effect on submitted jobs. |
| 176 |
|
| 177 |
If instead of a single job we had wanted to submit an array job, we could have |
| 178 |
replaced the else on line 18 and 19 with the following: |
| 179 |
|
| 180 |
.. code-block:: python |
| 181 |
:caption: example2.1.py |
| 182 |
:name: example-2-1 |
| 183 |
|
| 184 |
jobid = s.runBulkJobs(jt, 1, 30, 2) |
| 185 |
print('Your jobs have been submitted with IDs %s' % jobid) |
| 186 |
|
| 187 |
This code segment submits an array job with 15 tasks numbered 1, 3, 5, 7, etc. |
| 188 |
An important difference to note is that ``runBulkJobs()`` returns the job ids in |
| 189 |
a list. On the last line, we print all the job ids. |
| 190 |
|
| 191 |
Waiting for a Job |
| 192 |
----------------- |
| 193 |
|
| 194 |
Now we're going to extend our example to include waiting for a job to finish. |
| 195 |
|
| 196 |
|
| 197 |
.. code-block:: python |
| 198 |
:linenos: |
| 199 |
:caption: example3.py |
| 200 |
:name: example-3 |
| 201 |
:emphasize-lines: 21-22 |
| 202 |
|
| 203 |
#!/usr/bin/env python |
| 204 |
|
| 205 |
import drmaa |
| 206 |
import os |
| 207 |
|
| 208 |
def main(): |
| 209 |
""" |
| 210 |
Submit a job and wait for it to finish. |
| 211 |
Note, need file called sleeper.sh in home directory. |
| 212 |
""" |
| 213 |
with drmaa.Session() as s: |
| 214 |
print('Creating job template') |
| 215 |
jt = s.createJobTemplate() |
| 216 |
jt.remoteCommand = os.path.join(os.getcwd(), 'sleeper.sh') |
| 217 |
jt.args = ['42', 'Simon says:'] |
| 218 |
jt.joinFiles = True |
| 219 |
|
| 220 |
jobid = s.runJob(jt) |
| 221 |
print('Your job has been submitted with ID %s' % jobid) |
| 222 |
|
| 223 |
retval = s.wait(jobid, drmaa.Session.TIMEOUT_WAIT_FOREVER) |
| 224 |
print('Job: {0} finished with status {1}'.format(retval.jobId, retval.hasExited)) |
| 225 |
|
| 226 |
print('Cleaning up') |
| 227 |
s.deleteJobTemplate(jt) |
| 228 |
|
| 229 |
if __name__=='__main__': |
| 230 |
main() |
| 231 |
|
| 232 |
|
| 233 |
This example is very similar to :ref:`Example 2 <example-2>` except for line |
| 234 |
21. On line 21 we call ``wait()`` to wait for the job to end. We have to give |
| 235 |
``wait()`` both the ID of the job for which we want to wait, and also how long |
| 236 |
we are willing to wait for the job to finish. This could be a number of |
| 237 |
seconds, or it could be either ``TIMEOUT_WAIT_FOREVER`` or ``TIMEOUT_NO_WAIT``. |
| 238 |
``wait()`` returns a ``JobInfo`` tuple, which has the following attributes: |
| 239 |
``jobId``, ``hasExited``, ``hasSignal``, ``terminatedSignal``, ``hasCoreDump``, |
| 240 |
``wasAborted``, ``exitStatus``, and ``resourceUsage``. ``jobId`` is |
| 241 |
particularly useful if we passed in ``JOB_IDS_SESSION_ANY`` as the ID argument |
| 242 |
for ``wait()``, because without it we would have no way of knowing which job it |
| 243 |
actually waited for. Lastly, we print out the job ID and the exit status on |
| 244 |
line 22. |
| 245 |
|
| 246 |
An alternative to ``wait()`` when working with multiple jobs, such as jobs |
| 247 |
submitted by ``runBulkJobs()`` or multiple calls to ``runJob()`` is |
| 248 |
``synchronize()``. ``synchronize()`` waits for a set of jobs to finish. To use |
| 249 |
``synchronize()``, we could replace lines 18--22 with the following: |
| 250 |
|
| 251 |
.. code-block:: python |
| 252 |
:caption: example3.1.py |
| 253 |
:name: example-3-1 |
| 254 |
|
| 255 |
joblist = s.runBulkJobs(jt, 1, 30, 2) |
| 256 |
print('Your jobs have been submitted with IDs %s' % joblist) |
| 257 |
|
| 258 |
s.synchronize(joblist, drmaa.Session.TIMEOUT_WAIT_FOREVER, True) |
| 259 |
|
| 260 |
|
| 261 |
With the switch to ``runBulkJobs()`` we now have several jobs for which to wait. |
| 262 |
On line 20, instead of calling ``wait()``, we call ``synchronize()``. |
| 263 |
``synchronize()`` takes only three interesting parameters. The first is the list |
| 264 |
of string IDs for which to wait. If the special ID, ``JOB_IDS_SESSION_ALL``, |
| 265 |
appears in the array, ``synchronize()`` will wait for all jobs submitted via |
| 266 |
DRMAA during this session (i.e., since ``initialize()`` was called). The second |
| 267 |
is how long to wait for all the jobs in the list to finish. This is the same as |
| 268 |
the timeout parameter for ``wait()``. The third is whether this call to |
| 269 |
``synchronize()`` should clean up after the job. After a job completes, it |
| 270 |
leaves behind accounting information, such as exit status and usage, until |
| 271 |
either ``wait()`` or ``synchronize()`` with ``dispose`` set to ``True`` is |
| 272 |
called. It is the responsibility of the application to make sure one of these |
| 273 |
two functions is called for every job. Not doing so creates a memory leak. Note |
| 274 |
that calling ``synchronize()`` with dispose set to true flushes all accounting |
| 275 |
information for all jobs in the list. If you want to use ``synchronize()`` and |
| 276 |
still recover the accounting information, set ``dispose`` to ``False`` and call |
| 277 |
``wait()`` for each job. To do this in :ref:`Example 3 <example-3>`, we would |
| 278 |
replace lines 18--22 with the following: |
| 279 |
|
| 280 |
.. code-block:: python |
| 281 |
:caption: example3.2.py |
| 282 |
:name: example-3-2 |
| 283 |
|
| 284 |
joblist = s.runBulkJobs(jt, 1, 30, 2) |
| 285 |
print('Your jobs have been submitted with IDs %s' % joblist) |
| 286 |
|
| 287 |
s.synchronize(joblist, drmaa.Session.TIMEOUT_WAIT_FOREVER, False) |
| 288 |
for curjob in joblist: |
| 289 |
print('Collecting job ' + curjob) |
| 290 |
retval = s.wait(curjob, drmaa.Session.TIMEOUT_WAIT_FOREVER) |
| 291 |
print('Job: {0} finished with status {1}'.format(retval.jobId, |
| 292 |
retval.hasExited)) |
| 293 |
|
| 294 |
|
| 295 |
What's different is that on line 21 we set ``dispose`` to ``False``, and then on |
| 296 |
lines 22--26 we wait once for each job, printing the exit status and usage |
| 297 |
information as we did in :ref:`Example 3 <example-3>`. |
| 298 |
|
| 299 |
We pass ``joblist`` to ``synchronize()`` to wait for each job specifically. |
| 300 |
Otherwise, the ``wait()`` could end up waiting for a job submitted after the |
| 301 |
call to ``synchronize()``. |
| 302 |
|
| 303 |
Controlling a Job |
| 304 |
----------------- |
| 305 |
|
| 306 |
Now let's look at an example of how to control a job from DRMAA: |
| 307 |
|
| 308 |
.. code-block:: python |
| 309 |
:linenos: |
| 310 |
:caption: example4.py |
| 311 |
:name: example-4 |
| 312 |
:emphasize-lines: 20 |
| 313 |
|
| 314 |
#!/usr/bin/env python |
| 315 |
|
| 316 |
import drmaa |
| 317 |
import os |
| 318 |
|
| 319 |
def main(): |
| 320 |
"""Submit a job, then kill it. |
| 321 |
Note, need file called sleeper.sh in home directory. |
| 322 |
""" |
| 323 |
with drmaa.Session() as s: |
| 324 |
print('Creating job template') |
| 325 |
jt = s.createJobTemplate() |
| 326 |
jt.remoteCommand = os.path.join(os.getcwd(), 'sleeper.sh') |
| 327 |
jt.args = ['42', 'Simon says:'] |
| 328 |
jt.joinFiles = True |
| 329 |
|
| 330 |
jobid = s.runJob(jt) |
| 331 |
print('Your job has been submitted with ID %s' % jobid) |
| 332 |
# options are: SUSPEND, RESUME, HOLD, RELEASE, TERMINATE |
| 333 |
s.control(jobid, drmaa.JobControlAction.TERMINATE) |
| 334 |
|
| 335 |
print('Cleaning up') |
| 336 |
s.deleteJobTemplate(jt) |
| 337 |
|
| 338 |
if __name__=='__main__': |
| 339 |
main() |
| 340 |
|
| 341 |
|
| 342 |
This example is very similar to :ref:`Example 2 <example-2>` except for line |
| 343 |
20. On line 20 we use ``control()`` to delete the job we just submitted. Aside |
| 344 |
from deleting the job, we could have also used ``control()`` to suspend, |
| 345 |
resume, hold, or release it. |
| 346 |
|
| 347 |
Note that ``control()`` can be used to control jobs not submitted through DRMAA. |
| 348 |
Any valid SGE job ID could be passed to ``control()`` as the ID of the job to |
| 349 |
delete. |
| 350 |
|
| 351 |
Getting Job Status |
| 352 |
------------------ |
| 353 |
|
| 354 |
Here's an example of using DRMAA to query the status of a job: |
| 355 |
|
| 356 |
.. code-block:: python |
| 357 |
:linenos: |
| 358 |
:caption: example5.py |
| 359 |
:name: example-5 |
| 360 |
:emphasize-lines: 22-40 |
| 361 |
|
| 362 |
#!/usr/bin/env python |
| 363 |
|
| 364 |
import drmaa |
| 365 |
import time |
| 366 |
import os |
| 367 |
|
| 368 |
def main(): |
| 369 |
""" |
| 370 |
Submit a job, and check its progress. |
| 371 |
Note, need file called sleeper.sh in home directory. |
| 372 |
""" |
| 373 |
with drmaa.Session() as s: |
| 374 |
print('Creating job template') |
| 375 |
jt = s.createJobTemplate() |
| 376 |
jt.remoteCommand = os.path.join(os.getcwd(), 'sleeper.sh') |
| 377 |
jt.args = ['42', 'Simon says:'] |
| 378 |
jt.joinFiles=True |
| 379 |
|
| 380 |
jobid = s.runJob(jt) |
| 381 |
print('Your job has been submitted with ID %s' % jobid) |
| 382 |
|
| 383 |
# Who needs a case statement when you have dictionaries? |
| 384 |
decodestatus = {drmaa.JobState.UNDETERMINED: 'process status cannot be determined', |
| 385 |
drmaa.JobState.QUEUED_ACTIVE: 'job is queued and active', |
| 386 |
drmaa.JobState.SYSTEM_ON_HOLD: 'job is queued and in system hold', |
| 387 |
drmaa.JobState.USER_ON_HOLD: 'job is queued and in user hold', |
| 388 |
drmaa.JobState.USER_SYSTEM_ON_HOLD: 'job is queued and in user and system hold', |
| 389 |
drmaa.JobState.RUNNING: 'job is running', |
| 390 |
drmaa.JobState.SYSTEM_SUSPENDED: 'job is system suspended', |
| 391 |
drmaa.JobState.USER_SUSPENDED: 'job is user suspended', |
| 392 |
drmaa.JobState.DONE: 'job finished normally', |
| 393 |
drmaa.JobState.FAILED: 'job finished, but failed'} |
| 394 |
|
| 395 |
for ix in range(10): |
| 396 |
print('Checking %s of 10 times' % ix) |
| 397 |
print decodestatus(s.jobStatus(jobid)) |
| 398 |
time.sleep(5) |
| 399 |
|
| 400 |
print('Cleaning up') |
| 401 |
s.deleteJobTemplate(jt) |
| 402 |
|
| 403 |
if __name__=='__main__': |
| 404 |
main() |
| 405 |
|
| 406 |
Again, this example is very similar to :ref:`Example 2 <example-2>`, this time |
| 407 |
with the exception of lines 22--40. On line 36, we use ``jobStatus()`` to get |
| 408 |
the status of the job. Line 43 determine what the job status is and report it. |
| 409 |
|
| 410 |
Getting DRM information |
| 411 |
----------------------- |
| 412 |
|
| 413 |
Lastly, let's look at how to query the DRMAA library for information about the |
| 414 |
DRMS and the DRMAA implementation itself: |
| 415 |
|
| 416 |
.. code-block:: python |
| 417 |
:linenos: |
| 418 |
:caption: example6.py |
| 419 |
:name: example-6 |
| 420 |
:emphasize-lines: 9-12 |
| 421 |
|
| 422 |
#!/usr/bin/env python |
| 423 |
|
| 424 |
import drmaa |
| 425 |
|
| 426 |
def main(): |
| 427 |
""" Query the system. """ |
| 428 |
with drmaa.Session() as s: |
| 429 |
print('A DRMAA object was created') |
| 430 |
print('Supported contact strings: %s' % s.contact) |
| 431 |
print('Supported DRM systems: %s' % s.drmsInfo) |
| 432 |
print('Supported DRMAA implementations: %s' % s.drmaaImplementation) |
| 433 |
print('Version %s' % s.version) |
| 434 |
|
| 435 |
print('Exiting') |
| 436 |
|
| 437 |
if __name__=='__main__': |
| 438 |
main() |
| 439 |
|
| 440 |
On line 9, we get the contact string list. This is the list of contact strings |
| 441 |
that will be understood by this DRMAA instance. Normally on of these strings is |
| 442 |
used to select to which DRM this DRMAA instance should be bound. On line 10, we |
| 443 |
get the list of supported DRM systems. On line 11, we get the list of supported |
| 444 |
DRMAA implementations. On line 12, we get the version number of the DRMAA C |
| 445 |
binding specification supported by this DRMAA implementation. |