OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
Poller.h
Go to the documentation of this file.
1 #ifndef __POLLER_H__
2 #define __POLLER_H__
3 
4 #include <iostream>
5 
6 #include "Util/MPIHelper.h"
7 
18 class Poller {
19 
20 public:
21 
22  Poller(MPI_Comm comm, double delay = 0.1)
23  : comm_m(comm)
24  , is_running_(false)
25  , poll_delay_(delay)
26  {
27  last_polled_ = MPI_Wtime();
28  }
29 
30  virtual ~Poller()
31  {}
32 
33 protected:
35  MPI_Comm comm_m;
36 
38 
40  double last_polled_;
42  double poll_delay_;
43 
48  virtual bool onMessage(MPI_Status status, size_t recv_value) = 0;
50  virtual void onStop() = 0;
51 
53  virtual void setupPoll() = 0;
55  virtual void prePoll() = 0;
57  virtual void postPoll() = 0;
58 
62  virtual void run() {
63  MPI_Status status;
64  MPI_Request req;
65  size_t recv_value = 0;
66  int flag = 0;
67 
68  setupPoll();
69 
70  MPI_Irecv(&recv_value, 1, MPI_UNSIGNED_LONG, MPI_ANY_SOURCE, MPI_ANY_TAG,
71  comm_m, &req);
72 
73  is_running_ = true;
74 
75  while(true) {
76 
77  // regulate the amount of MPI_Test calls (expensive)
78  double tnow = MPI_Wtime();
79  if(tnow - last_polled_ > poll_delay_)
80  last_polled_ = tnow;
81  else
82  continue;
83 
84  prePoll();
85 
86  if(req != MPI_REQUEST_NULL) {
87  MPI_Test(&req, &flag, &status);
88  if(flag) {
89  if(status.MPI_TAG == MPI_STOP_TAG) {
90  is_running_ = false;
91  onStop();
92  return;
93  } else {
94  if(onMessage(status, recv_value))
95  MPI_Irecv(&recv_value, 1, MPI_UNSIGNED_LONG, MPI_ANY_SOURCE,
96  MPI_ANY_TAG, comm_m, &req);
97  else
98  break;
99  }
100  }
101  }
102 
103  postPoll();
104  }
105  }
106 };
107 
108 #endif
double last_polled_
time of last MPI_Test
Definition: Poller.h:40
virtual void postPoll()=0
executed after handling (if any) new request
double poll_delay_
delay in seconds between polls
Definition: Poller.h:42
virtual void prePoll()=0
executed before checking for new request
virtual void run()
Definition: Poller.h:62
virtual void setupPoll()=0
executed before starting polling loop
virtual bool onMessage(MPI_Status status, size_t recv_value)=0
Poller(MPI_Comm comm, double delay=0.1)
Definition: Poller.h:22
virtual void onStop()=0
enable implementation to react to STOP tag
An interface implementing the basics of a poll loop, posting an MPI_Irecv and waiting for new request...
Definition: Poller.h:18
bool is_running_
Definition: Poller.h:37
virtual ~Poller()
Definition: Poller.h:30
MPI_Comm comm_m
communicator the poller listens to requests
Definition: Poller.h:35
#define MPI_STOP_TAG
global stop tag to exit poll loop (
Definition: MPIHelper.h:44