Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines.
[simgrid.git] / teshsuite / smpi / type-indexed / type-indexed.c
1 /* Copyright (c) 2012-2021. 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;
13   int size;
14   MPI_Datatype type;
15   MPI_Datatype type2;
16   int blocklen[3]     = {2, 3, 1};
17   int displacement[3] = {0, 3, 8};
18   int buffer[27];
19   MPI_Status status;
20
21   MPI_Init(&argc, &argv);
22   MPI_Comm_size(MPI_COMM_WORLD, &size);
23   if (size < 2) {
24     printf("Please run with 2 processes.\n");
25     MPI_Finalize();
26     return 1;
27   }
28   MPI_Comm_rank(MPI_COMM_WORLD, &rank);
29
30   MPI_Type_contiguous(3, MPI_INT, &type2);
31   MPI_Type_commit(&type2);
32   MPI_Type_indexed(3, blocklen, displacement, type2, &type);
33   MPI_Type_commit(&type);
34
35   if (rank == 0) {
36     for (int 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     for (int i = 0; i < 27; i++)
43       buffer[i] = -1;
44     MPI_Recv(buffer, 1, type, 0, 123, MPI_COMM_WORLD, &status);
45     for (int i = 0; i < 27; i++)
46       printf("buffer[%d] = %d\n", i, buffer[i]);
47     fflush(stdout);
48   }
49
50   MPI_Type_free(&type);
51   MPI_Type_free(&type2);
52   MPI_Finalize();
53   return 0;
54 }