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"
41 #endif // def HAVE_LIBDL
45 #include "operators.h"
46 #include "relational.h"
54 * Small class that manages modules opened by libdl. It is used by compile_ex
55 * and link_ex in order to have a clean-up of opened modules and their
56 * associated source and so-files at the time of program termination. It is
57 * supposed to be statically instantiated once (see definition of
58 * global_excompiler below). The dtor of that object then performs the
59 * clean-up. On top of that it provides some functionality shared between
60 * different compile_ex and link_ex specializations.
65 * Holds all necessary information about opened modules.
70 std::string name; /**< filename with .so suffix */
71 bool clean_up; /**< if true, source and so-file will be deleted */
73 std::vector<filedesc> filelist; /**< List of all opened modules */
76 * Complete clean-up of opend modules is done on destruction.
80 for (std::vector<filedesc>::const_iterator it = filelist.begin(); it != filelist.end(); ++it) {
85 * Adds a new module to the list.
87 void add_opened_module(void* module, const std::string& name, bool clean_up)
92 fd.clean_up = clean_up;
93 filelist.push_back(fd);
96 * Closes a module and deletes the so-file if the associated clean-up flag is true.
98 void clean_up(const std::vector<filedesc>::const_iterator it)
102 remove(it->name.c_str());
106 * Creates a new C source file and adds a standard header. If filename is
107 * empty, a unique random name is produced and used.
109 void create_src_file(std::string& filename, std::ofstream& ofs)
111 if (filename.empty()) {
112 // fill filename with unique random word
113 const char* filename_pattern = "GiNaCXXXXXX";
114 char* new_filename = new char[strlen(filename_pattern)+1];
115 strcpy(new_filename, filename_pattern);
116 if (!mktemp(new_filename)) {
118 throw std::runtime_error("mktemp failed");
120 filename = std::string(new_filename);
121 ofs.open(new_filename, std::ios::out);
124 // use parameter as filename
125 ofs.open(filename.c_str(), std::ios::out);
129 throw std::runtime_error("could not create source code file for compilation");
132 ofs << "#include <stddef.h> " << std::endl;
133 ofs << "#include <stdlib.h> " << std::endl;
134 ofs << "#include <math.h> " << std::endl;
138 * Calls the shell script 'ginac-excompiler' to compile the produced C
139 * source file into an linkable so-file. On demand the C source file is
142 void compile_src_file(const std::string filename, bool clean_up)
144 std::string strcompile = "ginac-excompiler " + filename;
145 if (system(strcompile.c_str())) {
146 throw std::runtime_error("excompiler::compile_src_file: error compiling source file!");
149 remove(filename.c_str());
153 * Links a so-file whose filename is given.
155 void* link_so_file(const std::string filename, bool clean_up)
158 module = dlopen(filename.c_str(), RTLD_NOW);
159 if (module == NULL) {
160 throw std::runtime_error("excompiler::link_so_file: could not open compiled module!");
163 add_opened_module(module, filename, clean_up);
165 return dlsym(module, "compiled_ex");
168 * Removes a modules from the module list. Performs a clean-up before that.
169 * Every module with the given name will be affected.
171 void unlink(const std::string filename)
173 for (std::vector<filedesc>::iterator it = filelist.begin(); it != filelist.end();) {
174 if (it->name == filename) {
185 * This static object manages the modules opened by the complile_ex and link_ex
186 * functions. On program termination its dtor is called and all open modules
187 * are closed. The associated source and so-files are eventually deleted then
189 * In principle this could lead to a static deconstruction order fiasco, if
190 * other code from this library uses the compile_ex and link_ex functions
191 * (which it doesn't at the moment and won't in the likely future, so therefore
192 * we ignore this issue).
194 static excompiler global_excompiler;
196 void compile_ex(const ex& expr, const symbol& sym, FUNCP_1P& fp, const std::string filename)
199 ex expr_with_x = expr.subs(lst(sym==x));
202 std::string unique_filename = filename;
203 global_excompiler.create_src_file(unique_filename, ofs);
205 ofs << "double compiled_ex(double x)" << std::endl;
206 ofs << "{" << std::endl;
207 ofs << "double res = ";
208 expr_with_x.print(GiNaC::print_csrc_double(ofs));
209 ofs << ";" << std::endl;
210 ofs << "return(res); " << std::endl;
211 ofs << "}" << std::endl;
215 global_excompiler.compile_src_file(unique_filename, filename.empty());
216 // This is not standard compliant! ... no conversion between
217 // pointer-to-functions and pointer-to-objects ...
218 fp = (FUNCP_1P) global_excompiler.link_so_file(unique_filename+".so", filename.empty());
221 void compile_ex(const ex& expr, const symbol& sym1, const symbol& sym2, FUNCP_2P& fp, const std::string filename)
223 symbol x("x"), y("y");
224 ex expr_with_xy = expr.subs(lst(sym1==x, sym2==y));
227 std::string unique_filename = filename;
228 global_excompiler.create_src_file(unique_filename, ofs);
230 ofs << "double compiled_ex(double x, double y)" << std::endl;
231 ofs << "{" << std::endl;
232 ofs << "double res = ";
233 expr_with_xy.print(GiNaC::print_csrc_double(ofs));
234 ofs << ";" << std::endl;
235 ofs << "return(res); " << std::endl;
236 ofs << "}" << std::endl;
240 global_excompiler.compile_src_file(unique_filename, filename.empty());
241 // This is not standard compliant! ... no conversion between
242 // pointer-to-functions and pointer-to-objects ...
243 fp = (FUNCP_2P) global_excompiler.link_so_file(unique_filename+".so", filename.empty());
246 void compile_ex(const lst& exprs, const lst& syms, FUNCP_CUBA& fp, const std::string filename)
249 for (int count=0; count<syms.nops(); ++count) {
250 std::ostringstream s;
251 s << "a[" << count << "]";
252 replacements.append(syms.op(count) == symbol(s.str()));
255 std::vector<ex> expr_with_cname;
256 for (int count=0; count<exprs.nops(); ++count) {
257 expr_with_cname.push_back(exprs.op(count).subs(replacements));
261 std::string unique_filename = filename;
262 global_excompiler.create_src_file(unique_filename, ofs);
264 ofs << "void compiled_ex(const int* an, const double a[], const int* fn, double f[])" << std::endl;
265 ofs << "{" << std::endl;
266 for (int count=0; count<exprs.nops(); ++count) {
267 ofs << "f[" << count << "] = ";
268 expr_with_cname[count].print(GiNaC::print_csrc_double(ofs));
269 ofs << ";" << std::endl;
271 ofs << "}" << std::endl;
275 global_excompiler.compile_src_file(unique_filename, filename.empty());
276 // This is not standard compliant! ... no conversion between
277 // pointer-to-functions and pointer-to-objects ...
278 fp = (FUNCP_CUBA) global_excompiler.link_so_file(unique_filename+".so", filename.empty());
281 void link_ex(const std::string filename, FUNCP_1P& fp)
283 // This is not standard compliant! ... no conversion between
284 // pointer-to-functions and pointer-to-objects ...
285 fp = (FUNCP_1P) global_excompiler.link_so_file(filename, false);
288 void link_ex(const std::string filename, FUNCP_2P& fp)
290 // This is not standard compliant! ... no conversion between
291 // pointer-to-functions and pointer-to-objects ...
292 fp = (FUNCP_2P) global_excompiler.link_so_file(filename, false);
295 void link_ex(const std::string filename, FUNCP_CUBA& fp)
297 // This is not standard compliant! ... no conversion between
298 // pointer-to-functions and pointer-to-objects ...
299 fp = (FUNCP_CUBA) global_excompiler.link_so_file(filename, false);
302 void unlink_ex(const std::string filename)
304 global_excompiler.unlink(filename);
307 #else // def HAVE_LIBDL
310 * In case no working libdl has been found by configure, the following function
311 * stubs preserve the interface. Every function just raises an exception.
314 void compile_ex(const ex& expr, const symbol& sym, FUNCP_1P& fp, const std::string filename)
316 throw std::runtime_error("compile_ex has been disabled because of missing libdl!");
319 void compile_ex(const ex& expr, const symbol& sym1, const symbol& sym2, FUNCP_2P& fp, const std::string filename)
321 throw std::runtime_error("compile_ex has been disabled because of missing libdl!");
324 void compile_ex(const lst& exprs, const lst& syms, FUNCP_CUBA& fp, const std::string filename)
326 throw std::runtime_error("compile_ex has been disabled because of missing libdl!");
329 void link_ex(const std::string filename, FUNCP_1P& fp)
331 throw std::runtime_error("link_ex has been disabled because of missing libdl!");
334 void link_ex(const std::string filename, FUNCP_2P& fp)
336 throw std::runtime_error("link_ex has been disabled because of missing libdl!");
339 void link_ex(const std::string filename, FUNCP_CUBA& fp)
341 throw std::runtime_error("link_ex has been disabled because of missing libdl!");
344 void unlink_ex(const std::string filename)
346 throw std::runtime_error("unlink_ex has been disabled because of missing libdl!");
349 #endif // def HAVE_LIBDL