Qt method to learn how many cores, other CPU characteristics
-
I noticed the QSysInfo doesn't tell me how many cores that a CPU has, nor the sizes of the L1 L2 L3 caches, nor how many RAM is in the device. Is there another class that provides these details? Just curious.
-
I noticed the QSysInfo doesn't tell me how many cores that a CPU has, nor the sizes of the L1 L2 L3 caches, nor how many RAM is in the device. Is there another class that provides these details? Just curious.
@Publicnamer said in Qt means to learn how many cores, other CPU characteristics:
Is there another class that provides these details?
I don't think so. You will need to use some other library or system specific APIs. On Linux you can simply read from /proc/cpuinfo device file.
-
@Publicnamer said in Qt means to learn how many cores, other CPU characteristics:
Is there another class that provides these details?
I don't think so. You will need to use some other library or system specific APIs. On Linux you can simply read from /proc/cpuinfo device file.
@jsulm Yes that is what I'm doing now but I was hoping Qt supported learning the core count.
After all this would be useful in making sure that not too many threads are running. -
@Publicnamer said in Qt method to learn how many cores, other CPU characteristics:
but I was hoping Qt supported learning the core count.
What's wrong with QThread::idealThreadCount() ?
After all this would be useful in making sure that not too many threads are running.
Use QtConcurrent - it spawns the correct amount of threads with the help of QThreadPool
-
@Publicnamer said in Qt method to learn how many cores, other CPU characteristics:
but I was hoping Qt supported learning the core count.
What's wrong with QThread::idealThreadCount() ?
After all this would be useful in making sure that not too many threads are running.
Use QtConcurrent - it spawns the correct amount of threads with the help of QThreadPool
@Christian-Ehrlicher said in Qt method to learn how many cores, other CPU characteristics:
QThread::idealThreadCount()
Unluckily it is not the ideal number. It is the total number of available cores after I checked it out in my code. The ideal number of threads in an app may be 2/3 or half of it. Now I know how the default thread number for build in qt creator is set. The default setting makes build in qt creator slow. I have to reduce the number of threads for build manually. You may never want to use total number of available cores as ideal thread number. Some other jobs need some cores as well.
-
@JoeCFD said in Qt method to learn how many cores, other CPU characteristics:
The ideal number of threads in an app may be 2/3 or half of it.
Do you see the problem ? or - it depends on the use case.
I have to reduce the number of threads for build manually.
This is a problem of your IDE, not of Qt. Even MSVC spawns as much threads as cores are available when using multithreaded compilation. It spawns even more when you've unlucky.
And in QtCreator you can configure how many processes (not threads btw.) the build process should use.So what's your actual problem? Finding out the number of cores in your program or configuring your IDE to not use all cores since it slows down your computer (which is most likely because the ram for e.g. 8 compilers should be more than 16GB to not get into swapping).
-
@JoeCFD said in Qt method to learn how many cores, other CPU characteristics:
The ideal number of threads in an app may be 2/3 or half of it.
Do you see the problem ? or - it depends on the use case.
I have to reduce the number of threads for build manually.
This is a problem of your IDE, not of Qt. Even MSVC spawns as much threads as cores are available when using multithreaded compilation. It spawns even more when you've unlucky.
And in QtCreator you can configure how many processes (not threads btw.) the build process should use.So what's your actual problem? Finding out the number of cores in your program or configuring your IDE to not use all cores since it slows down your computer (which is most likely because the ram for e.g. 8 compilers should be more than 16GB to not get into swapping).
@Christian-Ehrlicher
I would add that hyperthreading is a factor.
On a quad-core CPU without hyperthreading, 3 threads may be ideal just to keep the rest of the system from freezing up,
but if there is hyperthreading then 6 or 7 may be ideal. -
@Publicnamer said in Qt method to learn how many cores, other CPU characteristics:
but if there is hyperthreading then 6 or 7 may be ideal.
Again a maybe - so how would you expect that a framework knows your use case? What about big/little architectures? What when there is not enough ram (as I said already above)? What when the I/O is the limiting factor? When is I/O the limiting factor?
You can only get the number of cores (which is 4 for a 2 CPU + HT iirc) and adjust the max cpu count to your workload. But I never had a problem simply using all cores for my workloads so all this is imo only a theoretic discussion. If you really have such a high specialized usecase you would not rely on such a high-level toolkit like Qt for this task.
-
@Publicnamer said in Qt method to learn how many cores, other CPU characteristics:
but if there is hyperthreading then 6 or 7 may be ideal.
Again a maybe - so how would you expect that a framework knows your use case? What about big/little architectures? What when there is not enough ram (as I said already above)? What when the I/O is the limiting factor? When is I/O the limiting factor?
You can only get the number of cores (which is 4 for a 2 CPU + HT iirc) and adjust the max cpu count to your workload. But I never had a problem simply using all cores for my workloads so all this is imo only a theoretic discussion. If you really have such a high specialized usecase you would not rely on such a high-level toolkit like Qt for this task.
@Christian-Ehrlicher These kinds of nitty gritty details are why libdispatch was created to take the programmer's focus off them and back to the business of writing applications.
https://github.com/nickhutchinson/libdispatch
Certainly Qt should embrace this.
-
@Publicnamer said in Qt method to learn how many cores, other CPU characteristics:
Certainly Qt should embrace this.
Why? What's the roblem using this library directly? What do you think you will gain? It simply doesn't make sense for Qt to handle such low-level stuff which is barely needed.
-
@Christian-Ehrlicher These kinds of nitty gritty details are why libdispatch was created to take the programmer's focus off them and back to the business of writing applications.
https://github.com/nickhutchinson/libdispatch
Certainly Qt should embrace this.
Hi
That library has a C API and is from 2015.
Qt threading support is at a much higher level of abstraction than this and
would really be a downgrade in many use cases.But nothing stops you from using it in Qt if it provides some information
you find useful.That said, did you try (c++ 11)
#include <thread> auto processor_count = std::thread::hardware_concurrency();