多处理器Boost :: Thread?所有线程在一个处理器上运行 [英] Multiprocessor Boost::Thread? All threads running on one processor

查看:276
本文介绍了多处理器Boost :: Thread?所有线程在一个处理器上运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个尴尬的并行问题,我想在多个处理器上执行。我认为 boost :: thread 会自动发送新的线程到新的处理器,但是它们都在与父进程相同的核心上执行。是否有可能让每个线程在不同的处理器上运行,或者我需要像MPI的东西?



我的怀疑是 boost :: thread 不是一个多处理器工具,它会做一些不是为它设计的东西。



编辑:我的问题归结为:为什么所有的线程在一个处理器上执行?有没有办法让 boost :: thread 将线程发送到不同的处理器?



这里是相关的示例我的代码:

  size_t lim = 1000; 
std :: deque< int> vals(lim);
std :: deque< boost :: thread *>线程;
int i = 0;
std :: deque< int> :: iterator it = vals.begin();
for(; it!= sigma.end(); it ++,i ++){
threads.push_back(new boost :: thread(doWork,it,i));
while(threads.size()> = maxConcurrentThreads){
threads.front() - > join();
delete threads.front();
threads.pop_front();
}
}
while(threads.size()){
threads.front() - > join()
threads.pop_front();
}

应该清楚的是, doWork 使用参数 i 进行一些计算,并将结果存储在 vals 中。我的想法是设置 maxConncurrentThreads 等于可用的核心数,然后每个线程将使用空闲的核心。我只需要有人确认 boost :: thread 不能以这种方式工作。



假设有一个更好的方法来限制并发线程的数量,而不是使用一个队列;随时骂我也是这样。)






这是 doWork 函数:

  doWork(std :: deque< int> :: iterator it,int i){
int ret = 0;
int size = 1000; //原来为1000,以后改为10000,000
for(int j = i; j ret + = j;
}
* it = ret;
return;
}






建议,问题是doWork函数最初只有1000个int添加。使用这么小的作业,调度线程比执行线程花费更长的时间,因此只有一个处理器在使用。使作业更长(增加10,000,000个int)产生了所需的行为。重点是: boost :: thread 默认情况下会使用多个内核,但是如果你的线程比调度线程少工作,

感谢大家帮助我理解这一点。

解决方案

您总是加入队列中的第一个线程。如果这个线程需要很长时间,它可能是唯一的线程。我想你想要的是在任何线程完成后开始一个新线程。



我不知道你为什么只得到一个有效的并发级别只有一个。



在看过doWork函数后,我认为它做了这么少的工作,比起在一个线程中第一名。尝试运行更多的工作(1000x)。


I have a embarrassingly parallel problem that I want to execute on multiple processors. I had supposed that boost::thread would automatically send new threads to new processors, but all of them are executing on the same core as the parent process. Is it possible to get each thread to run on a different processor, or do I need something like MPI?

My suspicion is that boost::thread is simply not a multi-processor tool, that I'm asking it to do something it's not designed for.

EDIT: my question boils down to this: Why do all the threads execute on one processor? Is there a way to get boost::thread to send threads to different processors?

Here's the relevant sample of my code:

size_t lim=1000;
std::deque<int> vals(lim);
std::deque<boost::thread *> threads;
int i=0; 
std::deque<int>::iterator it = vals.begin();
for (; it!=sigma.end(); it++, i++) {
  threads.push_back(new boost::thread(doWork, it, i));
  while (threads.size() >= maxConcurrentThreads) {
    threads.front()->join();
    delete threads.front();
    threads.pop_front();
  }
}
while(threads.size()) {
  threads.front()->join();
  threads.pop_front();
}

As should be clear, doWork does some calculation using the parameter i and stores the result in vals. My idea was that setting maxConncurrentThreads to be equal to the number of cores available, and then each thread would use the core that was idle. I just need someone to confirm that boost::thread cannot be made to work in this way.

(I'd guess that there's a better way to limit the number of concurrent threads than using a queue; feel free to scold me for that as well.)


Here's the doWork function:

void doWork(std::deque<int>::iterator it, int i) {
  int ret=0;
  int size = 1000; // originally 1000, later changed to 10,000,000
  for (int j=i; j<i+size; j++) {
    ret+=j;
  }
  *it=ret;
  return;
}


EDIT: As Martin James suggested, the problem was that the doWork function was initially only 1000 int additions. With such a small job, scheduling the thread took longer than executing the thread, so only one processor was in use. Making the job longer (adding 10,000,000 ints) yielded the desired behavior. The point being: boost::thread will use multiple cores by default, but if your threads do less work than scheduling the thread then you won't see any benefit from multithreading.

Thanks to everyone for aiding my understanding in this.

解决方案

You are always joining the first thread in the queue. If this thread is taking a long time it might be the only thread left. I guess what you want is to start a new thread once any thread has completed.

I don't know why you only get an effective concurrency level of only one though.

After having looked at the doWork function I think that it is doing so little work that it is taking less work than starting a thread in the first place. Try running it with more work (1000x).

这篇关于多处理器Boost :: Thread?所有线程在一个处理器上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