39

I am using \write18 enabled (or --shell-escape) with externalization to compile a bunch of PGFplots. The first run takes much time, and I noticed that pdflatex only runs on one CPU core. Is there any way to have pdflatex use multiple cores? (at least when \write18 is enabled)

M.Reza
  • 3,237
  • 12
    Since you explicitly mentioned the use of external: you can configure the external library to run in mode=list and make. The resulting makefile can be processed in parallel using make -j 8 -f <file>.makefile (in this case on 8 threads). Only feasible if you can resort to make, though... – Christian Feuersänger Oct 06 '13 at 14:12
  • 3
    Note that the keyword "external library" means that this question is no duplicate. – Christian Feuersänger Oct 06 '13 at 14:13
  • What OS are you using? At least on UNIX-like systems it may be enough to end the command line passed to \write18 with an ampersand character (&) to have the shell spawn a new process for the external command. – Daniel Nov 21 '13 at 13:20
  • 1
    Could you give a minimal working example (MWE) that illustrates your problem, in particular, allows us to experiment with your method of "compiling a bunch of PGFplots"? – Stephan Lehmke Nov 26 '13 at 13:45
  • @ChristianFeuersänger Could you add your comment as an answer? Furthermore, I looked into the code and I think that indeed, it might not be too much work and a nice addition to allow \write18{\pgf@tempa\string&} on systems that support this. The only problem would be not to include the image immediately afterwards, but wait at the end of the first run for all images to finish and then include them in the second run. On systems with several cores, this is probably much faster than waiting for every image to finish. – Stephan Lehmke Dec 03 '13 at 22:15
  • @StephanLehmke sorry, I fear that I have seen your message, but could not react right-away at that time. And than I forgot about it. – Christian Feuersänger Dec 25 '13 at 14:28
  • 1
    @StephanLehmke your feature request with asynchronous processing sounds interesting; but I fear that it would easily run out-of-control unless you restrict the number of background processes. And that, in turn, sounds like a very complicated method in TeX ... compiling twice would be simple. If you want to experiment with the code; feel free to do so. – Christian Feuersänger Dec 25 '13 at 14:30

1 Answers1

19

The externalize in tikz may generated individual picture for every \begin{tikzpicture}...\end{tikzpicture}.

  • \usepgfplotslibrary{external} let all pgf-plots outside of the compile.

Let pdflatex know those pictures are compiled with make.

  • \tikzexternalize[mode=list and make] let tikz to know we would use the make. Because at present only the make support multi-thread.

I'm lazy to set the name for every pgfplots figure, so I let it done by system.

  • external/system call set names for individual picture file, or the pdflatex doesn't know what file name should be set. (Origin code is from package tikz&pgf Manual for Version 2.10 page 345.)

My tex file is named test.tex

    \documentclass{standalone}

    \usepackage{pgfplots} %use tikz based pgfplots
      \usepgfplotslibrary{external}
        \tikzexternalize[mode=list and make]
        \tikzset{external/system call={pdflatex \tikzexternalcheckshellescape
          -halt-on-error -interaction=batchmode -jobname "\image" "\texsource"}
        } % to let pdflatex work
    %% compile picture: pdflatex --shell-escape xxxxxxx.tex

    \begin{document}

    \begin{tikzpicture}
    \begin{axis}[ xlabel=$x$, ylabel={$f(x) = x^2 - x +4$} ] 
      \addplot {x^2 - x +4}; 
    \end{axis} 
    \end{tikzpicture}

    \begin{tikzpicture}
    \begin{loglogaxis}[xlabel=Cost,ylabel=Gain]
      \addplot[color=red,mark=x] coordinates 
      { (10,100) (20,150) (40,225) (80,340) (160,510) (320,765) (640,1150) };
    \end{loglogaxis}
    \end{tikzpicture}

    \end{document}

After first pdflatex compiling, do the make -j 4 -f test.makefile. 4 compiles the pictures with 4 thread.

Then run pdflatex second time.

The option --shell-escape also work fine in my system.

My system is:

  1. Win 7(x64) + Miktex 2.9 + make under cygwin environment
  2. Linux(x64) + Texlive2013 + make
Tawei
  • 1,254
  • 12
  • 15