|
Packit |
0848f5 |
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
|
|
Packit |
0848f5 |
/*
|
|
Packit |
0848f5 |
* (C) 2001 by Argonne National Laboratory.
|
|
Packit |
0848f5 |
* See COPYRIGHT in top-level directory.
|
|
Packit |
0848f5 |
*/
|
|
Packit |
0848f5 |
#include "mpi.h"
|
|
Packit |
0848f5 |
#include <stdio.h>
|
|
Packit |
0848f5 |
#include "mpitest.h"
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
#define MAX_PROCESSES 10
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
int main(int argc, char **argv)
|
|
Packit |
0848f5 |
{
|
|
Packit |
0848f5 |
int rank, size, i, j;
|
|
Packit |
0848f5 |
int table[MAX_PROCESSES][MAX_PROCESSES];
|
|
Packit |
0848f5 |
int errors = 0;
|
|
Packit |
0848f5 |
int participants;
|
|
Packit |
0848f5 |
int displs[MAX_PROCESSES];
|
|
Packit |
0848f5 |
int recv_counts[MAX_PROCESSES];
|
|
Packit |
0848f5 |
MPI_Comm test_comm;
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
MTest_Init(&argc, &argv);
|
|
Packit |
0848f5 |
MPI_Comm_rank(MPI_COMM_WORLD, &rank;;
|
|
Packit |
0848f5 |
MPI_Comm_size(MPI_COMM_WORLD, &size);
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
/* A maximum of MAX_PROCESSES processes can participate */
|
|
Packit |
0848f5 |
participants = (size > MAX_PROCESSES) ? MAX_PROCESSES : size;
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
if (MAX_PROCESSES % participants) {
|
|
Packit |
0848f5 |
fprintf(stderr, "Number of processors must divide %d\n", MAX_PROCESSES);
|
|
Packit |
0848f5 |
MPI_Abort(MPI_COMM_WORLD, 1);
|
|
Packit |
0848f5 |
}
|
|
Packit |
0848f5 |
MPI_Comm_split(MPI_COMM_WORLD, rank < participants, rank, &test_comm);
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
if (rank < participants) {
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
/* Determine what rows are my responsibility */
|
|
Packit |
0848f5 |
int block_size = MAX_PROCESSES / participants;
|
|
Packit |
0848f5 |
int begin_row = rank * block_size;
|
|
Packit |
0848f5 |
int end_row = (rank + 1) * block_size;
|
|
Packit |
0848f5 |
int send_count = block_size * MAX_PROCESSES;
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
/* Fill in the displacements and recv_counts */
|
|
Packit |
0848f5 |
for (i = 0; i < participants; i++) {
|
|
Packit |
0848f5 |
displs[i] = i * block_size * MAX_PROCESSES;
|
|
Packit |
0848f5 |
recv_counts[i] = send_count;
|
|
Packit |
0848f5 |
}
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
/* Paint my rows my color */
|
|
Packit |
0848f5 |
for (i = begin_row; i < end_row; i++)
|
|
Packit |
0848f5 |
for (j = 0; j < MAX_PROCESSES; j++)
|
|
Packit |
0848f5 |
table[i][j] = rank + 10;
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
/* Everybody gets the gathered data */
|
|
Packit |
0848f5 |
if ((char *) &table[begin_row][0] != (char *) table + displs[rank] * sizeof(int))
|
|
Packit |
0848f5 |
MPI_Allgatherv(&table[begin_row][0], send_count, MPI_INT,
|
|
Packit |
0848f5 |
&table[0][0], recv_counts, displs, MPI_INT, test_comm);
|
|
Packit |
0848f5 |
else
|
|
Packit |
0848f5 |
MPI_Allgatherv(MPI_IN_PLACE, send_count, MPI_INT,
|
|
Packit |
0848f5 |
&table[0][0], recv_counts, displs, MPI_INT, test_comm);
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
/* Everybody should have the same table now.
|
|
Packit |
0848f5 |
*
|
|
Packit |
0848f5 |
* The entries are:
|
|
Packit |
0848f5 |
* Table[i][j] = (i/block_size) + 10;
|
|
Packit |
0848f5 |
*/
|
|
Packit |
0848f5 |
for (i = 0; i < MAX_PROCESSES; i++)
|
|
Packit |
0848f5 |
if ((table[i][0] - table[i][MAX_PROCESSES - 1] != 0))
|
|
Packit |
0848f5 |
errors++;
|
|
Packit |
0848f5 |
for (i = 0; i < MAX_PROCESSES; i++) {
|
|
Packit |
0848f5 |
for (j = 0; j < MAX_PROCESSES; j++) {
|
|
Packit |
0848f5 |
if (table[i][j] != (i / block_size) + 10)
|
|
Packit |
0848f5 |
errors++;
|
|
Packit |
0848f5 |
}
|
|
Packit |
0848f5 |
}
|
|
Packit |
0848f5 |
if (errors) {
|
|
Packit |
0848f5 |
/* Print out table if there are any errors */
|
|
Packit |
0848f5 |
for (i = 0; i < MAX_PROCESSES; i++) {
|
|
Packit |
0848f5 |
printf("\n");
|
|
Packit |
0848f5 |
for (j = 0; j < MAX_PROCESSES; j++)
|
|
Packit |
0848f5 |
printf(" %d", table[i][j]);
|
|
Packit |
0848f5 |
}
|
|
Packit |
0848f5 |
printf("\n");
|
|
Packit |
0848f5 |
}
|
|
Packit |
0848f5 |
}
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
MTest_Finalize(errors);
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
MPI_Comm_free(&test_comm);
|
|
Packit |
0848f5 |
MPI_Finalize();
|
|
Packit |
0848f5 |
return MTestReturnValue(errors);
|
|
Packit |
0848f5 |
}
|