Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Rewrite of the fat trees implementation
[simgrid.git] / teshsuite / smpi / indexed_test.c
1 /* Copyright (c) 2012-2014. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include <stdio.h>
8 #include "mpi.h"
9
10 int main(int argc, char *argv[])
11 {
12     int rank, size, i;
13     MPI_Datatype type, type2;
14     int blocklen[3] = { 2, 3, 1 };
15     int displacement[3] = { 0, 3, 8 };
16     int buffer[27];
17     MPI_Status status;
18
19     MPI_Init(&argc, &argv);
20     MPI_Comm_size(MPI_COMM_WORLD, &size);
21     if (size < 2)
22     {
23         printf("Please run with 2 processes.\n");
24         MPI_Finalize();
25         return 1;
26     }
27     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
28
29     MPI_Type_contiguous(3, MPI_INT, &type2);
30     MPI_Type_commit(&type2);
31     MPI_Type_indexed(3, blocklen, displacement, type2, &type);
32     MPI_Type_commit(&type);
33
34     if (rank == 0)
35     {
36         for (i=0; i<27; i++)
37             buffer[i] = i;
38         MPI_Send(buffer, 1, type, 1, 123, MPI_COMM_WORLD);
39     }
40
41     if (rank == 1)
42     {
43         for (i=0; i<27; i++)
44             buffer[i] = -1;
45         MPI_Recv(buffer, 1, type, 0, 123, MPI_COMM_WORLD, &status);
46         for (i=0; i<27; i++)
47             printf("buffer[%d] = %d\n", i, buffer[i]);
48         fflush(stdout);
49     }
50
51     MPI_Type_free(&type);
52     MPI_Type_free(&type2);
53     MPI_Finalize();
54     return 0;
55 }
56