简体   繁体   English

在几个核心上运行

[英]Run on several cores

Initially, I have a program, which I divided into several parts so that each part is executed by a specific core. 最初,我有一个程序,将我分为几个部分,以便每个部分都由特定的内核执行。 So, in my C++ project I have several "main files". 因此,在我的C ++项目中,我有几个“主文件”。 I would like to know if it is possible from Visual Studio 2017 to say "Such core executes such.cpp". 我想知道是否可以从Visual Studio 2017说“这样的核心执行such.cpp”。

Using the simple example of a counter and a display: The counter turns on core 1 and sends its data to the display on core 2. Is this possible to run on Visual Studio 2017? 使用一个简单的计数器和显示示例:计数器打开内核1并将其数据发送到内核2上的显示。这是否可以在Visual Studio 2017上运行?

No, this is not possible, and in fact it would be pointless. 不,这是不可能的,实际上这是没有意义的。

In your simple idea, core2 has nothing to do until core1 sends it some data, at which point core1 would be waiting for core2. 用您的简单想法,在core1向其发送一些数据之前,core2无关,此时core1将等待core2。 So at most one of the two cores is active at any time. 因此,两个核心中的一个最多在任何时间都处于活动状态。 It would be far more efficient to use one core for that. 为此使用一个内核会更加高效。

To use multiple cores in C++, you need <thread> . 要在C ++中使用多个内核,您需要<thread> Using <thread> is anything but automatic. 使用<thread>是自动的。 However, once you have threads, using multiple cores is automatic. 然而,一旦你有线程,采用多内核自动的。

There is a call in Windows that can limit your process to specific core: Windows中有一个调用可以将您的进程限制为特定的核心:

https://docs.microsoft.com/en-us/windows/desktop/api/winbase/nf-winbase-setprocessaffinitymask https://docs.microsoft.com/zh-cn/windows/desktop/api/winbase/nf-winbase-setprocessaffinitymask

In this case you have to launch multiple proceses, set their affinities and do different task in each one. 在这种情况下,您必须启动多个过程,设置它们的亲和力,并在每个过程中执行不同的任务。

And there is also one for threads: 还有一个线程:

https://docs.microsoft.com/en-us/windows/desktop/api/winbase/nf-winbase-setthreadaffinitymask https://docs.microsoft.com/zh-cn/windows/desktop/api/winbase/nf-winbase-setthreadaffinitymask

In a simple case you would launch new thread for each of your task, set their affinities, run tasks in threads and then wait for threads to join with your main thread. 在简单的情况下,您将为每个任务启动新线程,设置它们的亲和力,在线程中运行任务,然后等待线程与主线程联接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM