Empty map after using task parallelization


_ROOT Version:6.22/08
_Platform: linuxx8664gcc


Dear colleagues,

A few weeks ago, I learned how to parallelize tasks using the ROOT framework from a forum thread. (https://root-forum.cern.ch/t/parallelization-inside-a-root-macro/54453/5)
But now I face a different problem, and it might be I do not understand all the logic behind it.

Here a piece of my code:

std::map<std::string, std::unique_ptr<TH2D>> h2_;
std::vector<int> sts_address = {0x10018012, 0x10008012};
void BookHistograms(){

    auto createHisto = [](int module_i){
        int NbOfTimeBins = 2.*m_timeWindows / dClockCycle + 1;
        for (unsigned int i = 0; i < 16; i++){
            std::string name = Form("0x%x_ASIC_%u", module_i, i);
            LOG(info) << Form("Creating %s", name.c_str());
            h2_[name] = std::make_unique<TH2D>(name.c_str(), name.c_str(),
                NbOfTimeBins, -m_timeWindows-1.5625, m_timeWindows+1.5625,
                31, 1, 32);
        }
        return 1;
    };
    gROOT->SetBatch();
    ROOT::TProcessExecutor workers(sts_address.size());

    workers.Map(createHisto, sts_address);
    LOG(info) << h2_.size();
}

the problem I have is that after calling BookHistograms(), the print out for the line LOG(info) << h2_.size();, is

> 0

So, what am I missing here?

I just realized h2_it might being cloned by the lambda, but I cannot pass h2_ as a reference cause then I got :

error: 'h2_' cannot be captured because it does not have automatic storage duration
    auto createHisto = [&h2_](int module_i){

Any idea how to solve this in general?

The standard does not require operator[] to be data race free for std::map. And you checked that it is not thread safe.

I would suggest creating histograms in memory in parallel, but filling the container in one thread.

Good luck.

Hi @Dario_Ramirez,

Is it perhaps possible that you rely on RDataFrame for your analysis? The free bonus using it is that it automatically takes care of parallelizing the work for you. ROOT 6.22 already features RDataFrame, but I definitely suggest you move to the latest release 6.28, as a lot of performance improvements were added.

Feel free to ask for any help with it.

Cheers,
Vincenzo

Hi @vpadulan, Thanks for the advice; I will try to learn how to move all my analysis to this framework.
It will take time, so probably any issue I struggle with, will be a new forum thread :slight_smile:

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