Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
18aafd86e801516e2be82ad3e2f46680cb73191d
[simgrid.git] / teshsuite / smpi / macro-partial-shared / macro-partial-shared.c
1 /* Copyright (c) 2009-2015. 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 #include <stdint.h>
10 #include <inttypes.h>
11
12 // Set the elements between buf[start] and buf[stop-1] to (i+value)%256
13 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 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 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 // Return true iff "enough" elements are equal to (i+value)%256 between buf[start] and buf[stop-1].
37 int check_enough(uint8_t *buf, size_t start, size_t stop, uint8_t value) {
38   int page_size = 0x1000;
39   size_t size = stop-start;
40   if(size <= 2*page_size) // we are not sure to have a whole page that is shared
41     return 1;
42   size_t occ = count_all(buf, start, stop, value);
43   return occ >= size - 2*page_size;
44 }
45
46 int main(int argc, char *argv[])
47 {
48   MPI_Init(&argc, &argv);
49   int rank;
50   int size;
51   size_t mem_size = 0x10000000;
52   size_t shared_blocks[] = {
53     0,         0x1234567,
54     0x1300000, 0x1300010,
55     0x3456789, 0x3457890,
56     0x4444444, 0x5555555,
57     0x5555565, 0x5600000,
58     0x8000000, 0x10000000
59   };
60   int nb_blocks = (sizeof(shared_blocks)/sizeof(size_t))/2;
61   MPI_Comm_rank(MPI_COMM_WORLD, &rank);
62   MPI_Comm_size(MPI_COMM_WORLD, &size);
63   //Let's Allocate a shared memory buffer
64   uint8_t *buf;
65   buf = SMPI_PARTIAL_SHARED_MALLOC(mem_size, shared_blocks, nb_blocks);
66   set(buf, 0, mem_size, 0);
67   MPI_Barrier(MPI_COMM_WORLD);
68
69   // Process 0 write in shared blocks
70   if(rank == 0) {
71     for(int i = 0; i < nb_blocks; i++) {
72       size_t start = shared_blocks[2*i];
73       size_t stop = shared_blocks[2*i+1];
74       set(buf, start, stop, 42);
75     }
76   }
77   MPI_Barrier(MPI_COMM_WORLD);
78   // All processes check that their shared blocks have been written (at least partially)
79   for(int i = 0; i < nb_blocks; i++) {
80     size_t start = shared_blocks[2*i];
81     size_t stop = shared_blocks[2*i+1];
82     int is_shared = check_enough(buf, start, stop, 42);
83     printf("[%d] The result of the shared check for block (0x%lx, 0x%lx) is: %d\n", rank, start, stop, is_shared);
84   }
85
86
87   // Check the private blocks
88   MPI_Barrier(MPI_COMM_WORLD);
89   for(int i = 0; i < nb_blocks-1; i++) {
90     size_t start = shared_blocks[2*i+1];
91     size_t stop = shared_blocks[2*i+2];
92     int is_private = check_all(buf, start, stop, 0);
93     printf("[%d] The result of the private check for block (0x%lx, 0x%lx) is: %d\n", rank, start, stop, is_private);
94   }
95
96   SMPI_SHARED_FREE(buf);
97
98   MPI_Finalize();
99   return 0;
100 }