1 /* Copyright (c) 2009-2022. The SimGrid Team.
2 * All rights reserved. */
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. */
7 /* This example should be instructive to learn about SMPI_SHARED_CALL */
14 static void* hash(const char *str, uint64_t* ans)
16 const char *tohash = str;
18 printf("hashing !\n");
21 *ans = ((*ans << 5) + *ans) + c; /* hash * 33 + c */
28 int main(int argc, char *argv[])
30 MPI_Init(&argc, &argv);
33 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
34 MPI_Comm_size(MPI_COMM_WORLD, &size);
35 //Let's Allocate a shared memory buffer
36 uint64_t* buf = SMPI_SHARED_MALLOC(sizeof(uint64_t));
37 //one writes data in it
42 MPI_Barrier(MPI_COMM_WORLD);
43 //everyone reads from it.
44 printf("[%d] The value in the shared buffer is: %" PRIu64"\n", rank, *buf);
46 MPI_Barrier(MPI_COMM_WORLD);
47 //Try SMPI_SHARED_CALL function, which should call hash only once and for all.
48 static const char str[] = "onceandforall";
50 SMPI_SHARED_CALL(hash,str,str,buf);
53 MPI_Barrier(MPI_COMM_WORLD);
55 printf("[%d] After change, the value in the shared buffer is: %" PRIu64"\n", rank, *buf);
57 //try to send/receive shared data, to check if we skip the copies correctly.
59 MPI_Send(buf, 1, MPI_AINT, 1, 100, MPI_COMM_WORLD);
61 MPI_Recv(buf, 1, MPI_AINT, 0, 100, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
63 //same thing with an MPI_IN_PLACE collective (no)
65 MPI_Scatter(buf, 1, MPI_AINT, MPI_IN_PLACE, -1, MPI_DATATYPE_NULL, 0, MPI_COMM_WORLD);
67 MPI_Scatter(NULL, -1, MPI_DATATYPE_NULL, buf, 1, MPI_AINT, 0, MPI_COMM_WORLD);
68 SMPI_SHARED_FREE(buf);