You are not logged in.

#1 2011-03-25 18:33:07

Kosmonavt
Member
Registered: 2010-02-15
Posts: 100

[C++] Rational multithreading with boost::thread

Hi all,

I'm writing a small project to familiarize myself with basic multithreading concepts. I'm using C++ and Boost Thread library. In short, the program spawns some number of threads, they modify some object (actually, each thread modifies different elements of std::vector), then they join. And it repeats many many times (say 1 000 000), so basically it's an iterative task. Throwing out all auxiliary stuff, it looks like this model: http://pastebin.com/KNXjHhxd .

1) I made some optimizations to thread code, so it runs very quickly. And thread creation overhead becomes noticeable. I thought about making worker threads joining only at the end. But I need to somehow control them - algorithm requires all threads make a 1 operation over their elements, and only then to continue. How can join count be reduced in an elegant manner?

2) What book can you recommend to read about multithreading, especially in application to science-related tasks?

P.S. I know that this kind of tasks is solved much better using OpenCL or CUDA (my next aim is to rewrite this program to work with OpenCL). But I'm curious how it can be realised in a traditional way.

Offline

#2 2011-03-25 18:45:42

penguin
Member
From: Gotham City
Registered: 2010-12-31
Posts: 163

Re: [C++] Rational multithreading with boost::thread

I'm also using boost:threads for scientific computing, and when I googled I've come across a blog dedicated to massive multithreading. Can't find it now though.

But what's the benefit to using GPU? Is it that much faster than the typical CPU?
(I assume that CUDA and OpenCL is used in GPU)


I'll be following this thread closely.

Last edited by penguin (2011-03-25 18:47:30)

Offline

#3 2011-03-25 19:46:41

tavianator
Member
From: Waterloo, ON, Canada
Registered: 2007-08-21
Posts: 858
Website

Re: [C++] Rational multithreading with boost::thread

1)  Use a condition variable to signal to the main thread that the work is done, and another condition variable to signal to the worker threads that there's more work available.
2)  I've never read any books on parallelism, but maybe this along with the boost threading/synchronisation docs would be useful.

@penguin:  The benefit of offloading work onto a GPU is that GPUs are vector architectures.  For some types of computation, it's the equivalent of having hundreds of cores.  But for most types of computation, it's slower than a CPU.

Offline

#4 2011-03-25 20:23:03

Kosmonavt
Member
Registered: 2010-02-15
Posts: 100

Re: [C++] Rational multithreading with boost::thread

penguin wrote:

But what's the benefit to using GPU? Is it that much faster than the typical CPU?
(I assume that CUDA and OpenCL is used in GPU)

AFAIK, GPU is like a powerful dedicated FPU. It is fast with highly parallel tasks (e.g. image processing, many types of modelling etc) and with floating arithmetics. OpenCL is capable to work with CPUs and GPUs (ATI and nVidia), while CUDA is for nVidia cards only.

In my institute there's a group which performs researches in protein transformations. They say that using GPGPU greatly increases performance.

Offline

#5 2011-03-25 23:56:47

GogglesGuy
Member
From: Rocket City
Registered: 2005-03-29
Posts: 610
Website

Re: [C++] Rational multithreading with boost::thread

Offline

#6 2011-03-26 22:03:53

tavianator
Member
From: Waterloo, ON, Canada
Registered: 2007-08-21
Posts: 858
Website

Re: [C++] Rational multithreading with boost::thread

A barrier would work too, I think that would actually be cleaner.  I always forget about them because they're not mandated by POSIX, and I'm usually using pthreads.

Offline

#7 2011-03-29 16:12:25

Kosmonavt
Member
Registered: 2010-02-15
Posts: 100

Re: [C++] Rational multithreading with boost::thread

GogglesGuy wrote:

Hell yeah! Looks like I haven't read Boost::Thread manual thoroughly enough. I think it's the best solution in this case.

Offline

#8 2016-03-14 20:14:00

rahulgupta4u
Member
Registered: 2016-03-14
Posts: 2

Re: [C++] Rational multithreading with boost::thread

I found this link useful, contains lot of theory https://www.tutorialcup.com/cplusplus/m … eading.htm

Offline

#9 2016-03-15 00:33:55

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,424
Website

Re: [C++] Rational multithreading with boost::thread

Please don't necrobump old threads: https://wiki.archlinux.org/index.php/Fo … bumping.22



Closing


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

Board footer

Powered by FluxBB