Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
978722d37d2bcc21aff0ead0dedda38447750ae5
[simgrid.git] / src / smpi / 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 "src/smpi/private.h"
7 #include "src/smpi/smpi_f2c.hpp"
8 #include "src/smpi/smpi_process.hpp"
9
10 #include <cstdio>
11
12 namespace simgrid{
13 namespace smpi{
14
15 xbt_dict_t F2C::f2c_lookup_=nullptr;
16 int F2C::f2c_id_=0;
17
18 xbt_dict_t F2C::f2c_lookup(){
19   return f2c_lookup_;
20 }
21
22 void F2C::set_f2c_lookup(xbt_dict_t dict){
23   f2c_lookup_=dict;
24 }
25
26 void F2C::f2c_id_increment(){
27   f2c_id_++;
28 };
29
30 int F2C::f2c_id(){
31   return f2c_id_;
32 };
33
34 char* F2C::get_key(char* key, int id) {
35   std::snprintf(key, KEY_SIZE, "%x",id);
36   return key;
37 }
38
39 char* F2C::get_key_id(char* key, int id) {
40   std::snprintf(key, KEY_SIZE, "%x_%d",id, smpi_process()->index());
41   return key;
42 }
43
44 void F2C::delete_lookup(){
45   xbt_dict_free(&f2c_lookup_);
46 }
47
48 xbt_dict_t F2C::lookup(){
49   return f2c_lookup_;
50 }
51
52 void F2C::free_f(int id){
53   char key[KEY_SIZE];
54   xbt_dict_remove(f2c_lookup_, get_key(key, id));
55 }
56
57 int F2C::add_f(){
58   if(f2c_lookup_==nullptr){
59     f2c_lookup_=xbt_dict_new_homogeneous(nullptr);
60   }
61   char key[KEY_SIZE];
62   xbt_dict_set(f2c_lookup_, get_key(key, f2c_id_), this, nullptr);
63   f2c_id_increment();
64   return f2c_id_-1;
65 }
66
67 int F2C::c2f(){
68   if(f2c_lookup_==nullptr){
69     f2c_lookup_=xbt_dict_new_homogeneous(nullptr);
70   }
71
72   char* existing_key = xbt_dict_get_key(f2c_lookup_, this);
73   if(existing_key!=nullptr){
74     return atoi(existing_key);}
75   else{
76     return this->add_f();}
77 }
78
79 F2C* F2C::f2c(int id){
80   if(f2c_lookup_==nullptr){
81     f2c_lookup_=xbt_dict_new_homogeneous(nullptr);
82   }
83   char key[KEY_SIZE];
84   if(id >= 0){
85     return static_cast<F2C*>(xbt_dict_get_or_null(f2c_lookup_, get_key(key, id)));
86   }else
87     return NULL;
88 }
89
90 }
91 }