-
Notifications
You must be signed in to change notification settings - Fork 196
Expand file tree
/
Copy pathmodule.cpp
More file actions
258 lines (219 loc) · 6.96 KB
/
module.cpp
File metadata and controls
258 lines (219 loc) · 6.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#include "taco/codegen/module.h"
#include <iostream>
#include <fstream>
#include <dlfcn.h>
#include <unistd.h>
#if USE_OPENMP
#include <omp.h>
#endif
#include "taco/tensor.h"
#include "taco/error.h"
#include "taco/util/strings.h"
#include "taco/util/env.h"
#include "codegen/codegen_c.h"
#include "codegen/codegen_cuda.h"
#include "taco/cuda.h"
using namespace std;
namespace taco {
namespace ir {
std::string Module::chars = "abcdefghijkmnpqrstuvwxyz0123456789";
std::default_random_engine Module::gen = std::default_random_engine();
std::uniform_int_distribution<int> Module::randint =
std::uniform_int_distribution<int>(0, chars.length() - 1);
void Module::setJITTmpdir() {
tmpdir = util::getTmpdir();
}
void Module::setJITLibname() {
libname.resize(12);
for (int i=0; i<12; i++)
libname[i] = chars[randint(gen)];
}
void Module::addFunction(Stmt func) {
funcs.push_back(func);
}
void Module::compileToSource(string path, string prefix) {
if (!moduleFromUserSource) {
// create a codegen instance and add all the funcs
bool didGenRuntime = false;
header.str("");
header.clear();
source.str("");
source.clear();
taco_tassert(target.arch == Target::C99) <<
"Only C99 codegen supported currently";
std::shared_ptr<CodeGen> sourcegen =
CodeGen::init_default(source, CodeGen::ImplementationGen);
std::shared_ptr<CodeGen> headergen =
CodeGen::init_default(header, CodeGen::HeaderGen);
for (auto func: funcs) {
sourcegen->compile(func, !didGenRuntime);
headergen->compile(func, !didGenRuntime);
didGenRuntime = true;
}
}
ofstream source_file;
string file_ending = should_use_CUDA_codegen() ? ".cu" : ".c";
source_file.open(path+prefix+file_ending);
taco_uassert((bool)source_file) << "Could not open file '"
<< path+prefix+file_ending << "' for writing";
source_file << source.str();
source_file.close();
ofstream header_file;
header_file.open(path+prefix+".h");
taco_uassert((bool)header_file) << "Could not open file '"
<< path+prefix+".h" << "' for writing";
header_file << header.str();
header_file.close();
}
void Module::compileToStaticLibrary(string path, string prefix) {
taco_tassert(false) << "Compiling to a static library is not supported";
}
namespace {
void writeShims(vector<Stmt> funcs, string path, string prefix) {
stringstream shims;
for (auto func: funcs) {
if (should_use_CUDA_codegen()) {
CodeGen_CUDA::generateShim(func, shims);
}
else {
CodeGen_C::generateShim(func, shims);
}
}
ofstream shims_file;
if (should_use_CUDA_codegen()) {
shims_file.open(path+prefix+"_shims.cpp");
taco_uassert((bool)shims_file) << "Could not open file '"
<< path+prefix+"_shims.cpp" << "' for writing";
}
else {
shims_file.open(path+prefix+".c", ios::app);
taco_uassert((bool)shims_file) << "Could not open file '"
<< path+prefix+".c" << "' for writing";
}
shims_file << "#include \"" << path << prefix << ".h\"\n";
shims_file << shims.str();
shims_file.close();
}
} // anonymous namespace
string Module::compile() {
string cache_dir = util::getFromEnv("TACO_CACHE_DIR", "");
bool use_cache = cache_dir != "" && cacheStr != "";
if (use_cache && cache_dir.back() != '/') {
cache_dir += '/';
}
string dir = use_cache ? cache_dir : tmpdir;
string name = use_cache ? cacheStrHashed : libname;
string prefix = dir+name;
string fullpath = prefix + ".so";
string cc;
string cflags;
string file_ending;
string shims_file;
if (should_use_CUDA_codegen()) {
cc = util::getFromEnv("TACO_NVCC", "nvcc");
cflags = util::getFromEnv("TACO_NVCCFLAGS",
get_default_CUDA_compiler_flags());
file_ending = ".cu";
shims_file = prefix + "_shims.cpp";
}
else {
cc = util::getFromEnv(target.compiler_env, target.compiler);
#ifdef TACO_DEBUG
// In debug mode, compile the generated code with debug symbols and a
// low optimization level.
string defaultFlags = "-g -O0 -std=c99";
#else
// Otherwise, use the standard set of optimizing flags.
string defaultFlags = "-O3 -ffast-math -std=c99";
#endif
cflags = util::getFromEnv("TACO_CFLAGS", defaultFlags) + " -shared -fPIC";
#if USE_OPENMP
cflags += " -fopenmp";
#endif
file_ending = ".c";
shims_file = "";
}
string source_file;
string object_file;
if (use_cache) {
source_file = cache_dir + name + file_ending;
object_file = cache_dir + name + ".so";
} else {
source_file = prefix + file_ending;
object_file = fullpath;
}
string cmd = cc + " " + cflags + " " +
source_file + " " + shims_file + " " +
"-o " + object_file + " -lm";
bool cached = false;
if (use_cache) {
// first check if this file already exists in cache
ifstream cached_source_file(source_file);
ifstream cached_header_file(cache_dir + name + ".h");
ifstream cached_object_file(object_file);
// only run codegen if the files don't already exist
cached = cached_source_file.good() && cached_header_file.good() && cached_object_file.good();
}
if (!cached) {
// open the output file & write out the source
compileToSource(dir, name);
// write out the shims
writeShims(funcs, dir, name);
// now compile it
int err = system(cmd.data());
taco_uassert(err == 0) << "Compilation command failed:\n" << cmd
<< "\nreturned " << err;
}
// use dlsym() to open the compiled library
if (lib_handle) {
dlclose(lib_handle);
}
lib_handle = dlopen(fullpath.data(), RTLD_NOW | RTLD_LOCAL);
taco_uassert(lib_handle) << "Failed to load generated code, error is: " << dlerror();
return fullpath;
}
void Module::setSource(string source) {
this->source << source;
moduleFromUserSource = true;
}
string Module::getSource() {
return source.str();
}
void* Module::getFuncPtr(std::string name) {
return dlsym(lib_handle, name.data());
}
int Module::callFuncPackedRaw(std::string name, void** args) {
typedef int (*fnptr_t)(void**);
static_assert(sizeof(void*) == sizeof(fnptr_t),
"Unable to cast dlsym() returned void pointer to function pointer");
void* v_func_ptr = getFuncPtr(name);
fnptr_t func_ptr;
*reinterpret_cast<void**>(&func_ptr) = v_func_ptr;
#if USE_OPENMP
omp_sched_t existingSched;
ParallelSchedule tacoSched;
int existingChunkSize, tacoChunkSize;
int existingNumThreads = omp_get_max_threads();
omp_get_schedule(&existingSched, &existingChunkSize);
taco_get_parallel_schedule(&tacoSched, &tacoChunkSize);
switch (tacoSched) {
case ParallelSchedule::Static:
omp_set_schedule(omp_sched_static, tacoChunkSize);
break;
case ParallelSchedule::Dynamic:
omp_set_schedule(omp_sched_dynamic, tacoChunkSize);
break;
default:
break;
}
omp_set_num_threads(taco_get_num_threads());
#endif
int ret = func_ptr(args);
#if USE_OPENMP
omp_set_schedule(existingSched, existingChunkSize);
omp_set_num_threads(existingNumThreads);
#endif
return ret;
}
} // namespace ir
} // namespace taco