Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
wip. revising cmake for smpi.
[simgrid.git] / teshsuite / smpi / shared / shared.c
1 /* Copyright (c) 2009-2015. 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(char *str, uint64_t* ans)
15 {
16   *ans=5381;
17   int c;
18   printf("hashing !\n");
19   while ((c = *str++)!=0)
20     *ans = ((*ans << 5) + *ans) + c; /* hash * 33 + c */
21   return NULL;
22 }
23
24 int main(int argc, char *argv[])
25 {
26   MPI_Init(&argc, &argv);
27   int rank, size;
28   MPI_Comm_rank(MPI_COMM_WORLD, &rank);
29   MPI_Comm_size(MPI_COMM_WORLD, &size);
30   //Let's Allocate a shared memory buffer
31   uint64_t* buf = SMPI_SHARED_MALLOC(sizeof(uint64_t));
32   //one writes data in it
33   if(rank==0){
34     *buf=size;
35   }
36
37   MPI_Barrier(MPI_COMM_WORLD);
38   //everyone reads from it.
39   printf("[%d] The value in the shared buffer is: %" PRIu64"\n", rank, *buf);
40
41   MPI_Barrier(MPI_COMM_WORLD);
42   //Try SMPI_SHARED_CALL function, which should call hash only once and for all.
43   char *str = strdup("onceandforall");
44   if(rank==size-1){
45     SMPI_SHARED_CALL(hash,str,str,buf);  
46   }
47
48   MPI_Barrier(MPI_COMM_WORLD);
49
50   printf("[%d] After change, the value in the shared buffer is: %" PRIu64"\n", rank, *buf);
51
52   SMPI_SHARED_FREE(buf);
53   buf=NULL;
54   free(str);
55
56   MPI_Finalize();
57   return 0;
58 }