~ubuntu-branches/ubuntu/utopic/gridengine/utopic

« back to all changes in this revision

Viewing changes to source/libs/japi/howto/howto3_2.c

  • Committer: Bazaar Package Importer
  • Author(s): Mark Hymers
  • Date: 2008-06-25 22:36:13 UTC
  • Revision ID: james.westby@ubuntu.com-20080625223613-tvd9xlhuoct9kyhm
Tags: upstream-6.2~beta2
ImportĀ upstreamĀ versionĀ 6.2~beta2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*___INFO__MARK_BEGIN__*/
 
2
/*************************************************************************
 
3
 * 
 
4
 *  The Contents of this file are made available subject to the terms of
 
5
 *  the Sun Industry Standards Source License Version 1.2
 
6
 * 
 
7
 *  Sun Microsystems Inc., March, 2001
 
8
 * 
 
9
 * 
 
10
 *  Sun Industry Standards Source License Version 1.2
 
11
 *  =================================================
 
12
 *  The contents of this file are subject to the Sun Industry Standards
 
13
 *  Source License Version 1.2 (the "License"); You may not use this file
 
14
 *  except in compliance with the License. You may obtain a copy of the
 
15
 *  License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
 
16
 * 
 
17
 *  Software provided under this License is provided on an "AS IS" basis,
 
18
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 
19
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 
20
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 
21
 *  See the License for the specific provisions governing your rights and
 
22
 *  obligations concerning the Software.
 
23
 * 
 
24
 *   The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 
25
 * 
 
26
 *   Copyright: 2001 by Sun Microsystems, Inc.
 
27
 * 
 
28
 *   All Rights Reserved.
 
29
 * 
 
30
 ************************************************************************/
 
31
/*___INFO__MARK_END__*/
 
32
#include <stdio.h>
 
33
#include "drmaa.h"
 
