Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Kill unused static function.
[simgrid.git] / teshsuite / smpi / macro-partial-shared-communication / macro-partial-shared-communication.c
1 /* Copyright (c) 2017-2018. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include <stdio.h>
7 #include <mpi.h>
8 #include <stdint.h>
9 #include <inttypes.h>
10 #include <assert.h>
11
12 // Set the elements between buf[start] and buf[stop-1] to (i+value)%256
13 static void set(uint8_t *buf, size_t start, size_t stop, uint8_t value) {
14   for(size_t i = start; i < stop; i++) {
15     buf[i] = (i+value)%256;
16   }
17 }
18
19 // Return the number of times that an element is equal to (i+value)%256 between buf[start] and buf[stop-1].
20 static  int count_all(uint8_t *buf, size_t start, size_t stop, uint8_t value) {
21   size_t occ = 0;
22   for(size_t i = start ; i < stop ; i++) {
23     if(buf[i] == (i+value)%256) {
24       occ ++;
25     }
26   }
27   return occ;
28 }
29
30 // Return true iff the values from buf[start] to buf[stop-1] are all equal to (i+value)%256.
31 static int check_all(uint8_t *buf, size_t start, size_t stop, uint8_t value) {
32   size_t occ = count_all(buf, start, stop, value);
33   return occ == stop-start;
34 }
35
36 int main(int argc, char *argv[])
37 {
38   MPI_Init(&argc, &argv);
39   int rank;
40   int size;
41   size_t mem_size = 0x1000000;
42   size_t shared_blocks[] = {
43     0,         0x123456,
44     0x130000, 0x130001,
45     0x345678, 0x345789,
46     0x444444, 0x555555,
47     0x555556, 0x560000,
48     0x800000, 0x1000000
49   };
50   int nb_blocks = (sizeof(shared_blocks)/sizeof(size_t))/2;
51   MPI_Comm_rank(MPI_COMM_WORLD, &rank);
52   MPI_Comm_size(MPI_COMM_WORLD, &size);
53   //Let's Allocate a shared memory buffer
54   assert(size%2 == 0);
55   uint8_t *buf;
56   buf = SMPI_PARTIAL_SHARED_MALLOC(mem_size, shared_blocks, nb_blocks);
57   memset(buf, rank, mem_size);
58   MPI_Barrier(MPI_COMM_WORLD);
59
60   // Even processes write their rank in private blocks
61   if(rank%2 == 0) {
62     for(int i = 0; i < nb_blocks-1; i++) {
63       size_t start = shared_blocks[2*i+1];
64       size_t stop = shared_blocks[2*i+2];
65       set(buf, start, stop, rank);
66     }
67   }
68   // Then, even processes send their buffer to their successor
69   if(rank%2 == 0) {
70     MPI_Send(buf, mem_size, MPI_UINT8_T, rank+1, 0, MPI_COMM_WORLD);
71   }
72   else {
73     MPI_Recv(buf, mem_size, MPI_UINT8_T, rank-1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
74   }
75
76
77   // Odd processes verify that they successfully received the message
78   if(rank%2 == 1) {
79     for(int i = 0; i < nb_blocks-1; i++) {
80       size_t start = shared_blocks[2*i+1];
81       size_t stop = shared_blocks[2*i+2];
82       int comm = check_all(buf, start, stop, rank-1);
83       printf("[%d] The result of the (normal) communication check for block (0x%zx, 0x%zx) is: %d\n", rank, start, stop, comm);
84     }
85     memset(buf, rank, mem_size);
86   }
87
88   MPI_Barrier(MPI_COMM_WORLD);
89
90   // Then, even processes send a sub-part of their buffer their successor
91   // Note that the last block should not be copied entirely
92   if(rank%2 == 0) {
93     MPI_Send(buf+0x10000, mem_size-0xa00000, MPI_UINT8_T, rank+1, 0, MPI_COMM_WORLD);
94   }
95   else {
96     MPI_Recv(buf+0x10000, mem_size-0xa00000, MPI_UINT8_T, rank-1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
97   }
98
99
100   // Odd processes verify that they successfully received the message
101   if(rank%2 == 1) {
102     for(int i = 0; i < nb_blocks-1; i++) {
103       size_t start = shared_blocks[2*i+1];
104       size_t stop = shared_blocks[2*i+2];
105       int comm = check_all(buf, start, stop, rank-1);
106       printf("[%d] The result of the (shifted) communication check for block (0x%zx, 0x%zx) is: %d\n", rank, start, stop, comm);
107     }
108   }
109
110   SMPI_SHARED_FREE(buf);
111
112   MPI_Finalize();
113   return 0;
114 }