1 /* Copyright (c) 2009-2015. The SimGrid Team.
2 * All rights reserved. */
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. */
13 // Set the elements between buf[start] and buf[stop-1] to (i+value)%256
14 void set(uint8_t *buf, size_t start, size_t stop, uint8_t value) {
15 for(size_t i = start; i < stop; i++) {
16 buf[i] = (i+value)%256;
20 // Return the number of times that an element is equal to (i+value)%256 between buf[start] and buf[stop-1].
21 int count_all(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 int check_all(uint8_t *buf, size_t start, size_t stop, uint8_t value) {
33 size_t occ = count_all(buf, start, stop, value);
34 return occ == stop-start;
37 // Return true iff "enough" elements are equal to (i+value)%256 between buf[start] and buf[stop-1].
38 int check_enough(uint8_t *buf, size_t start, size_t stop, uint8_t value) {
39 int page_size = 0x1000;
40 size_t size = stop-start;
41 if(size <= 2*page_size) // we are not sure to have a whole page that is shared
43 size_t occ = count_all(buf, start, stop, value);
44 return occ >= size - 2*page_size;
47 int main(int argc, char *argv[])
49 MPI_Init(&argc, &argv);
52 size_t mem_size = 0x10000000;
53 size_t shared_blocks[] = {
61 int nb_blocks = (sizeof(shared_blocks)/sizeof(size_t))/2;
62 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
63 MPI_Comm_size(MPI_COMM_WORLD, &size);
64 //Let's Allocate a shared memory buffer
67 buf = SMPI_PARTIAL_SHARED_MALLOC(mem_size, shared_blocks, nb_blocks);
68 memset(buf, rank, mem_size);
69 MPI_Barrier(MPI_COMM_WORLD);
71 // Even processes write their rank in private blocks
73 for(int i = 0; i < nb_blocks-1; i++) {
74 size_t start = shared_blocks[2*i+1];
75 size_t stop = shared_blocks[2*i+2];
76 set(buf, start, stop, rank);
79 // Then, even processes send their buffer to their successor
81 MPI_Send(buf, mem_size, MPI_UINT8_T, rank+1, 0, MPI_COMM_WORLD);
84 MPI_Recv(buf, mem_size, MPI_UINT8_T, rank-1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
88 // Odd processes verify that they successfully received the message
90 for(int i = 0; i < nb_blocks-1; i++) {
91 size_t start = shared_blocks[2*i+1];
92 size_t stop = shared_blocks[2*i+2];
93 int comm = check_all(buf, start, stop, rank-1);
94 printf("[%d] The result of the (normal) communication check for block (0x%lx, 0x%lx) is: %d\n", rank, start, stop, comm);
96 memset(buf, rank, mem_size);
99 MPI_Barrier(MPI_COMM_WORLD);
101 // Then, even processes send a sub-part of their buffer their successor
102 // Note that the last block should not be copied entirely
104 MPI_Send(buf+0x10000, mem_size-0xa000000, MPI_UINT8_T, rank+1, 0, MPI_COMM_WORLD);
107 MPI_Recv(buf+0x10000, mem_size-0xa000000, MPI_UINT8_T, rank-1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
111 // Odd processes verify that they successfully received the message
113 for(int i = 0; i < nb_blocks-1; i++) {
114 size_t start = shared_blocks[2*i+1];
115 size_t stop = shared_blocks[2*i+2];
116 int comm = check_all(buf, start, stop, rank-1);
117 printf("[%d] The result of the (shifted) communication check for block (0x%lx, 0x%lx) is: %d\n", rank, start, stop, comm);
121 SMPI_SHARED_FREE(buf);