Logo AND Algorithmique Numérique Distribuée

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