summaryrefslogtreecommitdiff
path: root/tabulator-mpi/head.cpp
blob: bd270afed1bb550f786c6fa593cf18f1f6f8a325 (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <lib94/lib94.hpp>
#include <cstdio>
#include <mpi.h>

#include "constants.hpp"

static int **score_results;

static int get_result() {
  int result[3];
  MPI_Status status;
  MPI_Recv(result, 3, MPI_INT, MPI_ANY_SOURCE, 1, MPI_COMM_WORLD, &status);

  if (result[0] != -1)
    score_results[result[0]][result[1]] += result[2];

  return status.MPI_SOURCE;
}

void head_main(int comm_size, int warrior_count, const lib94::warrior *const *warriors) {
  score_results = new int *[warrior_count];
  for (int i = 0; i < warrior_count; ++i) {
    score_results[i] = new int[warrior_count];
    for (int j = 0; j < warrior_count; ++j)
      score_results[i][j] = 0;
  }

  for (int i = 0; i < warrior_count; ++i)
    for (int j = i + 1; j < warrior_count; ++j) {
      fprintf(stderr, "%s vs %s\n", warriors[i]->name.c_str(), warriors[j]->name.c_str());
      for (int x = 0; x < CHUNKS_PER_PAIR; ++x) {
        int rank = get_result();
        int message[3] = {i, j, x * ROUNDS_PER_CHUNK};
        MPI_Send(message, 2, MPI_INT, rank, 0, MPI_COMM_WORLD);
        fprintf(stderr, "sent rounds %d through %d to worker %d\n",
          x * ROUNDS_PER_CHUNK + 1, x * ROUNDS_PER_CHUNK + ROUNDS_PER_CHUNK, rank);
      }
      fputc('\n', stderr);
    }


  for (int i = 0; i < comm_size - 1; ++i) {
    int rank = get_result();
    int message[3] = {-1};
    MPI_Send(message, 3, MPI_INT, rank, 0, MPI_COMM_WORLD);
  }

  for (int i = 0; i < warrior_count; ++i)
    for (int j = i + 1; j < warrior_count; ++j)
      if (score_results[i][j] < 0) {
        score_results[j][i] = -score_results[i][j];
        score_results[i][j] = 0;
      }

  int *tab_widths = new int[warrior_count + 1];
  tab_widths[0] = 0;

  for (int i = 0; i < warrior_count; ++i) {
    int len = warriors[i]->name.size();
    if (len > tab_widths[0])
      tab_widths[0] = len;
    tab_widths[i + 1] = len > 5 ? len : 5;
  }

  printf(" %*s", tab_widths[0], "");
  for (int j = 0; j < warrior_count; ++j)
    printf(" | %*s", tab_widths[j + 1], warriors[j]->name.c_str());
  putchar('\n');

  putchar('-');
  for (int x = 0; x < tab_widths[0]; ++x)
    putchar('-');
  for (int j = 0; j < warrior_count; ++j) {
    printf("-+-");
    for (int x = 0; x < tab_widths[j + 1]; ++x)
      putchar('-');
  }
  printf("-\n");

  for (int i = 0; i < warrior_count; ++i) {
    printf(" %*s", tab_widths[0], warriors[i]->name.c_str());
    for (int j = 0; j < warrior_count; ++j)
      printf(" | %*d", tab_widths[j + 1], score_results[i][j]);
    putchar('\n');
  }
}