diff options
author | Benji Dial <benji@benjidial.net> | 2023-05-30 03:01:25 -0400 |
---|---|---|
committer | Benji Dial <benji@benjidial.net> | 2023-05-30 03:01:25 -0400 |
commit | 6678dccccdfffb9c40f64a828fd769c53bb2d29f (patch) | |
tree | 4a7f1c0b849b8bc4cebe5c9c0bc690a0e6e07146 /tabulator-mpi/head.cpp | |
parent | e39e53f96e0fb75847bd2f5eb40cac7c52e85886 (diff) | |
download | lib94-6678dccccdfffb9c40f64a828fd769c53bb2d29f.tar.gz |
mpi-based tabulator
Diffstat (limited to 'tabulator-mpi/head.cpp')
-rw-r--r-- | tabulator-mpi/head.cpp | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/tabulator-mpi/head.cpp b/tabulator-mpi/head.cpp new file mode 100644 index 0000000..e2372bc --- /dev/null +++ b/tabulator-mpi/head.cpp @@ -0,0 +1,83 @@ +#include <lib94/lib94.hpp> +#include <cstdio> +#include <mpi.h> + +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 < 10; ++x) { + int rank = get_result(); + int message[3] = {i, j, x * 100}; + MPI_Send(message, 2, MPI_INT, rank, 0, MPI_COMM_WORLD); + fprintf(stderr, "sent rounds %d through %d to worker %d\n", x * 100 + 1, x * 100 + 100, 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'); + } +} |