#include #include #include 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'); } }