2828from time import sleep
2929from typing import Any , Callable , Dict , Optional
3030
31- from tqdm .auto import tqdm # type: ignore
31+ from tqdm .auto import tqdm
3232
3333from cmdstanpy import _DOT_CMDSTAN , _DOT_CMDSTANPY
34-
3534from cmdstanpy .utils import (
3635 cmdstan_path ,
3736 do_command ,
@@ -94,7 +93,7 @@ def clean_all(verbose: bool = False) -> None:
9493 raise CmdStanInstallError (f'Command "make clean-all" failed\n { str (e )} ' )
9594
9695
97- def build (verbose : bool = False , progress : bool = True ) -> None :
96+ def build (verbose : bool = False , progress : bool = True , cores : int = 1 ) -> None :
9897 """
9998 Run command ``make build`` in the current directory, which must be
10099 the home directory of a CmdStan version (or GitHub repo).
@@ -107,8 +106,10 @@ def build(verbose: bool = False, progress: bool = True) -> None:
107106 Default is ``False``.
108107 :param progress: Boolean value; when ``True`` display progress progress bar.
109108 Default is ``True``.
109+ :param cores: Integer, number of cores to use in the ``make`` command.
110+ Default is 1 core.
110111 """
111- cmd = [MAKE , 'build' ]
112+ cmd = [MAKE , 'build' , f'-j { cores } ' ]
112113 try :
113114 if verbose :
114115 do_command (cmd )
@@ -201,19 +202,26 @@ def compile_example(verbose: bool = False) -> None:
201202 raise CmdStanInstallError (f'Command "make clean-all" failed\n { e } ' )
202203
203204
204- def rebuild_cmdstan (verbose : bool = False , progress : bool = True ) -> None :
205+ def rebuild_cmdstan (
206+ verbose : bool = False , progress : bool = True , cores : int = 1
207+ ) -> None :
205208 """
206209 Rebuilds the existing CmdStan installation.
207210 This assumes CmdStan has already been installed,
208211 though it need not be installed via CmdStanPy for
209212 this function to work.
210213
211214 :param verbose: Boolean value; when ``True``, show output from make command.
215+ Default is ``False``.
216+ :param progress: Boolean value; when ``True`` display progress progress bar.
217+ Default is ``True``.
218+ :param cores: Integer, number of cores to use in the ``make`` command.
219+ Default is 1 core.
212220 """
213221 try :
214222 with pushd (cmdstan_path ()):
215223 clean_all (verbose )
216- build (verbose , progress )
224+ build (verbose , progress , cores )
217225 compile_example (verbose )
218226 except ValueError as e :
219227 raise CmdStanInstallError (
@@ -222,7 +230,11 @@ def rebuild_cmdstan(verbose: bool = False, progress: bool = True) -> None:
222230
223231
224232def install_version (
225- cmdstan_version : str , overwrite : bool = False , verbose : bool = False
233+ cmdstan_version : str ,
234+ overwrite : bool = False ,
235+ verbose : bool = False ,
236+ progress : bool = True ,
237+ cores : int = 1 ,
226238) -> None :
227239 """
228240 Build specified CmdStan version by spawning subprocesses to
@@ -245,7 +257,7 @@ def install_version(
245257 )
246258 clean_all (verbose )
247259 print ('Rebuilding version {}' .format (cmdstan_version ))
248- build (verbose )
260+ build (verbose , progress = progress , cores = cores )
249261 print ('Test model compilation' )
250262 compile_example (verbose )
251263 print ('Installed {}' .format (cmdstan_version ))
@@ -418,6 +430,12 @@ def parse_cmdline_args() -> Dict[str, Any]:
418430 action = 'store_true' ,
419431 help = "flag, when specified show progress bar for CmdStan download" ,
420432 )
433+ parser .add_argument (
434+ "--cores" ,
435+ default = 1 ,
436+ type = int ,
437+ help = "number of cores to use while building" ,
438+ )
421439 if platform .system () == 'Windows' :
422440 # use compiler installed with install_cxx_toolchain
423441 # Install a new compiler if compiler not found
@@ -520,6 +538,8 @@ def main(args: Dict[str, Any]) -> None:
520538 cmdstan_version = cmdstan_version ,
521539 overwrite = args ['overwrite' ],
522540 verbose = args ['verbose' ],
541+ progress = progress ,
542+ cores = args ['cores' ],
523543 )
524544 except RuntimeError as e :
525545 print (e )
0 commit comments