Evita  0.16
evThreadedController.h
Go to the documentation of this file.
1 /*
2  *
3  * EVITA: Efficient Visualization of Terascale Datasets
4  * Copyright (C) 2000-2016 Team Evita
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  *
20  */
21 
22 /*
23  * This code was derived from vtk's vtkThreadedController class.
24  * The original copyright notice for the code is shown below.
25  *
26  * Modifications from the original source are:
27  * 09-may-2001: Added Send()/Receive() for pointer data
28  * 09-may-2001: Receive() change to be nonblocking or blocking
29  *
30  */
31 
32 /*=========================================================================
33 
34  Program: Visualization Toolkit
35  Module: $RCSfile: evThreadedController.h,v $
36  Language: C++
37  Date: $Date: 2016/05/15 20:48:50 $
38  Version: $Revision: 1.6 $
39 
40 Copyright (c) 1993-2001 Ken Martin, Will Schroeder, Bill Lorensen
41 All rights reserved.
42 
43 Redistribution and use in source and binary forms, with or without
44 modification, are permitted provided that the following conditions are met:
45 
46  * Redistributions of source code must retain the above copyright notice,
47  this list of conditions and the following disclaimer.
48 
49  * Redistributions in binary form must reproduce the above copyright notice,
50  this list of conditions and the following disclaimer in the documentation
51  and/or other materials provided with the distribution.
52 
53  * Neither name of Ken Martin, Will Schroeder, or Bill Lorensen nor the names
54  of any contributors may be used to endorse or promote products derived
55  from this software without specific prior written permission.
56 
57  * Modified source versions must be plainly marked as such, and must not be
58  misrepresented as being the original software.
59 
60 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
61 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63 ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR
64 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
66 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
67 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
68 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
69 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
70 
71 =========================================================================*/
72 // .NAME evThreadedController - Allows communication between running threads.
73 // .SECTION Description
74 // evThreadedController just uses a vtkMultiThreader to spawn threads.
75 // It the implements sends and receives using shared memory and reference
76 // counting.
77 
78 // Unfortunately, as this is written, it is not thread safe. All threads
79 // use the same controller object, so operations like adding an RMI could
80 // potentially conflict. We need to have our own RegisterAndGetGlobalController
81 // method to create different controllers for each thread. This would also
82 // simplify the GetLocalProcessId methods.
83 
84 
85 // .SECTION see also
86 // vtkDownStreamPort vtkUpStreamPort vtkMultiThreader evMultiProcessController
87 
88 #ifndef __evThreadedController_h
89 #define __evThreadedController_h
90 
91 #include "vtkObject.h"
93 #include "vtkMultiThreader.h"
94 
95 class evThreadedControllerMessage;
96 
97 // Added for evThreadedController
98 #define EVBLOCKING 0
99 #define EVNONBLOCKING 1
100 
101 
103 {
104 public:
105  static evThreadedController *New();
107  void PrintSelf(ostream& os, vtkIndent indent);
108 
109  // Description:
110  // This method is for setting up the processes.
111  void Initialize(int argc, char *arcv[]);
112 
113  // Description:
114  // This method returns an integer from 0 to (NumberOfProcesses-1)
115  // indicating which process we are in.
116  // Note: The correct controller is passed as an argument to
117  // the initial function (SingleMethod/MultipleMethod). Calling this
118  // method on another controller may give wrong results.
119  vtkGetMacro(LocalProcessId, int);
120 
121  // Description:
122  // Execute the SingleMethod (as define by SetSingleMethod) using
123  // this->NumberOfProcesses processes. This will only return when
124  // all the processes finish executing their methods.
125  void SingleMethodExecute();
126 
127  // Description:
128  // Execute the MultipleMethods (as define by calling SetMultipleMethod
129  // for each of the required this->NumberOfProcesses methods) using
130  // this->NumberOfProcesses processes.
131  void MultipleMethodExecute();
132 
133  //------------------ Communication --------------------
134 
135  // Description:
136  // This method sends data to another process. Tag eliminates ambiguity
137  // and is used to match sends with receives.
138  int Send(vtkDataObject *data, int remoteProcessId, int tag);
139  int Send(int *data, int length, int remoteProcessId, int tag);
140  int Send(unsigned long *data, int length, int remoteProcessId, int tag);
141  int Send(char *data, int length, int remoteProcessId, int tag);
142  int Send(double *data, int length, int remoteProcessId, int tag);
143  int Send(void **data, int remoteProcessId, int tag);
144 
145  void Clear(int remoteProcessId);
146 
147  // Description:
148  // This method receives data from a corresponding send. It blocks
149  // until the receive is finished.
150  int Receive(vtkDataObject *data, int remoteProcessId, int tag);
151  int Receive(int *data, int length, int remoteProcessId, int tag);
152  int Receive(unsigned long *data, int length, int remoteProcessId, int tag);
153  int Receive(char *data, int length, int remoteProcessId, int tag);
154  int Receive(double *data, int length, int remoteProcessId, int tag);
155  int Receive(void **data, int remoteProcessId, int tag);
156 
157  // Description:
158  // First method called after threads are spawned.
159  // It is public because the function evThreadedControllerStart
160  // is not a friend yet. You should not call this method.
161  void Start(int threadIdx);
162 
163 protected:
168 
169  void CreateProcessControllers();
170  evThreadedControllerMessage *NewMessage(vtkDataObject *object,
171  void *data, int dataLength);
172  void DeleteMessage(evThreadedControllerMessage *message);
173  void AddMessage(evThreadedControllerMessage *message);
174  evThreadedControllerMessage *FindMessage(int sendId, int tag);
175  void ClearMessages(int sendId);
176  void RemoveMessage(evThreadedControllerMessage *message);
177 
178  // Each Process/Thread has its own controller.
180 
181  // Required only for static access to threadId (GetLocalController).
182 #ifdef VTK_USE_PTHREADS
183  pthread_t ThreadIds[VTK_MP_CONTROLLER_MAX_PROCESSES];
184 #endif
185 #ifdef VTK_USE_SPROC
186  pid_t ThreadIds[VTK_MP_CONTROLLER_MAX_PROCESSES];
187 #endif
188 
189  // The id for this objects process.
192 
193  vtkMultiThreader *MultiThreader;
194  // Used internally to switch between multiple and single method execution.
196 
197  // It is not enough to block on the messages, we have to mutex
198  // the whole send interaction. I was trying to avoid a central
199  // mutex (oh well).
200  vtkMutexLock *MessageListLock;
201 
202  // This mutex is normally locked. It is used to block the execution
203  // of the receiving process when the send has not been called yet.
204  vtkMutexLock *Gate;
205 
206  // Double linked list.
207  evThreadedControllerMessage *MessageListStart;
208  evThreadedControllerMessage *MessageListEnd;
209 
210  // Trying to track down lockups.
211  FILE *LogFile;
212 
213  // The generic send and receive methods.
214  int Send(vtkDataObject *object, void *data, int dataLength,
215  int remoteProcessId, int tag);
216  int Receive(vtkDataObject *object, void *data, int dataLength,
217  int remoteProcessId, int tag);
218 
219  // For static GetGlobalController. Translates controller for thread0
220  // to controller for local thread.
222 
223 };
224 
225 
226 #endif
227 
228 
void PrintSelf(ostream &os, vtkIndent indent)
void operator=(const evThreadedController &)
Definition: evThreadedController.h:167
FILE * LogFile
Definition: evThreadedController.h:211
virtual int Receive(vtkDataObject *data, int remoteProcessId, int tag)
Definition: evThreadedController.h:102
virtual void SingleMethodExecute()=0
evThreadedControllerMessage * MessageListEnd
Definition: evThreadedController.h:208
vtkMutexLock * MessageListLock
Definition: evThreadedController.h:200
virtual void Initialize(int vtkNotUsed(argc), char *arcv[])
Definition: evMultiProcessController.h:133
vtkTypeMacro(evMultiProcessController, vtkObject)
vtkMutexLock * Gate
Definition: evThreadedController.h:204
int WaitingForId
Definition: evThreadedController.h:191
static evMultiProcessController * New()
int MultipleMethodFlag
Definition: evThreadedController.h:195
#define VTK_MP_CONTROLLER_MAX_PROCESSES
Definition: evMultiProcessController.h:91
evThreadedController(const evThreadedController &)
Definition: evThreadedController.h:166
virtual void MultipleMethodExecute()=0
vtkGetMacro(NumberOfProcesses, int)
Definition: evMultiProcessController.h:122
virtual int Send(vtkDataObject *data, int remoteProcessId, int tag)
vtkMultiThreader * MultiThreader
Definition: evThreadedController.h:193
int LocalProcessId
Definition: evThreadedController.h:190
virtual void Clear(int remoteProcessId)=0
evThreadedControllerMessage * MessageListStart
Definition: evThreadedController.h:207
virtual evMultiProcessController * GetLocalController()