vtkMultiThreader.cxx 10.1 KB
Newer Older
Lisa Avila's avatar
Lisa Avila committed
1
2
3
4
#include "vtkMultiThreader.h"

// These are the includes necessary for multithreaded rendering on an SGI
// using the sproc() call
Ken Martin's avatar
Ken Martin committed
5
#ifdef VTK_USE_SPROC
Lisa Avila's avatar
Lisa Avila committed
6
7
8
9
10
#include <sys/resource.h>
#include <sys/prctl.h>
#include <wait.h>
#endif

Ken Martin's avatar
Ken Martin committed
11
#ifdef VTK_USE_PTHREADS
Lisa Avila's avatar
Lisa Avila committed
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <pthread.h>
#endif

// Description:
// Constructor. Default all the methods to NULL. Since the
// ThreadInfoArray is static, the ThreadIDs can be initialized here
// and will not change.
vtkMultiThreader::vtkMultiThreader()
{
  int i;

  for ( i = 0; i < VTK_MAX_THREADS; i++ )
    {
    this->ThreadInfoArray[i].ThreadID = i;
    this->MultipleMethod[i] = NULL;
    }

  this->SingleMethod = NULL;

Ken Martin's avatar
Ken Martin committed
31
#ifdef VTK_USE_SPROC
Lisa Avila's avatar
Lisa Avila committed
32
33
34
35
36
  // Default the number of threads to be the number of available
  // processors if we are using sproc()
  this->ThreadCount             = prctl( PR_MAXPPROCS );
#endif

Ken Martin's avatar
Ken Martin committed
37
#ifdef VTK_USE_PTHREADS
Lisa Avila's avatar
Lisa Avila committed
38
39
40
41
42
  // Default the number of threads to be the number of available
  // processors if we are using pthreads()
  this->ThreadCount             = sysconf( _SC_NPROCESSORS_ONLN );
#endif

Ken Martin's avatar
Ken Martin committed
43
44
#ifndef VTK_USE_SPROC
#ifndef VTK_USE_PTHREADS
Lisa Avila's avatar
Lisa Avila committed
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
  // If we are not multithreading, the number of threads should
  // always be 1
  this->ThreadCount             = 1;
#endif  
#endif  

}

// Description:
// Destructor. Nothing allocated so nothing needs to be done here.
vtkMultiThreader::~vtkMultiThreader()
{
}

// Description:
// Set the user defined method that will be run on ThreadCount threads
// when SingleMethodExecute is called.
void vtkMultiThreader::SetSingleMethod( vtkThreadFunctionType (*f)(void *), 
					void *data )
{ 
  this->SingleMethod = f;
  this->SingleData   = data;
}

// Description:
// Set one of the user defined methods that will be run on ThreadCount
// threads when MultipleMethodExecute is called. This method should be
// called with index = 0, 1, ..,  ThreadCount-1 to set up all the
// required user defined methods
void vtkMultiThreader::SetMultipleMethod( int index, 
					  vtkThreadFunctionType (*f)(void *), 
					  void *data )
{ 
  // You can only set the method for 0 through ThreadCount-1
Charles Law's avatar
Charles Law committed
79
  if ( index >= this->ThreadCount ) {
Lisa Avila's avatar
Lisa Avila committed
80
    vtkErrorMacro( << "Can't set method " << index << 
Charles Law's avatar
Charles Law committed
81
82
    " with a thread count of " << this->ThreadCount );
    }
Lisa Avila's avatar
Lisa Avila committed
83
84
85
86
87
88
89
90
91
92
93
94
  else
    {
    this->MultipleMethod[index] = f;
    this->MultipleData[index]   = data;
    }
}

