Run on several cores


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". I would like to know if it is possible from Visual Studio 2017 to say "Such core executes 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?





You don't get to decide which core to use. OS does.
– Ron
Jul 2 at 12:35





i think you need much more help then what you can get on SO. you should document yourself a bit more about parallel execution
– Tyker
Jul 2 at 12:37





You don't have 4 separate int main() { ... } do you?
– NathanOliver
Jul 2 at 12:40


int main() { ... }





@Ron: Not in portable C++, but since it's essential to High-Performance Computing every modern OS does support that. It's called Thread Affinity. Having said that, it's understood that programming for such environments requires above-average skill, and the question suggests otherwise.
– MSalters
Jul 2 at 13:48





@MSalters I was under the impression the Thread Affinity does not guarantee the actual code will be executed on a particular core. And is to be avoided.
– Ron
Jul 2 at 14:00





2 Answers
2



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. 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>. Using <thread> is anything but automatic. However, once you have threads, using multiple cores is automatic.


<thread>


<thread>



There is a call in Windows that can limit your process to specific core:



https://docs.microsoft.com/en-us/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



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.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

How to set up datasource with Spring for HikariCP?

Display dokan vendor name on Woocommerce single product pages