如何编程多核 [英] How to program multicore

查看:75
本文介绍了如何编程多核的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用双核程序在一个双核计算机上使用一个核进行密集的计算并使用另一个核同时输出结果是否合适?我可以使用MPI库执行此任务吗?

我在双核计算机上尝试了多线程,其思想是一个线程进行计算,而另一个线程显示结果,但是两个内核不能同时处理.

谢谢,

Is it appropriate to have a program that uses a core to do an intensive calculation and another core to output the results simultaneously on a dualcore machine? can i use MPI library to do this task?

I tried multithreading on a dualcore machine with the same idea that one thread does the calculation while the other thread displaying results, but the two cores do not process simultaneously.

Thanks,

推荐答案

简短回答:您不能明确控制它.系统在多个内核上运行多个线程.在每个线程时间点,哪个线程加载哪个线程由系统线程调度程序决定.

但是,有一些方法可以通过编程方式影响此行为.
请记住,在几乎所有情况下都不建议这样做!能提供帮助的情况很少见.

请参阅以下Windows API:GetThreadAffinityMask/SetThreadAffinityMaskGetProcessAffinityMask/SetProcessAffinityMaskSetThreadIdealProcessor.

有关线程关联性的常规信息: http://en.wikipedia.org/wiki/Processor_affinity [ http://www.bluebytesoftware.com/blog/PermaLink,guid, 8c2fed10-75b2-416b-aabc-c18ce8fe2ed4.aspx [ ^ ].

在MSDN上,从这里开始:
http://msdn.microsoft.com/en-us/library/ms685096 (v = VS.85).aspx [ http://msdn.microsoft.com/en-us/library/ms684251 (v = vs.85).aspx [


并行计算是一个大话题,远远超出了CPU/内核的使用范围.
有关并行计算的方法,请参见:
http://technet.microsoft.com/en-us/library/ff919485 (v = ws.10).aspx [ http://www.microsoft.com/downloads/en/details.aspx?FamilyID = f9d74066-0dfd-4a38-9bad-bff5e15c9629 [ http://academic.research.microsoft.com/Publication/1693102/on-task-mapping-optimization-for-parallel-decoding-of-low-density-parity-check-codes-on [ http://en.wikipedia.org/wiki/Parallel_computing [
Short answer: you do not control it explicitly. The system runs several threads on several cores. Which core is loaded with which thread at each thread time spice is decided by the system threading scheduler.

However, there are ways to affect this behavior programmatically.
Remember, in almost all cases this is not recommended! The cases when it can help are very rare.

See the following Windows API: GetThreadAffinityMask/SetThreadAffinityMask, GetProcessAffinityMask/SetProcessAffinityMask and SetThreadIdealProcessor.

General information on thread affinity: http://en.wikipedia.org/wiki/Processor_affinity[^],
see also this discussion: The perils of thread affinity:
http://www.bluebytesoftware.com/blog/PermaLink,guid,8c2fed10-75b2-416b-aabc-c18ce8fe2ed4.aspx[^].

On MSDN, start here:
http://msdn.microsoft.com/en-us/library/ms685096(v=VS.85).aspx[^],
http://msdn.microsoft.com/en-us/library/ms684251(v=vs.85).aspx[^].

Again, don''t interfere in thread/process affinity unless you know exactly what are you doing!

(I know just one case when modification of affinity can be helpful: working with standard hardware (typically industrial) which is "bad" in the sense that it requires extensive polling as interrupt-driven operation is node accessible via API; in this case it is beneficial to dedicate on code to some threads serving these "bad" parts of equipment, which makes other threads'' timing to be more predictable.)



Parallel computing is a big topic which goes well above the usage of CPU/cores.
For approaches to parallel computing, see:
http://technet.microsoft.com/en-us/library/ff919485(v=ws.10).aspx[^],
http://www.microsoft.com/downloads/en/details.aspx?FamilyID=f9d74066-0dfd-4a38-9bad-bff5e15c9629[^],
http://academic.research.microsoft.com/Publication/1693102/on-task-mapping-optimization-for-parallel-decoding-of-low-density-parity-check-codes-on[^].

It will give you the idea on what''s involved. Do your research, find some more articles.

Start with: http://en.wikipedia.org/wiki/Parallel_computing[^].

—SA


但两个内核不能同时处理"

好吧,取决于每个线程的特殊性",在执行计算的线程与显示结果的线程之间可能会有某种类型的延迟.

IMO,除非您绝对需要它提供的功能,否则在使用更复杂的功能(例如mpi)之前,您应该使用线程和所有同步机制获得良好的经验.

反正祝你好运

M.
"but the two cores do not process simultaneously" ??


Well, depending on the "particular" of each thread there might be a delay of some kind between the thread that does the calculation and the thread that display the results.

IMO, you should get a good experience base using threads and all the synchronisation mechanisms before using something more complex (like mpi) unless you absolutly need features that it offers.

Anyway, good luck

M.


这篇关于如何编程多核的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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