1 /* Copyright (c) 2017-2019. The SimGrid Team. All rights reserved. */
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. */
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] = (i+value)%256;
18 // Return the number of times that an element is equal to (i+value)%256 between buf[start] and buf[stop-1].
19 static int count_all(uint8_t *buf, size_t start, size_t stop, uint8_t value) {
21 for(size_t i = start ; i < stop ; i++) {
22 if(buf[i] == (i+value)%256) {
29 // Return true iff the values from buf[start] to buf[stop-1] are all equal to (i+value)%256.
30 static int check_all(uint8_t *buf, size_t start, size_t stop, uint8_t value) {
31 size_t occ = count_all(buf, start, stop, value);
32 return occ == stop-start;
35 // Return true iff "enough" elements are equal to (i+value)%256 between buf[start] and buf[stop-1].
36 static int check_enough(uint8_t *buf, size_t start, size_t stop, uint8_t value) {
37 int page_size = 0x1000;
38 size_t size = stop-start;
39 if(size <= 2*page_size) // we are not sure to have a whole page that is shared
41 size_t occ = count_all(buf, start, stop, value);
42 return occ >= size - 2*page_size;
45 int main(int argc, char *argv[])
47 MPI_Init(&argc, &argv);
50 size_t mem_size = 0x1000000;
51 size_t shared_blocks[] = {
59 int nb_blocks = (sizeof(shared_blocks)/sizeof(size_t))/2;
60 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
61 MPI_Comm_size(MPI_COMM_WORLD, &size);
62 //Let's Allocate a shared memory buffer
64 buf = SMPI_PARTIAL_SHARED_MALLOC(mem_size, shared_blocks, nb_blocks);
65 set(buf, 0, mem_size, 0);
66 MPI_Barrier(MPI_COMM_WORLD);
68 // Process 0 write in shared blocks
70 for(int i = 0; i < nb_blocks; i++) {
71 size_t start = shared_blocks[2*i];
72 size_t stop = shared_blocks[2*i+1];
73 set(buf, start, stop, 42);
76 MPI_Barrier(MPI_COMM_WORLD);
77 // All processes check that their shared blocks have been written (at least partially)
78 for(int i = 0; i < nb_blocks; i++) {
79 size_t start = shared_blocks[2*i];
80 size_t stop = shared_blocks[2*i+1];
81 int is_shared = check_enough(buf, start, stop, 42);
82 printf("[%d] The result of the shared check for block (0x%zx, 0x%zx) is: %d\n", rank, start, stop, is_shared);
86 // Check the private blocks
87 MPI_Barrier(MPI_COMM_WORLD);
88 for(int i = 0; i < nb_blocks-1; i++) {
89 size_t start = shared_blocks[2*i+1];
90 size_t stop = shared_blocks[2*i+2];
91 int is_private = check_all(buf, start, stop, 0);
92 printf("[%d] The result of the private check for block (0x%zx, 0x%zx) is: %d\n", rank, start, stop, is_private);
95 SMPI_SHARED_FREE(buf);