summaryrefslogtreecommitdiff
path: root/tabulator-mpi/worker.cpp
blob: 511a27a0a23fa4d35b4680663029276c6eadf9b4 (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
#include <lib94/lib94.hpp>
#include <ctime>
#include <mpi.h>

#include "constants.hpp"

static void do_round(const lib94::warrior *w1, const lib94::warrior *w2, int &w1_wins, int& w2_wins) {
  const lib94::warrior *ws[2] = {w1, w2};

  lib94::instruction background = {
    .op = lib94::DAT, .mod = lib94::F,
    .amode = lib94::DIRECT, .bmode = lib94::DIRECT,
    .anumber = 0, .bnumber = 0
  };

  lib94::clear_core(background);

  lib94::init_round(ws, 2);

  for (int i = 0; i < STEPS_TO_TIE; ++i) {
    const lib94::warrior *result = lib94::single_step<false>();
    if (result == w1) {
      ++w2_wins;
      return;
    }
    if (result == w2) {
      ++w1_wins;
      return;
    }
  }
}

void worker_main(const lib94::warrior *const *warriors) {
  lib94::seed_prng(time(0));
  int buffer[4] = {-1};

  while (1) {
    MPI_Send(buffer, 4, MPI_INT, 0, 1, MPI_COMM_WORLD);
    MPI_Recv(buffer, 4, MPI_INT, 0, 0, MPI_COMM_WORLD, 0);

    if (buffer[0] == -1)
      return;

    buffer[2] = 0;
    buffer[3] = 0;

    for (int i = 0; i < ROUNDS_PER_CHUNK; ++i)
      do_round(warriors[buffer[0]], warriors[buffer[1]], buffer[2], buffer[3]);
  }
}