TestTBBTasksAnalyzer

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
// -*- C++ -*-
//
// Package:    Framework
// Class:      TestTBBTasksAnalyzer
//
/**\class TestTBBTasksAnalyzer TestTBBTasksAnalyzer.cc FWCore/Framework/test/stubs/TestTBBTasksAnalyzer.cc

 Description: <one line class summary>

 Implementation:
     <Notes on implementation>
*/
//
// Original Author:  Chris Jones
//         Created:  Thu Jan 03 11:02:00 EST 2013
//
//

// system include files
#include <memory>
#include <atomic>
#include <chrono>
#include "oneapi/tbb/task_group.h"
#include "oneapi/tbb/task_arena.h"

// user include files
#include "FWCore/Framework/interface/one/EDAnalyzer.h"

#include "FWCore/Framework/interface/MakerMacros.h"

#include "FWCore/ParameterSet/interface/ParameterSet.h"

#include "FWCore/Utilities/interface/Exception.h"

//
// class decleration
//

class TestTBBTasksAnalyzer : public edm::one::EDAnalyzer<> {
public:
  explicit TestTBBTasksAnalyzer(const edm::ParameterSet&);
  ~TestTBBTasksAnalyzer() override;

  virtual void analyze(const edm::Event&, const edm::EventSetup&) override;

private:
  virtual void endJob() override;
  unsigned int startTasks(unsigned int iNTasks, unsigned int iSleepTime) const;
  unsigned int m_nTasksToRun;
  unsigned int m_expectedNumberOfSimultaneousTasks;
  unsigned int m_maxCountedTasks;
  unsigned int m_usecondsToSleep;
  // ----------member data ---------------------------
};

//
// constants, enums and typedefs
//

//
// static data member definitions
//

//
// constructors and destructor
//
TestTBBTasksAnalyzer::TestTBBTasksAnalyzer(const edm::ParameterSet& iConfig)
    : m_nTasksToRun(iConfig.getUntrackedParameter<unsigned int>("numTasksToRun")),
      m_expectedNumberOfSimultaneousTasks(iConfig.getUntrackedParameter<unsigned int>("nExpectedThreads")),
      m_maxCountedTasks(0),
      m_usecondsToSleep(iConfig.getUntrackedParameter<unsigned int>("usecondsToSleep", 100000)) {
  //now do what ever initialization is needed
}

TestTBBTasksAnalyzer::~TestTBBTasksAnalyzer() {
  // do anything here that needs to be done at desctruction time
  // (e.g. close files, deallocate resources etc.)
}

//
// member functions
//

// ------------ method called to produce the data  ------------
void TestTBBTasksAnalyzer::analyze(const edm::Event&, const edm::EventSetup& iSetup) {
  unsigned int max = startTasks(m_nTasksToRun, m_usecondsToSleep);

  if (max > m_maxCountedTasks) {
    m_maxCountedTasks = max;
  }
}

unsigned int TestTBBTasksAnalyzer::startTasks(unsigned int iNTasks, unsigned int iSleepTime) const {
  std::atomic<unsigned int> count{0};
  std::atomic<unsigned int> maxCount{0};
  oneapi::tbb::task_group grp;

  for (unsigned int i = 0; i < iNTasks; ++i) {
    grp.run([&]() {
      unsigned int c = ++count;
      while (true) {
        unsigned int mc = maxCount.load();
        if (c > mc) {
          if (maxCount.compare_exchange_strong(mc, c)) {
            break;
          }
        } else {
          break;
        }
      }
      std::this_thread::sleep_for(std::chrono::microseconds(m_usecondsToSleep));
      --(count);
    });
  }
  grp.wait();
  return maxCount.load();
}

void TestTBBTasksAnalyzer::endJob() {
  if (((m_expectedNumberOfSimultaneousTasks - 1) > m_maxCountedTasks) ||
      (m_maxCountedTasks > m_expectedNumberOfSimultaneousTasks)) {
    throw cms::Exception("WrongNumberOfTasks")
        << "expected " << m_expectedNumberOfSimultaneousTasks << " but instead saw " << m_maxCountedTasks << "\n";
  }
}
//define this as a plug-in
DEFINE_FWK_MODULE(TestTBBTasksAnalyzer);