Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically setting jobs (-j) flag for a multicore machine?

I have a Makefile on a machine that has a ton of cores in it, but I always seem to forget to write -jX when compiling my project and it takes way longer than it should.

Is there some way I can set the -j flag through an environment variable or some other persistent config file so that make will automatically execute multiple jobs in parallel on this machine?

like image 547
KarateSnowMachine Avatar asked Jan 24 '11 03:01

KarateSnowMachine


People also ask

What is J flag in make?

The -j flag tells the make command simultaneously to run independent targets. If you follow the -j option with an integer, then the integer specifies the maximum number of concurrent jobs that can be executed to build the targets.

Does GCC use multiple cores?

Currently when passing "-flto" for enabling Link-Time Optimizations with the GCC compiler, it defaults to using a single core/thread for carrying out the optimizations and code generation.


1 Answers

I'm assuming you're using Linux. This is from my ~/.bashrc

# parallel make export NUMCPUS=`grep -c '^processor' /proc/cpuinfo` alias pmake='time nice make -j$NUMCPUS --load-average=$NUMCPUS' 

sample usage

samm@host src> echo $NUMCPUS 8 samm@host src> pmake 

becomes time nice make -j8 --load-average=8.

To answer your specific question about putting this into a Makefile, I don't find it practical to sprinkle this logic all over my Makefiles. Putting this into a top level Makefile also isn't a great solution since I often build from sub-directories and wish to build them in parallel as well. However, if you have a fairly flat source hierarchy, it may work for you.

like image 118
Sam Miller Avatar answered Sep 22 '22 23:09

Sam Miller