简体   繁体   English

如何仅在一个CPU上运行C ++程序?

[英]How to run a C++ program only on one CPU?

I've been reading around about how to check how many CPUs or cores my machine has (MacBook OS X, Sierra, v. 10.12, 2GHz Intel Core i7), but there are many different answers, for example here: 我一直在阅读有关如何检查我的计算机有多少个CPU或内核(MacBook OS X,Sierra,v。10.12、2GHz Intel Core i7)的信息,但是这里有很多不同的答案,例如:

How to discover number of *logical* cores on Mac OS X? 如何在Mac OS X上发现*逻辑*内核的数量?

what I would need though is to make sure that my C++ program runs just 1 one CPU (and, if possible, only on one core, ie, without scheduling, ie, my program should have a dedicated core to run on => I'm not sure how my Mac OS X architecture is actually organised). 不过,我需要确保C ++程序仅在一个CPU上运行1个(如果可能的话,只能在一个内核上运行,即无需调度,即我的程序应具有专用内核才能在=>上运行)我不确定我的Mac OS X架构的实际组织方式)。

I am not sure if this should be done at implementation, compilation or execution level. 我不确定是否应该在实现,编译或执行级别完成此操作。 I've seen people talking about taskset for Ubuntu, but I'm not sure if that's the right tool for me (maybe it does not even exist for Mac OS X). 我见过人们在谈论taskset Ubuntu的,但我不知道这是对我来说是合适的工具(也许它根本不存在于Mac OS X)。

Note: if you feel this question should be asked in another Stack Exchange's website, just tell me, and I will move it to there. 注意:如果您认为应该在另一个Stack Exchange的网站上询问此问题,请告诉我,我将其移至该位置。 Actually I would like that my solution is cross-platform, so maybe this is not the best place to ask this question. 实际上,我希望我的解决方案是跨平台的,所以也许这不是问这个问题的最佳地点。

Processes are scheduled, the idea of a non-scheduled process is an oxymoron. 流程是有计划的,非计划流程的想法是矛盾的。

That said, restricting yourself to one CPU is pretty much the default in C++. 也就是说,将自己限制为一个CPU几乎是C ++中的默认设置。 main starts on one thread, and unless you create additional threads that's all you get. main从一个线程开始,除非您创建其他线程,否则您将获得全部。

You mention that you want a "dedicated" core. 您提到您想要一个“专用”核心。 There's the idea of pinning a thread to a core, which sort of achieves that, but you can figure out what happens if two programs pin themselves to the same core. 有一种将线程固定到核心的想法,可以实现这种目的,但是您可以弄清楚如果两个程序将自己固定到同一核心会发生什么。 Another core might be fully unused while the two programs share that pinned core. 当两个程序共享该固定内核时,另一个内核可能完全未使用。 This is more of a feature for supercomputers, where cores do not have uniform access to memory, and you should match CPU core and memory allocations. 这是超级计算机的一项功能,超级计算机的内核无法统一访问内存,因此您应该匹配CPU内核和内存分配。

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

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