有没有办法在循环处理器时并行运行具有多个功能的程序? [英] Is there a way to run a program with multiple functions in parallel while cycling the processors?

查看:101
本文介绍了有没有办法在循环处理器时并行运行具有多个功能的程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望编写以下格式的代码:

I wish to write a code of the following format:

main()
{
}
a(){}
b(){}
c(){}
d(){}
.
.
.
.

k(){}



我希望并行运行代码,以便第1个5个函数本身具有处理器(假设系统上有12个内核可用)
在第1个5中的任何一个完成执行之后,将第6个功能分配给该第六个功能,依此类推.

这些功能彼此独立,并以顺序方式运行.我希望通过使它们并行运行来减少执行时间.我该如何编写这样的程序?
请帮忙.
谢谢:)



I wish to run the code in parallel such that 1st 5 functions have a processor to themselves(assuming 12 cores are available on the system)
After any of the 1st 5 finishes execution, the 6th function is assigned to that and so on.

The functions are independent of each other and run in a sequential manner. I wish to reduce the execution time by making them run in parallel. How do I write such a program?
Please help.
Thanks :)

推荐答案

您最好的选择是使用任务并行库(TPL). TPL将自动扩展以包括运行它的机器上的所有内核.所以你可以做这样的事情

Your best bet would be to use the Task Parallel Library (TPL). The TPL will automatically scale to include all cores on the machine in which it runs. So you could do something like this

void q1()
       { }
       void q2()
       { }
       void q3()
       {}





void Run()
      {
          Action[] actions = new Action[] { q1, q2, q3 };
          Parallel.Invoke(actions);
      }



如果您需要执行诸如取消线程,顺序运行某些操作或从函数返回值之类的操作,则可以使用像这样的Task类



If you need to do things like cancel the threads, run certain actions sequentially or return values from the function then you can use the Task Class Like this

Task.Factory.StartNew(q1).ContinueWith(t => q2()).ContinueWith(t => q3());



这是来自TPA的Sacha Barber的大量文章

任务并行库:n之1 [



Here is a good set of Articles from Sacha Barber on the TPL

Task Parallel Library: 1 of n[^]

Hope this helps


首先:在大多数情况下,您不应明确控制它.您能解释为什么这样做吗?

系统在多个内核上运行多个线程.在每个线程时间点,哪个线程加载哪个线程由系统线程调度程序决定.

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

请参阅以下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 [
—SA
First of all: in most cases, you should not control it explicitly. Could you explain why doing so?

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

—SA


这篇关于有没有办法在循环处理器时并行运行具有多个功能的程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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