1 /* Copyright (c) 2019-2021. 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. */
8 #include <simgrid/actor.h>
9 #include <simgrid/host.h>
12 XBT_LOG_NEW_DEFAULT_CATEGORY(smpi_test, "Messages specific for this SMPI example");
25 static int global_rank;
26 void* req_wait(void* bar);
28 // Thread creation helper
30 static void thread_create_wrapper(XBT_ATTRIB_UNUSED int argc, XBT_ATTRIB_UNUSED char* argv[])
32 int the_global_rank = global_rank;
33 struct threadwrap* t = (struct threadwrap*)sg_actor_self_get_data();
34 XBT_INFO("new thread has parameter rank %d and global variable rank %d", ((struct param*)(t->param))->rank,
41 static void mpi_thread_create(const char* name, void* (*f)(void*), void* param)
43 struct threadwrap* threadwrap = xbt_malloc(sizeof(*threadwrap));
45 threadwrap->param = param;
46 sg_actor_t actor = sg_actor_init(name, sg_host_self());
47 sg_actor_set_data(actor, threadwrap);
48 sg_actor_start(actor, thread_create_wrapper, 0, NULL);
51 static void mythread_create(const char* name, MPI_Request* req, int rank)
53 struct param* param = xbt_malloc(sizeof(*param));
56 mpi_thread_create(name, req_wait, param);
61 void* req_wait(void* bar)
63 struct param* param = (struct param*)bar;
66 char err_string[1024];
68 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
70 XBT_INFO("%d has MPI rank %d and global variable rank %d", param->rank, rank, global_rank);
71 XBT_INFO("%d waiting request", rank);
72 int ret = MPI_Wait(param->req, &status);
73 MPI_Error_string(ret, err_string, &length);
74 XBT_INFO("%d request done, return %s", rank, err_string);
75 XBT_INFO("%d still has MPI rank %d and global variable %d", param->rank, rank, global_rank);
80 int main(int argc, char* argv[])
86 MPI_Init(&argc, &argv);
87 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
88 MPI_Comm_size(MPI_COMM_WORLD, &size);
89 XBT_INFO("I'm %d/%d", rank, size);
94 MPI_Isend(&c, 1, MPI_CHAR, 1, 0, MPI_COMM_WORLD, &req);
95 mythread_create("wait send", &req, rank);
96 } else if (rank == 1) {
97 MPI_Irecv(&c, 1, MPI_CHAR, 0, 0, MPI_COMM_WORLD, &req);
98 mythread_create("wait recv", &req, rank);
101 sg_actor_sleep_for(1 + rank);
103 XBT_INFO("finally %d", c);