1 /* Copyright (c) 2017-2020. 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] = (uint8_t)((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 size_t count_all(const uint8_t* buf, size_t start, size_t stop, uint8_t value)
23 for(size_t i = start ; i < stop ; i++) {
24 if(buf[i] == (i+value)%256) {
31 // Return true iff the values from buf[start] to buf[stop-1] are all equal to (i+value)%256.
32 static int check_all(const uint8_t* buf, size_t start, size_t stop, uint8_t value)
34 size_t occ = count_all(buf, start, stop, value);
35 return occ == stop-start;
38 int main(int argc, char *argv[])
40 MPI_Init(&argc, &argv);
43 size_t mem_size = 0x1000000;
44 size_t shared_blocks[] = {
52 int nb_blocks = (sizeof(shared_blocks)/sizeof(size_t))/2;
53 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
54 MPI_Comm_size(MPI_COMM_WORLD, &size);
55 //Let's Allocate a shared memory buffer
58 buf = SMPI_PARTIAL_SHARED_MALLOC(mem_size, shared_blocks, nb_blocks);
59 memset(buf, rank, mem_size);
60 MPI_Barrier(MPI_COMM_WORLD);
62 // Even processes write their rank in private blocks
64 for(int i = 0; i < nb_blocks-1; i++) {
65 size_t start = shared_blocks[2*i+1];
66 size_t stop = shared_blocks[2*i+2];
67 set(buf, start, stop, (uint8_t)rank);
70 // Then, even processes send their buffer to their successor
72 MPI_Send(buf, (int)mem_size, MPI_UINT8_T, rank + 1, 0, MPI_COMM_WORLD);
75 MPI_Recv(buf, (int)mem_size, MPI_UINT8_T, rank - 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
79 // Odd processes verify that they successfully received the message
81 for(int i = 0; i < nb_blocks-1; i++) {
82 size_t start = shared_blocks[2*i+1];
83 size_t stop = shared_blocks[2*i+2];
84 int comm = check_all(buf, start, stop, (uint8_t)(rank - 1));
85 printf("[%d] The result of the (normal) communication check for block (0x%zx, 0x%zx) is: %d\n", rank, start, stop, comm);
87 memset(buf, rank, mem_size);
90 MPI_Barrier(MPI_COMM_WORLD);
92 // Then, even processes send a sub-part of their buffer their successor
93 // Note that the last block should not be copied entirely
95 MPI_Send(buf + 0x10000, (int)(mem_size - 0xa00000), MPI_UINT8_T, rank + 1, 0, MPI_COMM_WORLD);
98 MPI_Recv(buf + 0x10000, (int)(mem_size - 0xa00000), MPI_UINT8_T, rank - 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
102 // Odd processes verify that they successfully received the message
104 for(int i = 0; i < nb_blocks-1; i++) {
105 size_t start = shared_blocks[2*i+1];
106 size_t stop = shared_blocks[2*i+2];
107 int comm = check_all(buf, start, stop, (uint8_t)(rank - 1));
108 printf("[%d] The result of the (shifted) communication check for block (0x%zx, 0x%zx) is: %d\n", rank, start, stop, comm);
112 SMPI_SHARED_FREE(buf);