// Description:
// Execute the method set as the SingleMethod on ThreadCount threads.
void vtkMultiThreader::SingleMethodExecute()
{

Ken Martin's avatar
Ken Martin committed
95
#ifdef VTK_USE_SPROC
Lisa Avila's avatar
Lisa Avila committed
96
97
98
99
100
  int                thread_loop;
  siginfo_t          info_ptr;
  int                process_id[VTK_MAX_THREADS];
#endif

Ken Martin's avatar
Ken Martin committed
101
#ifdef VTK_USE_PTHREADS
Lisa Avila's avatar
Lisa Avila committed
102
103
104
105
106
107
108
109
110
111
112
113
114
  int                thread_loop;
  pthread_t          process_id[VTK_MAX_THREADS];
#endif

  if ( !this->SingleMethod )
    {
    vtkErrorMacro( << "No single method set!" );
    return;
    }

  // We are using sproc (on SGIs), pthreads(on Suns), or a single thread
  // (the default)  

Ken Martin's avatar
Ken Martin committed
115
#ifdef VTK_USE_SPROC
Lisa Avila's avatar
Lisa Avila committed
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
  // Using sproc() on an SGI
  //
  // We want to use sproc to start this->ThreadCount - 1 additional
  // threads which will be used to call this->SingleMethod(). The
  // parent thread will also call this routine.  When it is done,
  // it will wait for all the children to finish. 
  //
  // First, start up the this->ThreadCount-1 processes.  Keep track
  // of their process ids for use later in the waitid call
  for ( thread_loop = 0; thread_loop < this->ThreadCount-1; thread_loop++ )
    {
    this->ThreadInfoArray[thread_loop].UserData    = this->SingleData;
    this->ThreadInfoArray[thread_loop].ThreadCount = this->ThreadCount;
    process_id[thread_loop] = 
      sproc( this->SingleMethod, PR_SADDR, 
	     ( (void *)(&this->ThreadInfoArray[thread_loop]) ) );
    }
  
  // Now, the parent thread calls this->SingleMethod() itself
  this->ThreadInfoArray[this->ThreadCount-1].UserData    = this->SingleData;
  this->ThreadInfoArray[this->ThreadCount-1].ThreadCount = this->ThreadCount;
  this->SingleMethod( 
    (void *)(&this->ThreadInfoArray[this->ThreadCount-1]) );

  // The parent thread has finished this->SingleMethod() - so now it
  // waits for each of the other processes to exit
  for ( thread_loop = 0; thread_loop < this->ThreadCount-1; thread_loop++ )
    {
    waitid( P_PID, (id_t) process_id[thread_loop], &info_ptr, WEXITED );
    }
#endif

Ken Martin's avatar
Ken Martin committed
148
#ifdef VTK_USE_PTHREADS
Lisa Avila's avatar
Lisa Avila committed
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
  // Using POSIX threads
  //
  // We want to use pthread_create to start this->ThreadCount - 1 additional
  // threads which will be used to call this->SingleMethod(). The
  // parent thread will also call this routine.  When it is done,
  // it will wait for all the children to finish. 
  //
  // First, start up the this->ThreadCount-1 processes.  Keep track
  // of their process ids for use later in the pthread_join call

pthread_attr_t attr;

pthread_attr_init(&attr);
pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);

  for ( thread_loop = 0; thread_loop < this->ThreadCount-1; thread_loop++ )
    {
    this->ThreadInfoArray[thread_loop].UserData    = this->SingleData;
    this->ThreadInfoArray[thread_loop].ThreadCount = this->ThreadCount;
    pthread_create( &(process_id[thread_loop]),
		      &attr, this->SingleMethod,  
		      ( (void *)(&this->ThreadInfoArray[thread_loop]) ) );
    }
  
  // Now, the parent thread calls this->SingleMethod() itself
  this->ThreadInfoArray[this->ThreadCount-1].UserData    = this->SingleData;
  this->ThreadInfoArray[this->ThreadCount-1].ThreadCount = this->ThreadCount;
  this->SingleMethod( 
    (void *)(&this->ThreadInfoArray[this->ThreadCount - 1]) );

  // The parent thread has finished this->SingleMethod() - so now it
  // waits for each of the other processes to exit
  for ( thread_loop = 0; thread_loop < this->ThreadCount-1; thread_loop++ )
    {
    pthread_join( process_id[thread_loop], NULL );
    }
#endif

Ken Martin's avatar
Ken Martin committed
187
188
#ifndef VTK_USE_SPROC
#ifndef VTK_USE_PTHREADS
Lisa Avila's avatar
Lisa Avila committed
189
190
191
192
193
194
195
196
197
198
199
200
  // There is no multi threading, so there is only one thread.
  this->ThreadInfoArray[0].UserData    = this->SingleData;
  this->ThreadInfoArray[0].ThreadCount = this->ThreadCount;
  this->SingleMethod( (void *)(&this->ThreadInfoArray[0]) );
#endif
#endif
}

