[5] | 1 | /* $Id$ */ |
---|
[1] | 2 | /* |
---|
| 3 | * PSNC DRMAA for SLURM |
---|
[13] | 4 | * Copyright (C) 2011 Poznan Supercomputing and Networking Center |
---|
[1] | 5 | * |
---|
| 6 | * This program is free software: you can redistribute it and/or modify |
---|
| 7 | * it under the terms of the GNU General Public License as published by |
---|
| 8 | * the Free Software Foundation, either version 3 of the License, or |
---|
| 9 | * (at your option) any later version. |
---|
| 10 | * |
---|
| 11 | * This program is distributed in the hope that it will be useful, |
---|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 14 | * GNU General Public License for more details. |
---|
| 15 | * |
---|
| 16 | * You should have received a copy of the GNU General Public License |
---|
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
| 18 | */ |
---|
| 19 | |
---|
| 20 | #include <string.h> |
---|
| 21 | #include <unistd.h> |
---|
| 22 | |
---|
| 23 | #include <drmaa_utils/iter.h> |
---|
| 24 | #include <drmaa_utils/conf.h> |
---|
| 25 | #include <slurm_drmaa/job.h> |
---|
| 26 | #include <slurm_drmaa/session.h> |
---|
| 27 | #include <slurm_drmaa/util.h> |
---|
| 28 | |
---|
| 29 | #include <slurm/slurm.h> |
---|
| 30 | |
---|
| 31 | static char *slurmdrmaa_session_run_job(fsd_drmaa_session_t *self, const fsd_template_t *jt); |
---|
| 32 | |
---|
| 33 | static fsd_iter_t *slurmdrmaa_session_run_bulk( fsd_drmaa_session_t *self,const fsd_template_t *jt, int start, int end, int incr ); |
---|
| 34 | |
---|
| 35 | static fsd_job_t *slurmdrmaa_session_new_job( fsd_drmaa_session_t *self, const char *job_id ); |
---|
| 36 | |
---|
| 37 | fsd_drmaa_session_t * |
---|
| 38 | slurmdrmaa_session_new( const char *contact ) |
---|
| 39 | { |
---|
| 40 | slurmdrmaa_session_t *volatile self = NULL; |
---|
| 41 | TRY |
---|
| 42 | { |
---|
| 43 | self = (slurmdrmaa_session_t*)fsd_drmaa_session_new(contact); |
---|
| 44 | |
---|
| 45 | fsd_realloc( self, 1, slurmdrmaa_session_t ); |
---|
| 46 | |
---|
| 47 | self->super.run_job = slurmdrmaa_session_run_job; |
---|
| 48 | self->super.run_bulk = slurmdrmaa_session_run_bulk; |
---|
| 49 | self->super.new_job = slurmdrmaa_session_new_job; |
---|
| 50 | |
---|
| 51 | self->super.load_configuration( &self->super, "slurm_drmaa" ); |
---|
| 52 | } |
---|
| 53 | EXCEPT_DEFAULT |
---|
| 54 | { |
---|
| 55 | fsd_free( self ); |
---|
| 56 | fsd_exc_reraise(); |
---|
| 57 | } |
---|
| 58 | END_TRY |
---|
| 59 | return (fsd_drmaa_session_t*)self; |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | |
---|
| 63 | char * |
---|
| 64 | slurmdrmaa_session_run_job( |
---|
| 65 | fsd_drmaa_session_t *self, |
---|
| 66 | const fsd_template_t *jt |
---|
| 67 | ) |
---|
| 68 | { |
---|
| 69 | char *job_id = NULL; |
---|
| 70 | fsd_iter_t *volatile job_ids = NULL; |
---|
| 71 | |
---|
| 72 | TRY |
---|
| 73 | { |
---|
| 74 | job_ids = self->run_bulk( self, jt, 0, 0, 0 ); /* single job run as bulk job specialization */ |
---|
| 75 | job_id = fsd_strdup( job_ids->next( job_ids ) ); |
---|
| 76 | } |
---|
| 77 | FINALLY |
---|
| 78 | { |
---|
| 79 | if( job_ids ) |
---|
| 80 | job_ids->destroy( job_ids ); |
---|
| 81 | } |
---|
| 82 | END_TRY |
---|
| 83 | return job_id; |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | |
---|
| 87 | fsd_iter_t * |
---|
| 88 | slurmdrmaa_session_run_bulk( |
---|
| 89 | fsd_drmaa_session_t *self, |
---|
| 90 | const fsd_template_t *jt, |
---|
| 91 | int start, int end, int incr ) |
---|
| 92 | { |
---|
| 93 | fsd_job_t *volatile job = NULL; |
---|
| 94 | char **volatile job_ids = NULL; |
---|
[35] | 95 | unsigned n_jobs = 1; |
---|
[1] | 96 | volatile bool connection_lock = false; |
---|
| 97 | fsd_environ_t *volatile env = NULL; |
---|
| 98 | job_desc_msg_t job_desc; |
---|
| 99 | submit_response_msg_t *submit_response = NULL; |
---|
[38] | 100 | job_info_msg_t *job_info = NULL; |
---|
| 101 | |
---|
| 102 | /* zero out the struct, and set default vaules */ |
---|
| 103 | slurm_init_job_desc_msg( &job_desc ); |
---|
[1] | 104 | |
---|
| 105 | TRY |
---|
| 106 | { |
---|
[38] | 107 | unsigned i; |
---|
[42] | 108 | if( start != end ) { |
---|
[35] | 109 | n_jobs = (end - start) / incr + 1; |
---|
[42] | 110 | } else { |
---|
| 111 | n_jobs = 1; |
---|
[38] | 112 | } |
---|
[1] | 113 | |
---|
[42] | 114 | fsd_calloc( job_ids, n_jobs+1, char* ); |
---|
| 115 | |
---|
| 116 | if ( start != 0 || end != 0 || incr != 0 ) { |
---|
| 117 | job_desc.array_inx = fsd_asprintf( "%d-%d:%d", start, end, incr ); |
---|
| 118 | } |
---|
| 119 | |
---|
[38] | 120 | connection_lock = fsd_mutex_lock( &self->drm_connection_mutex ); |
---|
| 121 | slurmdrmaa_job_create_req( self, jt, (fsd_environ_t**)&env , &job_desc ); |
---|
| 122 | if(slurm_submit_batch_job(&job_desc,&submit_response)){ |
---|
| 123 | fsd_exc_raise_fmt( |
---|
| 124 | FSD_ERRNO_INTERNAL_ERROR,"slurm_submit_batch_job: %s",slurm_strerror(slurm_get_errno())); |
---|
| 125 | } |
---|
[1] | 126 | |
---|
[38] | 127 | connection_lock = fsd_mutex_unlock( &self->drm_connection_mutex ); |
---|
[1] | 128 | |
---|
[38] | 129 | fsd_log_debug(("job %u submitted", submit_response->job_id)); |
---|
[1] | 130 | |
---|
[42] | 131 | if ( start != 0 || end != 0 || incr != 0 ) { |
---|
[38] | 132 | if ( SLURM_SUCCESS == slurm_load_job( &job_info, submit_response->job_id, 0) ) |
---|
| 133 | { |
---|
| 134 | fsd_assert( job_info->record_count == n_jobs ); |
---|
| 135 | for(i=0; i < job_info->record_count; i++) |
---|
| 136 | { |
---|
| 137 | job_ids[i] = fsd_asprintf( "%d", job_info->job_array[i].job_id); |
---|
[35] | 138 | |
---|
[38] | 139 | job = slurmdrmaa_job_new( fsd_strdup(job_ids[i]) ); |
---|
| 140 | job->session = self; |
---|
| 141 | job->submit_time = time(NULL); |
---|
| 142 | self->jobs->add( self->jobs, job ); |
---|
| 143 | job->release( job ); |
---|
| 144 | job = NULL; |
---|
| 145 | } |
---|
| 146 | } else { |
---|
| 147 | fsd_exc_raise_fmt( FSD_ERRNO_INTERNAL_ERROR,"slurm_load_job: %s",slurm_strerror(slurm_get_errno())); |
---|
[1] | 148 | } |
---|
[38] | 149 | } else { |
---|
[1] | 150 | job_ids[0] = fsd_asprintf( "%d", submit_response->job_id); /* .0*/ |
---|
| 151 | |
---|
| 152 | job = slurmdrmaa_job_new( fsd_strdup(job_ids[0]) ); /* TODO: ??? */ |
---|
| 153 | job->session = self; |
---|
| 154 | job->submit_time = time(NULL); |
---|
| 155 | self->jobs->add( self->jobs, job ); |
---|
| 156 | job->release( job ); |
---|
| 157 | job = NULL; |
---|
[38] | 158 | } |
---|
[1] | 159 | } |
---|
| 160 | ELSE |
---|
| 161 | { |
---|
| 162 | if ( !connection_lock ) |
---|
| 163 | connection_lock = fsd_mutex_lock( &self->drm_connection_mutex ); |
---|
| 164 | |
---|
| 165 | slurm_free_submit_response_response_msg ( submit_response ); |
---|
| 166 | } |
---|
| 167 | FINALLY |
---|
| 168 | { |
---|
| 169 | |
---|
| 170 | |
---|
| 171 | if( connection_lock ) |
---|
| 172 | fsd_mutex_unlock( &self->drm_connection_mutex ); |
---|
| 173 | |
---|
| 174 | if( job ) |
---|
| 175 | job->release( job ); |
---|
| 176 | |
---|
| 177 | if( fsd_exc_get() != NULL ) |
---|
| 178 | fsd_free_vector( job_ids ); |
---|
| 179 | |
---|
| 180 | slurmdrmaa_free_job_desc(&job_desc); |
---|
| 181 | } |
---|
| 182 | END_TRY |
---|
| 183 | |
---|
| 184 | return fsd_iter_new( job_ids, n_jobs ); |
---|
| 185 | } |
---|
| 186 | |
---|
| 187 | |
---|
| 188 | fsd_job_t * |
---|
| 189 | slurmdrmaa_session_new_job( fsd_drmaa_session_t *self, const char *job_id ) |
---|
| 190 | { |
---|
| 191 | fsd_job_t *job; |
---|
| 192 | job = slurmdrmaa_job_new( fsd_strdup(job_id) ); |
---|
| 193 | job->session = self; |
---|
| 194 | return job; |
---|
| 195 | } |
---|