Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Black magic.
[simgrid.git] / src / smpi / mpi / smpi_f2c.cpp
1 /* Copyright (c) 2007-2020. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "smpi_f2c.hpp"
7 #include "private.hpp"
8 #include "src/smpi/include/smpi_actor.hpp"
9
10 #include <cstdio>
11
12 int mpi_in_place_;
13 int mpi_bottom_;
14 int mpi_status_ignore_;
15 int mpi_statuses_ignore_;
16
17 namespace simgrid{
18 namespace smpi{
19
20 std::unordered_map<std::string, F2C*>* F2C::f2c_lookup_ = nullptr;
21 int F2C::f2c_id_ = 0;
22
23 // Keep it non trivially-constructible, or it will break MC+smpi on FreeBSD with Clang (don't ask why)
24 F2C::F2C() = default;
25
26 std::unordered_map<std::string, F2C*>* F2C::f2c_lookup()
27 {
28   return f2c_lookup_;
29 }
30
31 void F2C::set_f2c_lookup(std::unordered_map<std::string, F2C*>* map)
32 {
33   f2c_lookup_ = map;
34 }
35
36 void F2C::f2c_id_increment(){
37   f2c_id_++;
38 }
39
40 int F2C::f2c_id(){
41   return f2c_id_;
42 }
43
44 char* F2C::get_my_key(char* key) {
45   std::snprintf(key, KEY_SIZE, "%d", my_f2c_id_);
46   return key;
47 }
48
49 char* F2C::get_key(char* key, int id) {
50   std::snprintf(key, KEY_SIZE, "%d", id);
51   return key;
52 }
53
54 void F2C::delete_lookup(){
55   delete f2c_lookup_;
56 }
57
58 std::unordered_map<std::string, F2C*>* F2C::lookup()
59 {
60   return f2c_lookup_;
61 }
62
63 void F2C::free_f(int id)
64 {
65   char key[KEY_SIZE];
66   f2c_lookup_->erase(get_key(key,id));
67 }
68
69 int F2C::add_f()
70 {
71   if (f2c_lookup_ == nullptr)
72     f2c_lookup_ = new std::unordered_map<std::string, F2C*>();
73
74   char key[KEY_SIZE];
75   my_f2c_id_=f2c_id_;
76   (*f2c_lookup_)[get_my_key(key)] = this;
77   f2c_id_increment();
78   return my_f2c_id_;
79 }
80
81 int F2C::c2f()
82 {
83   if (f2c_lookup_ == nullptr) {
84     f2c_lookup_ = new std::unordered_map<std::string, F2C*>();
85   }
86
87   if(my_f2c_id_==-1)
88     /* this function wasn't found, add it */
89     return this->add_f();
90   else
91     return my_f2c_id_;
92 }
93
94 F2C* F2C::f2c(int id)
95 {
96   if (f2c_lookup_ == nullptr)
97     f2c_lookup_ = new std::unordered_map<std::string, F2C*>();
98
99   if(id >= 0){
100     char key[KEY_SIZE];
101     auto comm = f2c_lookup_->find(get_key(key,id));
102     return comm == f2c_lookup_->end() ? nullptr : comm->second;
103   }else
104     return nullptr;
105 }
106
107 }
108 }