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. */
7 /* This example should be instructive to learn about SMPI_SHARED_CALL */
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) {
17 for(int i = start ; i < stop ; i++) {
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;
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
37 int occ = count_all(buf, start, stop, value);
38 return occ >= size - 2*page_size;
41 int main(int argc, char *argv[])
43 MPI_Init(&argc, &argv);
46 int mem_size = 0x10000000;
47 int shared_blocks[] = {
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
60 buf = SMPI_PARTIAL_SHARED_MALLOC(mem_size, shared_blocks, nb_blocks);
61 memset(buf, 0, mem_size);
62 MPI_Barrier(MPI_COMM_WORLD);
64 // Process 0 write in shared blocks
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);
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);
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);
91 SMPI_SHARED_FREE(buf);