Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
remove f77 attr tests
[simgrid.git] / teshsuite / smpi / shared.c
1 /* Copyright (c) 2009-2012. The SimGrid Team. All rights reserved.          */
2
3 /* This example should be instructive to learn about SMPI_SAMPLE_LOCAL and 
4    SMPI_SAMPLE_GLOBAL macros for execution sampling */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 #include <stdio.h>
10 #include <mpi.h>
11 #include <stdint.h>
12 #include <inttypes.h>
13
14 void* hash(char *str, uint64_t* ans);
15
16 void* hash(char *str, uint64_t* ans)
17 {
18   *ans=5381;
19   int c;
20   printf("hashing !\n");
21   while ((c = *str++)!=0)
22     *ans = ((*ans << 5) + *ans) + c; /* hash * 33 + c */
23   return NULL;
24 }
25
26
27 int main(int argc, char *argv[])
28 {
29   MPI_Init(&argc, &argv);
30   int rank, size;
31   MPI_Comm_rank(MPI_COMM_WORLD, &rank);
32   MPI_Comm_size(MPI_COMM_WORLD, &size);
33   //Let's Allocate a shared memory buffer
34   uint64_t* buf = SMPI_SHARED_MALLOC(sizeof(uint64_t));
35   //one writes data in it
36   if(rank==0){
37     *buf=size;  
38   }
39   
40   MPI_Barrier(MPI_COMM_WORLD);
41   //everyobne reads from it. 
42   printf("[%d] The value in the shared buffer is: %" PRIu64"\n", rank, *buf);
43   
44   
45   MPI_Barrier(MPI_COMM_WORLD);
46   //Try SMPI_SHARED_CALL function, which should call hash only once and for all.
47   char *str = strdup("onceandforall");
48   if(rank==size-1){
49     SMPI_SHARED_CALL(hash,str,str,buf);  
50   }
51   
52   MPI_Barrier(MPI_COMM_WORLD);
53   
54   printf("[%d] After change, the value in the shared buffer is: %" PRIu64"\n", rank, *buf);
55   
56   SMPI_SHARED_FREE(buf);  
57   buf=NULL;  
58   free(str);
59     
60   MPI_Finalize();
61   return 0;
62 }