SAGA C++ API 1.6
job.cpp
Go to the documentation of this file.
00001 #if defined(__WAVE__) && defined(SAGA_CREATE_PREPROCESSED_FILES)
00002 #pragma wave option(preserve: 2, line: 1, output: "preprocessed/job.cpp")
00003 #endif
00004 //  Copyright (c) 2005-2009 Hartmut Kaiser
00005 // 
00006 //  Distributed under the Boost Software License, Version 1.0. (See accompanying 
00007 //  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
00008 
00009 #if defined(__WAVE__) && defined(SAGA_CREATE_PREPROCESSED_FILES)
00010 #pragma wave option(output: null)
00011 #endif
00012 
00013 //  this is needed in every file including detail/attribute_impl.hpp and not 
00014 //  belonging to the engine
00015 #define SAGA_NO_IMPORT_ATTRIBUTE
00016 #define SAGA_EXPORT_ATTRIBUTE       SAGA_JOB_PACKAGE_EXPORT
00017 
00018 #define SAGA_NO_IMPORT_PERMISSIONS
00019 #define SAGA_EXPORT_PERMISSIONS     SAGA_JOB_PACKAGE_EXPORT
00020 
00021 #include <boost/assign/list_inserter.hpp>
00022 #include <boost/assign/std/vector.hpp>
00023 
00024 // include job API and implementation
00025 #include <saga/saga/job.hpp>
00026 #include <saga/saga/adaptors/metric.hpp>
00027 #include <saga/impl/job.hpp>
00028 #include <saga/saga/detail/call.hpp>
00029 
00030 #include <saga/saga/detail/attribute_impl.hpp>
00031 #include <saga/saga/detail/permissions_impl.hpp>
00032 
00033 #ifdef SAGA_DEBUG
00034 #include <saga/saga/packages/job/preprocessed/job.cpp>
00035 #else
00036 
00037 #if defined(__WAVE__) && defined(SAGA_CREATE_PREPROCESSED_FILES)
00038 #pragma wave option(preserve: 2, line: 1, output: "preprocessed/job.cpp")
00039 #endif
00040 
00041 namespace saga { namespace job {
00042 
00043     namespace metrics
00044     {
00046         saga::metrics::init_data const job_metric_data[] = 
00047         {
00048           // task specific metrics
00049             {
00050                 saga::metrics::task_state,
00051                 "Metric to monitor the current state of the job, "
00052                     "e.g. New, Running, Canceled, Done, or Failed",
00053                 saga::attributes::metric_mode_readonly,
00054                 "1", 
00055                 saga::attributes::metric_type_enum,
00056                 "New"   // saga::job::New
00057             },
00058 
00059             // job specific metrics
00060             {
00061                 saga::job::metrics::state_detail,
00062                 "Metric fires on state detail changes of the job.",
00063                 saga::attributes::metric_mode_readonly,
00064                 "1", 
00065                 saga::attributes::metric_type_string,
00066                 ""
00067             },
00068             {
00069                 saga::job::metrics::signal,
00070                 "Metric fires as a job receives a signal, and has a "
00071                     "value indicating the signal number.",
00072                 saga::attributes::metric_mode_readonly,
00073                 "1", 
00074                 saga::attributes::metric_type_int,
00075                 ""
00076             },
00077             {
00078                 saga::job::metrics::cpu_time,
00079                 "Metric gives access to number of cpu seconds consumed by the job.",
00080                 saga::attributes::metric_mode_readonly,
00081                 "seconds", 
00082                 saga::attributes::metric_type_int,
00083                 ""
00084             },
00085             {
00086                 saga::job::metrics::memory_use,
00087                 "Metric gives access to the current aggregate memory usage "
00088                     "of the job.",
00089                 saga::attributes::metric_mode_readonly,
00090                 "megabyte", 
00091                 saga::attributes::metric_type_float,
00092                 "0.0"
00093             },
00094             {
00095                 saga::job::metrics::vmemory_use,
00096                 "Metric gives access to the current aggregate virtual memory "
00097                     "usage of the job.",
00098                 saga::attributes::metric_mode_readonly,
00099                 "megabyte", 
00100                 saga::attributes::metric_type_float,
00101                 "0.0"
00102             },
00103             {
00104                 saga::job::metrics::performance,
00105                 "Metric gives access to the current performance of the job.",
00106                 saga::attributes::metric_mode_readonly,
00107                 "FLOPS", 
00108                 saga::attributes::metric_type_float,
00109                 "0.0"
00110             },
00111         };
00113     }
00114 
00115     namespace detail 
00116     {
00117 
00118     SAGA_JOB_PACKAGE_EXPORT std::string get_state_name(state s)
00119     {
00120         switch (s) {
00121         case saga::job::New: return "New";
00122         case saga::job::Running:  return "Running";
00123         case saga::job::Done:  return "Done";
00124         case saga::job::Canceled: return "Canceled";
00125         case saga::job::Failed: return "Failed";
00126         case saga::job::Suspended: return "Suspended";
00127         default:
00128         case saga::job::Unknown:
00129             return "Unknown";
00130         }
00131     }
00132 
00133     }
00134 
00136   job::job()
00137   {
00138   }
00139   
00140   job::job(saga::impl::job* impl)
00141     : task(impl)
00142   {
00143       // initialize attributes
00144       using namespace boost::assign;
00145       std::vector<std::string> valid_keys;
00146       valid_keys += 
00147               attributes::jobid,
00148               attributes::execution_hosts,
00149               attributes::created,
00150               attributes::started,
00151               attributes::finished,
00152               attributes::working_directory,
00153               attributes::exitcode,
00154               attributes::termsig,
00155               attributes::job_service
00156           ;
00157 
00158       // initialize list of valid keys
00159       this->attribute_base::init_keynames(valid_keys);
00160 
00161       strmap_type attributes_scalar_ro;
00162       insert(attributes_scalar_ro)
00163               (attributes::jobid, "")
00164           ;
00165 
00166       strmap_type attributes_scalar_rw;
00167       insert(attributes_scalar_rw)
00168               (attributes::created, "")
00169               (attributes::started, "")
00170               (attributes::finished, "")
00171               (attributes::working_directory, "")
00172               (attributes::exitcode, "")
00173               (attributes::termsig, "")
00174               (attributes::job_service, "")
00175           ;
00176 
00177       strmap_type attributes_vector_rw;
00178       insert(attributes_vector_rw)
00179               (attributes::execution_hosts, "")
00180           ;
00181 
00182       // initialize attribute implementation
00183       this->attribute_base::init (attributes_scalar_ro, attributes_scalar_rw,
00184           strmap_type(), attributes_vector_rw);
00185       
00186       // initialize metrics
00187       std::vector<saga::metric> metrics;
00188       for (unsigned int i = 0; 
00189            i < sizeof(saga::job::metrics::job_metric_data)/sizeof(saga::metrics::init_data);
00190            ++i)
00191       {
00192           saga::metrics::init_data const* p = &saga::job::metrics::job_metric_data[i];
00193           saga::metric m(*this, p->name, p->description, p->mode, p->unit, 
00194               p->type, p->value);
00195           metrics.push_back(m);
00196       }
00197       this->monitorable_base::init (metrics);
00198   }
00199   
00200   job::~job (void)
00201   {
00202   }
00203 
00205   job::job (saga::object const& o)
00206     : saga::task (o)
00207   {
00208       if (this->get_type() != saga::object::Job)
00209       {
00210           SAGA_THROW("Bad type conversion.", saga::BadParameter);
00211       }
00212   }
00213 
00214   job &job::operator= (saga::object const& o)
00215   {
00216       return saga::task::operator=(o), *this;
00217   }
00218 
00220   saga::impl::job* job::get_impl() const
00221   { 
00222       typedef saga::object base_type;
00223       return static_cast<saga::impl::job*>(this->base_type::get_impl()); 
00224   }
00225 
00226   TR1::shared_ptr <saga::impl::job> job::get_impl_sp(void) const
00227   { 
00228       // FIXME: this needs documentation
00229       typedef saga::object base_type;
00230       return TR1::static_pointer_cast <saga::impl::job> (
00231           this->base_type::get_impl_sp());
00232   }
00233 
00234   bool job::is_impl_valid() const
00235   {
00236       typedef saga::object base_type;
00237       if (!this->base_type::is_impl_valid()) 
00238           return false;
00239 
00240       if (TR1::dynamic_pointer_cast<saga::impl::job>(
00241               this->base_type::get_impl_sp()))
00242       {
00243           return true;
00244       }
00245       return false;
00246   }
00247 
00248   // job task interface
00249   SAGA_CALL_IMP_0_EX(job, run, job_run)
00250   SAGA_CALL_IMP_1_EX(job, cancel, job_cancel, double)
00251   SAGA_CALL_IMP_1_EX(job, wait, job_wait, double)
00252 
00253   // job inspection
00254   SAGA_CALL_IMP_0 (job, get_job_id)
00255   SAGA_CALL_IMP_0 (job, get_state)
00256   SAGA_CALL_IMP_0 (job, get_description)
00257   SAGA_CALL_IMP_0 (job, get_stdin)
00258   SAGA_CALL_IMP_0 (job, get_stdout)
00259   SAGA_CALL_IMP_0 (job, get_stderr)
00260 
00261   // job management
00262   SAGA_CALL_IMP_0 (job, suspend)
00263   SAGA_CALL_IMP_0 (job, resume)
00264   SAGA_CALL_IMP_0 (job, checkpoint)
00265   SAGA_CALL_IMP_1 (job, migrate, description)
00266   SAGA_CALL_IMP_1 (job, signal, int)
00267 
00268   }   // namspace job
00269 
00270   namespace detail
00271   {
00273     //  implement the attribute functions (we need to explicitly specialize 
00274     //  the template because the functions are not implemented inline)
00275     template struct SAGA_JOB_PACKAGE_EXPORT_REPEAT attribute<job::job>;
00276 
00277     template struct SAGA_JOB_PACKAGE_EXPORT attribute_priv<job::job, task_base::Sync>;
00278     template struct SAGA_JOB_PACKAGE_EXPORT attribute_priv<job::job, task_base::Async>;
00279     template struct SAGA_JOB_PACKAGE_EXPORT attribute_priv<job::job, task_base::Task>;
00280 
00281     template struct SAGA_JOB_PACKAGE_EXPORT attribute_sync<job::job>;
00282 
00284     //  implement the permissions functions (we need to explicitly specialize 
00285     //  the template because the functions are not implemented inline)
00286     template struct SAGA_JOB_PACKAGE_EXPORT_REPEAT permissions<job::job>;
00287 
00288     template struct SAGA_JOB_PACKAGE_EXPORT permissions_priv<job::job, task_base::Sync>;
00289     template struct SAGA_JOB_PACKAGE_EXPORT permissions_priv<job::job, task_base::Async>;
00290     template struct SAGA_JOB_PACKAGE_EXPORT permissions_priv<job::job, task_base::Task>;
00291   }
00292 
00293   namespace adaptors
00294   {
00295     saga::job::state job_state_value_to_enum(std::string const& val)
00296     {
00297       if (val == saga::job::attributes::job_state_new) 
00298         return saga::job::New;
00299 
00300       if (val == saga::job::attributes::job_state_done) 
00301         return saga::job::Done;
00302 
00303       if (val == saga::job::attributes::job_state_running) 
00304         return saga::job::Running;
00305 
00306       if (val == saga::job::attributes::job_state_failed) 
00307         return saga::job::Failed;
00308 
00309       if (val == saga::job::attributes::job_state_canceled) 
00310         return saga::job::Canceled;
00311 
00312       if (val == saga::job::attributes::job_state_suspended) 
00313         return saga::job::Suspended;
00314 
00315       return saga::job::Unknown;
00316     }
00317 
00318     std::string job_state_enum_to_value(int s)
00319     {
00320       return saga::job::detail::get_state_name((saga::job::state)s);
00321     }
00322   }
00323 
00325 } // namespace saga
00326 
00327 #endif
00328 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines