Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines for 2022.
[simgrid.git] / teshsuite / smpi / macro-partial-shared / macro-partial-shared.c
1 /* Copyright (c) 2017-2022. 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
11 // Set the elements between buf[start] and buf[stop-1] to (i+value)%256
12 static void set(uint8_t *buf, size_t start, size_t stop, uint8_t value) {
13   for(size_t i = start; i < stop; i++) {
14     buf[i] = (uint8_t)((i + value) % 256);
15   }
16 }
17
18 // Return the number of times that an element is equal to (i+value)%256 between buf[start] and buf[stop-1].
19 static size_t count_all(const uint8_t* buf, size_t start, size_t stop, uint8_t value)
20 {
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(const uint8_t* buf, size_t start, size_t stop, uint8_t value)
32 {
33   size_t occ = count_all(buf, start, stop, value);
34   return occ == stop-start;
35 }
36
37 // Return true iff "enough" elements are equal to (i+value)%256 between buf[start] and buf[stop-1].
38 static int check_enough(const uint8_t* buf, size_t start, size_t stop, uint8_t value)
39 {
40   int page_size = 0x1000;
41   size_t size = stop-start;
42   if(size <= 2*page_size) // we are not sure to have a whole page that is shared
43     return 1;
44   size_t occ = count_all(buf, start, stop, value);
45   return occ >= size - 2*page_size;
46 }
47
48 int main(int argc, char *argv[])
49 {
50   MPI_Init(&argc, &argv);
51   int rank;
52   int size;
53   size_t mem_size = 0x1000000;
54   size_t shared_blocks[] = {
55     0,         0x123456,
56     0x130000, 0x130001,
57     0x345678, 0x345789,
58     0x444444, 0x555555,
59     0x555556, 0x560000,
60     0x800000, 0x1000000
61   };
62   int nb_blocks = (sizeof(shared_blocks)/sizeof(size_t))/2;
63   MPI_Comm_rank(MPI_COMM_WORLD, &rank);
64   MPI_Comm_size(MPI_COMM_WORLD, &size);
65   //Let's Allocate a shared memory buffer
66   uint8_t *buf;
67   buf = SMPI_PARTIAL_SHARED_MALLOC(mem_size, shared_blocks, nb_blocks);
68   set(buf, 0, mem_size, 0);
69   MPI_Barrier(MPI_COMM_WORLD);
70
71   // Process 0 write in shared blocks
72   if(rank == 0) {
73     for(int i = 0; i < nb_blocks; i++) {
74       size_t start = shared_blocks[2*i];
75       size_t stop = shared_blocks[2*i+1];
76       set(buf, start, stop, 42);
77     }
78   }
79   MPI_Barrier(MPI_COMM_WORLD);
80   // All processes check that their shared blocks have been written (at least partially)
81   for(int i = 0; i < nb_blocks; i++) {
82     size_t start = shared_blocks[2*i];
83     size_t stop = shared_blocks[2*i+1];
84     int is_shared = check_enough(buf, start, stop, 42);
85     printf("[%d] The result of the shared check for block (0x%zx, 0x%zx) is: %d\n", rank, start, stop, is_shared);
86   }
87
88
89   // Check the private blocks
90   MPI_Barrier(MPI_COMM_WORLD);
91   for(int i = 0; i < nb_blocks-1; i++) {
92     size_t start = shared_blocks[2*i+1];
93     size_t stop = shared_blocks[2*i+2];
94     int is_private = check_all(buf, start, stop, 0);
95     printf("[%d] The result of the private check for block (0x%zx, 0x%zx) is: %d\n", rank, start, stop, is_private);
96   }
97
98   SMPI_SHARED_FREE(buf);
99
100   MPI_Finalize();
101   return 0;
102 }