Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
4a31be638648dd6aa9ddfce929eab7749228d299
[simgrid.git] / src / smpi / mpi / smpi_f2c.cpp
1 /* Copyright (c) 2007-2017. 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 "private.h"
7 #include "smpi_f2c.hpp"
8 #include "smpi_process.hpp"
9
10 #include <cstdio>
11
12 namespace simgrid{
13 namespace smpi{
14
15 std::unordered_map<std::string, F2C*>* F2C::f2c_lookup_ = nullptr;
16 int F2C::f2c_id_ = 0;
17
18 std::unordered_map<std::string, F2C*>* F2C::f2c_lookup()
19 {
20   return f2c_lookup_;
21 }
22
23 void F2C::set_f2c_lookup(std::unordered_map<std::string, F2C*>* map)
24 {
25   f2c_lookup_ = map;
26 }
27
28 void F2C::f2c_id_increment(){
29   f2c_id_++;
30 };
31
32 int F2C::f2c_id(){
33   return f2c_id_;
34 };
35
36 char* F2C::get_key(char* key, int id) {
37   std::snprintf(key, KEY_SIZE, "%x", static_cast<unsigned>(id));
38   return key;
39 }
40
41 char* F2C::get_key_id(char* key, int id) {
42   std::snprintf(key, KEY_SIZE, "%x_%d", static_cast<unsigned>(id), smpi_process()->index());
43   return key;
44 }
45
46 void F2C::delete_lookup(){
47   delete f2c_lookup_;
48 }
49
50 std::unordered_map<std::string, F2C*>* F2C::lookup()
51 {
52   return f2c_lookup_;
53 }
54
55 void F2C::free_f(int id)
56 {
57   char key[KEY_SIZE];
58   f2c_lookup_->erase(get_key(key, id));
59 }
60
61 int F2C::add_f()
62 {
63   if (f2c_lookup_ == nullptr)
64     f2c_lookup_ = new std::unordered_map<std::string, F2C*>;
65
66   char key[KEY_SIZE];
67   (*f2c_lookup_)[get_key(key, f2c_id_)] = this;
68   f2c_id_increment();
69   return f2c_id_-1;
70 }
71
72 int F2C::c2f()
73 {
74   if (f2c_lookup_ == nullptr) {
75     f2c_lookup_ = new std::unordered_map<std::string, F2C*>;
76   }
77
78   for (auto const& elm : *f2c_lookup_)
79     if (elm.second == this)
80       return std::stoi(elm.first);
81
82   /* this function wasn't found, add it */
83   return this->add_f();
84 }
85
86 F2C* F2C::f2c(int id)
87 {
88   if (f2c_lookup_ == nullptr)
89     f2c_lookup_ = new std::unordered_map<std::string, F2C*>;
90
91   if(id >= 0){
92     char key[KEY_SIZE];
93     auto comm = f2c_lookup_->find(get_key(key, id));
94     return comm == f2c_lookup_->end() ? nullptr : comm->second;
95   }else
96     return nullptr;
97 }
98
99 }
100 }