Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines for 2022.
[simgrid.git] / src / smpi / include / smpi_f2c.hpp
1 /* Handle Fortran - C conversion for MPI Types*/
2
3 /* Copyright (c) 2010-2022. 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 #include <string>
15
16 namespace simgrid{
17 namespace smpi{
18
19 class F2C {
20 private:
21   using f2c_lookup_type = std::unordered_map<unsigned int, F2C*>;
22
23   // We use a single lookup table for every type.
24   // Beware of collisions if id in mpif.h is not unique
25   static std::unique_ptr<f2c_lookup_type> f2c_lookup_;
26   static int f2c_id_;
27   static f2c_lookup_type::size_type num_default_handles_;
28   int my_f2c_id_ = -1;
29   bool deleted_ = false;
30   std::string call_location_;
31
32 protected:
33   static void allocate_lookup()
34   {
35     if (not f2c_lookup_)
36       f2c_lookup_ = std::make_unique<f2c_lookup_type>();
37   }
38   int f2c_id() const { return my_f2c_id_; }
39   static int global_f2c_id() { return f2c_id_; }
40   static void f2c_id_increment() { f2c_id_++; }
41
42 public:
43   void mark_as_deleted() { deleted_ = true; };
44   bool deleted() const { return deleted_; }
45   static f2c_lookup_type* lookup() { return f2c_lookup_.get(); }
46   F2C();
47   virtual ~F2C() = default;
48   virtual std::string name() const = 0;
49
50   int add_f();
51   static void free_f(int id) { if(id!=-1) f2c_lookup_->erase(id); }
52   int c2f();
53
54   // This method should be overridden in all subclasses to avoid casting the result when calling it.
55   // For the default one, the MPI_*_NULL returned is assumed to be NULL.
56   static F2C* f2c(int id);
57   static void finish_initialization() { num_default_handles_ = f2c_lookup_->size(); }
58   static f2c_lookup_type::size_type get_num_default_handles() { return num_default_handles_; }
59   const std::string& call_location() const { return call_location_; }
60 };
61
62 }
63 }
64
65 #endif