[12] | 1 | /* $Id$ */ |
---|
[1] | 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 |
---|
[51] | 40 | #define WITH_BULK 0 |
---|
[1] | 41 | #define EXPORT_ENVS 0 |
---|
| 42 | #define DEBUG 1 |
---|
[49] | 43 | #define N_JOBS 8 |
---|
[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 | |
---|
| 61 | void |
---|
| 62 | test_1(void); |
---|
| 63 | |
---|
| 64 | drmaa_job_template_t * |
---|
| 65 | construct_job( char *argv[], char *fds[3], const char *cwd ); |
---|
| 66 | |
---|
| 67 | void |
---|
| 68 | time_fmt( time_t t, char *buf, size_t buflen ); |
---|
| 69 | |
---|
| 70 | |
---|
| 71 | int |
---|
| 72 | main( int argc, char *argv[] ) |
---|
| 73 | { |
---|
| 74 | test_1(); |
---|
| 75 | return 0; |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | void |
---|
| 79 | test_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 ); |
---|
[49] | 109 | sprintf( job_path, "/bin/sleep" ); |
---|
[1] | 110 | argv[0] = job_path; |
---|
[49] | 111 | argv[1] = "5"; |
---|
[1] | 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 | } |
---|
[49] | 129 | #if ! WITH_BULK |
---|
[1] | 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 | |
---|
[49] | 147 | #else |
---|
[1] | 148 | { |
---|
[49] | 149 | char *argv[] = { "/bin/sleep", "5", "$drmaa_incr_ph$", NULL }; |
---|
[1] | 150 | char *fds[] = { "b.$drmaa_incr_ph$.stdin", "b.$drmaa_incr_ph$.stdout", |
---|
| 151 | "b.$drmaa_incr_ph$.stderr" }; |
---|
[49] | 152 | bulk_job = construct_job( argv, fds, "." ); |
---|
[1] | 153 | } |
---|
[49] | 154 | rc = drmaa_run_bulk_jobs( &bulk_job_ids, bulk_job, 1, N_JOBS, 2, err_msg, ERR_LEN ); |
---|
[1] | 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 | |
---|
| 179 | drmaa_job_template_t * |
---|
| 180 | construct_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_REMOTE_COMMAND, NULL, |
---|
| 200 | NULL , NULL |
---|
| 201 | }; |
---|
| 202 | #if EXPORT_ENVS |
---|
| 203 | const char *exported_envs[] = { |
---|
| 204 | "HOME", "PATH", "LD_LIBRARY_PATH", |
---|
| 205 | "USER", "USERNAME", |
---|
| 206 | "LANG", "LC_ALL", "LC_CTYPE", "LC_MESSAGES", "NLSPATH", |
---|
| 207 | "DISPLAY", |
---|
| 208 | NULL |
---|
| 209 | }; |
---|
| 210 | char *env[ 32 ]; |
---|
| 211 | #endif |
---|
| 212 | |
---|
| 213 | drmaa_job_template_t *job; |
---|
| 214 | char err_msg[ ERR_LEN ] = ""; |
---|
| 215 | const char *const *i; |
---|
| 216 | int rc; |
---|
| 217 | |
---|
[49] | 218 | scalar_attrs[ 1] = "/bin/sleep"; |
---|
[1] | 219 | |
---|
| 220 | printf( "allocaing template\n" ); |
---|
| 221 | rc = drmaa_allocate_job_template( &job, err_msg, ERR_LEN ); |
---|
| 222 | if( rc ) drmaa_error( "drmaa_allocate_job_template" ); |
---|
| 223 | |
---|
| 224 | { |
---|
| 225 | for( i = scalar_attrs; *i; ) |
---|
| 226 | { |
---|
| 227 | const char *name = *i++; |
---|
| 228 | const char *value = *i++; |
---|
| 229 | printf( "set %s=%s\n", name, value ); |
---|
| 230 | rc = drmaa_set_attribute( job, name, value, err_msg, ERR_LEN ); |
---|
| 231 | if( rc ) drmaa_error( "drmaa_set_attribute" ); |
---|
| 232 | } |
---|
| 233 | # if EXPORT_ENVS |
---|
| 234 | for( i=exported_envs, j=env; *i!=NULL; i++ ) |
---|
| 235 | { |
---|
| 236 | const char *value = getenv( *i ); |
---|
| 237 | if( value != NULL ) |
---|
| 238 | { |
---|
| 239 | *j = malloc( strlen(*i) + 1 + strlen(value) + 1 ); |
---|
| 240 | sprintf( *j, "%s=%s", *i, value ); |
---|
| 241 | printf( "env: %s\n", *j ); |
---|
| 242 | j++; |
---|
| 243 | } |
---|
| 244 | } |
---|
| 245 | *j = NULL; |
---|
| 246 | # endif |
---|
| 247 | } |
---|
| 248 | |
---|
| 249 | rc = drmaa_set_vector_attribute( job, DRMAA_V_ARGV, (const char**)argv+1, err_msg, ERR_LEN ); |
---|
| 250 | if( rc ) drmaa_error("drmaa_set_vector_attribute"); |
---|
| 251 | |
---|
| 252 | #if SEND_MAIL |
---|
| 253 | char *mail_list[] = { "lukasz.ciesnik@gmail.com", NULL }; |
---|
| 254 | rc = drmaa_set_vector_attribute( job, DRMAA_V_EMAIL, (const char**)mail_list, err_msg, ERR_LEN ); |
---|
| 255 | if( rc ) drmaa_error( "drmaa_set_vector_attribute(DRMAA_V_EMAIL)" ); |
---|
| 256 | #endif |
---|
| 257 | |
---|
| 258 | #if EXPORT_ENVS |
---|
| 259 | rc = drmaa_set_vector_attribute( job, DRMAA_V_ENV, (const char**)env, err_msg, ERR_LEN ); |
---|
| 260 | if( rc ) drmaa_error( "drmaa_set_vector_attribute(DRMAA_V_ENV)" ); |
---|
| 261 | #endif |
---|
| 262 | |
---|
| 263 | return job; |
---|
| 264 | } |
---|
| 265 | |
---|
| 266 | |
---|
| 267 | void |
---|
| 268 | time_fmt( time_t t, char *buf, size_t buflen ) |
---|
| 269 | { |
---|
| 270 | struct tm timem; |
---|
| 271 | gmtime_r( &t, &timem ); |
---|
| 272 | strftime( buf, buflen, "%Y/%m/%d %H:%M:%S", &timem ); |
---|
| 273 | } |
---|
| 274 | |
---|
| 275 | |
---|
| 276 | |
---|