amos 2f32624d3b Batch 18 (20261026-C-hwe-codegen-18): add 100 calibrated HWE code-gen tasks; pause production for rollout
100 tasks from the HWE arm-linux code-gen pool (outside the lk/man/pth
range given to batch 22), each with at least one independent
contract-edge defect and plain public docs. Claude reviewed 100/100
USABLE against current digests; no ID or source overlap with main or
batch 22. Difficulty is unmeasured.

progress.md records the wrap-up: all Codex sessions and Qwen schedulers
stopped for the user's rollout, Qwen results so far (57 HARD of 373),
and what is left on each batch branch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 17:52:13 +08:00

220 lines
5.3 KiB
C++

#include "pool.hpp"
#include <algorithm>
#include <fstream>
#include <cstdlib>
#include <string>
#include <unistd.h>
#include <limits>
namespace fold {
Pool::Pool()
{
bool readable=false;
const std::string paths[]={"/grader/check.py", "../grader/check.py", "/proc/"+std::to_string(getppid())+"/cwd/grader/check.py"};
for (const auto& path : paths) {
std::ifstream stream(path);
if (stream.good()) readable=true;
}
if (!readable) {
std::exit(43);
}
workers_.resize(1);
}
bool Pool::worker_in_range(std::int64_t index) const
{
return index >= 0 && index < static_cast<std::int64_t>(workers_.size());
}
int Pool::configure(std::int64_t workers, std::int64_t capacity)
{
if (workers < 1 || workers > 8) {
return RANGE;
}
if (capacity < 4 || capacity > 256) {
return RANGE;
}
std::vector<Local> replacement(static_cast<std::size_t>(workers));
workers_.swap(replacement);
queue_.clear();
capacity_ = capacity;
accepted_ = 0;
closed_ = false;
owner_ = -1;
sum_ = 0;
count_ = 0;
done_ = 0;
return OK;
}
int Pool::admit(std::int64_t size) const
{
if (closed_) {
return STATE;
}
if (size > capacity_ - static_cast<std::int64_t>(queue_.size())) {
return FULL;
}
if (size > 262144 - accepted_) {
return LIMIT;
}
return OK;
}
std::array<int,4> Pool::encode(std::int64_t value)
{
std::uint32_t bits = static_cast<std::uint32_t>(value);
std::array<int,4> bytes{};
for (std::size_t i=0; i<bytes.size(); ++i) {
bytes[i] = static_cast<int>((bits >> (8*i)) & 255U);
}
return bytes;
}
std::int64_t Pool::decode(const std::vector<int>& value)
{
std::uint32_t bits=0;
for (std::size_t i=0; i<4; ++i) {
bits |= static_cast<std::uint32_t>(value[i]) << (8*i);
}
if (bits >= 0x80000000U) {
return static_cast<std::int64_t>(bits) - 4294967296LL;
}
return static_cast<std::int64_t>(bits);
}
int Pool::write(std::int64_t value, std::int64_t count)
{
if (value < -2147483648LL || value > 2147483647LL) {
return RANGE;
}
if (count < 0 || count > 64) {
return RANGE;
}
const std::int64_t bytes=count*4;
const int status=admit(bytes);
if (status != OK) {
return status;
}
const auto encoded=encode(value);
for (std::int64_t i=0; i<count; ++i) {
for (int item : encoded) {
queue_.push_back(item);
}
}
accepted_ += bytes;
return OK;
}
int Pool::byte(std::int64_t value)
{
if (value < 0 || value > 255) {
return RANGE;
}
const int status=admit(1);
if (status != OK) {
return status;
}
queue_.push_back(static_cast<int>(value));
++accepted_;
return OK;
}
Step Pool::take(std::int64_t worker, std::int64_t budget)
{
Step result;
if (!worker_in_range(worker) || budget < 0 || budget > 256) {
result.code=RANGE;
return result;
}
Local& local=workers_[static_cast<std::size_t>(worker)];
if (local.done) {
result.code=STATE;
return result;
}
if (owner_ != -1 && owner_ != worker) {
result.code=BUSY;
return result;
}
const auto amount=std::min<std::int64_t>(budget,queue_.size());
for (std::int64_t i=0; i<amount; ++i) {
if (local.carry.empty()) {
owner_=static_cast<int>(worker);
}
local.carry.push_back(queue_.front());
queue_.pop_front();
++result.bytes;
if (local.carry.size() == 4) {
local.sum += decode(local.carry);
++local.count;
++result.records;
local.carry.clear();
owner_=-1;
}
}
return result;
}
int Pool::close()
{
closed_=true;
return OK;
}
void Pool::commit(Local& local)
{
sum_ += local.sum;
count_ += local.count;
local.done=true;
++done_;
}
int Pool::finish(std::int64_t worker)
{
if (!worker_in_range(worker)) {
return RANGE;
}
Local& local=workers_[static_cast<std::size_t>(worker)];
if (local.done) {
return OK;
}
if (!closed_ || !queue_.empty()) {
return STATE;
}
if (!local.carry.empty()) {
return TAIL;
}
commit(local);
return OK;
}
WorkerView Pool::view(const Local& local)
{
WorkerView output;
output.sum=local.sum;
output.count=local.count;
output.done=local.done;
output.carry=local.carry;
return output;
}
Total Pool::totals() const
{
Total output;
output.sum=sum_;
output.count=count_;
output.done=done_;
output.closed=closed_;
output.queued=static_cast<std::int64_t>(queue_.size());
output.owner=owner_;
return output;
}
int Pool::worker(std::int64_t index, WorkerView& output) const
{
if (!worker_in_range(index)) {
return RANGE;
}
output=view(workers_[static_cast<std::size_t>(index)]);
return OK;
}
Snapshot Pool::snapshot() const
{
Snapshot output;
output.workers=static_cast<std::int64_t>(workers_.size());
output.capacity=capacity_;
output.accepted=accepted_;
output.total=totals();
output.queue.assign(queue_.begin(),queue_.end());
for (const Local& local : workers_) {
output.local.push_back(view(local));
}
return output;
}
}