ATLAS Offline Software
Loading...
Searching...
No Matches
LocalDriver.cxx File Reference
#include <EventLoop/LocalDriver.h>
#include <sstream>
#include <TSystem.h>
#include <AsgMessaging/StatusCode.h>
#include <EventLoop/Job.h>
#include <EventLoop/ManagerData.h>
#include <EventLoop/MessageCheck.h>
#include <RootCoreUtils/Assert.h>
#include <RootCoreUtils/ShellExec.h>
#include <mutex>
#include <thread>

Go to the source code of this file.

Functions

 ClassImp (EL::LocalDriver) namespace EL

Function Documentation

◆ ClassImp()

ClassImp ( EL::LocalDriver )
Author
Nils Krumnack

Definition at line 29 of file LocalDriver.cxx.

32{
33 void LocalDriver ::
34 testInvariant () const
35 {}
36
37
38
39 LocalDriver ::
40 LocalDriver ()
41 {
42 RCU_NEW_INVARIANT (this);
43 }
44
45
46
47 ::StatusCode LocalDriver ::
48 doManagerStep (Detail::ManagerData& data) const
49 {
50 RCU_READ_INVARIANT (this);
51 using namespace msgEventLoop;
52 ANA_CHECK (BatchDriver::doManagerStep (data));
53 switch (data.step)
54 {
55 case Detail::ManagerStep::batchScriptVar:
56 {
57 data.batchSkipReleaseSetup = true;
58 }
59 break;
60
61 case Detail::ManagerStep::submitJob:
62 case Detail::ManagerStep::doResubmit:
63 {
64 // safely ignoring: resubmit
65
66 const std::string dockerImage {
67 data.options.castString(Job::optDockerImage)};
68 const std::string dockerOptions {
69 data.options.castString(Job::optDockerOptions)};
70 int numParallelProcs
71 = data.options.castDouble (Job::optNumParallelProcs, 1);
72 if (numParallelProcs < 0)
73 {
74 ANA_MSG_ERROR ("invalid number of parallel processes: " << numParallelProcs);
75 return StatusCode::FAILURE;
76 }
77
78 std::ostringstream basedirName;
79 basedirName << data.submitDir << "/tmp";
80 if (!data.resubmit)
81 {
82 if (gSystem->MakeDirectory (basedirName.str().c_str()) != 0)
83 {
84 ANA_MSG_ERROR ("failed to create directory " << basedirName.str());
85 return StatusCode::FAILURE;
86 }
87 }
88 auto submitSingle = [&] (std::size_t index) noexcept -> StatusCode
89 {
90 try
91 {
92 std::ostringstream dirName;
93 dirName << basedirName.str() << "/" << index;
94 if (gSystem->MakeDirectory (dirName.str().c_str()) != 0)
95 {
96 ANA_MSG_ERROR ("failed to create directory " + dirName.str());
97 return StatusCode::FAILURE;
98 }
99
100 std::ostringstream cmd;
101 cmd << "cd " << dirName.str() << " && ";
102 if (!dockerImage.empty())
103 cmd << "docker run --rm -v " << RCU::Shell::quote (data.submitDir) << ":" << RCU::Shell::quote (data.submitDir) << " " << dockerOptions << " " << dockerImage << " ";
104 cmd << RCU::Shell::quote (data.submitDir) << "/submit/run " << index;
105 RCU::Shell::exec (cmd.str());
106 } catch (std::exception& e)
107 {
108 ANA_MSG_ERROR ("exception in job " << index << ": " << e.what());
109 return StatusCode::FAILURE;
110 }
111 return StatusCode::SUCCESS;
112 };
113 if (numParallelProcs == 1)
114 {
115 for (std::size_t index : data.batchJobIndices)
116 {
117 if (submitSingle (index).isFailure())
118 return StatusCode::FAILURE;
119 }
120 } else
121 {
122 if (numParallelProcs == 0)
123 numParallelProcs = std::thread::hardware_concurrency();
124 if (numParallelProcs > int (data.batchJobIndices.size()))
125 numParallelProcs = data.batchJobIndices.size();
126 std::vector<std::thread> threads;
127 std::mutex mutex;
128 auto indexIter = data.batchJobIndices.begin();
129 bool abort = false;
130 while (threads.size() < unsigned (numParallelProcs))
131 {
132 threads.emplace_back ([&] () noexcept
133 {
134 std::unique_lock<std::mutex> lock (mutex);
135 while (indexIter != data.batchJobIndices.end() && !abort)
136 {
137 auto myindex = *indexIter;
138 ++ indexIter;
139 lock.unlock();
140 if (submitSingle (myindex).isFailure())
141 {
142 abort = true;
143 return;
144 }
145 lock.lock ();
146 }
147 });
148 }
149 for (auto& thread : threads)
150 thread.join();
151 if (abort)
152 return StatusCode::FAILURE;
153 }
154 data.submitted = true;
155 }
156 break;
157
158 default:
159 break;
160 }
161 return ::StatusCode::SUCCESS;
162 }
163}
#define RCU_NEW_INVARIANT(x)
Definition Assert.h:221
#define RCU_READ_INVARIANT(x)
Definition Assert.h:217
#define ANA_MSG_ERROR(xmsg)
Macro printing error messages.
#define ANA_CHECK(EXP)
check whether the given expression was successful
virtual void lock()=0
Interface to allow an object to lock itself when made const in SG.
STL class.
void exec(const std::string &cmd)
effects: execute the given command guarantee: strong failures: out of memory II failures: system fail...
Definition ShellExec.cxx:27
std::string quote(const std::string &name)
effects: quote the given name to protect it from the shell returns: the quoted name guarantee: strong...
Definition ShellExec.cxx:65
Definition index.py:1