38int main(
int argc,
char **argv)
41#ifdef MAESTRO_REQUEST_MPI
44 bool framed =
false, capabilities =
false, validate =
false;
45 for (
int i = 1; i < argc; ++i)
47 const std::string arg(argv[i]);
48 if (arg ==
"--framed-result")
50 else if (arg ==
"--capabilities")
52 else if (arg ==
"--validate")
56 std::cerr <<
"Unknown argument: " << arg <<
'\n';
60#ifdef MAESTRO_REQUEST_MPI
61 int provided = MPI_THREAD_SINGLE;
62 if (MPI_Init_thread(&argc, &argv, MPI_THREAD_FUNNELED, &provided) != MPI_SUCCESS)
64 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
65 MPI_Comm_size(MPI_COMM_WORLD, &ranks);
66 if (provided < MPI_THREAD_FUNNELED)
68 MPI_Abort(MPI_COMM_WORLD, 3);
75 if (!capabilities && rank == 0)
78 while (std::cin.read(buffer,
sizeof(buffer)) || std::cin.gcount())
80 input.append(buffer,
static_cast<size_t>(std::cin.gcount()));
82 throw std::invalid_argument(
"Request exceeds 16 MiB");
85#ifdef MAESTRO_REQUEST_MPI
86 if (rank == 0 && framed)
88 const auto *job = std::getenv(
"SLURM_JOB_ID");
89 const auto *step = std::getenv(
"SLURM_STEP_ID");
91 std::cout <<
"MAESTRO_STEP_V2 " << job <<
'.' << step <<
'\n' << std::flush;
93 uint64_t length = input.size();
94 MPI_Bcast(&length, 1, MPI_UINT64_T, 0, MPI_COMM_WORLD);
96 throw std::invalid_argument(
"Oversized collective request");
98 MPI_Bcast(input.data(),
static_cast<int>(length), MPI_BYTE, 0, MPI_COMM_WORLD);
101 int valid =
Success(result) ? 1 : 0;
102#ifdef MAESTRO_REQUEST_MPI
104 MPI_Allreduce(&valid, &allValid, 1, MPI_INT, MPI_MIN, MPI_COMM_WORLD);
108 json::object{{
"ok",
false}, {
"error", json::object{{
"code",
"collective_validation_failure"}, {
"message",
"A peer rejected the request"}}}};
110 if (valid && !capabilities && !validate)
117 std::cerr << json::serialize(result) <<
'\n';
118#ifdef MAESTRO_REQUEST_MPI
121 MPI_Abort(MPI_COMM_WORLD, 1);
125#ifdef MAESTRO_REQUEST_MPI
126 MPI_Barrier(MPI_COMM_WORLD);
129 throw std::runtime_error(json::serialize(shutdown));
130 result[
"mpi"] = json::object{{
"ranks", ranks}, {
"result_rank", 0}, {
"aggregation",
"one_replicated_result"}};
137 catch (
const std::exception &error)
139 json::object result{{
"schema_version", 2}, {
"ok",
false}, {
"error", json::object{{
"code",
"worker_failure"}, {
"message", error.what()}}}};
143 std::cerr << json::serialize(result) <<
'\n';
144#ifdef MAESTRO_REQUEST_MPI
146 MPI_Finalized(&finalized);
148 MPI_Abort(MPI_COMM_WORLD, 1);