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

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

问题描述

我有,我想在多个处理器上执行一个尴尬的并行问题。我早知道的boost ::线程将自动发送新主题新处理器,但它们都是在同一个核心的父进程执行。是否有可能让每个线程在不同的处理器上运行,或者我需要像MPI?

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?

我怀疑是的boost ::线程根本就不是一个多处理器的工具,那我要求它做什么它不是专为。

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.

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

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?

下面是我的code的相关样本:

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();
}

由于要清晰,的doWork 做一些计算使用参数 I ,结果在<$ C店$ C>丘壑。我的想法是,设置 maxConncurrentThreads 等于核心数量可用,然后每个线程将使用闲置的核心。我只是需要有人来确认的boost ::线程无法进行这样的工作。

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.)

这里的的doWork 功能:

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;
}


编辑:马丁·詹姆斯认为,问题是,函数的doWork最初只有1000 INT补充。有了这样一个小的工作,调度线程时间长于执行线程,因此只有一个处理器是在使用中。使工作更长(增加10,000,000整数)得到期望的行为。问题的关键是:的boost ::线程将会的默认情况下使用多个内核,但如果你的线程少做的工作比调度线程,那么你赢了看不到从多线程任何好处。


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.

已经看了的doWork功能后,我认为这是这样做,它正在采取较少的工作比在第一时间启动一个线程这么少的工作。试着用更多的工作(1000倍),运行它。

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).

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

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