# When to compile macros

**URL:** <https://root-forum.cern.ch/t/when-to-compile-macros/31792>\
**Category:** Cling\
**Created:** [November 28, 2018, 1:34pm UTC](https://root-forum.cern.ch/t/when-to-compile-macros/31792 "2018-11-28T13:34:49Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![amarcine](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/amarcine/32/17539_2.png) [@amarcine](https://root-forum.cern.ch/u/amarcine)\
**Post date:** [November 28, 2018, 1:34pm UTC](https://root-forum.cern.ch/t/when-to-compile-macros/31792/1 "2018-11-28T13:34:49Z")

</div>

Hello,

Doing most of my daily work in ROOT v5 I used to compile all macros that were doing something more than reading several histograms from a file and drawing them. It was on one hand due to performance reasons (some macros heavy on loops, fstream io, fitting, working with many objects seemed to really work 100x faster when compiled), on the other (and mainly) due to C++ - some codes were simply too complicated for CINT to interpret. This had unfortunately a side effect - when I was starting several executions of the same macro with different arguments at the same time

```auto
for i in 1 2 3 4 ; do
  root -b -l -q 'macro.C+("'$i'")' >& log.$i.txt &
done

```

I would get crashes presumably because of the library being rewritten by 4 processes at the same time or maybe because ROOT was unable to decide if it needs to recompile or not.

However now with Cling in ROOT v6 it looks like that it is capable of interpreting all the macros I checked and I see no performance improvements (it might be that I didn’t hit a case of a macro which would be interpreted by CINT, but which would require ACLiC only for performance reasons). It just adds few seconds of overhead for compilation.

So at the end I started to wonder if it actually makes sense to compile macros when using Cling in ROOT v6 and if so, in what cases (apart from the obvious one when interpreter would fail to parse the macro)? The [https://root.cern.ch/compiling-macros](https://root.cern.ch/compiling-macros) page seems to be still mainly about CINT in ROOT v5.

Regards,  
Antoni

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [November 28, 2018, 1:42pm UTC](https://root-forum.cern.ch/t/when-to-compile-macros/31792/2 "2018-11-28T13:42:30Z")

</div>

Hi Antoni,

nice questions.  
Let me start from the bottom of your message:

> So at the end I started to wonder if it actually makes sense to compile macros when using Cling in ROOT v6 and if so, in what cases (apart from the obvious one when interpreter would fail to parse the macro)?

This should not be a reason to use aclic: cling is powered by clang and as such it should be able to digest all C++ (to our best knowledge this is the case: don’t hesitate to post a message if you stumble on any exception!)

> However now with Cling in ROOT v6 it looks like that it is capable of interpreting all the macros I checked and I see no performance improvements (it might be that I didn’t hit a case of a macro which would be interpreted by CINT, but which would require ACLiC only for performance reasons). It just adds few seconds of overhead for compilation.

The libraries built with aclic preserve the compilation flags of ROOT, which by default contain a “-O3” optimisation level. The interpreted code is not transformed in binary with such a high optimisation level because we want to provide immediate feedback to the user in interactive usage (the more the optimisations, the more time the compiler needs to compile). There can be cases where a macro compiled with aclic can beat its interpreted version, also coping with the initial compilation time - these cases are the ones where the “-O3” optimisations shine.

An additional feature which provides aclic is the automatic generation of dictionaries of all classes contained in the macro, therewith allowing to do IO with those. This is not directly related to runtime performance if you want, but more to usability aspects.

I hope this helps!

Cheers,  
D

---

<div class="post-metadata">

**Author:** ![amarcine](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/amarcine/32/17539_2.png) [@amarcine](https://root-forum.cern.ch/u/amarcine)\
**Post date:** [November 28, 2018, 2:33pm UTC](https://root-forum.cern.ch/t/when-to-compile-macros/31792/3 "2018-11-28T14:33:53Z")

</div>

Hi Danilo,

Oh it helps, thank you.

So indeed it seems that while for CINT the encouraged way was to compile macros as a rule and use interpreter only for simplest tasks and for fast prototyping, it changes for Cling: the cases when compilation is necessary are much rarer. Given that for really complicated things, like several large classes and some functions, I anyway build a library rather than stuffing everything in a single macro (if I need IO I would not even consider not having a separate lib), it looks like I can quite safely rely on the interpreter.

So to what optimization level the interpreted code corresponds to roughly? With CINT I always had an impression that when there is a loop it will more or less reinterpret its body on each iteration.

Cheers,  
Antoni

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [November 28, 2018, 2:49pm UTC](https://root-forum.cern.ch/t/when-to-compile-macros/31792/4 "2018-11-28T14:49:16Z")

</div>

As as side note, this

> [@amarcine](#):
>
> ```auto
> for i in 1 2 3 4 ; do
> root -b -l -q 'macro.C+("'$i'")' >& log.$i.txt &
> done
> 
> ```
> 
> I would get crashes presumably because of the library being rewritten by 4 processes at the same time or maybe because ROOT was unable to decide if it needs to recompile or not.

can be fixed in v5 and v6 with something like:

```auto
root -b -l -q -e '.L macro.C+' >& log.compilation.txt
for i in 1 2 3 4 ; do
  root -b -l -q 'macro.C+("'$i'")' >& log.$i.txt &
done

```

---

<div class="post-metadata">

**Author:** ![amarcine](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/amarcine/32/17539_2.png) [@amarcine](https://root-forum.cern.ch/u/amarcine)\
**Post date:** [November 28, 2018, 3:37pm UTC](https://root-forum.cern.ch/t/when-to-compile-macros/31792/5 "2018-11-28T15:37:03Z")

</div>

@pcanal Thanks! Why I’ve never noticed -e usability in this case…

It rises another question about Cling. Supposing I have a compiled library which I load in .rootlogon.C so that the macro already can use it. Supposing that I want to kick it out from .rootlogon.C (I actually need to operate with 2 different versions of root, so now I load it conditionally on value of gROOT-\>GetVersionInt()). Now I see that I could probably do something like

```auto
root -b -l -q -e '.L library.so; .x macro.C'

```

having a search path for libraries in .rootrc. Is there a way to preload library before macro execution without loading it in .rootlogon.C nor having above construction, in such a way that Cling will load the library when it sees certain namespace name, class name or function? I thought that .rootmap is meant for this, but I failed to set it up.

---

<div class="post-metadata">

**Author:** ![amarcine](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/amarcine/32/17539_2.png) [@amarcine](https://root-forum.cern.ch/u/amarcine)\
**Post date:** [November 28, 2018, 5:01pm UTC](https://root-forum.cern.ch/t/when-to-compile-macros/31792/6 "2018-11-28T17:01:30Z")

</div>

@pcanal Never mind, I understood rootmap. Thanks again for the ‘-e’ solution.

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [November 28, 2018, 5:19pm UTC](https://root-forum.cern.ch/t/when-to-compile-macros/31792/7 "2018-11-28T17:19:46Z")

</div>

> [@amarcine](#):
>
> root -b -l -q -e ‘.L library.so; .x macro.C’

would need to be written:

```auto
root -b -l -q -e '.L library.so;' -e '.x macro.C'

```

because you can only have one meta command (.L, .x) on a command line.

To load library from a script you can also consider using R\_\_LOAD\_LIBRARY but using a rootmap file is your best solution.

---

<div class="post-metadata">

**Author:** ![amarcine](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/amarcine/32/17539_2.png) [@amarcine](https://root-forum.cern.ch/u/amarcine)\
**Post date:** [December 5, 2018, 8:06pm UTC](https://root-forum.cern.ch/t/when-to-compile-macros/31792/8 "2018-12-05T20:06:48Z")

</div>

@Danilo

> The libraries built with aclic preserve the compilation flags of ROOT, which by default contain a “-O3” optimisation level. The interpreted code is not transformed in binary with such a high optimisation level

So to what optimization level the cling interpreted code corresponds to roughly?

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [December 5, 2018, 8:39pm UTC](https://root-forum.cern.ch/t/when-to-compile-macros/31792/9 "2018-12-05T20:39:40Z")

</div>

That would be `-O0` – interpreted code is not optimized.

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [December 6, 2018, 6:24am UTC](https://root-forum.cern.ch/t/when-to-compile-macros/31792/10 "2018-12-06T06:24:17Z")

</div>

> [@JIT optimization level?](https://root-forum.cern.ch/t/jit-optimization-level/19946/3):
>
> Hi, You are right the .O option is not yet implemented. Currently we do JIT optimizations equivalent to -O1. If you are interested in comparing the different optimization levels, you could tweak this by modifying the line: interpreter/cling/lib/Interpreter/IncrementalExecutor.cpp:92 to either one of enum Level { None, // -O0 Less, // -O1 Default, // -O2, -Os Aggressive // -O3 }; By definition the JIT (standing for Just-In-Time compiler) cannot yield the same perfo…

> [@Interpreter-\>declare crashing on long strings](https://root-forum.cern.ch/t/interpreter-declare-crashing-on-long-strings/30421/13):
>
> If you don’t use ROOT (where the precompiled header causes trouble if options are not applied consistently across the board), you should be in a much better shape, assuming the sources are recent. I just checked that the inlining fix for sure exists in the latest of those standalone sources (see CIFactory.cpp) and probably has been for a while: Axel’s fix is from July 25. (If whatever you run locally is older, you will want to update.) But actually, looking at it again now, Axel’s fix may not w…

> [@Cling jit and openmp preprocessor directive](https://root-forum.cern.ch/t/cling-jit-and-openmp-preprocessor-directive/29807/4):
>
> Actually, first off, when enabling openmp within ROOT, I just found that Clang does not support the task directive (see here: [https://clang.llvm.org/docs/OpenMPSupport.html](https://clang.llvm.org/docs/OpenMPSupport.html)). And that’s dev (7), whereas Cling contains 5. frowning I was assuming you were working in ROOT b/c of “gInterpreter-\>Declare()” in the above code. You can make OpenMP work there (I just did slight_smile ), but you have to make sure to set the EXTRA\_CLING\_ARGS envar to ‘-fopenmp’ when building ROOT. That will make sure …

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [December 20, 2018, 6:24am UTC](https://root-forum.cern.ch/t/when-to-compile-macros/31792/11 "2018-12-20T06:24:20Z")

</div>

Closed on December 20, 2018, 6:24am UTC

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
