source: trunk/test/example.c @ 12

Revision 12, 7.5 KB checked in by mmamonski, 13 years ago (diff)

version 1.0.7 release candidate

  • Property svn:keywords set to Id
Line 
1/* $Id$ */
2/*
3 *  DRMAA library for Torque/PBS
4 *  Copyright (C) 2006-2007
5 *
6 *  FedStage Systems <http://www.fedstage.com/>,
7 *  Poznan Supercomputing and Networking Center <http://www.man.poznan.pl/>,
8 *  and the OpenDSP project <http://sourceforge.net/projects/opendsp/>
9 *
10 *  This library is free software; you can redistribute it and/or
11 *  modify it under the terms of the GNU Lesser General Public
12 *  License as published by the Free Software Foundation; either
13 *  version 2.1 of the License, or (at your option) any later version.
14 *
15 *  This library is distributed in the hope that it will be useful,
16 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 *  Lesser General Public License for more details.
19 *
20 *  You should have received a copy of the GNU Lesser General Public
21 *  License along with this library; if not, write to the Free Software
22 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
23 */
24/**
25 * @file test.c
26 * DRMAA library test / usage example.
27 */
28
29#include <drmaa.h>
30
31#include <unistd.h>
32#include <stdlib.h>
33#include <stdio.h>
34#include <string.h>
35#include <time.h>
36#include <limits.h>
37#include <errno.h>
38
39#define SEND_MAIL     0
40#define WITH_BULK     0
41#define EXPORT_ENVS   0
42#define DEBUG         1
43#define N_JOBS        1
44
45#define ERR_LEN      DRMAA_ERROR_STRING_BUFFER
46#define JOB_ID_LEN   256
47
48#define errno_error( func ) do{                      \
49         fprintf( stderr, "%s:%d: %s: %s\n",               \
50                         __FILE__, __LINE__, func, strerror(errno) );  \
51         exit( 1 );                                        \
52 } while(0)
53
54#define drmaa_error( func ) do{                      \
55                fprintf( stderr, "%s:%d: %s: %s\n> %s\n",        \
56                                __FILE__, __LINE__, func,                    \
57                                drmaa_strerror(rc), err_msg );               \
58                exit(1);                                         \
59        }while(0)
60
61void
62test_1(void);
63
64drmaa_job_template_t *
65construct_job( char *argv[], char *fds[3], const char *cwd );
66
67void
68time_fmt( time_t t, char *buf, size_t buflen );
69
70
71int
72main( int argc, char *argv[] )
73{
74        test_1();
75        return 0;
76}
77
78void
79test_1(void)
80{
81        char err_msg[ ERR_LEN ] = "";
82        char cwd[ PATH_MAX ];
83        char job_ids[ N_JOBS ][ JOB_ID_LEN ];
84#if WITH_BULK
85        drmaa_job_ids_t *bulk_job_ids = NULL;
86#endif
87        int rc;
88        int i;
89        drmaa_job_template_t *jobs[ N_JOBS ];
90        drmaa_job_template_t *bulk_job = NULL;
91
92        if( getcwd( cwd, PATH_MAX ) == NULL )
93                errno_error("getcwd");
94
95        printf( "connecting\n" );
96        rc = drmaa_init( NULL, err_msg, ERR_LEN );
97        if( rc )  drmaa_error("drmaa_init");
98
99        for( i = 0;  i < N_JOBS;  i++ )
100         {
101                char arg[ 3 ];
102                char job_path[ PATH_MAX ];
103                char *argv[3];
104                char fds_t[3][64];
105                char *fds[3];
106                int j;
107                printf( "constructing %d\n", i );
108                sprintf( arg, "%d", i );
109                sprintf( job_path, "./test_job" );
110                argv[0] = job_path;
111                argv[1] = arg;
112                argv[2] = NULL;
113                for( j = 0;  j < 3;  j++ )
114                        fds[j] = fds_t[j];
115                sprintf( fds[0], "$drmaa_wd_ph$/%d.stdin",  i );
116                sprintf( fds[1], "$drmaa_wd_ph$/%d.stdout", i );
117                sprintf( fds[2], "$drmaa_wd_ph$/%d.stderr", i );
118                jobs[i] = construct_job( argv, fds, cwd );
119
120#if 0
121                char start_time_buf[ 20 ];
122                time_fmt( time(NULL)+15*i, start_time_buf, sizeof(start_time_buf) );
123                printf( "%d: drmaa_start_time=%s\n", i, start_time_buf );
124                rc = drmaa_set_attribute( jobs[i], "drmaa_start_time", start_time_buf,
125                                err_msg, ERR_LEN );
126                if( rc )  drmaa_error("drmaa_set_attribute(\"drmaa_set_attribute\")");
127#endif
128         }
129
130        for( i = 0;  i < N_JOBS;  i++ )
131         {
132                char job_id[ DRMAA_JOBNAME_BUFFER ];
133                int stat;
134                drmaa_attr_values_t *rusage;
135
136                printf( "running %d\n", i );
137                rc = drmaa_run_job( job_ids[i], JOB_ID_LEN, jobs[ i ], err_msg, ERR_LEN );
138                if( rc )  drmaa_error( "drmaa_run_job" );
139
140                sleep(1);
141                rc = drmaa_wait( job_ids[i], job_id, sizeof(job_id), &stat, DRMAA_TIMEOUT_WAIT_FOREVER, &rusage, err_msg, ERR_LEN );
142                if( rc )
143                        drmaa_error( "drmaa_job_wait" );
144                printf( "stat=%d\n", stat );
145         }
146
147#if WITH_BULK
148         {
149                char *argv[] = { "./job", "bulk", "$drmaa_incr_ph$", NULL };
150                char *fds[]  = { "b.$drmaa_incr_ph$.stdin", "b.$drmaa_incr_ph$.stdout",
151                        "b.$drmaa_incr_ph$.stderr" };
152                bulk_job = construct_job( argv, fds, WD );
153         }
154        rc = drmaa_run_bulk_jobs( &bulk_job_ids, bulk_job, 1, N_JOBS, 1, err_msg, ERR_LEN );
155        if( rc )  drmaa_error("drmaa_run_bulk_jobs");
156
157        if( bulk_job_ids != NULL )
158                drmaa_release_job_ids( bulk_job_ids );
159#endif
160
161        for( i = 0;  i < N_JOBS;  i++ )
162         {
163                rc = drmaa_delete_job_template( jobs[i], err_msg, ERR_LEN );
164                if( rc )  drmaa_error("drmaa_delete_job_template");
165         }
166
167        if( bulk_job != NULL )
168         {
169                rc = drmaa_delete_job_template( bulk_job, err_msg, ERR_LEN );
170                if( rc )  drmaa_error("drmaa_delete_job_template");
171         }
172
173        rc = drmaa_exit( err_msg, ERR_LEN );
174        if( rc )  drmaa_error("drmaa_exit");
175}
176
177
178
179drmaa_job_template_t *
180construct_job( char *argv[], char *fds[3], const char *cwd )
181{
182#if 0
183        const char *scalar_attrs[] = {
184                DRMAA_WD            , cwd,
185                DRMAA_REMOTE_COMMAND, argv[0],
186                DRMAA_JOB_NAME      , "jobname",
187                DRMAA_INPUT_PATH    , fds[0],
188                DRMAA_OUTPUT_PATH   , fds[1],
189                DRMAA_ERROR_PATH    , fds[2],
190                /*
191                DRMAA_DURATION_SLIMIT , "00:00:30",
192                DRMAA_DURATION_HLIMIT , "00:01:00",
193                DRMAA_WCT_SLIMIT      , "00:00:02",
194                DRMAA_WCT_HLIMIT      , "00:00:02", */
195                NULL                , NULL
196        };
197#endif
198        const char *scalar_attrs[] = {
199                DRMAA_WD            , NULL,
200                DRMAA_REMOTE_COMMAND, NULL,
201                DRMAA_JOB_NAME      , NULL,
202                DRMAA_INPUT_PATH    , NULL,
203                DRMAA_OUTPUT_PATH   , NULL,
204                DRMAA_ERROR_PATH    , NULL,
205                DRMAA_NATIVE_SPECIFICATION, "-l host=eeyore,nodes=1 -W x=FLAGS:ADVRES:usecase.0",
206                NULL                , NULL
207        };
208#if EXPORT_ENVS
209        const char *exported_envs[] = {
210                "HOME", "PATH", "LD_LIBRARY_PATH",
211                "USER", "USERNAME",
212                "LANG", "LC_ALL", "LC_CTYPE", "LC_MESSAGES", "NLSPATH",
213                "DISPLAY",
214                NULL
215        };
216        char *env[ 32 ];
217#endif
218
219        drmaa_job_template_t *job;
220        char err_msg[ ERR_LEN ] = "";
221        const char *const *i;
222        int rc;
223
224        scalar_attrs[ 1] = cwd;
225        scalar_attrs[ 3] = argv[0];
226        scalar_attrs[ 5] = "jobname";
227        scalar_attrs[ 7] = fds[0];
228        scalar_attrs[ 9] = fds[1];
229        scalar_attrs[11] = fds[2];
230
231        printf( "allocaing template\n" );
232        rc = drmaa_allocate_job_template( &job, err_msg, ERR_LEN );
233        if( rc )  drmaa_error( "drmaa_allocate_job_template" );
234
235         {
236                for( i = scalar_attrs;  *i;   )
237                 {
238                        const char *name  = *i++;
239                        const char *value = *i++;
240                        printf( "set %s=%s\n", name, value );
241                        rc = drmaa_set_attribute( job, name, value, err_msg, ERR_LEN );
242                        if( rc )  drmaa_error( "drmaa_set_attribute" );
243                 }
244#               if EXPORT_ENVS
245                for( i=exported_envs, j=env;  *i!=NULL;  i++ )
246                 {
247                        const char *value = getenv( *i );
248                        if( value != NULL )
249                         {
250                                *j = malloc( strlen(*i) + 1 + strlen(value) + 1 );
251                                sprintf( *j, "%s=%s", *i, value );
252                                printf( "env: %s\n", *j );
253                                j++;
254                         }
255                 }
256                *j = NULL;
257#               endif
258         }
259
260        rc = drmaa_set_vector_attribute( job, DRMAA_V_ARGV, (const char**)argv+1, err_msg, ERR_LEN );
261        if( rc )  drmaa_error("drmaa_set_vector_attribute");
262
263#if SEND_MAIL
264        char *mail_list[] = { "lukasz.ciesnik@gmail.com", NULL };
265        rc = drmaa_set_vector_attribute( job, DRMAA_V_EMAIL, (const char**)mail_list, err_msg, ERR_LEN );
266        if( rc )  drmaa_error( "drmaa_set_vector_attribute(DRMAA_V_EMAIL)" );
267#endif
268
269#if EXPORT_ENVS
270        rc = drmaa_set_vector_attribute( job, DRMAA_V_ENV,  (const char**)env,  err_msg, ERR_LEN );
271        if( rc )  drmaa_error( "drmaa_set_vector_attribute(DRMAA_V_ENV)" );
272#endif
273
274        return job;
275}
276
277
278void
279time_fmt( time_t t, char *buf, size_t buflen )
280{
281        struct tm timem;
282        gmtime_r( &t, &timem );
283        strftime( buf, buflen, "%Y/%m/%d %H:%M:%S", &timem );
284}
285
286
287
Note: See TracBrowser for help on using the repository browser.