1 /** @file excompiler.cpp
3 * Functions to facilitate the conversion of a ex to a function pointer suited for
4 * fast numerical integration.
9 * GiNaC Copyright (C) 1999-2010 Johannes Gutenberg University Mainz, Germany
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 #include "excompiler.h"
34 #include "operators.h"
35 #include "relational.h"
40 #endif // def HAVE_LIBDL
53 * Small class that manages modules opened by libdl. It is used by compile_ex
54 * and link_ex in order to have a clean-up of opened modules and their
55 * associated source and so-files at the time of program termination. It is
56 * supposed to be statically instantiated once (see definition of
57 * global_excompiler below). The dtor of that object then performs the
58 * clean-up. On top of that it provides some functionality shared between
59 * different compile_ex and link_ex specializations.
64 * Holds all necessary information about opened modules.
69 std::string name; /**< filename with .so suffix */
70 bool clean_up; /**< if true, source and so-file will be deleted */
72 std::vector<filedesc> filelist; /**< List of all opened modules */
75 * Complete clean-up of opend modules is done on destruction.
79 for (std::vector<filedesc>::const_iterator it = filelist.begin(); it != filelist.end(); ++it) {
84 * Adds a new module to the list.
86 void add_opened_module(void* module, const std::string& name, bool clean_up)
91 fd.clean_up = clean_up;
92 filelist.push_back(fd);
95 * Closes a module and deletes the so-file if the associated clean-up flag is true.
97 void clean_up(const std::vector<filedesc>::const_iterator it)
101 remove(it->name.c_str());
105 * Creates a new C source file and adds a standard header. If filename is
106 * empty, a unique random name is produced and used.
108 void create_src_file(std::string& filename, std::ofstream& ofs)
110 if (filename.empty()) {
111 // fill filename with unique random word
112 const char* filename_pattern = "./GiNaCXXXXXX";
113 char* new_filename = new char[strlen(filename_pattern)+1];
114 strcpy(new_filename, filename_pattern);
115 if (!mktemp(new_filename)) {
116 delete[] new_filename;
117 throw std::runtime_error("mktemp failed");
119 filename = std::string(new_filename);
120 ofs.open(new_filename, std::ios::out);
121 delete[] new_filename;
123 // use parameter as filename
124 ofs.open(filename.c_str(), std::ios::out);
128 throw std::runtime_error("could not create source code file for compilation");
131 ofs << "#include <stddef.h> " << std::endl;
132 ofs << "#include <stdlib.h> " << std::endl;
133 ofs << "#include <math.h> " << std::endl;
137 * Calls the shell script 'ginac-excompiler' to compile the produced C
138 * source file into an linkable so-file. On demand the C source file is
141 void compile_src_file(const std::string filename, bool clean_up)
143 std::string strcompile = "ginac-excompiler " + filename;
144 if (system(strcompile.c_str())) {
145 throw std::runtime_error("excompiler::compile_src_file: error compiling source file!");
148 remove(filename.c_str());
152 * Links a so-file whose filename is given.
154 void* link_so_file(const std::string filename, bool clean_up)
157 module = dlopen(filename.c_str(), RTLD_NOW);
158 if (module == NULL) {
159 throw std::runtime_error("excompiler::link_so_file: could not open compiled module!");
162 add_opened_module(module, filename, clean_up);
164 return dlsym(module, "compiled_ex");
167 * Removes a modules from the module list. Performs a clean-up before that.
168 * Every module with the given name will be affected.
170 void unlink(const std::string filename)
172 for (std::vector<filedesc>::iterator it = filelist.begin(); it != filelist.end();) {
173 if (it->name == filename) {
184 * This static object manages the modules opened by the complile_ex and link_ex
185 * functions. On program termination its dtor is called and all open modules
186 * are closed. The associated source and so-files are eventually deleted then
188 * In principle this could lead to a static deconstruction order fiasco, if
189 * other code from this library uses the compile_ex and link_ex functions
190 * (which it doesn't at the moment and won't in the likely future, so therefore
191 * we ignore this issue).
193 static excompiler global_excompiler;
195 void compile_ex(const ex& expr, const symbol& sym, FUNCP_1P& fp, const std::string filename)
198 ex expr_with_x = expr.subs(lst(sym==x));
201 std::string unique_filename = filename;
202 global_excompiler.create_src_file(unique_filename, ofs);
204 ofs << "double compiled_ex(double x)" << std::endl;
205 ofs << "{" << std::endl;
206 ofs << "double res = ";
207 expr_with_x.print(GiNaC::print_csrc_double(ofs));
208 ofs << ";" << std::endl;
209 ofs << "return(res); " << std::endl;
210 ofs << "}" << std::endl;
214 global_excompiler.compile_src_file(unique_filename, filename.empty());
215 // This is not standard compliant! ... no conversion between
216 // pointer-to-functions and pointer-to-objects ...
217 fp = (FUNCP_1P) global_excompiler.link_so_file(unique_filename+".so", filename.empty());
220 void compile_ex(const ex& expr, const symbol& sym1, const symbol& sym2, FUNCP_2P& fp, const std::string filename)
222 symbol x("x"), y("y");
223 ex expr_with_xy = expr.subs(lst(sym1==x, sym2==y));
226 std::string unique_filename = filename;
227 global_excompiler.create_src_file(unique_filename, ofs);
229 ofs << "double compiled_ex(double x, double y)" << std::endl;
230 ofs << "{" << std::endl;
231 ofs << "double res = ";
232 expr_with_xy.print(GiNaC::print_csrc_double(ofs));
233 ofs << ";" << std::endl;
234 ofs << "return(res); " << std::endl;
235 ofs << "}" << std::endl;
239 global_excompiler.compile_src_file(unique_filename, filename.empty());
240 // This is not standard compliant! ... no conversion between
241 // pointer-to-functions and pointer-to-objects ...
242 fp = (FUNCP_2P) global_excompiler.link_so_file(unique_filename+".so", filename.empty());
245 void compile_ex(const lst& exprs, const lst& syms, FUNCP_CUBA& fp, const std::string filename)
248 for (std::size_t count=0; count<syms.nops(); ++count) {
249 std::ostringstream s;
250 s << "a[" << count << "]";
251 replacements.append(syms.op(count) == symbol(s.str()));
254 std::vector<ex> expr_with_cname;
255 for (std::size_t count=0; count<exprs.nops(); ++count) {
256 expr_with_cname.push_back(exprs.op(count).subs(replacements));
260 std::string unique_filename = filename;
261 global_excompiler.create_src_file(unique_filename, ofs);
263 ofs << "void compiled_ex(const int* an, const double a[], const int* fn, double f[])" << std::endl;
264 ofs << "{" << std::endl;
265 for (std::size_t count=0; count<exprs.nops(); ++count) {
266 ofs << "f[" << count << "] = ";
267 expr_with_cname[count].print(GiNaC::print_csrc_double(ofs));
268 ofs << ";" << std::endl;
270 ofs << "}" << std::endl;
274 global_excompiler.compile_src_file(unique_filename, filename.empty());
275 // This is not standard compliant! ... no conversion between
276 // pointer-to-functions and pointer-to-objects ...
277 fp = (FUNCP_CUBA) global_excompiler.link_so_file(unique_filename+".so", filename.empty());
280 void link_ex(const std::string filename, FUNCP_1P& fp)
282 // This is not standard compliant! ... no conversion between
283 // pointer-to-functions and pointer-to-objects ...
284 fp = (FUNCP_1P) global_excompiler.link_so_file(filename, false);
287 void link_ex(const std::string filename, FUNCP_2P& fp)
289 // This is not standard compliant! ... no conversion between
290 // pointer-to-functions and pointer-to-objects ...
291 fp = (FUNCP_2P) global_excompiler.link_so_file(filename, false);
294 void link_ex(const std::string filename, FUNCP_CUBA& fp)
296 // This is not standard compliant! ... no conversion between
297 // pointer-to-functions and pointer-to-objects ...
298 fp = (FUNCP_CUBA) global_excompiler.link_so_file(filename, false);
301 void unlink_ex(const std::string filename)
303 global_excompiler.unlink(filename);
306 #else // def HAVE_LIBDL
309 * In case no working libdl has been found by configure, the following function
310 * stubs preserve the interface. Every function just raises an exception.
313 void compile_ex(const ex& expr, const symbol& sym, FUNCP_1P& fp, const std::string filename)
315 throw std::runtime_error("compile_ex has been disabled because of missing libdl!");
318 void compile_ex(const ex& expr, const symbol& sym1, const symbol& sym2, FUNCP_2P& fp, const std::string filename)
320 throw std::runtime_error("compile_ex has been disabled because of missing libdl!");
323 void compile_ex(const lst& exprs, const lst& syms, FUNCP_CUBA& fp, const std::string filename)
325 throw std::runtime_error("compile_ex has been disabled because of missing libdl!");
328 void link_ex(const std::string filename, FUNCP_1P& fp)
330 throw std::runtime_error("link_ex has been disabled because of missing libdl!");
333 void link_ex(const std::string filename, FUNCP_2P& fp)
335 throw std::runtime_error("link_ex has been disabled because of missing libdl!");
338 void link_ex(const std::string filename, FUNCP_CUBA& fp)
340 throw std::runtime_error("link_ex has been disabled because of missing libdl!");
343 void unlink_ex(const std::string filename)
345 throw std::runtime_error("unlink_ex has been disabled because of missing libdl!");
348 #endif // def HAVE_LIBDL