34
 
 
35
int main (int argc, char **argv) {
 
36
   char error[DRMAA_ERROR_STRING_BUFFER];
 
37
   int errnum = 0;
 
38
   drmaa_job_template_t *jt = NULL;
 
39
 
 
40
   errnum = drmaa_init (NULL, error, DRMAA_ERROR_STRING_BUFFER);
 
41
 
 
42
   if (errnum != DRMAA_ERRNO_SUCCESS) {
 
43
      fprintf (stderr, "Could not initialize the DRMAA library: %s\n", error);
 
44
      return 1;
 
45
   }
 
46
 
 
47
   errnum = drmaa_allocate_job_template (&jt, error, DRMAA_ERROR_STRING_BUFFER);
 
48
 
 
49
   if (errnum != DRMAA_ERRNO_SUCCESS) {
 
50
      fprintf (stderr, "Could not create job template: %s\n", error);
 
51
   }
 
52
   else {
 
53
      errnum = drmaa_set_attribute (jt, DRMAA_REMOTE_COMMAND, "sleeper.sh",
 
54
                                   error, DRMAA_ERROR_STRING_BUFFER);
 
55
 
 
56
      if (errnum != DRMAA_ERRNO_SUCCESS) {
 
57
         fprintf (stderr, "Could not set attribute \"%s\": %s\n",
 
58
                  DRMAA_REMOTE_COMMAND, error);
 
59
      }
 
60
      else {
 
61
         const char *args[2] = {"5", NULL};
 
62
         
 
63
         errnum = drmaa_set_vector_attribute (jt, DRMAA_V_ARGV, args, error,
 
64
                                              DRMAA_ERROR_STRING_BUFFER);
 
65
      }
 
66
      
 
67
      if (errnum != DRMAA_ERRNO_SUCCESS) {
 
68
         fprintf (stderr, "Could not set attribute \"%s\": %s\n",
 
69
                  DRMAA_REMOTE_COMMAND, error);
 
70
      }
 
71
      else {
 
72
         drmaa_job_ids_t *ids = NULL;
 
73
         int start = 1;
 
74
         int end = 30;
 
75
         int step = 2;
 
76
 
 
77
         errnum = drmaa_run_bulk_jobs (&ids, jt, start, end, step, error,
 
78
                                       DRMAA_ERROR_STRING_BUFFER);
 
79
 
 
80
         if (errnum != DRMAA_ERRNO_SUCCESS) {
 
81
            fprintf (stderr, "Could not submit job: %s\n", error);
 
82
         }
 
83
         else {
 
84
            char jobid[DRMAA_JOBNAME_BUFFER];
 
85
            const char *jobids[2] = {DRMAA_JOB_IDS_SESSION_ALL, NULL};
 
86
 
 
87
            while (drmaa_get_next_job_id (ids, jobid, DRMAA_JOBNAME_BUFFER)
 
88
                                                     == DRMAA_ERRNO_SUCCESS) {
 
89
               printf ("A job task has been submitted with id %s\n", jobid);
 
90
            }
 
91
            
 
92
            errnum = drmaa_synchronize (jobids, DRMAA_TIMEOUT_WAIT_FOREVER,
 
93
                                        0, error, DRMAA_ERROR_STRING_BUFFER);
 
94
            
 
95
            if (errnum != DRMAA_ERRNO_SUCCESS) {
 
96
               fprintf (stderr, "Could not wait for jobs: %s\n", error);
 
97
            }
 
98
            else {
 
99
               char jobid[DRMAA_JOBNAME_BUFFER];
 
100
               int status = 0;
 
101
               drmaa_attr_values_t *rusage = NULL;
 
102
               int count = 0;
 
103
               
 
104
               for (count = start; count < end; count += step) {
 
105
                  errnum = drmaa_wait (DRMAA_JOB_IDS_SESSION_ANY, jobid,
 
106
                                       DRMAA_JOBNAME_BUFFER, &status,
 
107
                                       DRMAA_TIMEOUT_WAIT_FOREVER, &rusage,
 
108
                                       error, DRMAA_ERROR_STRING_BUFFER);
 
109
 
 
110
                  if (errnum != DRMAA_ERRNO_SUCCESS) {
 
111
                     fprintf (stderr, "Could not wait for job: %s\n", error);
 
112
                  }
 
113
                  else {
 
114
                     char usage[DRMAA_ERROR_STRING_BUFFER];
 
115
                     int aborted = 0;
 
116
 
 
117
                     drmaa_wifaborted(&aborted, status, NULL, 0);
 
118
 
 
119
                     if (aborted == 1) {
 
120
                        printf("Job %s never ran\n", jobid);
 
121
                     }
 
122
                     else {
 
123
                        int exited = 0;
 
124
 
 
125
                        drmaa_wifexited(&exited, status, NULL, 0);
 
126
 
 
127
                        if (exited == 1) {
 
128
                           int exit_status = 0;
 
129
 
 
130
                           drmaa_wexitstatus(&exit_status, status, NULL, 0);
 
131
                           printf("Job %s finished regularly with exit status %d\n",
 
132
                                  jobid, exit_status);
 
133
                        }
 
134
                        else {
 
135
                           int signaled = 0;
 
136
 
 
137
                           drmaa_wifsignaled(&signaled, status, NULL, 0);
 
138
 
 
139
                           if (signaled == 1) {
 
140
                              char termsig[DRMAA_SIGNAL_BUFFER+1];
 
141
 
 
142
                              drmaa_wtermsig(termsig, DRMAA_SIGNAL_BUFFER, status, NULL, 0);
 
143
                              printf("Job %s finished due to signal %s\n", jobid, termsig);
 
144
                           }
 
145
                           else {
 
146
                              printf("Job %s finished with unclear conditions\n", jobid);
 
147
                           }
 
148
                        } /* else */
 
149
                     } /* else */
 
150
 
 
151
                     printf ("Job Usage:\n");
 
152
 
 
153
                     while (drmaa_get_next_attr_value (rusage, usage, DRMAA_ERROR_STRING_BUFFER)
 
154
                                                                          == DRMAA_ERRNO_SUCCESS) {
 
155
                        printf ("  %s\n", usage);
 
156
                     }
 
157
 
 
158
                     drmaa_release_attr_values (rusage);
 
159
                  } /* else */
 
160
               } /* for */
 
161
            } /* else */
 
162
         } /* else */
 
163
 
 
164
         drmaa_release_job_ids (ids);
 
165
      } /* else */
 
166
 
 
167
      errnum = drmaa_delete_job_template (jt, error, DRMAA_ERROR_STRING_BUFFER);
 
168
 
 
169
      if (errnum != DRMAA_ERRNO_SUCCESS) {
 
170
         fprintf (stderr, "Could not delete job template: %s\n", error);
 
171
      }
 
172
   } /* else */
 
173
 
 
174
   errnum = drmaa_exit (error, DRMAA_ERROR_STRING_BUFFER);
 
175
 
 
176
   if (errnum != DRMAA_ERRNO_SUCCESS) {
 
177
      fprintf (stderr, "Could not shut down the DRMAA library: %s\n", error);
 
178
      return 1;
 
179
   }
 
180
 
 
181
   return 0;
 
182
}