OpenMP's tasks and parallel for loops seem like a good choice for concurrent processing within a specific process, but I'm not sure if it covers all use-cases.
What if I just want to spawn off an asynchronous task that doesn't need to return anything and I don't want to wait for it, I just want it to run in the background then end when done... does OpenMP have this ability? And if not can I safely use std:thread whilst also using OpenMP pragmas for other things?
And what if the spawned thread itself uses OpenMP task group, whilst the parent thread is also using another OpenMP task group for something else? Is this going to cause issues?
Related
I'm trying to create a task manager, which accepts tasks and runs each task as a new thread, using C++ and (currently) std::thread on a Linux environment .
the task manager accepts normal tasks and priority tasks.
when a priority task arrives, all normal tasks need to be halted until the priority task is done.
I'm keeping all normal task threads in a std::vector, but I couldn't find a proper function to halt those threads.
is there a way, preferably not using locks, to implement the wanted behavior?
maybe with <pthread> or boost threads?
There is no direct way to interrupt a thread from the outside.
Boost interruption points are handy to stop things once for all but that's not equivalent to a pause.
I would suggest you to implement your own "interruption" class with a condition variable (and yes a mutex) to check and wait efficiently anywhere inside your tasks. But it is up to you to explicitely call these interruptions.
Maybe another way would be make your priority tasks multithreadable so that you can allocate more threads to fulfill them => the scheduler is more likely to complete them first but that's not sure so forget what i said.
Sorry, I don't aknowledge anything better then this.
I'm creating a multi-threaded program which highly relays on boost::fibers when it comes to executing the actual execution heavy code.
What I want to achieve:
My main thread knows when to activate and run which fiber and when to collect the future provided after the execution of the fiber.
As my software is running in a highly parallel environment with over 100 cores, I start as many worker threads if possible. The worker threads should run a fiber scheduler like boost::fibers::algo::work_stealing. Therefore they should execute all the fibers my main thread produces.
What's the issue:
As my main thread is quite buisy with creating and timing all the fibers for the over 100 worker threads I want to avoid the main thread from joining the execution of any fibers.
This means, my main thread should solely care about starting fibers and gathering their futures after they completed executing.
However, I do not know how to exclude my main fiber from executing fibers, too.
Possible, naive, solutions:
Even though I don't know how to solve my problem properly, I thougt about a few possibilities.
Create my own fiber scheduler: The scheduler is a customization point of boost::fibers. Hence, there might be the possibility to wirte a custome scheduler which excludes the main tread from execution.
Use a boost::fibers::buffered_channel to transmit tasks between threads: However, I don't if this is a good solution as this removes the great possibilities from using a fiber scheduler.
Ohter great way I don't know yet: I gues there might be another, simple way from excluding the main thread, which creates the fibers, from participating in the execution scheduling of boost::fibers
As I'm quite new in the boost::fibers library, I wonder what is the best way to achieve my goal?
Regards and thank you for your help!
I want to create a thread or task (more than one to be exact) that goes and does some non CPU intensive work that will take a lot of time because of external causes, such a HTTP request or a file IO operation from a slow disk. I could do this with async await in C# and would be exactly what i am trying to do here. Spawn a thread or task and let it do it's own thing while i continue with execution of the program and simply let it return the result whenever ready. The problem with TBB i have is that all tasks i can make think they are created for a CPU intensive work.
Is what TBB calls GUI Thread what i want in this case ? I would need more than one, is that possible ? Can you point me to the right direction ? Should i look for another library that provides threading and is available for multiple OS ?
Any I/O blocking activity is poorly modeled by a task -- since tasks are meant to run to completion, it's just not what tasks are for. You will not find any TBB task-based approach that circumvents this. Since what you want is a thread, and you want it to work more-or-less nicely with other TBB code you already have, just use TBB's native thread class to solve the problem as you would with any other threading API. You won't need to set priority or anything else on this TBB-managed thread, because it'll get to its blocking call and then not take up any further time until the resource is available.
About the only thing I can think of specifically in TBB is that a task can be assigned a priority. But this isn't the same thing as a thread priority. TBB task priorities only dictate when a task will be selected from the ready pool, but like you said - once the task is running, it's expected to be working hard. The way to do use this to solve the problem you mentioned is to break your IO work into segments, then submit them into the work pool as a series of (dependent) low-priority tasks. But I don't think this gets to your real problem ...
The GUI Thread you mentioned is a pattern in the TBB patterns document that says how to offload a task and then wait for a callback to signal that it's complete. It's not altogether different from an async. I don't think this solves your problem either.
I think the best way for you here is to make an OS-level thread. That's pthreads on Linux or windows threads on Windows. Then you'll want to call this on it: http://msdn.microsoft.com/en-us/library/windows/desktop/ms686277(v=vs.85).aspx ... if you happen to be in C++11, you could use a std::thread to create the thread and then call thread::native_handle to get a handle to call the Windows API to set the priority.
I have a C++ program that performs some lengthy computation in parallel using OpenMP. Now that program also has to respond to user input and update some graphics. So far I've been starting my computations from the main / GUI thread, carefully balancing the workload so that it is neither to short to mask the OpenMP threading overhead nor to long so the GUI becomes unresponsive.
Clearly I'd like to fix that by running everything concurrently. As far as I can tell, OpenMP 2.5 doesn't provide a good mechanism for doing this. I assume it wasn't intended for this type of problem. I also wouldn't want to dedicate an entire core to the GUI thread, it just needs <10% of one for its work.
I thought maybe separating the computation into a separate pthread which launches the parallel constructs would be a good way of solving this. I coded this up but had OpenMP crash when invoked from the pthread, similar to this bug: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=36242 . Note that I was not trying to launch parallel constructs from more than one thread at a time, OpenMP was only used in one pthread throughout the program.
It seems I can neither use OpenMP to schedule my GUI work concurrently nor use pthreads to have the parallel constructs run concurrently. I was thinking of just handling my GUI work in a separate thread, but that happens to be rather ugly in my case and might actually not work due to various libraries I use.
What's the textbook solution here? I'm sure others have used OpenMP in a program that needs to concurrently deal with a GUI / networking etc., but I haven't been able to find any information using Google or the OpenMP forum.
Thanks!
There is no textbook solution. The textbook application for OpenMP is non-interactive programs that read input files, do heavy computation, and write output files, all using the same thread pool of size ~ #CPUs in your supercomputer. It was not designed for concurrent execution of interactive and computation code and I don't think interop with any threads library is guaranteed by the spec.
Leaving theory aside, you seem to have encountered a bug in the GCC implementation of OpenMP. Please file a bug report with the GCC maintainers and for the time being, either look for a different compiler or run your GUI code in a separate process, communicating with the OpenMP program over some IPC mechanism. (E.g., async I/O over sockets.)
I have a piece of code which uses Boost threads to speed up the calculation, but I need to debug it and want to run them in series, not in parallel. How do I do that?
Unless I'm missing something, just debug it using a single thread. Forget about multi-threading unless you get the algorithm right.
Assuming you meant "to speed up the calculation", threads running in series will not help performance at all. Actually, it would cost you performance for the overhead on the threads, because you're not parallelizing any work.
If you're so inclined to run them in series, just make sure each one waits for the current thread to finish executing before allowing another to run? I'm probably missing something here.
You can create a semaphore for each thread, and then signal the 1st semaphore in the main thread, and each thread can signal the next semaphore at its end.
But, still, why do you need to debug your app this way? It is very useful to debug the app with all threads running so that you can see if any race conditions happen, or anything like that.
Put breakpoints in all your threads. Your debugger should have a command to step through or start just one thread. The rest of your threads will remain suspended, so they won't interfere with your single-threaded debugging. Once the one thread terminates, you can resume all the threads, or you can continue debugging in the next thread.
Assign only a single processor core to your process. On Windows, you can do so with SetProcessAffinityMask