1 /** @file excompiler.cpp
3 * Class to facilitate the conversion of a ex to a function pointer suited for
4 * fast numerical integration. */
7 * GiNaC Copyright (C) 1999-2005 Johannes Gutenberg University Mainz, Germany
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "excompiler.h"
35 #include "operators.h"
36 #include "relational.h"
54 for (std::vector<filedesc>::const_iterator it = filelist.begin(); it != filelist.end(); ++it) {
56 std::string strremove = "rm " + it->name;
57 system(strremove.c_str());
60 void add(void* module, std::string name)
65 filelist.push_back(fd);
67 std::vector<filedesc> filelist;
72 FP_dim1 compile(const ex& expr, const symbol& sym)
75 ex expr_with_x = expr.subs(lst(sym==argx));
77 char filename[] = "/tmp/GiNaCXXXXXX";
79 int fno = mkstemp(filename);
81 std::ofstream ofs(filename);
83 ofs << "#include <stddef.h> " << std::endl;
84 ofs << "#include <stdlib.h> " << std::endl;
85 ofs << "#include <math.h> " << std::endl;
87 ofs << "double compiled_ex(double argx)" << std::endl;
88 ofs << "{" << std::endl;
89 ofs << "double res = ";
90 expr_with_x.print(GiNaC::print_csrc_double(ofs));
91 ofs << ";" << std::endl;
92 ofs << "return(res); " << std::endl;
93 ofs << "}" << std::endl;
97 std::string strcompile = "ginac-excompiler " + std::string(filename);
98 system(strcompile.c_str());
100 std::string strremove = "rm " + std::string(filename) + " " + std::string(filename) + ".o";
101 system(strremove.c_str());
103 std::string strsofile = std::string(filename) + ".so";
105 module = dlopen(strsofile.c_str(), RTLD_NOW);
106 if (module == NULL) {
107 throw std::runtime_error("excompiler: could not open compiled module!");
110 _exc.add(module, strsofile);
112 return (FP_dim1) dlsym(module, "compiled_ex");
115 FP_cuba compile(const lst& exprs, const lst& syms)
118 for (int count=0; count<syms.nops(); ++count) {
119 std::ostringstream s;
120 s << "a[" << count << "]";
121 replacements.append(syms.op(count) == symbol(s.str()));
124 std::vector<ex> expr_with_cname;
125 for (int count=0; count<exprs.nops(); ++count) {
126 expr_with_cname.push_back(exprs.op(count).subs(replacements));
129 char filename[] = "/tmp/GiNaCXXXXXX";
131 int fno = mkstemp(filename);
133 std::ofstream ofs(filename);
135 ofs << "#include <stddef.h> " << std::endl;
136 ofs << "#include <stdlib.h> " << std::endl;
137 ofs << "#include <math.h> " << std::endl;
139 ofs << "void compiled_ex(const int* an, const double a[], const int* fn, double f[])" << std::endl;
140 ofs << "{" << std::endl;
141 for (int count=0; count<exprs.nops(); ++count) {
142 ofs << "f[" << count << "] = ";
143 expr_with_cname[count].print(GiNaC::print_csrc_double(ofs));
144 ofs << ";" << std::endl;
146 ofs << "}" << std::endl;
150 std::string strcompile = "ginac-excompiler " + std::string(filename);
151 system(strcompile.c_str());
153 std::string strremove = "rm " + std::string(filename) + " " + std::string(filename) + ".o";
154 system(strremove.c_str());
156 std::string strsofile = std::string(filename) + ".so";
158 module = dlopen(strsofile.c_str(), RTLD_NOW);
159 if (module == NULL) {
160 throw std::runtime_error("excompiler: could not open compiled module!");
163 _exc.add(module, strsofile);
165 return (FP_cuba) dlsym(module, "compiled_ex");