The Distributed Resource Management Application API
by Frederic Pariente

Listing One

#include <stdio.h>
#include "drmaa.h"
int main (int argc, char **argv) {
   char error[DRMAA_ERROR_STRING_BUFFER];
   if (drmaa_init (NULL, error, DRMAA_ERROR_STRING_BUFFER)
      != DRMAA_ERRNO_SUCCESS) {
   fprintf (stderr, "Could not initialize the DRMAA library: %s\n", error);
   return 1;
}
fprintf (stdout, "Successfully started the DRMAA library\n");
if (drmaa_exit (error, DRMAA_ERROR_STRING_BUFFER)
      != DRMAA_ERRNO_SUCCESS) {
   fprintf (stderr, "Could not shut down the DRMAA library: %s\n", error);
   return 1;
   }
   return 0;
}


Listing Two

#include <stdio.h>
#include "drmaa.h"
int main (int argc, char **argv) {
   char error[DRMAA_ERROR_STRING_BUFFER];
   drmaa_job_template_t *jt = NULL;
   const char *args[2] = {"jobargument", NULL};
   char jobid[DRMAA_JOBNAME_BUFFER];
   if (drmaa_init (NULL, error, DRMAA_ERROR_STRING_BUFFER)
       != DRMAA_ERRNO_SUCCESS) {
   fprintf (stderr, "Could not initialize the DRMAA library: %s\n", error);
   return 1;
}
if (drmaa_allocate_job_template (&jt, error, DRMAA_ERROR_STRING_BUFFER)
       != DRMAA_ERRNO_SUCCESS) {
   fprintf (stderr, "Could not create job template: %s\n", error);
   goto exit_drmaa;
}
if (drmaa_set_attribute (jt, DRMAA_REMOTE_COMMAND, "jobname",
                error, DRMAA_ERROR_STRING_BUFFER) != DRMAA_ERRNO_SUCCESS) {
   fprintf (stderr, "Could not set attribute \"%s\": %s\n",
                                              DRMAA_REMOTE_COMMAND, error);
   goto exit_drmaa;
}
if (drmaa_set_vector_attribute (jt, DRMAA_V_ARGV, args, error,
      DRMAA_ERROR_STRING_BUFFER) != DRMAA_ERRNO_SUCCESS) {
   fprintf (stderr, "Could not set attribute \"%s\": %s\n",
                                              DRMAA_REMOTE_COMMAND, error);
   goto exit_drmaa;
}
fprintf (stdout, "Your job has been submitted with id %s\n", jobid);
if (drmaa_delete_job_template (jt, error, DRMAA_ERROR_STRING_BUFFER)
      != DRMAA_ERRNO_SUCCESS)
   fprintf (stderr, "Could not delete job template: %s\n", error);
exit_drmaa:
   if (drmaa_exit (error, DRMAA_ERROR_STRING_BUFFER)
      != DRMAA_ERRNO_SUCCESS) {
   fprintf (stderr, "Could not shut down the DRMAA library: %s\n", error);
   return 1;
  }
  return 0;
}

Listing Three

(...)
int status = 0;
drmaa_attr_values_t *rusage = NULL;
if (drmaa_wait (DRMAA_JOB_IDS_SESSION_ANY, jobid, DRMAA_JOBNAME_BUFFER, 
        &status, DRMAA_TIMEOUT_WAIT_FOREVER, &rusage,
        error, DRMAA_ERROR_STRING_BUFFER) != DRMAA_ERRNO_SUCCESS) {
   fprintf (stderr, "Could not wait for job: %s\n", error);
} else {
   char usage[DRMAA_ERROR_STRING_BUFFER];
   int aborted = 0;
   drmaa_wifaborted(&aborted, status, NULL, 0);
   if (aborted == 1) {
      printf("Job %s never ran\n", jobid);
   } else {
   int exited = 0;
   drmaa_wifexited(&exited, status, NULL, 0);
   if (exited == 1) {
      int exit_status = 0;
      drmaa_wexitstatus(&exit_status, status, NULL, 0);
      printf("Job %s finished regularly with exit status %d\n", 
                                                    jobid, exit_status);
   } else {
      int signaled = 0;
      drmaa_wifsignaled(&signaled, status, NULL, 0);
      if (signaled == 1) {
         char termsig[DRMAA_SIGNAL_BUFFER+1];
         drmaa_wtermsig(termsig, DRMAA_SIGNAL_BUFFER, status, NULL, 0);
         printf("Job %s finished due to signal %s\n", jobid, termsig);
      } else {
         printf("Job %s finished with unclear conditions\n", jobid);
      }
    }
  }
}
drmaa_release_attr_values (rusage);
(...)


Listing Four
#ifndef __DTHREAD_H
#define __DTHREAD_H
#include "drmaa.h"
typedef char dthread_t[DRMAA_JOBNAME_BUFFER];
typedef drmaa_job_template_t *dthread_attr_t;
extern int dthread_create (dthread_t *,const dthread_attr_t *,char *,void *);
extern int dthread_join (dthread_t, void **);
extern int dthread_attr_init (dthread_attr_t *);
extern int dthread_attr_destroy (dthread_attr_t *);
#endif /* __DTHREAD_H */


