crown  1.0.0
src/mapping-ilp.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 
00022 #include <fstream>
00023 
00024 #include <pelib/Scalar.hpp>
00025 #include <pelib/argument_parsing.hpp>
00026 #include <pelib/dl.h>
00027 
00028 #include <crown/crown-scheduler.hpp>
00029 #include <crown/CrownMappingILP.hpp>
00030 
00031 #ifdef debug
00032 #undef debug
00033 #endif
00034 
00035 #define debug(var) cout << "[" << __FILE__ << ":" << __FUNCTION__ << ":" << __LINE__ << "] " << #var << " = \"" << (var) << "\"" << endl;
00036 
00037 using namespace std;
00038 using namespace pelib;
00039 using namespace crown;
00040 
00041 #ifdef __cplusplus
00042 extern "C" {
00043 #endif
00044 
00045 struct args
00046 {
00047         double alpha, eta, zeta, kappa;
00048         bool showError, showOutput;
00049         pelib_argument_stream alloc, config;
00050         
00051 };
00052 typedef struct args args_t;
00053 
00054 static args_t
00055 parse(char **args)
00056 {
00057         args_t out;
00058         out.showError = false;
00059         out.showOutput = false;
00060         out.alpha = 3;
00061         out.zeta = 0.649;
00062         out.eta = 0.018611;
00063         out.kappa = 52.639161335;
00064         pelib_argument_stream_init(&out.alloc);
00065         pelib_argument_stream_init(&out.config);
00066 
00067         for(; args[0] != NULL; args++)
00068         {
00069                 if(strcmp(args[0], "--show-stdout") == 0)
00070                 {
00071                         out.showOutput = true;
00072                         continue;
00073                 }
00074                 if(strcmp(args[0], "--show-stderr") == 0)
00075                 {
00076                         out.showError = true;
00077                         continue;
00078                 }
00079                 if(strcmp(args[0], "--alpha") == 0)
00080                 {
00081                         args++;
00082                         stringstream str(args[0]);
00083                         str >> out.alpha;
00084                         continue;
00085                 }
00086                 if(strcmp(args[0], "--eta") == 0)
00087                 {
00088                         args++;
00089                         stringstream str(args[0]);
00090                         str >> out.eta;
00091                         continue;
00092                 }
00093                 if(strcmp(args[0], "--zeta") == 0)
00094                 {
00095                         args++;
00096                         stringstream str(args[0]);
00097                         str >> out.zeta;
00098                         continue;
00099                 }
00100                 if(strcmp(args[0], "--kappa") == 0)
00101                 {
00102                         args++;
00103                         stringstream str(args[0]);
00104                         str >> out.kappa;
00105                         continue;
00106                 }
00107                 if(strcmp(args[0], "--allocation") == 0)
00108                 {
00109                         args++;
00110                         args += pelib_argument_stream_parse(args, &out.alloc) - 1;
00111                         continue;
00112                 }
00113                 if(strcmp(args[0], "--config") == 0)
00114                 {
00115                         args++;
00116                         args += pelib_argument_stream_parse(args, &out.config) - 1;
00117                         continue;
00118                 }
00119         }
00120 
00121         return out;
00122 }
00123 
00124 const CrownMapping*
00125 crown_mapping(size_t argc, char **argv)
00126 {
00127         args_t args = parse(argv);
00128         void *libAlloc = load_lib(args.alloc.library);
00129         void *libConfig = load_lib(args.config.library);
00130         CrownAllocation* (*allocate)(size_t argc, char **argv);
00131         CrownConfig* (*conf)(size_t argc, char **argv);
00132         CrownAllocation* alloc = NULL;
00133         CrownConfig *config = NULL;
00134 
00135         // Load crown allocation
00136         if(args.alloc.library != NULL)
00137         {
00138                 void *libConfig = load_lib(args.alloc.library);
00139                 allocate = (CrownAllocation* (*)(size_t argc, char **argv))load_function(libConfig, "crown_allocation");
00140                 alloc = allocate(args.alloc.argc, args.alloc.argv);
00141         }
00142 
00143         // Load crown config
00144         if(args.config.library != NULL)
00145         {
00146                 void *libConfig = load_lib(args.config.library);
00147                 conf = (CrownConfig* (*)(size_t argc, char **argv))load_function(libConfig, "crown_config");
00148                 config = conf(args.config.argc, args.config.argv);
00149         }
00150 
00151         // Convert solver output to general schedule
00152         CrownMapping *map = new CrownMappingILP(config, alloc, args.showOutput, args.showError);
00153 
00154         // Destroy object instances by libraries
00155         if(args.alloc.library != NULL)
00156         {
00157                 void (*del)(CrownAllocation *) = (void (*)(CrownAllocation*))load_function(libAlloc, "crown_delete_algebra");
00158                 del(alloc);
00159                 destroy_lib(libAlloc);
00160         }
00161 
00162         // Destroy object instances by libraries
00163         if(args.config.library != NULL)
00164         {
00165                 void (*del)(CrownConfig *) = (void (*)(CrownConfig*))load_function(libConfig, "crown_delete_config");
00166                 del(config);
00167                 destroy_lib(libConfig);
00168         }
00169 
00170         return map;
00171 }
00172 
00173 const CrownScheduler*
00174 crown_scheduler(size_t argc, char **argv, const CrownScheduler *composite)
00175 {
00176         return crown_mapping(argc, argv);
00177 }
00178 
00179 void
00180 crown_delete_algebra(pelib::Algebra *alg)
00181 {
00182         delete alg;
00183 }
00184 
00185 #ifdef __cplusplus
00186 }
00187 #endif
00188