summaryrefslogtreecommitdiff
path: root/tabulator-mpi/main.cpp
blob: cb60ecbfc188ad4c1fce1442547c01f90a18cc8e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#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), {});

  try {
    return lib94::compile_warrior(source);
  }

  catch (const lib94::compiler_exception &ex) {
    fprintf(stderr, "error in %s on line %u: %s\n", file, ex.line_number, ex.message.c_str());
    exit(1);
  }
}

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);

  if (comm_size < 2) {
    fprintf(stderr, "at least two processes are required\n");
    return 1;
  }

  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();
}