Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enable the /MP MSVC compiler argument for parallel compilation #1846

Merged

Conversation

imrichardcole
Copy link
Contributor

@imrichardcole imrichardcole commented Sep 5, 2024

As detailed here:

https://learn.microsoft.com/en-us/cpp/build/reference/mp-build-with-multiple-processes?view=msvc-170

This enables parallel compilation. The build generator can already parallelise at the project level i.e -j10 for example, but this enables parallel compilation.

On my machine, times without this change:

Debug = 32.02s
Release = 34.99s

And times with:

Debug = 11.30s
Release = 12.88s

@LebedevRI
Copy link
Collaborator

Does the build generator not produce one compiler invocation per source file?
https://learn.microsoft.com/en-us/cpp/build/reference/mp-build-with-multiple-processes?view=msvc-170#number-of-processes

@imrichardcole
Copy link
Contributor Author

imrichardcole commented Sep 5, 2024

@LebedevRI it won't spawn more than there are source files, but /MP on it's own is driven by the number of logical cores on the host machine:

Or you can use the default, which is equal to the number of effective processors in a computer, if you omit the processMax argument.

It can be limited by adding a number e.g /MP2. What we normally do with this setting is work out the number of cores on the machine, then subtract 2. I can implement that here if required.

@LebedevRI
Copy link
Collaborator

My question is: is that generator effectively always in Unity build / jumbo build mode?
I don't understand why this would help otherwise.

But i'll leave this for @dmah42. It's probably fine i guess.

@imrichardcole
Copy link
Contributor Author

An example might be easier.

Assume we have a VS solution with three projects, each one builds an exe and there are no dependencies between them. If we pass -j3 at the cmake build stage, then MSBuild will attempt to build all three projects in parallel.

If each project has 10 cpp files, without /MP each source will be built in a serial fashion i.e we'd use 3 cores. By specifying /MP you can then enable parallel compilation. So if we had a machine with 30 cores, in theory the above example would use all of them (3 projects x 10 source files each)

Hope that clarifies?

@LebedevRI
Copy link
Collaborator

I see. Sounds like the solution is to use a better generator (ninja?).

@dmah42
Copy link
Member

dmah42 commented Sep 5, 2024

ninja might be a solution but if folks want to use msbuild this seems like a healthy improvement.

@dmah42 dmah42 merged commit 08fdf6e into google:main Sep 5, 2024
80 checks passed
@imrichardcole
Copy link
Contributor Author

You raise an interesting point about ninja. By using a tracing python package to visualize .ninja_log into a flame graph, it still seems to be serial:

image

I'll take a look at this later today as I had also assumed /MP was redundant when using ninja.

@LebedevRI
Copy link
Collaborator

Huh, that sounds weird. ninja really isn't serial even if you don't specify -j<N>.

@imrichardcole imrichardcole deleted the task/use-parallel-compilation-with-msvc branch October 22, 2024 08:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants