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-2007 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"
39 #endif // def HAVE_LIBDL
43 #include "operators.h"
44 #include "relational.h"
52 * Small class that manages modules opened by libdl. It is used by compile_ex
53 * and link_ex in order to have a clean-up of opened modules and their
54 * associated source and so-files at the time of program termination. It is
55 * supposed to be statically instantiated once (see definition of
56 * global_excompiler below). The dtor of that object then performs the
57 * clean-up. On top of that it provides some functionality shared between
58 * different compile_ex and link_ex specializations.
63 * Holds all necessary information about opened modules.
68 std::string name; /**< filename with .so suffix */
69 bool clean_up; /**< if true, source and so-file will be deleted */
71 std::vector<filedesc> filelist; /**< List of all opened modules */
74 * Complete clean-up of opend modules is done on destruction.
78 for (std::vector<filedesc>::const_iterator it = filelist.begin(); it != filelist.end(); ++it) {
83 * Adds a new module to the list.
85 void add_opened_module(void* module, const std::string& name, bool clean_up)
90 fd.clean_up = clean_up;
91 filelist.push_back(fd);
94 * Closes a module and deletes the so-file if the associated clean-up flag is true.
96 void clean_up(const std::vector<filedesc>::const_iterator it)
100 remove(it->name.c_str());
104 * Creates a new C source file and adds a standard header. If filename is
105 * empty, a unique random name is produced and used.
107 void create_src_file(std::string& filename, std::ofstream& ofs)
109 if (filename.empty()) {
110 // fill filename with unique random word
111 const char* filename_pattern = "GiNaCXXXXXX";
112 char* new_filename = new char[strlen(filename_pattern)+1];
113 strcpy(new_filename, filename_pattern);
114 if (!mktemp(new_filename)) {
116 throw std::runtime_error("mktemp failed");
118 filename = std::string(new_filename);
119 ofs.open(new_filename, std::ios::out);
122 // use parameter as filename
123 ofs.open(filename.c_str(), std::ios::out);
127 throw std::runtime_error("could not create source code file for compilation");
130 ofs << "#include <stddef.h> " << std::endl;
131 ofs << "#include <stdlib.h> " << std::endl;
132 ofs << "#include <math.h> " << std::endl;
136 * Calls the shell script 'ginac-excompiler' to compile the produced C
137 * source file into an linkable so-file. On demand the C source file is
140 void compile_src_file(const std::string filename, bool clean_up)
142 std::string strcompile = "ginac-excompiler " + filename;
143 if (system(strcompile.c_str())) {
144 throw std::runtime_error("excompiler::compile_src_file: error compiling source file!");
147 remove(filename.c_str());
151 * Links a so-file whose filename is given.
153 void* link_so_file(const std::string filename, bool clean_up)
156 module = dlopen(filename.c_str(), RTLD_NOW);
157 if (module == NULL) {
158 throw std::runtime_error("excompiler::link_so_file: could not open compiled module!");
161 add_opened_module(module, filename, clean_up);
163 return dlsym(module, "compiled_ex");
166 * Removes a modules from the module list. Performs a clean-up before that.
167 * Every module with the given name will be affected.
169 void unlink(const std::string filename)
171 for (std::vector<filedesc>::iterator it = filelist.begin(); it != filelist.end();) {
172 if (it->name == filename) {
183 * This static object manages the modules opened by the complile_ex and link_ex
184 * functions. On program termination its dtor is called and all open modules
185 * are closed. The associated source and so-files are eventually deleted then
187 * In principle this could lead to a static deconstruction order fiasco, if
188 * other code from this library uses the compile_ex and link_ex functions
189 * (which it doesn't at the moment and won't in the likely future, so therefore
190 * we ignore this issue).
192 static excompiler global_excompiler;
194 void compile_ex(const ex& expr, const symbol& sym, FUNCP_1P& fp, const std::string filename)
197 ex expr_with_x = expr.subs(lst(sym==x));
200 std::string unique_filename = filename;
201 global_excompiler.create_src_file(unique_filename, ofs);
203 ofs << "double compiled_ex(double x)" << std::endl;
204 ofs << "{" << std::endl;
205 ofs << "double res = ";
206 expr_with_x.print(GiNaC::print_csrc_double(ofs));
207 ofs << ";" << std::endl;
208 ofs << "return(res); " << std::endl;
209 ofs << "}" << std::endl;
213 global_excompiler.compile_src_file(unique_filename, filename.empty());
214 // This is not standard compliant! ... no conversion between
215 // pointer-to-functions and pointer-to-objects ...
216 fp = (FUNCP_1P) global_excompiler.link_so_file(unique_filename+".so", filename.empty());
219 void compile_ex(const ex& expr, const symbol& sym1, const symbol& sym2, FUNCP_2P& fp, const std::string filename)
221 symbol x("x"), y("y");
222 ex expr_with_xy = expr.subs(lst(sym1==x, sym2==y));
225 std::string unique_filename = filename;
226 global_excompiler.create_src_file(unique_filename, ofs);
228 ofs << "double compiled_ex(double x, double y)" << std::endl;
229 ofs << "{" << std::endl;
230 ofs << "double res = ";
231 expr_with_xy.print(GiNaC::print_csrc_double(ofs));
232 ofs << ";" << std::endl;
233 ofs << "return(res); " << std::endl;
234 ofs << "}" << std::endl;
238 global_excompiler.compile_src_file(unique_filename, filename.empty());
239 // This is not standard compliant! ... no conversion between
240 // pointer-to-functions and pointer-to-objects ...
241 fp = (FUNCP_2P) global_excompiler.link_so_file(unique_filename+".so", filename.empty());
244 void compile_ex(const lst& exprs, const lst& syms, FUNCP_CUBA& fp, const std::string filename)
247 for (int count=0; count<syms.nops(); ++count) {
248 std::ostringstream s;
249 s << "a[" << count << "]";
250 replacements.append(syms.op(count) == symbol(s.str()));
253 std::vector<ex> expr_with_cname;
254 for (int count=0; count<exprs.nops(); ++count) {
255 expr_with_cname.push_back(exprs.op(count).subs(replacements));
259 std::string unique_filename = filename;
260 global_excompiler.create_src_file(unique_filename, ofs);
262 ofs << "void compiled_ex(const int* an, const double a[], const int* fn, double f[])" << std::endl;
263 ofs << "{" << std::endl;
264 for (int count=0; count<exprs.nops(); ++count) {
265 ofs << "f[" << count << "] = ";
266 expr_with_cname[count].print(GiNaC::print_csrc_double(ofs));
267 ofs << ";" << std::endl;
269 ofs << "}" << std::endl;
273 global_excompiler.compile_src_file(unique_filename, filename.empty());
274 // This is not standard compliant! ... no conversion between
275 // pointer-to-functions and pointer-to-objects ...
276 fp = (FUNCP_CUBA) global_excompiler.link_so_file(unique_filename+".so", filename.empty());
279 void link_ex(const std::string filename, FUNCP_1P& fp)
281 // This is not standard compliant! ... no conversion between
282 // pointer-to-functions and pointer-to-objects ...
283 fp = (FUNCP_1P) global_excompiler.link_so_file(filename, false);
286 void link_ex(const std::string filename, FUNCP_2P& fp)
288 // This is not standard compliant! ... no conversion between
289 // pointer-to-functions and pointer-to-objects ...
290 fp = (FUNCP_2P) global_excompiler.link_so_file(filename, false);
293 void link_ex(const std::string filename, FUNCP_CUBA& fp)
295 // This is not standard compliant! ... no conversion between
296 // pointer-to-functions and pointer-to-objects ...
297 fp = (FUNCP_CUBA) global_excompiler.link_so_file(filename, false);
300 void unlink_ex(const std::string filename)
302 global_excompiler.unlink(filename);
305 #elif // def HAVE_LIBDL
308 * In case no working libdl has been found by configure, the following function
309 * stubs preserve the interface. Every function just raises an exception.
312 void compile_ex(const ex& expr, const symbol& sym, FUNCP_1P& fp, const std::string filename)
314 throw std::runtime_error("compile_ex has been disabled because of missing libdl!");
317 void compile_ex(const ex& expr, const symbol& sym1, const symbol& sym2, FUNCP_2P& fp, const std::string filename)
319 throw std::runtime_error("compile_ex has been disabled because of missing libdl!");
322 void compile_ex(const lst& exprs, const lst& syms, FUNCP_CUBA& fp, const std::string filename)
324 throw std::runtime_error("compile_ex has been disabled because of missing libdl!");
327 void link_ex(const std::string filename, FUNCP_1P& fp)
329 throw std::runtime_error("link_ex has been disabled because of missing libdl!");
332 void link_ex(const std::string filename, FUNCP_2P& fp)
334 throw std::runtime_error("link_ex has been disabled because of missing libdl!");
337 void link_ex(const std::string filename, FUNCP_CUBA& fp)
339 throw std::runtime_error("link_ex has been disabled because of missing libdl!");
342 void unlink_ex(const std::string filename)
344 throw std::runtime_error("unlink_ex has been disabled because of missing libdl!");
347 #endif // def HAVE_LIBDL