Listing Five

#include <pthread.h>
(...)
pthread_t tid;
void *price_func(void *), *arg;
float price;
pthread_attr_t attr;
(...)
/* spawn off computation to another CPU */
pthread_attr_init (&attr);
pthread_attr_setscope (&attr, PTHREAD_SCOPE_SYSTEM);
pthread_create (&tid, &attr, price_func, arg);
(...)
/* retrieve result */
pthread_join (tid, &price);

Listing Six

#include "dthread.h"
(...)
dthread_t tid;
char *price_func, *arg;
float price;
dthread_attr_t attr;
(...)
/* spawn off computation to the grid */
dthread_attr_init (&attr);
dthread_create (&tid, &attr, price_func, arg);
(...)
/* retrieve result */
dthread_join (tid, &price);


Listing Seven

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include "dthread.h"
void _init () {
   char error[DRMAA_ERROR_STRING_BUFFER];
   if (drmaa_init (NULL, error, DRMAA_ERROR_STRING_BUFFER)
      != DRMAA_ERRNO_SUCCESS)
   fprintf (stderr, "Could not initialize the dthread library: %s\n", error);
}
void _fini () {
   char error[DRMAA_ERROR_STRING_BUFFER];
   if (drmaa_exit (error, DRMAA_ERROR_STRING_BUFFER)
      != DRMAA_ERRNO_SUCCESS)
   fprintf (stderr, "Could not shut down the dthread library: %s\n", error);
}
int dthread_attr_init (dthread_attr_t *attr) {
   char error[DRMAA_ERROR_STRING_BUFFER];
   if (drmaa_allocate_job_template (attr, error, DRMAA_ERROR_STRING_BUFFER)
      != DRMAA_ERRNO_SUCCESS)
   return ELIBACC;
   if (drmaa_set_attribute (*attr, DRMAA_OUTPUT_PATH, ":/dev/null",
                 error, DRMAA_ERROR_STRING_BUFFER) != DRMAA_ERRNO_SUCCESS)
   return ELIBACC;
   if (drmaa_set_attribute (*attr, DRMAA_ERROR_PATH, ":/dev/null",
                 error, DRMAA_ERROR_STRING_BUFFER) != DRMAA_ERRNO_SUCCESS)
   return ELIBACC;
   return 0;
}
int dthread_attr_destroy (dthread_attr_t *attr) {
   char error[DRMAA_ERROR_STRING_BUFFER];
   drmaa_job_template_t *jt = *attr;
   if (drmaa_delete_job_template (jt, error, DRMAA_ERROR_STRING_BUFFER)
      != DRMAA_ERRNO_SUCCESS)
   return ELIBACC;
return 0;
}
int dthread_create (dthread_t *thread, const dthread_attr_t *attr,
                                            char *threadname, void *arg) {
   char error[DRMAA_ERROR_STRING_BUFFER];
   drmaa_job_template_t *jt = *attr;
   const char *args[2] = {arg, NULL};
   if (drmaa_set_attribute (jt, DRMAA_REMOTE_COMMAND, threadname,
      error, DRMAA_ERROR_STRING_BUFFER) != DRMAA_ERRNO_SUCCESS)
   return ELIBACC;
   if (drmaa_set_vector_attribute (jt, DRMAA_V_ARGV, args, error,
      DRMAA_ERROR_STRING_BUFFER) != DRMAA_ERRNO_SUCCESS)
   return ELIBACC;
   if (drmaa_run_job (thread, DRMAA_JOBNAME_BUFFER, jt, error,
      DRMAA_ERROR_STRING_BUFFER) != DRMAA_ERRNO_SUCCESS)
   return EAGAIN;
   return 0;
}
int dthread_join (dthread_t thread, void **returnvalue) {
   char error[DRMAA_ERROR_STRING_BUFFER];
   char jobid[DRMAA_JOBNAME_BUFFER];
   int status, exited;
   int *exit;
   drmaa_attr_values_t *rusage = NULL;
   char usage[DRMAA_ERROR_STRING_BUFFER];
   if (drmaa_wait (thread, jobid, DRMAA_JOBNAME_BUFFER,
             &status, DRMAA_TIMEOUT_WAIT_FOREVER, &rusage,
             error, DRMAA_ERROR_STRING_BUFFER) != DRMAA_ERRNO_SUCCESS)
   return ELIBACC;
   drmaa_wifexited (&exited, status, NULL, 0);
   if (exited != 1)
      return EINVAL;
   exit = (int *)malloc (sizeof (int));
   drmaa_wexitstatus (exit, status, NULL, 0);
   *returnvalue = exit;
   drmaa_release_attr_values (rusage);
   return 0;
}







5


