# Parallel canvas generation and saving

**URL:** <https://root-forum.cern.ch/t/parallel-canvas-generation-and-saving/32658>\
**Category:** ROOT\
**Tags:** graphics, io\
**Created:** [February 8, 2019, 7:04pm UTC](https://root-forum.cern.ch/t/parallel-canvas-generation-and-saving/32658 "2019-02-08T19:04:40Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ahesam](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ahesam/32/19455_2.png) [@ahesam](https://root-forum.cern.ch/u/ahesam)\
**Post date:** [February 8, 2019, 7:04pm UTC](https://root-forum.cern.ch/t/parallel-canvas-generation-and-saving/32658/1 "2019-02-08T19:04:41Z")

</div>

\_ROOT Version:\_6.13/08  
\_Platform:\_Ubuntu 16.04  
\_Compiler:\_gcc-5.5.0

Hello,

I have a bunch of histograms that I want to draw on a canvas (one histogram per canvas) and save to an image file on disk. At the moment I do this sequentially and that works fine. However, I was hoping to speed this up a little, by writing to file in parallel. Is there any way this can be done “natively” with ROOT? I couldn’t find anything relevant, other than “parallel write” being a future plan in ROOT development.

I tried the following myself with OpenMP:

[parallel\_write.cc](https://root-forum.cern.ch/uploads/default/original/3X/2/0/20c12a6f5f9ed4ebf37ff6a7b3a05d1dce8daa55.cc) (1.1 KB)

Compilation command:

```auto
g++ parallel_write.cc -o parallel_write `root-config --cflags --libs` -fopenmp

```

This segfaults like such:

```auto
***Error in `./parallel_write': double free or corruption (fasttop): 0x00007f77a4000cb0***

```

Any help is appreciated.

Ahmad

---

<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:** [February 8, 2019, 10:56pm UTC](https://root-forum.cern.ch/t/parallel-canvas-generation-and-saving/32658/2 "2019-02-08T22:56:56Z")

</div>

Hi,  
Is TBufferMerger fitting the bill?

---

<div class="post-metadata">

**Author:** ![sbinet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/sbinet/32/13166_2.png) [@sbinet](https://root-forum.cern.ch/u/sbinet)\
**Post date:** [February 8, 2019, 11:11pm UTC](https://root-forum.cern.ch/t/parallel-canvas-generation-and-saving/32658/3 "2019-02-08T23:11:00Z")

</div>

AFAICT, the OP isn’t interested in writing to a root file in parallel, but creating PNG files in parallel.

---

<div class="post-metadata">

**Author:** ![ahesam](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ahesam/32/19455_2.png) [@ahesam](https://root-forum.cern.ch/u/ahesam)\
**Post date:** [February 9, 2019, 8:38am UTC](https://root-forum.cern.ch/t/parallel-canvas-generation-and-saving/32658/4 "2019-02-09T08:38:54Z")

</div>

Yes exactly @sbinet. In my example code I let each omp thread create a canvas, and call hist-\>Draw() on one of the N histograms in a vector of histograms, and Print() to a .png file with a unique name (resulting in N .png files).

---

<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:** [February 9, 2019, 10:29am UTC](https://root-forum.cern.ch/t/parallel-canvas-generation-and-saving/32658/5 "2019-02-09T10:29:57Z")

</div>

I see.  
Then it comes down to whether this code can be executed in parallel or not:

```auto
TH2S* h = vec[i];
TCanvas *c = new TCanvas(std::to_string(i).c_str());
h->Draw("colz");
std::string name = std::string(h->GetName());
c->Print((name + ".png").c_str());
delete c;

```

It seems not: `gdb` shows there are at least two places that remain thread-unsafe even with `ROOT::EnablelThreadSafety`, in `TApplication` and `TPad` (the problem is not in I/O: parallel writing of different TFile’s is supported in general).

A solution that removes the thread-safety discussion altogether – multi-processing:

```auto
   auto draw = [](TH2S *h) {                                                                                            
      TCanvas *c = new TCanvas((std::string("c_") + h->GetName()).c_str());                                             
      h->Draw("colz");                                                                                                  
      std::string name = std::string(h->GetName());                                                                     
      c->Print((name + ".png").c_str());                                                                                
      delete c;                                                                                                         
      return 0;                                                                                                         
   };                                                                                                                   
                                                                                                                        
   ROOT::TProcessExecutor e(4);                                                                                         
   e.Map(draw, vec);

```

This should work, and might give you a good speed-up depending on your actual application: `TProcessExecutor` forks ROOT and executes that lambda function in a different process for each argument.

Hope this helps,  
Enrico

---

<div class="post-metadata">

**Author:** ![ahesam](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ahesam/32/19455_2.png) [@ahesam](https://root-forum.cern.ch/u/ahesam)\
**Post date:** [February 9, 2019, 3:32pm UTC](https://root-forum.cern.ch/t/parallel-canvas-generation-and-saving/32658/6 "2019-02-09T15:32:14Z")

</div>

Thanks Enrico, that’s exactly what I was looking for. It works perfectly.

For 18 histograms (each having 4.8M bins), this resulted in a speedup of about 13X using 18 threads (on a 16 core machine), compared to a single-thread execution.

Good stuff 😃

Ahmad

Edit: mentioned speedup is for the parallel part. Overall speedup of application including serial part is about 9X.

---

<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:** [February 23, 2019, 3:32pm UTC](https://root-forum.cern.ch/t/parallel-canvas-generation-and-saving/32658/7 "2019-02-23T15:32:26Z")

</div>

Closed on February 23, 2019, 3:32pm UTC

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