1// Copyright 2009-2021 Intel Corporation
2// SPDX-License-Identifier: Apache-2.0
3
4#include "state.h"
5#include "../../common/lexers/streamfilters.h"
6
7namespace embree
8{
9 MutexSys g_printMutex;
10
11 State::ErrorHandler State::g_errorHandler;
12
13 State::ErrorHandler::ErrorHandler()
14 : thread_error(createTls()) {}
15
16 State::ErrorHandler::~ErrorHandler()
17 {
18 Lock<MutexSys> lock(errors_mutex);
19 for (size_t i=0; i<thread_errors.size(); i++)
20 delete thread_errors[i];
21 destroyTls(thread_error);
22 thread_errors.clear();
23 }
24
25 RTCError* State::ErrorHandler::error()
26 {
27 RTCError* stored_error = (RTCError*) getTls(thread_error);
28 if (stored_error) return stored_error;
29
30 Lock<MutexSys> lock(errors_mutex);
31 stored_error = new RTCError(RTC_ERROR_NONE);
32 thread_errors.push_back(stored_error);
33 setTls(thread_error,stored_error);
34 return stored_error;
35 }
36
37 State::State ()
38 : enabled_cpu_features(getCPUFeatures()),
39 enabled_builder_cpu_features(enabled_cpu_features),
40 frequency_level(FREQUENCY_SIMD256)
41 {
42 tri_accel = "default";
43 tri_builder = "default";
44 tri_traverser = "default";
45
46 tri_accel_mb = "default";
47 tri_builder_mb = "default";
48 tri_traverser_mb = "default";
49
50 quad_accel = "default";
51 quad_builder = "default";
52 quad_traverser = "default";
53
54 quad_accel_mb = "default";
55 quad_builder_mb = "default";
56 quad_traverser_mb = "default";
57
58 line_accel = "default";
59 line_builder = "default";
60 line_traverser = "default";
61
62 line_accel_mb = "default";
63 line_builder_mb = "default";
64 line_traverser_mb = "default";
65
66 hair_accel = "default";
67 hair_builder = "default";
68 hair_traverser = "default";
69
70 hair_accel_mb = "default";
71 hair_builder_mb = "default";
72 hair_traverser_mb = "default";
73
74 object_accel = "default";
75 object_builder = "default";
76 object_accel_min_leaf_size = 1;
77 object_accel_max_leaf_size = 1;
78
79 object_accel_mb = "default";
80 object_builder_mb = "default";
81 object_accel_mb_min_leaf_size = 1;
82 object_accel_mb_max_leaf_size = 1;
83
84 max_spatial_split_replications = 1.2f;
85 useSpatialPreSplits = false;
86
87 tessellation_cache_size = 128*1024*1024;
88
89 subdiv_accel = "default";
90 subdiv_accel_mb = "default";
91
92 grid_accel = "default";
93 grid_builder = "default";
94 grid_accel_mb = "default";
95 grid_builder_mb = "default";
96
97 instancing_open_min = 0;
98 instancing_block_size = 0;
99 instancing_open_factor = 8.0f;
100 instancing_open_max_depth = 32;
101 instancing_open_max = 50000000;
102
103 float_exceptions = false;
104 quality_flags = -1;
105 scene_flags = -1;
106 verbose = 0;
107 benchmark = 0;
108
109 numThreads = 0;
110 numUserThreads = 0;
111
112#if TASKING_INTERNAL
113 set_affinity = true;
114#else
115 set_affinity = false;
116#endif
117
118 start_threads = false;
119 enable_selockmemoryprivilege = false;
120#if defined(__LINUX__)
121 hugepages = true;
122#else
123 hugepages = false;
124#endif
125 hugepages_success = true;
126
127 alloc_main_block_size = 0;
128 alloc_num_main_slots = 0;
129 alloc_thread_block_size = 0;
130 alloc_single_thread_alloc = -1;
131
132 error_function = nullptr;
133 error_function_userptr = nullptr;
134
135 memory_monitor_function = nullptr;
136 memory_monitor_userptr = nullptr;
137 }
138
139 State::~State() {
140 }
141
142 bool State::hasISA(const int isa) {
143 return (enabled_cpu_features & isa) == isa;
144 }
145
146 bool State::checkISASupport() {
147#if defined(__ARM_NEON)
148 /*
149 * NEON CPU type is a mixture of NEON and SSE2
150 */
151
152 bool hasSSE2 = (getCPUFeatures() & enabled_cpu_features) & CPU_FEATURE_SSE2;
153
154 /* this will be true when explicitly initialize Device with `isa=neon` config */
155 bool hasNEON = (getCPUFeatures() & enabled_cpu_features) & CPU_FEATURE_NEON;
156
157 return hasSSE2 || hasNEON;
158#else
159 return (getCPUFeatures() & enabled_cpu_features) == enabled_cpu_features;
160#endif
161 }
162
163 void State::verify()
164 {
165 /* verify that calculations stay in range */
166 assert(rcp(min_rcp_input)*FLT_LARGE+FLT_LARGE < 0.01f*FLT_MAX);
167
168 /* here we verify that CPP files compiled for a specific ISA only
169 * call that same or lower ISA version of non-inlined class member
170 * functions */
171#if defined(DEBUG)
172#if defined(EMBREE_TARGET_SSE2)
173#if !defined(__ARM_NEON)
174 assert(sse2::getISA() <= SSE2);
175#endif
176#endif
177#if defined(EMBREE_TARGET_SSE42)
178 assert(sse42::getISA() <= SSE42);
179#endif
180#if defined(EMBREE_TARGET_AVX)
181 assert(avx::getISA() <= AVX);
182#endif
183#if defined(EMBREE_TARGET_AVX2)
184 assert(avx2::getISA() <= AVX2);
185#endif
186#if defined (EMBREE_TARGET_AVX512)
187 assert(avx512::getISA() <= AVX512);
188#endif
189#endif
190 }
191
192 const char* symbols[3] = { "=", ",", "|" };
193
194 bool State::parseFile(const FileName& fileName)
195 {
196 FILE* f = fopen(fileName.c_str(),"r");
197 if (!f) return false;
198 Ref<Stream<int> > file = new FileStream(f,fileName);
199
200 std::vector<std::string> syms;
201 for (size_t i=0; i<sizeof(symbols)/sizeof(void*); i++)
202 syms.push_back(symbols[i]);
203
204 Ref<TokenStream> cin = new TokenStream(new LineCommentFilter(file,"#"),
205 TokenStream::alpha+TokenStream::ALPHA+TokenStream::numbers+"_.",
206 TokenStream::separators,syms);
207 parse(cin);
208 return true;
209 }
210
211 void State::parseString(const char* cfg)
212 {
213 if (cfg == nullptr) return;
214
215 std::vector<std::string> syms;
216 for (size_t i=0; i<sizeof(symbols)/sizeof(void*); i++)
217 syms.push_back(symbols[i]);
218
219 Ref<TokenStream> cin = new TokenStream(new StrStream(cfg),
220 TokenStream::alpha+TokenStream::ALPHA+TokenStream::numbers+"_.",
221 TokenStream::separators,syms);
222 parse(cin);
223 }
224
225 int string_to_cpufeatures(const std::string& isa)
226 {
227 if (isa == "sse" ) return SSE;
228 else if (isa == "sse2") return SSE2;
229 else if (isa == "sse3") return SSE3;
230 else if (isa == "ssse3") return SSSE3;
231 else if (isa == "sse41") return SSE41;
232 else if (isa == "sse4.1") return SSE41;
233 else if (isa == "sse42") return SSE42;
234 else if (isa == "sse4.2") return SSE42;
235 else if (isa == "avx") return AVX;
236 else if (isa == "avxi") return AVXI;
237 else if (isa == "avx2") return AVX2;
238 else if (isa == "avx512") return AVX512;
239 else return SSE2;
240 }
241
242 void State::parse(Ref<TokenStream> cin)
243 {
244 /* parse until end of stream */
245 while (cin->peek() != Token::Eof())
246 {
247 const Token tok = cin->get();
248
249 if (tok == Token::Id("threads") && cin->trySymbol("="))
250 numThreads = cin->get().Int();
251
252 else if (tok == Token::Id("user_threads")&& cin->trySymbol("="))
253 numUserThreads = cin->get().Int();
254
255 else if (tok == Token::Id("set_affinity")&& cin->trySymbol("="))
256 set_affinity = cin->get().Int();
257
258 else if (tok == Token::Id("affinity")&& cin->trySymbol("="))
259 set_affinity = cin->get().Int();
260
261 else if (tok == Token::Id("start_threads")&& cin->trySymbol("="))
262 start_threads = cin->get().Int();
263
264 else if (tok == Token::Id("isa") && cin->trySymbol("=")) {
265 std::string isa_str = toLowerCase(cin->get().Identifier());
266 enabled_cpu_features = string_to_cpufeatures(isa_str);
267 enabled_builder_cpu_features = enabled_cpu_features;
268 }
269
270 else if (tok == Token::Id("max_isa") && cin->trySymbol("=")) {
271 std::string isa_str = toLowerCase(cin->get().Identifier());
272 enabled_cpu_features &= string_to_cpufeatures(isa_str);
273 enabled_builder_cpu_features &= enabled_cpu_features;
274 }
275
276 else if (tok == Token::Id("max_builder_isa") && cin->trySymbol("=")) {
277 std::string isa_str = toLowerCase(cin->get().Identifier());
278 enabled_builder_cpu_features &= string_to_cpufeatures(isa_str);
279 }
280
281 else if (tok == Token::Id("frequency_level") && cin->trySymbol("=")) {
282 std::string freq = cin->get().Identifier();
283 if (freq == "simd128") frequency_level = FREQUENCY_SIMD128;
284 else if (freq == "simd256") frequency_level = FREQUENCY_SIMD256;
285 else if (freq == "simd512") frequency_level = FREQUENCY_SIMD512;
286 }
287
288 else if (tok == Token::Id("enable_selockmemoryprivilege") && cin->trySymbol("=")) {
289 enable_selockmemoryprivilege = cin->get().Int();
290 }
291 else if (tok == Token::Id("hugepages") && cin->trySymbol("=")) {
292 hugepages = cin->get().Int();
293 }
294
295 else if (tok == Token::Id("float_exceptions") && cin->trySymbol("="))
296 float_exceptions = cin->get().Int();
297
298 else if ((tok == Token::Id("tri_accel") || tok == Token::Id("accel")) && cin->trySymbol("="))
299 tri_accel = cin->get().Identifier();
300 else if ((tok == Token::Id("tri_builder") || tok == Token::Id("builder")) && cin->trySymbol("="))
301 tri_builder = cin->get().Identifier();
302 else if ((tok == Token::Id("tri_traverser") || tok == Token::Id("traverser")) && cin->trySymbol("="))
303 tri_traverser = cin->get().Identifier();
304
305 else if ((tok == Token::Id("tri_accel_mb") || tok == Token::Id("accel_mb")) && cin->trySymbol("="))
306 tri_accel_mb = cin->get().Identifier();
307 else if ((tok == Token::Id("tri_builder_mb") || tok == Token::Id("builder_mb")) && cin->trySymbol("="))
308 tri_builder_mb = cin->get().Identifier();
309 else if ((tok == Token::Id("tri_traverser_mb") || tok == Token::Id("traverser_mb")) && cin->trySymbol("="))
310 tri_traverser_mb = cin->get().Identifier();
311
312 else if ((tok == Token::Id("quad_accel")) && cin->trySymbol("="))
313 quad_accel = cin->get().Identifier();
314 else if ((tok == Token::Id("quad_builder")) && cin->trySymbol("="))
315 quad_builder = cin->get().Identifier();
316 else if ((tok == Token::Id("quad_traverser")) && cin->trySymbol("="))
317 quad_traverser = cin->get().Identifier();
318
319 else if ((tok == Token::Id("quad_accel_mb")) && cin->trySymbol("="))
320 quad_accel_mb = cin->get().Identifier();
321 else if ((tok == Token::Id("quad_builder_mb")) && cin->trySymbol("="))
322 quad_builder_mb = cin->get().Identifier();
323 else if ((tok == Token::Id("quad_traverser_mb")) && cin->trySymbol("="))
324 quad_traverser_mb = cin->get().Identifier();
325
326 else if ((tok == Token::Id("line_accel")) && cin->trySymbol("="))
327 line_accel = cin->get().Identifier();
328 else if ((tok == Token::Id("line_builder")) && cin->trySymbol("="))
329 line_builder = cin->get().Identifier();
330 else if ((tok == Token::Id("line_traverser")) && cin->trySymbol("="))
331 line_traverser = cin->get().Identifier();
332
333 else if ((tok == Token::Id("line_accel_mb")) && cin->trySymbol("="))
334 line_accel_mb = cin->get().Identifier();
335 else if ((tok == Token::Id("line_builder_mb")) && cin->trySymbol("="))
336 line_builder_mb = cin->get().Identifier();
337 else if ((tok == Token::Id("line_traverser_mb")) && cin->trySymbol("="))
338 line_traverser_mb = cin->get().Identifier();
339
340 else if (tok == Token::Id("hair_accel") && cin->trySymbol("="))
341 hair_accel = cin->get().Identifier();
342 else if (tok == Token::Id("hair_builder") && cin->trySymbol("="))
343 hair_builder = cin->get().Identifier();
344 else if (tok == Token::Id("hair_traverser") && cin->trySymbol("="))
345 hair_traverser = cin->get().Identifier();
346
347 else if (tok == Token::Id("hair_accel_mb") && cin->trySymbol("="))
348 hair_accel_mb = cin->get().Identifier();
349 else if (tok == Token::Id("hair_builder_mb") && cin->trySymbol("="))
350 hair_builder_mb = cin->get().Identifier();
351 else if (tok == Token::Id("hair_traverser_mb") && cin->trySymbol("="))
352 hair_traverser_mb = cin->get().Identifier();
353
354 else if (tok == Token::Id("object_accel") && cin->trySymbol("="))
355 object_accel = cin->get().Identifier();
356 else if (tok == Token::Id("object_builder") && cin->trySymbol("="))
357 object_builder = cin->get().Identifier();
358 else if (tok == Token::Id("object_accel_min_leaf_size") && cin->trySymbol("="))
359 object_accel_min_leaf_size = cin->get().Int();
360 else if (tok == Token::Id("object_accel_max_leaf_size") && cin->trySymbol("="))
361 object_accel_max_leaf_size = cin->get().Int();
362
363 else if (tok == Token::Id("object_accel_mb") && cin->trySymbol("="))
364 object_accel_mb = cin->get().Identifier();
365 else if (tok == Token::Id("object_builder_mb") && cin->trySymbol("="))
366 object_builder_mb = cin->get().Identifier();
367 else if (tok == Token::Id("object_accel_mb_min_leaf_size") && cin->trySymbol("="))
368 object_accel_mb_min_leaf_size = cin->get().Int();
369 else if (tok == Token::Id("object_accel_mb_max_leaf_size") && cin->trySymbol("="))
370 object_accel_mb_max_leaf_size = cin->get().Int();
371
372 else if (tok == Token::Id("instancing_open_min") && cin->trySymbol("="))
373 instancing_open_min = cin->get().Int();
374 else if (tok == Token::Id("instancing_block_size") && cin->trySymbol("=")) {
375 instancing_block_size = cin->get().Int();
376 instancing_open_factor = 0.0f;
377 }
378 else if (tok == Token::Id("instancing_open_max_depth") && cin->trySymbol("="))
379 instancing_open_max_depth = cin->get().Int();
380 else if (tok == Token::Id("instancing_open_factor") && cin->trySymbol("=")) {
381 instancing_block_size = 0;
382 instancing_open_factor = cin->get().Float();
383 }
384 else if (tok == Token::Id("instancing_open_max") && cin->trySymbol("="))
385 instancing_open_max = cin->get().Int();
386
387 else if (tok == Token::Id("subdiv_accel") && cin->trySymbol("="))
388 subdiv_accel = cin->get().Identifier();
389 else if (tok == Token::Id("subdiv_accel_mb") && cin->trySymbol("="))
390 subdiv_accel_mb = cin->get().Identifier();
391
392 else if (tok == Token::Id("grid_accel") && cin->trySymbol("="))
393 grid_accel = cin->get().Identifier();
394 else if (tok == Token::Id("grid_accel_mb") && cin->trySymbol("="))
395 grid_accel_mb = cin->get().Identifier();
396
397 else if (tok == Token::Id("verbose") && cin->trySymbol("="))
398 verbose = cin->get().Int();
399 else if (tok == Token::Id("benchmark") && cin->trySymbol("="))
400 benchmark = cin->get().Int();
401
402 else if (tok == Token::Id("quality")) {
403 if (cin->trySymbol("=")) {
404 Token flag = cin->get();
405 if (flag == Token::Id("low")) quality_flags = RTC_BUILD_QUALITY_LOW;
406 else if (flag == Token::Id("medium")) quality_flags = RTC_BUILD_QUALITY_MEDIUM;
407 else if (flag == Token::Id("high")) quality_flags = RTC_BUILD_QUALITY_HIGH;
408 }
409 }
410
411 else if (tok == Token::Id("scene_flags")) {
412 scene_flags = 0;
413 if (cin->trySymbol("=")) {
414 do {
415 Token flag = cin->get();
416 if (flag == Token::Id("dynamic") ) scene_flags |= RTC_SCENE_FLAG_DYNAMIC;
417 else if (flag == Token::Id("compact")) scene_flags |= RTC_SCENE_FLAG_COMPACT;
418 else if (flag == Token::Id("robust")) scene_flags |= RTC_SCENE_FLAG_ROBUST;
419 } while (cin->trySymbol("|"));
420 }
421 }
422
423 else if (tok == Token::Id("max_spatial_split_replications") && cin->trySymbol("="))
424 max_spatial_split_replications = cin->get().Float();
425
426 else if (tok == Token::Id("presplits") && cin->trySymbol("="))
427 useSpatialPreSplits = cin->get().Int() != 0 ? true : false;
428
429 else if (tok == Token::Id("tessellation_cache_size") && cin->trySymbol("="))
430 tessellation_cache_size = size_t(cin->get().Float()*1024.0f*1024.0f);
431 else if (tok == Token::Id("cache_size") && cin->trySymbol("="))
432 tessellation_cache_size = size_t(cin->get().Float()*1024.0f*1024.0f);
433
434 else if (tok == Token::Id("alloc_main_block_size") && cin->trySymbol("="))
435 alloc_main_block_size = cin->get().Int();
436 else if (tok == Token::Id("alloc_num_main_slots") && cin->trySymbol("="))
437 alloc_num_main_slots = cin->get().Int();
438 else if (tok == Token::Id("alloc_thread_block_size") && cin->trySymbol("="))
439 alloc_thread_block_size = cin->get().Int();
440 else if (tok == Token::Id("alloc_single_thread_alloc") && cin->trySymbol("="))
441 alloc_single_thread_alloc = cin->get().Int();
442
443 cin->trySymbol(","); // optional , separator
444 }
445 }
446
447 bool State::verbosity(size_t N) {
448 return N <= verbose;
449 }
450
451 void State::print()
452 {
453 std::cout << "general:" << std::endl;
454 std::cout << " build threads = " << numThreads << std::endl;
455 std::cout << " build user threads = " << numUserThreads << std::endl;
456 std::cout << " start_threads = " << start_threads << std::endl;
457 std::cout << " affinity = " << set_affinity << std::endl;
458 std::cout << " frequency_level = ";
459 switch (frequency_level) {
460 case FREQUENCY_SIMD128: std::cout << "simd128" << std::endl; break;
461 case FREQUENCY_SIMD256: std::cout << "simd256" << std::endl; break;
462 case FREQUENCY_SIMD512: std::cout << "simd512" << std::endl; break;
463 default: std::cout << "error" << std::endl; break;
464 }
465
466 std::cout << " hugepages = ";
467 if (!hugepages) std::cout << "disabled" << std::endl;
468 else if (hugepages_success) std::cout << "enabled" << std::endl;
469 else std::cout << "failed" << std::endl;
470
471 std::cout << " verbosity = " << verbose << std::endl;
472 std::cout << " cache_size = " << float(tessellation_cache_size)*1E-6 << " MB" << std::endl;
473 std::cout << " max_spatial_split_replications = " << max_spatial_split_replications << std::endl;
474
475 std::cout << "triangles:" << std::endl;
476 std::cout << " accel = " << tri_accel << std::endl;
477 std::cout << " builder = " << tri_builder << std::endl;
478 std::cout << " traverser = " << tri_traverser << std::endl;
479
480 std::cout << "motion blur triangles:" << std::endl;
481 std::cout << " accel = " << tri_accel_mb << std::endl;
482 std::cout << " builder = " << tri_builder_mb << std::endl;
483 std::cout << " traverser = " << tri_traverser_mb << std::endl;
484
485 std::cout << "quads:" << std::endl;
486 std::cout << " accel = " << quad_accel << std::endl;
487 std::cout << " builder = " << quad_builder << std::endl;
488 std::cout << " traverser = " << quad_traverser << std::endl;
489
490 std::cout << "motion blur quads:" << std::endl;
491 std::cout << " accel = " << quad_accel_mb << std::endl;
492 std::cout << " builder = " << quad_builder_mb << std::endl;
493 std::cout << " traverser = " << quad_traverser_mb << std::endl;
494
495 std::cout << "line segments:" << std::endl;
496 std::cout << " accel = " << line_accel << std::endl;
497 std::cout << " builder = " << line_builder << std::endl;
498 std::cout << " traverser = " << line_traverser << std::endl;
499
500 std::cout << "motion blur line segments:" << std::endl;
501 std::cout << " accel = " << line_accel_mb << std::endl;
502 std::cout << " builder = " << line_builder_mb << std::endl;
503 std::cout << " traverser = " << line_traverser_mb << std::endl;
504
505 std::cout << "hair:" << std::endl;
506 std::cout << " accel = " << hair_accel << std::endl;
507 std::cout << " builder = " << hair_builder << std::endl;
508 std::cout << " traverser = " << hair_traverser << std::endl;
509
510 std::cout << "motion blur hair:" << std::endl;
511 std::cout << " accel = " << hair_accel_mb << std::endl;
512 std::cout << " builder = " << hair_builder_mb << std::endl;
513 std::cout << " traverser = " << hair_traverser_mb << std::endl;
514
515 std::cout << "subdivision surfaces:" << std::endl;
516 std::cout << " accel = " << subdiv_accel << std::endl;
517
518 std::cout << "grids:" << std::endl;
519 std::cout << " accel = " << grid_accel << std::endl;
520 std::cout << " builder = " << grid_builder << std::endl;
521
522 std::cout << "motion blur grids:" << std::endl;
523 std::cout << " accel = " << grid_accel_mb << std::endl;
524 std::cout << " builder = " << grid_builder_mb << std::endl;
525
526 std::cout << "object_accel:" << std::endl;
527 std::cout << " min_leaf_size = " << object_accel_min_leaf_size << std::endl;
528 std::cout << " max_leaf_size = " << object_accel_max_leaf_size << std::endl;
529
530 std::cout << "object_accel_mb:" << std::endl;
531 std::cout << " min_leaf_size = " << object_accel_mb_min_leaf_size << std::endl;
532 std::cout << " max_leaf_size = " << object_accel_mb_max_leaf_size << std::endl;
533 }
534}
535