2

I work with a virtual machine computer cluster with many amd64 processors and Debian Squeeze. Previously, I've successfully executed shell scripts in parallel on it (with GNU Parallel). Now, I'd like to use boost::threads. I run this program:

#include <boost/thread.hpp>

using namespace std;
boost::thread_group g;

void foo()
{
    for(int i = 0; i < 1000000000; ++i) 
        for(int i = 0; i < 1000000000; ++i);
}

int main(int argc, char* argv[])
{
    g.add_thread(new boost::thread(foo));
    g.add_thread(new boost::thread(foo));
    g.add_thread(new boost::thread(foo));

    g.join_all();
}

All these threads run on a single processor, that is used in 300% (according to top command). How to make these three threads to run on three separate processors? Is it possible with boost::threads?

Note: This Multiprocessor Boost::Thread? All threads running on one processor, despite the title, is about multicore system whereas mine is truly about multiprocessor system.

Community
  • 1
  • 1
cpp
  • 3,743
  • 3
  • 24
  • 38
  • 1
    I'm assuming you mean you have a multi-socket SMP system. Does your computer use NUMA? – Dai Sep 28 '13 at 03:59
  • @dai, this is a virtual machine. I can execute shell scripts in parallel on it (with GNU Parallel). I don't know about SMP nor NUMA. – cpp Sep 28 '13 at 04:23

1 Answers1

2

It's running correctly. You're spawning three threads and all three are running concurrently. The reported CPU usage in multi-threaded applications is the sum of the CPU usage of all the threads. You have three threads that each use 100%, therefore you have 300% usage.

Multiple cores and multiple CPU sockets appear the same in nearly all threading libraries. You have to go out of your way to tell the difference, eg. libNUMA.

Adam
  • 16,808
  • 7
  • 52
  • 98
  • yes, they probably run concurrently, but I'am not sure they run on separate processors. – cpp Sep 28 '13 at 04:31
  • 1
    There are programs that can tell you CPU usage per CPU. Some examples here: http://www.cyberciti.biz/tips/how-do-i-find-out-linux-cpu-utilization.html – Adam Sep 28 '13 at 04:41
  • You use the word cluster, but then talk about SMP threads. Do you have a single motherboard with multiple CPU sockets or multiple motherboards linked by a network? A single motherboard is not a cluster. These are two completely different beasts. – Adam Sep 28 '13 at 04:42
  • It's not probably, they definitely are running concurrently. – Adam Sep 28 '13 at 04:44
  • this is a remote computer, I login via SSH. I don't know the details of its implementation, but those who built it call it a virtual machine of cluster type. – cpp Sep 29 '13 at 05:19
  • `boost::thread::hardware_concurrency()` gives my the number of processors, so probably boost sees them as cores. Also `mpstat -P ALL 2` shows 3 processors used in 100%. Thus the threads of my program do run on separate processors. Thanx. – cpp Sep 29 '13 at 05:24
  • Again, processor == core in this context. If you have two 4-core chips then they will appear as CPU 0-7. But it doesn't sound like you even know how many sockets there are (and whether what your VM sees reflects the hardware), and that's good, because frankly it doesn't matter. There are very few cases where it makes any difference at all and it doesn't look like yours is one of them. – Adam Sep 29 '13 at 10:09