Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fa915c425c31395a97e32a912e24fff56a5f80f9
[simgrid.git] / src / smpi / include / smpi_f2c.hpp
1 /* Handle Fortran - C conversion for MPI Types*/
2
3 /* Copyright (c) 2010-2021. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 #ifndef SMPI_F2C_HPP_INCLUDED
10 #define SMPI_F2C_HPP_INCLUDED
11
12 #include <memory>
13 #include <unordered_map>
14
15 namespace simgrid{
16 namespace smpi{
17
18 class F2C {
19 private:
20   using f2c_lookup_type = std::unordered_map<unsigned int, F2C*>;
21
22   // We use a single lookup table for every type.
23   // Beware of collisions if id in mpif.h is not unique
24   static std::unique_ptr<f2c_lookup_type> f2c_lookup_;
25   static int f2c_id_;
26   static f2c_lookup_type::size_type num_default_handles_;
27   int my_f2c_id_ = -1;
28   bool deleted_ = false;
29
30 protected:
31   static void allocate_lookup()
32   {
33     if (not f2c_lookup_)
34       f2c_lookup_ = std::make_unique<f2c_lookup_type>();
35   }
36   int f2c_id() { return my_f2c_id_; }
37   static int global_f2c_id() { return f2c_id_; }
38   static void f2c_id_increment() { f2c_id_++; }
39   void mark_as_deleted() { deleted_ = true; };
40
41 public:
42   bool deleted() const { return deleted_; }
43   static f2c_lookup_type* lookup() { return f2c_lookup_.get(); }
44   F2C();
45   virtual ~F2C() = default;
46
47   int add_f();
48   static void free_f(int id) { if(id!=-1) f2c_lookup_->erase(id); }
49   int c2f();
50
51   // This method should be overridden in all subclasses to avoid casting the result when calling it.
52   // For the default one, the MPI_*_NULL returned is assumed to be NULL.
53   static F2C* f2c(int id);
54   static void finish_initialization() { num_default_handles_ = f2c_lookup_->size(); }
55   static f2c_lookup_type::size_type get_num_default_handles() { return num_default_handles_; }
56 };
57
58 }
59 }
60
61 #endif