crown
1.0.0
|
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/CrownScalingILP.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, mapping, 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.mapping); 00066 pelib_argument_stream_init(&out.config); 00067 00068 for(; args[0] != NULL; args++) 00069 { 00070 if(strcmp(args[0], "--show-stdout") == 0) 00071 { 00072 out.showOutput = true; 00073 continue; 00074 } 00075 if(strcmp(args[0], "--show-stderr") == 0) 00076 { 00077 out.showError = true; 00078 continue; 00079 } 00080 if(strcmp(args[0], "--alpha") == 0) 00081 { 00082 args++; 00083 stringstream str(args[0]); 00084 str >> out.alpha; 00085 continue; 00086 } 00087 if(strcmp(args[0], "--eta") == 0) 00088 { 00089 args++; 00090 stringstream str(args[0]); 00091 str >> out.eta; 00092 continue; 00093 } 00094 if(strcmp(args[0], "--zeta") == 0) 00095 { 00096 args++; 00097 stringstream str(args[0]); 00098 str >> out.zeta; 00099 continue; 00100 } 00101 if(strcmp(args[0], "--kappa") == 0) 00102 { 00103 args++; 00104 stringstream str(args[0]); 00105 str >> out.kappa; 00106 continue; 00107 } 00108 if(strcmp(args[0], "--allocation") == 0) 00109 { 00110 args++; 00111 args += pelib_argument_stream_parse(args, &out.alloc) - 1; 00112 continue; 00113 } 00114 if(strcmp(args[0], "--mapping") == 0) 00115 { 00116 args++; 00117 args += pelib_argument_stream_parse(args, &out.mapping) - 1; 00118 continue; 00119 } 00120 if(strcmp(args[0], "--config") == 0) 00121 { 00122 args++; 00123 args += pelib_argument_stream_parse(args, &out.config) - 1; 00124 continue; 00125 } 00126 } 00127 00128 return out; 00129 } 00130 00131 const CrownScaling* 00132 crown_scaling(size_t argc, char **argv) 00133 { 00134 args_t args = parse(argv); 00135 void *libAlloc = load_lib(args.alloc.library); 00136 void *libMapping = load_lib(args.mapping.library); 00137 void *libConfig = load_lib(args.config.library); 00138 CrownAllocation* (*allocate)(size_t argc, char **argv); 00139 CrownMapping* (*map)(size_t argc, char **argv); 00140 CrownConfig* (*conf)(size_t argc, char **argv); 00141 CrownAllocation* alloc = NULL; 00142 CrownMapping* mapping = NULL; 00143 CrownConfig *config = NULL; 00144 00145 // Load crown allocation 00146 if(args.alloc.library != NULL) 00147 { 00148 void *libConfig = load_lib(args.alloc.library); 00149 allocate = (CrownAllocation* (*)(size_t argc, char **argv))load_function(libConfig, "crown_allocation"); 00150 alloc = allocate(args.alloc.argc, args.alloc.argv); 00151 } 00152 00153 // Load crown mapping 00154 if(args.mapping.library != NULL) 00155 { 00156 void *libConfig = load_lib(args.mapping.library); 00157 map = (CrownMapping* (*)(size_t argc, char **argv))load_function(libConfig, "crown_mapping"); 00158 mapping = map(args.mapping.argc, args.mapping.argv); 00159 } 00160 00161 // Load crown config 00162 if(args.config.library != NULL) 00163 { 00164 void *libConfig = load_lib(args.config.library); 00165 conf = (CrownConfig* (*)(size_t argc, char **argv))load_function(libConfig, "crown_config"); 00166 config = conf(args.config.argc, args.config.argv); 00167 } 00168 00169 // Convert solver output to general schedule 00170 CrownScaling *scal = new CrownScalingILP(config, alloc, mapping, args.showOutput, args.showError); 00171 00172 // Destroy object instances by libraries 00173 if(args.alloc.library != NULL) 00174 { 00175 void (*del)(CrownAllocation *) = (void (*)(CrownAllocation*))load_function(libAlloc, "crown_delete_algebra"); 00176 del(alloc); 00177 destroy_lib(libAlloc); 00178 } 00179 00180 // Destroy object instances by libraries 00181 if(args.mapping.library != NULL) 00182 { 00183 void (*del)(CrownMapping *) = (void (*)(CrownMapping*))load_function(libMapping, "crown_delete_algebra"); 00184 del(mapping); 00185 destroy_lib(libMapping); 00186 } 00187 00188 // Destroy object instances by libraries 00189 if(args.config.library != NULL) 00190 { 00191 void (*del)(CrownConfig *) = (void (*)(CrownConfig*))load_function(libConfig, "crown_delete_config"); 00192 del(config); 00193 destroy_lib(libConfig); 00194 } 00195 00196 return scal; 00197 } 00198 00199 const CrownScheduler* 00200 crown_scheduler(size_t argc, char **argv, const CrownScheduler *composite) 00201 { 00202 return crown_scaling(argc, argv); 00203 } 00204 00205 void 00206 crown_delete_algebra(pelib::Algebra *alg) 00207 { 00208 delete alg; 00209 } 00210 00211 #ifdef __cplusplus 00212 } 00213 #endif 00214 00215