Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Single threaded option #146

Open
mikeheddes opened this issue Mar 22, 2019 · 5 comments
Open

Single threaded option #146

mikeheddes opened this issue Mar 22, 2019 · 5 comments

Comments

@mikeheddes
Copy link

I'm working on a project within the ACT at ESA for which I try to compile dcgp to WebAssembly with Emscripten. I managed to compile all its dependencies and create a .wasm file which I can use in the browser (see main issue #10). But when running code that uses piranha's polynomial computation it throws an error because it tries to run on multiple threads and WebAssembly by default currently only supports single threaded code.

# the error
at std::__2::__throw_system_error(int, char const*) [__ZNSt3__220__throw_system_errorEiPKc] (wasm-function[2090]:1)
at std::__2::thread::thread<piranha::impl::task_queue::runner, void>(piranha::impl::task_queue::runner&&) [__ZNSt3__26threadC2IN7piranha4impl10task_queue6runnerEJEvEEOT_DpOT0_] (wasm-function[1018]:1826)
at piranha::impl::task_queue::task_queue(unsigned int, bool) [__ZN7piranha4impl10task_queueC2Ejb] (wasm-function[1005]:864)
at piranha::impl::get_initial_thread_queues() [__ZN7piranha4impl25get_initial_thread_queuesEv] (wasm-function[95]:3385)
at ___cxx_global_var_init_24 (wasm-function[94]:12)
at globalCtors (wasm-function[62]:12)

This error can be resolved by enabling the experimental #enable-webassembly-threads flag in chrome://flags (see this comment) but that is not a reasonable solution to ask of the users. A better solution until multi-threading is supported by most browsers by default would be to only use the main thread.

I tried using piranha's settings to make piranha run single-threaded:

piranha::settings::set_n_threads(1);

Which gives a similar error as before:

at std::__2::__throw_system_error(int, char const*) [__ZNSt3__220__throw_system_errorEiPKc] (wasm-function[2090]:1)
at std::__2::thread::thread<piranha::impl::task_queue::runner, void>(piranha::impl::task_queue::runner&&)[__ZNSt3__26threadC2IN7piranha4impl10task_queue6runnerEJEvEEOT_DpOT0_] (wasm-function[1018]:1826)
at piranha::impl::task_queue::task_queue(unsigned int, bool)[__ZN7piranha4impl10task_queueC2Ejb] (wasm-function[1005]:864)
at piranha::thread_pool_<void>::create_new_queues(unsigned int, bool)[__ZN7piranha12thread_pool_IvE17create_new_queuesEjb] (wasm-function[1098]:3374)
at piranha::thread_pool_<void>::resize(unsigned int)[__ZN7piranha12thread_pool_IvE6resizeEj] (wasm-function[1096]:165)
at piranha::settings_<void>::set_n_threads(unsigned int)[__ZN7piranha9settings_IvE13set_n_threadsEj] (wasm-function[82]:38)

I think that setting the threads to 1 still creates one thread separate to the main thread. Setting the threads to 0 results in an exception by the piranha implementation.

Is there a way to make piranha single-threaded? And how would that be?

I'm thinking of adding a flag like THREADING which can be set in cmake and is ON by default to maintain the current implementation and could be switched to OFF to disable the threading code and adds the single-threaded alternative.

@bluescarni
Copy link
Owner

Hi Mike,

piranha's thread pool is automatically created at program startup. You can disable the use of multithreading after program startup using the set_n_threads() function, but currently there is no way of avoiding the initial creation of the pool.

I am not opposed to adding a build system option to completely disable multithreading, but I currently do not have the bandwidth to do it myself. The code changes that would be required should not be too hard, because the thread pool is used only (I think) during series multiplication, and eliminating the use of any function from the thread_pool class should be enough to prevent the initial creation of the global pool.

I can provide you assistance if you decide to go down this road, you can ping me over on the gitter channel:

https://gitter.im/bluescarni/piranha

@bluescarni
Copy link
Owner

To expand a bit, the thread pool code is here:

https://github.com/bluescarni/piranha/blob/master/include/piranha/thread_pool.hpp

The inclusion of this header and the use of at least one of its methods will trigger the creation of the global thread pool on startup. In order to make a single-threaded option for piranha, you'd need to look in the source code for all uses of the thread pool class and replace them with serial code bracketed in #if defined(PIRANHA_SINGLE_THREAD) (or similar).

@mikeheddes
Copy link
Author

Hi Francesco,
Thanks for your comment. I will try to add the define flags. I'm fairly new to C++ so it might take some time. If I get stuck I will contact you on the gitter channel.
Thanks.

@mikeheddes
Copy link
Author

I managed to compile piranha single threaded. It was indeed not hard, adding a PIRANHA_SINGLE_THREAD flag in some files was enough.

I will add the CMake configuration and prepare a pull request.

Note that since dcgp depends on piranha v0.11 I branched from the v0.11 tag. I'm not sure what you have in mind for the PR, which branch it should be merged with, master or other.

@bluescarni
Copy link
Owner

bluescarni commented Mar 29, 2019

@mikeheddes yes, this option should be applied to the stable (0.11) branch. There's a couple of fixes I would like to make to the stable branch as well, so perhaps we can make a 0.12 release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants