summaryrefslogtreecommitdiff
path: root/tabulator-mpi/main.cpp
diff options
context:
space:
mode:
authorBenji Dial <benji@benjidial.net>2023-05-30 03:01:25 -0400
committerBenji Dial <benji@benjidial.net>2023-05-30 03:01:25 -0400
commit6678dccccdfffb9c40f64a828fd769c53bb2d29f (patch)
tree4a7f1c0b849b8bc4cebe5c9c0bc690a0e6e07146 /tabulator-mpi/main.cpp
parente39e53f96e0fb75847bd2f5eb40cac7c52e85886 (diff)
downloadlib94-6678dccccdfffb9c40f64a828fd769c53bb2d29f.tar.gz
mpi-based tabulator
Diffstat (limited to 'tabulator-mpi/main.cpp')
-rw-r--r--tabulator-mpi/main.cpp46
1 files changed, 46 insertions, 0 deletions
diff --git a/tabulator-mpi/main.cpp b/tabulator-mpi/main.cpp
new file mode 100644
index 0000000..f76ed70
--- /dev/null
+++ b/tabulator-mpi/main.cpp
@@ -0,0 +1,46 @@
+#include <lib94/lib94.hpp>
+#include <fstream>
+#include <cstdio>
+#include <mpi.h>
+
+void head_main(int comm_size, int warrior_count, const lib94::warrior *const *warriors);
+
+void worker_main(const lib94::warrior *const *warriors);
+
+const lib94::warrior *load_warrior(const char *file) {
+ std::ifstream stream(file);
+ if (!stream) {
+ fprintf(stderr, "could not open %s\n", file);
+ exit(1);
+ }
+
+ std::string source(std::istreambuf_iterator<char>(stream), {});
+
+ auto w = lib94::compile_warrior(source);
+
+ if (std::holds_alternative<std::string>(w)) {
+ fprintf(stderr, "error compiling %s: %s\n", file, std::get<std::string>(w).c_str());
+ exit(1);
+ }
+
+ return std::get<lib94::warrior *>(w);
+}
+
+int main(int argc, char **argv) {
+ MPI_Init(&argc, &argv);
+
+ int comm_rank, comm_size;
+ MPI_Comm_rank(MPI_COMM_WORLD, &comm_rank);
+ MPI_Comm_size(MPI_COMM_WORLD, &comm_size);
+
+ const lib94::warrior **warriors = new const lib94::warrior *[argc - 1];
+ for (int i = 0; i < argc - 1; ++i)
+ warriors[i] = load_warrior(argv[i + 1]);
+
+ if (comm_rank == 0)
+ head_main(comm_size, argc - 1, warriors);
+ else
+ worker_main(warriors);
+
+ MPI_Finalize();
+}