Logo AND Algorithmique Numérique Distribuée

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