parallel_sim.hpp
Go to the documentation of this file.
1 /**********************************************************************
2  * parallel_sim.hpp -- Primitives used for parallel simulation *
3  * *
4  * Author: Hosein Attarzadeh (shan2@kth.se) *
5  * *
6  * Purpose: Providing primitive elements required for simulating *
7  * ForSyDe models using MPI *
8  * *
9  * Usage: This file is included automatically *
10  * *
11  * License: BSD3 *
12  *******************************************************************/
13 
14 #ifndef PARALLELSIM_HPP
15 #define PARALLELSIM_HPP
16 
24 #include <mpi.h>
25 
26 namespace ForSyDe
27 {
28 
29 namespace SY
30 {
31 
32 using namespace sc_core;
33 
34 
36 
39 template <typename T1>
40 class sender : public sy_process
41 {
42 public:
44 
46 
50  sender(sc_module_name _name,
51  int destination,
52  int tag
53  ) : sy_process(_name), iport1("iport1"),
54  destination(destination), tag(tag)
55  {
56 #ifdef FORSYDE_INTROSPECTION
57  arg_vec.push_back(std::make_tuple("destination",std::to_string(destination)));
58  arg_vec.push_back(std::make_tuple("tag",std::to_string(tag)));
59 #endif
60  }
61 
63  std::string forsyde_kind() const {return "SY::sender";}
64 
65 private:
66  int destination;
67  int tag;
68 
69  // Inputs and output variables
70  T1* ival1;
71 
72  //Implementing the abstract semantics
73  void init()
74  {
75  ival1 = new T1;
76  }
77 
78  void prep()
79  {
80  abst_ext<T1> temp_val = iport1.read();
81  *ival1 = unsafe_from_abst_ext(temp_val);
82  }
83 
84  void exec() {}
85 
86  void prod()
87  {
88  MPI_Request request;
89  MPI_Status status;
90  MPI_Isend(ival1, sizeof(T1), MPI_BYTE, destination, tag, MPI_COMM_WORLD, &request);
91  int flag=0;
92  while (true)
93  {
94  MPI_Test(&request, &flag, &status);
95  if (flag) break; else wait(0,SC_NS);
96  }
97  }
98 
99  void clean()
100  {
101  delete ival1;
102  }
103 
104 #ifdef FORSYDE_INTROSPECTION
105  void bindInfo()
106  {
107  boundInChans.resize(1); // only one input port
108  boundInChans[0].port = &iport1;
109  }
110 #endif
111 };
112 
114 
118 template <typename T0>
119 class receiver : public sy_process
120 {
121 public:
123 
125 
129  receiver(sc_module_name _name,
130  int source,
131  int tag
132  ) : sy_process(_name), oport1("oport1"),
133  source(source), tag(tag)
134  {
135 #ifdef FORSYDE_INTROSPECTION
136  arg_vec.push_back(std::make_tuple("source",std::to_string(source)));
137  arg_vec.push_back(std::make_tuple("tag",std::to_string(tag)));
138 #endif
139  }
140 
142  std::string forsyde_kind() const {return "SY::receiver";}
143 
144 private:
145  int source;
146  int tag;
147 
148  // Inputs and output variables
149  abst_ext<T0>* oval1;
150 
151  //Implementing the abstract semantics
152  void init()
153  {
154  oval1 = new abst_ext<T0>;
155  }
156 
157  void prep()
158  {
159  T0 temp_val;
160  MPI_Status status;
161  MPI_Request request;
162  MPI_Irecv(&temp_val, sizeof(T0), MPI_BYTE, source, tag, MPI_COMM_WORLD, &request);
163  int flag=0;
164  while (true)
165  {
166  MPI_Test(&request, &flag, &status);
167  if (flag) break; else wait(0,SC_NS);
168  }
169  set_val(*oval1, temp_val);
170  }
171 
172  void exec() {}
173 
174  void prod()
175  {
176  WRITE_MULTIPORT(oport1, *oval1)
177  }
178 
179  void clean()
180  {
181  delete oval1;
182  }
183 
184 #ifdef FORSYDE_INTROSPECTION
185  void bindInfo()
186  {
187  boundOutChans.resize(1); // only one output port
188  boundOutChans[0].port = &oport1;
189  }
190 #endif
191 };
192 
193 
194 }
195 }
196 
197 #endif
sender(sc_module_name _name, int destination, int tag)
The constructor requires the module name.
Definition: parallel_sim.hpp:50
The namespace for ForSyDe.
Definition: abssemantics.hpp:30
std::string forsyde_kind() const
Specifying from which process constructor is the module built.
Definition: parallel_sim.hpp:63
std::string forsyde_kind() const
Specifying from which process constructor is the module built.
Definition: parallel_sim.hpp:142
SY_in< T1 > iport1
port for the input channel
Definition: parallel_sim.hpp:43
Process constructor for a source process.
Definition: sy_process_constructors.hpp:1092
SY_out< T0 > oport1
port for the output channel
Definition: parallel_sim.hpp:122
The process constructor which defines the abstract semantics of execution.
Definition: abssemantics.hpp:212
Process constructor for a sender process with one input.
Definition: parallel_sim.hpp:40
Process constructor for a receiver process with one output.
Definition: parallel_sim.hpp:119
receiver(sc_module_name _name, int source, int tag)
The constructor requires the module name.
Definition: parallel_sim.hpp:129