显式C ++ - 多处理器系统的代码 [英] Explicit C++-Code for multiprocessor systems

查看:60
本文介绍了显式C ++ - 多处理器系统的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好(当然还有女孩......),



我们经常有两个或更多巨大阵列的代码结构,如:



Hello guys (and girls of course...),

we often have code structures with two or more huge arrays like:

for (int i = 0; i < size1; i++) 
{
	for (int j = 0; j < size2; j++)
	{
		// compare members of array[i] with members of array[j] and do something
		// ...
		// if done break
	}
}





这可能是一个非常耗时的过程。



如何在多处理器系统上拆分这些例程

in smart方式,我的意思是在处理器1只运行这个特定部分的方式

和processor2运行另一个?



或者如何在几个线程中拆分例程并明确赋值

thread1到processor1和thread2到processor2例如?



几年前我读了一篇关于这个的文章,但我忘记了所有。



也许是一个想法或一些例子?



在此先感谢您,并提出最好的问候。



This can be a very time consuming process.

How is it possible to split these routines on a multiprocessor system
in a smart way, I mean in a way that processor1 runs only this specific part
and processor2 runs another one ?

Or how to split the routines in several threads and assign explicitly
thread1 to processor1 and thread2 to processor2 for example ?

Some years ago I read an article about this, but I have forgotten all.

Perhaps an idea or some examples ?

Thanks in advance and best regards.

推荐答案

让代码在多个处理器上运行的唯一方法是在多个线程中运行它,两者是相同的。我不认为这会发生这种情况,但是将每个外环循环到一个线程,是增加赔率的方法,然后我认为这取决于应用程序的配置方式和线程是如何创建的,我不知道对C ++线程有足够的了解,可以在这方面提供更多细节。我会让它使用很多线程,然后测试它以查看多个内核是否忙碌。如果他们不这样做,那么找出原因,但多线程必须是让许多内核运行的唯一方法,这就是你需要启动的地方。
The only way to get code to run on more than one processor, is to run it in more than one thread, the two are the same. I don't think it's guarenteed this will happen, but farming each outer loop to a thread, is the way to increase your odds, then I think it comes down to how the app is configured and the threads are created, I don't know enough about C++ threading to give more details on that front. I'd make it use a lot of threads, then test it to see if multiple cores get busy. If they don't, look to work out why, but multiple threads has to be the only way to get many cores running, so that's where you need to start.


OpenMP [ ^ ]是另一种方式。它受Visual C ++和GCC支持。使用OpenMP,编译器处理产生和管理线程的细节。



请记住,你仍然必须以竞争条件,锁定的方式编写代码等等......正如其他人已经提到的那样优雅地处理。



此外,在多级循环中,努力只将最外层的循环拆分为线程。创建线程需要一些开销。一旦创建了一个线程,请确保它有很多工作要做。



为小型操作生成许多线程,可能会减慢你的代码速度。
OpenMP[^] is another way to go with this. It's supported by Visual C++ and GCC. With OpenMP, the compiler handles the details of spawning and managing threads.

Keep in mind that you still have to write your code in such a way that race conditions, locking etc..., are handled gracefully as others have already mentioned.

Also, in multi-level loops, make an effort to only split your outermost loops out into threads. Creating threads takes some overhead. Once a thread has been created, make sure it has plenty to do.

Spawning many threads for small operations, will likely slow your code down.


下班后我找到了上面提到的文章

新的多线程C ++ 11特性。



作者在一个例子中建议使用

std: :async像这样:



After work I found the mentioned article about
new multithreading C++11 features.

The author suggests in an example the use of
std::async like this:

// Sequential processing
int func1(int start, int end)
{
	for (int i=start; i<end;i++)>
	{
		// processing...
	}
	return result;
}

// Multi thread processing
int N = GetCPUNum();

int func2(int start, int end)
{
	return std::async(func1(start, (end-start)/N))+
		std::async(func1(start+(end-start)/N,start+2*(end-start)/N))+
			...
}





即N多线程函数中的一个顺序函数调用的划分

调用。



这篇文章是由Rainer Grimm撰写的,人们可以在网上找到一个有趣的C ++ 11 OverView as pdf文件。



祝你好运



i.e the division of one sequential function call in N multi-threaded function
calls.

The article is written by Rainer Grimm and one can find in the net an
interesting C++11 OverView as pdf file.

Best regards


这篇关于显式C ++ - 多处理器系统的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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