Does anyone know if there is, in c++, any way to determine at runtime the cpu characteristics of the machine that compiled the code? For example, in gcc (which I'm using) the preprocessor variable _OPTIMIZE_ indicates compile-time optimization, but I can't find anything on cpu type.

I'm doing some cloud computing on a heterogeneous cluster. Some of the nodes are non-interoperable (code compiled on one will not run on another), while others are partly interoperable (code compiled on one will run on another, but at sub-optimal speed). The first case is easy to check for and deal with, but second case is trickier to detect in an automated way.

4 Replies 4

There are typically a set of vert compiler-specific macros that contains such information. You need to read its documentation.

Are you trying to get the specific cpu model or just the cpu architecture?

At least with most compilers, you'd have to do something to collect the relevant characteristics at build time, and use some sort of script to embed the data into your executable (e.g., generate a small function that returned the relevant data.

I'm not sure this is much (any?) easier that writing code to detect the sort of CPU you're running on, and select code optimized for that CPU.

I think you should approach it the other way around, either compile for a very generic CPU and allow for suboptimal performance. Or investigate what architectures your nodes have, compile optimized versions for those CPU's and then at deploy time make a selection which executable you should actually install (or run). You will have to look in the documentation on your cloud provider on how you can know what type of CPU you are running on. (API or shell commands)

Your Reply

By clicking “Post Your Reply”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.