载入中...
搜索中...
未找到
CrowdScheduler.cpp
浏览该文件的文档.
2
3namespace eve::crowd {
4Result<void> Crowd::setWorkScheduler(CrowdExecution execution, std::shared_ptr<CrowdWorkScheduler> scheduler) {
5 if (impl_->budgetActive)
7 Diagnostic::error(DiagnosticCode::Conflict, "Cannot configure an active Crowd", "scheduler"));
8 if ((execution != CrowdExecution::Serial && execution != CrowdExecution::Parallel) ||
9 (execution == CrowdExecution::Parallel && !scheduler))
11 "Parallel execution requires a scheduler lease", "scheduler"));
12 impl_->execution = execution;
13 impl_->workScheduler = execution == CrowdExecution::Parallel ? std::move(scheduler) : nullptr;
14 return Result<void>::success();
15}
16
17void Crowd::Impl::dispatchWork(std::size_t items, const std::function<void(std::size_t, std::size_t)>& body) {
18 constexpr std::size_t grain = 256;
19 const int partitions = int(items / grain + (items % grain != 0));
20 const auto partition = [&](int index) {
21 const auto first = std::size_t(index) * grain;
22 body(first, std::min(items, first + grain));
23 };
25 for (int index = 0; index < partitions; ++index) partition(index);
26 return;
27 }
28 auto result = workScheduler->execute(partitions, partition);
29 if (!result) throw SchedulerFailure{result.status()};
30}
31} // namespace eve::crowd
float grain
std::int32_t first
Poco::JSON::Object::Ptr result
std::string body
uint32_t index
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
Move-only operation result carrying either a value or Status.
Definition Result.h:176
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:185
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
Result< void > setWorkScheduler(CrowdExecution execution, std::shared_ptr< CrowdWorkScheduler > scheduler)
Select serial execution or inject a shared synchronous scheduler lease.
CrowdExecution
Explicit execution selection; serial does not require a provider.
CrowdExecution execution
std::shared_ptr< CrowdWorkScheduler > workScheduler
void dispatchWork(std::size_t items, const std::function< void(std::size_t, std::size_t)> &body)