I have an Intel I7 dual core 64 bit.
I am compiling a relatively long C++ code in Visual Studio Community 2019.
Looking to statistics just 25% of the processor is being used.
How can I compile using 100% of the processor?
I assume what you meant to ask is "how can I speed up my builds?".
The answer depends on your code base. Tell us about it.
Are there many translation units in the code base?
Is there much template code?
What is the approximate size of the code base?
How long are your builds taking?
I've activated multi-core compilation with my i5-2400 in the past and the time saving using all 4 cores is not that great. The bottlenecks were RAM and HD access speeds. SSD, just so you know.
MBozzi I have only one source code file, I was readings here what means translation units, therefore if I have only one source code file I have only one translation unit right?
I do not use templates, the code now has approximately 1900 lines, so many for's loops, 13 functions, creating approximately 20 text files, and many includes, including OpenCV library. I am doing changes in 120 images from a folder. This is taking near 2 hours.
FurryGuy I made the tutorial that you sent (thank you!), I rebuild and debug but still being shown that only 25% of the processors are being used.
> I am doing changes in 120 images from a folder. This is taking near 2 hours.
You're confusing "building" with "running".
Compiling 1900 lines in a single source file would take a few seconds at most, and you're done.
The thing that's taking 2 hours is your program working it's way through 120 images (which seems a lot like one image per minute). I'm guessing you just press the green "go" arrow to run the program directly in visual studio.
How does your program find these images to process?
- you have a list in a text file
- you tell the program a directory, and it finds all the files for you
- you tell the program a list of files on the command line
Let's say you pass a directory on the command line, using a prompt in a console window myprog.exe path\to\images
This is a crude proof of concept, it requires basically zero modifications to your code and minimal changes to how you organise and run the program.
What you could do easily is separate your images into two equal folders
Then you can do this myprog.exe path\to\images1
And in another console window, do this myprog.exe path\to\images2
You're now running the program twice, you should see the total CPU usage now 50% and the job is done when you get back from lunch.
If this works, there are things you can do in the program to improve things automatically.
Yes Salem C you are right I pass the directory via prompt.
Your suggestion is a very good idea, but I think that the problem is when I create the release version this may will not be able to be done comfortably for the user.
At this moment I am reading some articles about Parallelism and other kinds of optimizations as Registers (looks do not work nowadays in C++17), Macros etc.
So in your main, I imagine a loop which opens a directory and extracts one file at a time to then pass that filename onto some function which does all the work.
1 2 3
for ( filename in dir ) {
process(filename);
}
You might do something like this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
constint maxt = 4; // how many cores you have
std::thread t[maxt];
int nt = 0;
for ( filename in dir ) {
// process each filename on a separate thread
// which should end up on a separate core
t[nt++] = std::thread(process,filename);
if ( nt == maxt ) {
// maxed out the cores, now wait for them to finish
// this batch of files
for ( int r = 0 ; r < nt ; r++ ) {
t[i].join();
}
nt = 0;
}
}
OpenCV itself seems to be thread safe, but some things aren't
- global variables that are written to
- any standard container like vector, list
- writing to the same open file
But if you've never tried to write thread-safe code before, chances are you've messed up somewhere, and all you'll get are crashes / hangs / garbage results.
Separating the work on the command line and running separate processes is falling off a log easy by comparison. https://www.cplusplus.com/forum/beginner/276731/#msg1194275
Processes are completely isolated and independent of one another.