From d0c6a3ba14ecc66174e638c59c86f2a84c8cc0df Mon Sep 17 00:00:00 2001 From: fis Date: Wed, 20 Oct 2021 17:35:00 +0800 Subject: [PATCH 1/2] Avoid omp reduction in rank metric. --- src/metric/rank_metric.cc | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/metric/rank_metric.cc b/src/metric/rank_metric.cc index 193938c0f8e6..470b6cc1aac0 100644 --- a/src/metric/rank_metric.cc +++ b/src/metric/rank_metric.cc @@ -192,24 +192,27 @@ struct EvalRank : public Metric, public EvalRankConfig { } } + CHECK(tparam_); + std::vector sum_tloc(tparam_->Threads(), 0.0); + if (!rank_gpu_ || tparam_->gpu_id < 0) { const auto &labels = info.labels_.ConstHostVector(); const auto &h_preds = preds.ConstHostVector(); dmlc::OMPException exc; - #pragma omp parallel reduction(+:sum_metric) +#pragma omp parallel num_threads(tparam_->Threads()) { exc.Run([&]() { // each thread takes a local rec PredIndPairContainer rec; - #pragma omp for schedule(static) +#pragma omp for schedule(static) for (bst_omp_uint k = 0; k < ngroups; ++k) { exc.Run([&]() { rec.clear(); for (unsigned j = gptr[k]; j < gptr[k + 1]; ++j) { rec.emplace_back(h_preds[j], static_cast(labels[j])); } - sum_metric += this->EvalGroup(&rec); + sum_tloc[omp_get_thread_num()] += this->EvalGroup(&rec); }); } }); @@ -217,6 +220,8 @@ struct EvalRank : public Metric, public EvalRankConfig { exc.Rethrow(); } + sum_metric = std::accumulate(sum_tloc.cbegin(), sum_tloc.cend(), 0.0); + if (distributed) { bst_float dat[2]; dat[0] = static_cast(sum_metric); From 61cbae96d954bc1e851a9f2e11307a64de93d57a Mon Sep 17 00:00:00 2001 From: fis Date: Thu, 21 Oct 2021 13:53:31 +0800 Subject: [PATCH 2/2] Fix. --- src/metric/rank_metric.cc | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/metric/rank_metric.cc b/src/metric/rank_metric.cc index 470b6cc1aac0..797a1d54ac84 100644 --- a/src/metric/rank_metric.cc +++ b/src/metric/rank_metric.cc @@ -217,11 +217,10 @@ struct EvalRank : public Metric, public EvalRankConfig { } }); } + sum_metric = std::accumulate(sum_tloc.cbegin(), sum_tloc.cend(), 0.0); exc.Rethrow(); } - sum_metric = std::accumulate(sum_tloc.cbegin(), sum_tloc.cend(), 0.0); - if (distributed) { bst_float dat[2]; dat[0] = static_cast(sum_metric);