Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Initialize arrays at declaration, and remove use of deprecated bzero().
[simgrid.git] / teshsuite / smpi / indexed_test.c
1 #include <stdio.h>
2 #include "mpi.h"
3
4 int main(int argc, char *argv[])
5 {
6     int rank, size, i;
7     MPI_Datatype type, type2;
8     int blocklen[3] = { 2, 3, 1 };
9     int displacement[3] = { 0, 3, 8 };
10     int buffer[27];
11     MPI_Status status;
12
13     MPI_Init(&argc, &argv);
14     MPI_Comm_size(MPI_COMM_WORLD, &size);
15     if (size < 2)
16     {
17         printf("Please run with 2 processes.\n");
18         MPI_Finalize();
19         return 1;
20     }
21     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
22
23     MPI_Type_contiguous(3, MPI_INT, &type2);
24     MPI_Type_commit(&type2);
25     MPI_Type_indexed(3, blocklen, displacement, type2, &type);
26     MPI_Type_commit(&type);
27
28     if (rank == 0)
29     {
30         for (i=0; i<27; i++)
31             buffer[i] = i;
32         MPI_Send(buffer, 1, type, 1, 123, MPI_COMM_WORLD);
33     }
34
35     if (rank == 1)
36     {
37         for (i=0; i<27; i++)
38             buffer[i] = -1;
39         MPI_Recv(buffer, 1, type, 0, 123, MPI_COMM_WORLD, &status);
40         for (i=0; i<27; i++)
41             printf("buffer[%d] = %d\n", i, buffer[i]);
42         fflush(stdout);
43     }
44
45     MPI_Type_free(&type);
46     MPI_Type_free(&type2);
47     MPI_Finalize();
48     return 0;
49 }
50