Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add more tests for partial shared malloc.
[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 /* This example should be instructive to learn about SMPI_SHARED_CALL */
8
9 #include <stdio.h>
10 #include <mpi.h>
11 #include <stdint.h>
12 #include <inttypes.h>
13
14 // Return the number of occurences of the given value between buf[start] and buf[stop-1].
15 int count_all(uint8_t *buf, int start, int stop, uint8_t value) {
16   int occ = 0;
17   for(int i = start ; i < stop ; i++) {
18     if(buf[i] == value) {
19       occ ++;
20     }
21   }
22   return occ;
23 }
24
25 // Return true iff the values from buf[start] to buf[stop-1] are all equal to value.
26 int check_all(uint8_t *buf, int start, int stop, uint8_t value) {
27   int occ = count_all(buf, start, stop, value);
28   return occ == stop-start;
29 }
30
31 // Return true iff "enough" occurences of the given value are between buf[start] and buf[stop-1].
32 int check_enough(uint8_t *buf, int start, int stop, uint8_t value) {
33   int page_size = 0x1000;
34   int size = stop-start;
35   if(size <= 2*page_size) // we are not sure to have a whole page that is shared
36     return 1;
37   int occ = count_all(buf, start, stop, value);
38   return occ >= size - 2*page_size;
39 }
40
41 int main(int argc, char *argv[])
42 {
43   MPI_Init(&argc, &argv);
44   int rank;
45   int size;
46   int mem_size = 0x10000000;
47   int shared_blocks[] = {
48     0,         0x1234567,
49     0x1300000, 0x1300010,
50     0x3456789, 0x3457890,
51     0x4444444, 0x5555555,
52     0x5555565, 0x5600000,
53     0x8000000, 0x10000000
54   };
55   int nb_blocks = (sizeof(shared_blocks)/sizeof(int))/2;
56   MPI_Comm_rank(MPI_COMM_WORLD, &rank);
57   MPI_Comm_size(MPI_COMM_WORLD, &size);
58   //Let's Allocate a shared memory buffer
59   uint8_t *buf;
60   buf = SMPI_PARTIAL_SHARED_MALLOC(mem_size, shared_blocks, nb_blocks);
61   memset(buf, 0, mem_size);
62   MPI_Barrier(MPI_COMM_WORLD);
63
64   // Process 0 write in shared blocks
65   if(rank == 0) {
66     for(int i = 0; i < nb_blocks; i++) {
67       int start = shared_blocks[2*i];
68       int stop = shared_blocks[2*i+1];
69       memset(buf+start, 42, stop-start);
70     }
71   }
72   MPI_Barrier(MPI_COMM_WORLD);
73   // All processes check that their shared blocks have been written (at least partially)
74   for(int i = 0; i < nb_blocks; i++) {
75     int start = shared_blocks[2*i];
76     int stop = shared_blocks[2*i+1];
77     int is_shared = check_enough(buf, start, stop, 42);
78     printf("[%d] The result of the shared check for block (0x%x, 0x%x) is: %d\n", rank, start, stop, is_shared);
79   }
80
81
82   // Check the private blocks
83   MPI_Barrier(MPI_COMM_WORLD);
84   for(int i = 0; i < nb_blocks-1; i++) {
85     int start = shared_blocks[2*i+1];
86     int stop = shared_blocks[2*i+2];
87     int is_private = check_all(buf, start, stop, 0);
88     printf("[%d] The result of the private check for block (0x%x, 0x%x) is: %d\n", rank, start, stop, is_private);
89   }
90
91   SMPI_SHARED_FREE(buf);
92
93   MPI_Finalize();
94   return 0;
95 }