1 /* Copyright (c) 2017. 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. */
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;
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) {
22 for(size_t i = start ; i < stop ; i++) {
23 if(buf[i] == (i+value)%256) {
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;
36 // Return true iff "enough" elements are equal to (i+value)%256 between buf[start] and buf[stop-1].
37 static 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
42 size_t occ = count_all(buf, start, stop, value);
43 return occ >= size - 2*page_size;
46 int main(int argc, char *argv[])
48 MPI_Init(&argc, &argv);
51 size_t mem_size = 0x1000000;
52 size_t shared_blocks[] = {
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
66 buf = SMPI_PARTIAL_SHARED_MALLOC(mem_size, shared_blocks, nb_blocks);
67 memset(buf, rank, mem_size);
68 MPI_Barrier(MPI_COMM_WORLD);
70 // Even processes write their rank in private blocks
72 for(int i = 0; i < nb_blocks-1; i++) {
73 size_t start = shared_blocks[2*i+1];
74 size_t stop = shared_blocks[2*i+2];
75 set(buf, start, stop, rank);
78 // Then, even processes send their buffer to their successor
80 MPI_Send(buf, mem_size, MPI_UINT8_T, rank+1, 0, MPI_COMM_WORLD);
83 MPI_Recv(buf, mem_size, MPI_UINT8_T, rank-1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
87 // Odd processes verify that they successfully received the message
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 comm = check_all(buf, start, stop, rank-1);
93 printf("[%d] The result of the (normal) communication check for block (0x%zx, 0x%zx) is: %d\n", rank, start, stop, comm);
95 memset(buf, rank, mem_size);
98 MPI_Barrier(MPI_COMM_WORLD);
100 // Then, even processes send a sub-part of their buffer their successor
101 // Note that the last block should not be copied entirely
103 MPI_Send(buf+0x10000, mem_size-0xa00000, MPI_UINT8_T, rank+1, 0, MPI_COMM_WORLD);
106 MPI_Recv(buf+0x10000, mem_size-0xa00000, MPI_UINT8_T, rank-1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
110 // Odd processes verify that they successfully received the message
112 for(int i = 0; i < nb_blocks-1; i++) {
113 size_t start = shared_blocks[2*i+1];
114 size_t stop = shared_blocks[2*i+2];
115 int comm = check_all(buf, start, stop, rank-1);
116 printf("[%d] The result of the (shifted) communication check for block (0x%zx, 0x%zx) is: %d\n", rank, start, stop, comm);
120 SMPI_SHARED_FREE(buf);