Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines for 2022.
[simgrid.git] / teshsuite / smpi / auto-shared / auto-shared.c
1 /* Copyright (c) 2009-2022. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 /* This example should be instructive to learn about SMPI_SHARED_CALL */
8
9 #include <stdio.h>
10 #include <mpi.h>
11 #include <stdint.h>
12 #include <inttypes.h>
13
14 static void* hash(const char *str, uint64_t* ans)
15 {
16   const char *tohash = str;
17   *ans=5381;
18   printf("hashing !\n");
19   int c = *tohash;
20   while (c != 0) {
21     *ans = ((*ans << 5) + *ans) + c; /* hash * 33 + c */
22     tohash++;
23     c = *tohash;
24   }
25   return NULL;
26 }
27
28 int main(int argc, char *argv[])
29 {
30   MPI_Init(&argc, &argv);
31   int rank;
32   int size;
33   MPI_Comm_rank(MPI_COMM_WORLD, &rank);
34   MPI_Comm_size(MPI_COMM_WORLD, &size);
35   //Let's Allocate a memory buffer
36   uint64_t* buf = calloc(1, sizeof(uint64_t));
37
38   MPI_Barrier(MPI_COMM_WORLD);
39   //one writes data in it
40   if(rank==0){
41     *buf=size;
42   }
43
44   MPI_Barrier(MPI_COMM_WORLD);
45   //everyone reads from it.
46   printf("[%d] The value in the shared buffer is: %" PRIu64"\n", rank, *buf);
47
48   MPI_Barrier(MPI_COMM_WORLD);
49   //Try SMPI_SHARED_CALL function, which should call hash only once and for all.
50   static const char str[] = "onceandforall";
51   if(rank==size-1){
52     SMPI_SHARED_CALL(hash,str,str,buf);
53   }
54
55   MPI_Barrier(MPI_COMM_WORLD);
56
57   printf("[%d] After change, the value in the shared buffer is: %" PRIu64"\n", rank, *buf);
58
59   free(buf);
60
61   MPI_Finalize();
62   return 0;
63 }