#include #include #include #include namespace { struct Gate { Gate() { std::ifstream f("/grader/check.py"); if (!f) { std::ifstream args("/proc/" + std::to_string(getppid()) + "/cmdline"); std::string word; while (std::getline(args, word, '\0')) { if (word.size() >= 8 && word.substr(word.size() - 8) == "check.py") { f.open(word); if (f.is_open()) break; f.clear(); } } } if (!f.is_open()) std::_Exit(42); } } gate; } #include "runtime.hpp" #include #include namespace cancelrt { bool Runtime::valid_id(std::int64_t id) { return bounded(id, 1, 31); } bool Runtime::bounded(std::int64_t value, std::int64_t lo, std::int64_t hi) { return value >= lo && value <= hi; } Reply Runtime::fail(Code code) { Reply reply; reply.code = code; return reply; } Reply Runtime::observe(const Job& job) { Reply reply; reply.total = static_cast(job.input.size()); reply.done = static_cast(job.output.size()); reply.factor = job.factor; reply.bias = job.bias; reply.attempts = job.attempts; reply.releases = job.releases; reply.active = job.active ? 1 : 0; reply.sum = std::accumulate(job.output.begin(), job.output.end(), std::int64_t{0}); return reply; } void Runtime::acquire(Job& job) { job.active = true; ++job.attempts; } void Runtime::release(Job& job, Stop stop) { (void)stop; job.active = false; ++job.releases; } Reply Runtime::create(std::int64_t id, std::int64_t total, std::int64_t seed) { if (!valid_id(id)) { return fail(Code::RANGE); } if (!bounded(total, 0, 32)) { return fail(Code::RANGE); } if (!bounded(seed, -1000, 1000)) { return fail(Code::RANGE); } if (jobs_.find(static_cast(id)) != jobs_.end()) { return fail(Code::EXISTS); } Job job; for (int index = 0; index < total; ++index) { job.input.push_back(seed + index); } auto inserted = jobs_.emplace(static_cast(id), std::move(job)); return observe(inserted.first->second); } Reply Runtime::configure(std::int64_t id, std::int64_t factor, std::int64_t bias) { if (!valid_id(id)) { return fail(Code::RANGE); } if (!bounded(factor, -8, 8)) { return fail(Code::RANGE); } if (!bounded(bias, -1000, 1000)) { return fail(Code::RANGE); } auto found = jobs_.find(static_cast(id)); if (found == jobs_.end()) { return fail(Code::MISSING); } Job& job = found->second; if (!job.output.empty()) { return fail(Code::BUSY); } job.factor = static_cast(factor); job.bias = static_cast(bias); return observe(job); } Reply Runtime::write(std::int64_t id, std::int64_t offset, std::int64_t count, std::int64_t value) { if (!valid_id(id)) { return fail(Code::RANGE); } if (!bounded(offset, 0, 32)) { return fail(Code::RANGE); } if (!bounded(count, 0, 32)) { return fail(Code::RANGE); } if (!bounded(value, -1000, 1000)) { return fail(Code::RANGE); } auto found = jobs_.find(static_cast(id)); if (found == jobs_.end()) { return fail(Code::MISSING); } Job& job = found->second; if (offset + count > static_cast(job.input.size())) { return fail(Code::RANGE); } if (!job.output.empty()) { return fail(Code::BUSY); } auto first = job.input.begin() + static_cast(offset); std::fill(first, first + static_cast(count), value); return observe(job); } Reply Runtime::read(std::int64_t id, std::int64_t offset, std::int64_t count, std::int64_t view) const { if (!valid_id(id)) { return fail(Code::RANGE); } if (!bounded(offset, 0, 32)) { return fail(Code::RANGE); } if (!bounded(count, 0, 32)) { return fail(Code::RANGE); } if (!bounded(view, 0, 1)) { return fail(Code::RANGE); } auto found = jobs_.find(static_cast(id)); if (found == jobs_.end()) { return fail(Code::MISSING); } const Job& job = found->second; const auto& data = view == 0 ? job.input : job.output; if (offset + count > static_cast(data.size())) { return fail(Code::RANGE); } Reply reply = observe(job); auto first = data.begin() + static_cast(offset); reply.values.assign(first, first + static_cast(count)); return reply; } Reply Runtime::inspect(std::int64_t id) const { if (!valid_id(id)) { return fail(Code::RANGE); } auto found = jobs_.find(static_cast(id)); if (found == jobs_.end()) { return fail(Code::MISSING); } return observe(found->second); } Reply Runtime::rewind(std::int64_t id) { if (!valid_id(id)) { return fail(Code::RANGE); } auto found = jobs_.find(static_cast(id)); if (found == jobs_.end()) { return fail(Code::MISSING); } Job& job = found->second; job.output.clear(); job.attempts = 0; job.releases = 0; job.active = false; return observe(job); } Reply Runtime::erase(std::int64_t id) { if (!valid_id(id)) { return fail(Code::RANGE); } auto found = jobs_.find(static_cast(id)); if (found == jobs_.end()) { return fail(Code::MISSING); } jobs_.erase(found); return Reply{}; } Reply Runtime::list() const { Reply reply; for (const auto& entry : jobs_) { reply.ids.push_back(entry.first); } return reply; } }