+ Reply to Thread
Results 1 to 3 of 3

Thread: Access the LSF APIs in Python (with help of SWIG)

  1. #1
    tmetsch is offline Junior Member
    Join Date
    May 20th, 2010
    Posts
    3
    Downloads
    0
    Uploads
    0

    Default Access the LSF APIs in Python (with help of SWIG)

    To access the LSF APIs in Python I used SWIG to map lsf.h (Adding lsbatch is possible but left out to reduce complexity). First step is the creation of the lsf.i file which SWIG will use:

    Code:
    /* File: lsf.i */
    %module lsf
    
    %{
    #define SWIG_FILE_WITH_INIT
    #include "lsf/lsf.h"
    %}
    %ignore ls_placeoftype;
    %ignore ls_loadoftype;
    %ignore ls_lostconnection;
    %ignore ls_gethostrespriority;
    %ignore ls_verrlog;
    %ignore getBEtime;
    %ignore ls_nioinit;
    %ignore ls_nioselect;
    %ignore ls_nioctl;
    %ignore ls_nionewtask;
    %ignore ls_nioremovetask;
    %ignore ls_niowrite;
    %ignore ls_nioclose;
    %ignore ls_nioread;
    %ignore ls_niotasks;
    %ignore ls_niostatus;
    %ignore ls_niokill;
    %ignore ls_niosetdebug;
    %ignore ls_niodump;
    %ignore ls_readrexlog;
    
    // howto handle char**
    %typemap(in) char ** {
        int size = PyList_Size($input);
        int i = 0;
        $1 = (char **) malloc((size+1)*sizeof(char *));
        for (i = 0; i < size; i++) {
          PyObject *o = PyList_GetItem($input,i);
          $1[i] = PyBytes_AsString(PyUnicode_AsUTF8String(PyList_GetItem($input,i)));
        }
        $1[i] = 0;
    }
    // cleanup char**
    %typemap(freearg) char ** {
      free((char *) $1);
    }
    //
    %include "lsf/lsf.h"
    
    It does nothing more then saying: please provided me with all which is defined in the lsf.h file. This will not work out of the box and some routines need to be excluded/ignored. Next to that we need to define a so called typemap to convert a Python list to a char **. More typemaps might be needed for e.g. return values, and other LSG APIs calls but are left out to reduce complexity.

    Now run SWIG and compile the code:

    Code:
    swig -python lsf.i
    gcc -c lsf_wrap.c -I/usr/include/python3.1
    ld -shared lsf_wrap.o -o _lsf.so ${LSF_LIBDIR}/liblsf.a ${LSF_LIBDIR}/libbat.a -lc
    
    The result will be a _lsf.so and lsf.py file which now can be used:

    Code:
    import lsf
    
    if __name__ == '__main__':
        print(lsf.ls_getclustername())
        print(lsf.ls_initrex(1, 0))
        print(lsf.ls_rexecv("ubuntu", ["/bin/ls"], 0))
    
    I run this example on a Ubuntu 10.04 machine running Python 3.1; SWIG 1.3.40 and LSF 7.0.6
    Last edited by tmetsch; May 21st, 2010 at 10:22 AM.

  2. #2
    tmetsch is offline Junior Member
    Join Date
    May 20th, 2010
    Posts
    3
    Downloads
    0
    Uploads
    0

    Default Job submission using the Python APIs

    The example of a job submission in the Platform LSF Programmer's Guide written in C (Page 64, LSF Version 7.0.6) would reflect to the following code in Python:

    Code:
        submitreq = lsf.submit()
        submitreq.command = "/bin/sleep 1000"
        submitreq.options = 0
        submitreq.options2 = 0
    
        limits = []
        for i in range (0, lsf.LSF_RLIM_NLIMITS):
            limits.append(lsf.DEFAULT_RLIMIT)
    
        submitreq.rLimits = limits
    
        submitreq.beginTime = 0
        submitreq.termTime = 0
        submitreq.numProcessors = 1
        submitreq.maxNumProcessors = 1;
    
        submitreply = lsf.submitReply()
    
        print(lsf.lsb_init("test"))
        print(lsf.lsb_submit(submitreq, submitreply))
    
    The following lsf.i file was used for SWIG:

    Code:
    /* File: lsf.i */
    %module lsf
    
    %{
    #define SWIG_FILE_WITH_INIT
    #include "/opt/lsf/7.0/include/lsf/lsf.h"
    #include "/opt/lsf/7.0/include/lsf/lsbatch.h"
    %}
    
    %ignore ls_placeoftype;
    %ignore ls_loadoftype;
    %ignore ls_lostconnection;
    %ignore ls_gethostrespriority;
    %ignore ls_verrlog;
    %ignore getBEtime;
    %ignore ls_nioinit;
    %ignore ls_nioselect;
    %ignore ls_nioctl;
    %ignore ls_nionewtask;
    %ignore ls_nioremovetask;
    %ignore ls_niowrite;
    %ignore ls_nioclose;
    %ignore ls_nioread;
    %ignore ls_niotasks;
    %ignore ls_niostatus;
    %ignore ls_niokill;
    %ignore ls_niosetdebug;
    %ignore ls_niodump;
    %ignore ls_readrexlog;
    
    // howto handle char **
    %typemap(in) char ** {
        int size = PyList_Size($input);
        int i = 0;
        $1 = (char **) malloc((size+1)*sizeof(char *));
        for (i = 0; i < size; i++) {
          PyObject *o = PyList_GetItem($input,i);
          $1[i] = PyBytes_AsString(PyUnicode_AsUTF8String(PyList_GetItem($input,i)));
        }
        $1[i] = 0;
    }
    
    // cleanup of char **
    %typemap(freearg) char ** {
      free((char *) $1);
    }
    
    // typemap for time_t
    %typemap(in) time_t {
        $1 = (time_t) PyLong_AsLong($input);
    }
    
    %typemap(out) time_t {
        $result = PyLong_FromLong((long)$1);
    }
    
    %typemap(freearg) time_t {
        free((time_t *) $1);
    }
    
    // handle int arrays in struct...
    %typemap(in) int [ANY] (int temp[$1_dim0]) {
      int i;
      for (i = 0; i < $1_dim0; i++) {
        PyObject *o = PySequence_GetItem($input,i);
          temp[i] = (int) PyInt_AsLong(o);
      }
      $1 = temp;
    }
    // allow to set members of int array
    %typemap(memberin) int [ANY] {
      int i;
      for (i = 0; i < $1_dim0; i++) {
          $1[i] = $input[i];
      }
    }
    // access int arrays
    %typemap(out) int [ANY] {
      int i;
      $result = PyList_New($1_dim0);
      for (i = 0; i < $1_dim0; i++) {
        PyObject *o = PyLong_FromDouble((int) $1[i]);
        PyList_SetItem($result,i,o);
      }
    }
    
    //
    %include "/opt/lsf/7.0/include/lsf/lsf.h"
    %include "/opt/lsf/7.0/include/lsf/lsbatch.h"
    

  3. #3
    tmetsch is offline Junior Member
    Join Date
    May 20th, 2010
    Posts
    3
    Downloads
    0
    Uploads
    0

    Default Python LSF wrapper

    This is released under an LGPL license: https://github.com/tmetsch/pylsf

+ Reply to Thread

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts