crown  1.0.0
src/crown-binary.cpp
Go to the documentation of this file.
00001 /*
00002  Copyright 2015 Nicolas Melot
00003 
00004  This file is part of Crown.
00005 
00006  Crown is free software: you can redistribute it and/or modify
00007  it under the terms of the GNU General Public License as published by
00008  the Free Software Foundation, either version 3 of the License, or
00009  (at your option) any later version.
00010 
00011  Crown is distributed in the hope that it will be useful,
00012  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00014  GNU General Public License for more details.
00015 
00016  You should have received a copy of the GNU General Public License
00017  along with Crown. If not, see <http://www.gnu.org/licenses/>.
00018 
00019 */
00020 
00021 #include <pelib/Scalar.hpp>
00022 #include <pelib/argument_parsing.hpp>
00023 #include <pelib/scheduler.h>
00024 #include <pelib/dl.h>
00025 
00026 #include <crown/crown-scheduler.hpp>
00027 #include <crown/CrownAllocation.hpp>
00028 #include <crown/CrownMapping.hpp>
00029 #include <crown/CrownScaling.hpp>
00030 #include <crown/CrownBinary.hpp>
00031 
00032 #ifdef debug
00033 #undef debug
00034 #endif
00035 
00036 #define debug(var) cout << "[" << __FILE__ << ":" << __FUNCTION__ << ":" << __LINE__ << "] " << #var << " = \"" << (var) << "\"" << endl;
00037 
00038 using namespace std;
00039 using namespace pelib;
00040 using namespace crown;
00041 
00042 #ifdef __cplusplus
00043 extern "C" {
00044 #endif
00045 
00046 struct args
00047 {
00048         double alpha, eta, zeta, kappa, precision;
00049         pelib_argument_stream mapping, scaling, config;
00050         bool showError, showOutput;
00051         
00052 };
00053 typedef struct args args_t;
00054 
00055 static args_t
00056 parse(char **args)
00057 {
00058         args_t out;
00059         out.showError = false;
00060         out.showOutput = false;
00061         out.alpha = 3;
00062         out.zeta = 0.649;
00063         out.eta = 0.018611;
00064         out.kappa = 52.639161335;
00065         out.precision = 3;
00066         //out.configSrc = BINARY;
00067         pelib_argument_stream_init(&out.mapping);
00068         pelib_argument_stream_init(&out.scaling);
00069         pelib_argument_stream_init(&out.config);
00070 
00071         for(; args[0] != NULL; args++)
00072         {
00073                 if(strcmp(args[0], "--show-stdout") == 0)
00074                 {
00075                         out.showOutput = true;
00076                         continue;
00077                 }
00078                 if(strcmp(args[0], "--show-stderr") == 0)
00079                 {
00080                         out.showError = true;
00081                         continue;
00082                 }
00083                 if(strcmp(args[0], "--alpha") == 0)
00084                 {
00085                         args++;
00086                         stringstream str(args[0]);
00087                         str >> out.alpha;
00088                         continue;
00089                 }
00090                 if(strcmp(args[0], "--eta") == 0)
00091                 {
00092                         args++;
00093                         stringstream str(args[0]);
00094                         str >> out.eta;
00095                         continue;
00096                 }
00097                 if(strcmp(args[0], "--zeta") == 0)
00098                 {
00099                         args++;
00100                         stringstream str(args[0]);
00101                         str >> out.zeta;
00102                         continue;
00103                 }
00104                 if(strcmp(args[0], "--kappa") == 0)
00105                 {
00106                         args++;
00107                         stringstream str(args[0]);
00108                         str >> out.kappa;
00109                         continue;
00110                 }
00111                 if(strcmp(args[0], "--precision") == 0)
00112                 {
00113                         args++;
00114                         stringstream str(args[0]);
00115                         str >> out.kappa;
00116                         continue;
00117                 }
00118                 if(strcmp(args[0], "--config") == 0)
00119                 {
00120                         args++;
00121                         args += pelib_argument_stream_parse(args, &out.config) - 1;
00122                         continue;
00123                 }
00124                 if(strcmp(args[0], "--mapping") == 0)
00125                 {
00126                         args++;
00127                         args += pelib_argument_stream_parse(args, &out.mapping) - 1;
00128                         continue;
00129                 }
00130                 if(strcmp(args[0], "--scaling") == 0)
00131                 {
00132                         args++;
00133                         args += pelib_argument_stream_parse(args, &out.scaling) - 1;
00134                         continue;
00135                 }
00136         }
00137 
00138         return out;
00139 }
00140 
00141 const CrownScheduler*
00142 crown_scheduler(size_t argc, char **argv, const CrownScheduler *composite)
00143 {
00144         args_t args = parse(argv);
00145 
00146         void *libMapping = load_lib(args.mapping.library);
00147         void *libScaling = load_lib(args.scaling.library);
00148         void *libConfig = load_lib(args.config.library);
00149 
00150         CrownMapping* (*map)(size_t argc, char **argv);
00151         CrownScaling* (*scal)(size_t argc, char **argv);
00152         CrownConfig* (*conf)(size_t argc, char **argv);
00153 
00154         Algebra param;
00155         param.insert(new Scalar<float>("alpha", args.alpha));
00156         param.insert(new Scalar<float>("eta", args.eta));
00157         param.insert(new Scalar<float>("zeta", args.zeta));
00158         param.insert(new Scalar<float>("kappa", args.kappa));
00159 
00160         CrownMapping* mapping = NULL;
00161         CrownScaling* scaling = NULL;
00162         CrownConfig *config = NULL;
00163 
00164         // Load crown mapping
00165         if(args.mapping.library != NULL)
00166         {
00167                 void *libConfig = load_lib(args.mapping.library);
00168                 map = (CrownMapping* (*)(size_t argc, char **argv))load_function(libConfig, "crown_mapping");
00169                 mapping = map(args.mapping.argc, args.mapping.argv);
00170         }
00171 
00172         // Load crown scaling
00173         if(args.scaling.library != NULL)
00174         {
00175                 void *libConfig = load_lib(args.scaling.library);
00176                 scal = (CrownScaling* (*)(size_t argc, char **argv))load_function(libConfig, "crown_scaling");
00177                 scaling = scal(args.scaling.argc, args.scaling.argv);
00178         }
00179 
00180         // Load crown config
00181         if(args.config.library != NULL)
00182         {
00183                 void *libConfig = load_lib(args.config.library);
00184                 conf = (CrownConfig* (*)(size_t argc, char **argv))load_function(libConfig, "crown_config");
00185                 config = conf(args.config.argc, args.config.argv);
00186         }
00187 
00188         CrownScheduler *scheduler;
00189         // Compute a schedule
00190         scheduler = new CrownBinary(param, config, mapping, scaling, args.precision, args.showOutput, args.showError);
00191 
00192         // Destroy object instances by libraries
00193         if(args.mapping.library != NULL)
00194         {
00195                 void (*del)(CrownMapping *) = (void (*)(CrownMapping*))load_function(libMapping, "crown_delete_algebra");
00196                 del(mapping);
00197                 destroy_lib(libMapping);
00198         }
00199 
00200         // Destroy object instances by libraries
00201         if(args.scaling.library != NULL)
00202         {
00203                 void (*del)(CrownScaling *) = (void (*)(CrownScaling*))load_function(libScaling, "crown_delete_algebra");
00204                 del(scaling);
00205                 destroy_lib(libScaling);
00206         }
00207 
00208         // Destroy object instances by libraries
00209         if(args.config.library != NULL)
00210         {
00211                 void (*del)(CrownConfig *) = (void (*)(CrownConfig*))load_function(libConfig, "crown_delete_config");
00212                 del(config);
00213                 destroy_lib(libConfig);
00214         }
00215 
00216         return scheduler;
00217 }
00218 
00219 #ifdef __cplusplus
00220 }
00221 #endif
00222