void vtkMultiThreader::MultipleMethodExecute()
{
  int                thread_loop;

Ken Martin's avatar
Ken Martin committed
201
#ifdef VTK_USE_SPROC
Lisa Avila's avatar
Lisa Avila committed
202
203
204
205
  siginfo_t          info_ptr;
  int                process_id[VTK_MAX_THREADS];
#endif

Ken Martin's avatar
Ken Martin committed
206
#ifdef VTK_USE_PTHREADS
Lisa Avila's avatar
Lisa Avila committed
207
208
209
210
211
212
213
214
215
216
217
218
219
220
  pthread_t          process_id[VTK_MAX_THREADS];
#endif


  for ( thread_loop = 0; thread_loop < this->ThreadCount; thread_loop++ )
    if ( this->MultipleMethod[thread_loop] == (void *)NULL)
      {
      vtkErrorMacro( << "No multiple method set for: " << thread_loop );
      return;
      }

  // We are using sproc (on SGIs), pthreads(on Suns), or a single thread
  // (the default)  

Ken Martin's avatar
Ken Martin committed
221
#ifdef VTK_USE_SPROC
Lisa Avila's avatar
Lisa Avila committed
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
  // Using sproc() on an SGI
  //
  // We want to use sproc to start this->ThreadCount - 1 additional
  // threads which will be used to call the ThreadCount-1 methods
  // defined in this->MultipleMethods[](). The parent thread
  // will call this->MultipleMethods[ThreadCount-1]().  When it is done,
  // it will wait for all the children to finish. 
  //
  // First, start up the this->ThreadCount-1 processes.  Keep track
  // of their process ids for use later in the waitid call
  for ( thread_loop = 0; thread_loop < this->ThreadCount-1; thread_loop++ )
    {
    this->ThreadInfoArray[thread_loop].UserData = 
      this->MultipleData[thread_loop];
    this->ThreadInfoArray[thread_loop].ThreadCount = this->ThreadCount;
    process_id[thread_loop] = 
      sproc( this->MultipleMethod[thread_loop], PR_SADDR, 
	     ( (void *)(&this->ThreadInfoArray[thread_loop]) ) );
    }
  
  // Now, the parent thread calls the last method itself
  this->ThreadInfoArray[this->ThreadCount-1].UserData = 
    this->MultipleData[this->ThreadCount-1];
  this->ThreadInfoArray[this->ThreadCount-1].ThreadCount = this->ThreadCount;
  (this->MultipleMethod[this->ThreadCount-1])( 
    (void *)(&this->ThreadInfoArray[this->ThreadCount-1]) );

  // The parent thread has finished its method - so now it
  // waits for each of the other processes (created with sproc) to
  // exit
  for ( thread_loop = 0; thread_loop < this->ThreadCount-1; thread_loop++ )
    {
    waitid( P_PID, (id_t) process_id[thread_loop], &info_ptr, WEXITED );
    }
#endif

Ken Martin's avatar
Ken Martin committed
258
#ifdef VTK_USE_PTHREADS
Lisa Avila's avatar
Lisa Avila committed
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
  // Using POSIX threads
  //
  // We want to use pthread_create to start this->ThreadCount - 1 additional
  // threads which will be used to call the ThreadCount-1 methods
  // defined in this->MultipleMethods[](). The parent thread
  // will call this->MultipleMethods[ThreadCount-1]().  When it is done,
  // it will wait for all the children to finish. 
  //
  // First, start up the this->ThreadCount-1 processes.  Keep track
  // of their process ids for use later in the pthread_join call
  for ( thread_loop = 0; thread_loop < this->ThreadCount-1; thread_loop++ )
    {
    this->ThreadInfoArray[thread_loop].UserData = 
      this->MultipleData[thread_loop];
    this->ThreadInfoArray[thread_loop].ThreadCount = this->ThreadCount;
    pthread_create( &(process_id[thread_loop]),
		      NULL, this->MultipleMethod[thread_loop],  
		      ( (void *)(&this->ThreadInfoArray[thread_loop]) ) );
    }
  
  // Now, the parent thread calls the last method itself
  this->ThreadInfoArray[this->ThreadCount-1].UserData = 
    this->MultipleData[this->ThreadCount-1];
  this->ThreadInfoArray[this->ThreadCount-1].ThreadCount = this->ThreadCount;
  (this->MultipleMethod[this->ThreadCount-1])( 
    (void *)(&this->ThreadInfoArray[this->ThreadCount - 1]) );

  // The parent thread has finished its method - so now it
  // waits for each of the other processes to exit
  for ( thread_loop = 0; thread_loop < this->ThreadCount-1; thread_loop++ )
    {
    pthread_join( process_id[thread_loop], NULL );
    }
#endif

Ken Martin's avatar
Ken Martin committed
294
295
#ifndef VTK_USE_SPROC
#ifndef VTK_USE_PTHREADS
Lisa Avila's avatar
Lisa Avila committed
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
  // There is no multi threading, so there is only one thread.
  this->ThreadInfoArray[0].UserData    = this->MultipleData[0];
  this->ThreadInfoArray[0].ThreadCount = this->ThreadCount;
  (this->MultipleMethod[0])( (void *)(&this->ThreadInfoArray[0]) );
#endif
#endif
}

// Description:
// Print method for the multithreader
void vtkMultiThreader::PrintSelf(ostream& os, vtkIndent indent)
{
  
  os << indent << "Thread Count: " << this->ThreadCount << "\n";

}