vtkMultiThreader.cxx 15.8 KB
Newer Older
Ken Martin's avatar
Ken Martin committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkMultiThreader.cxx
  Language:  C++
  Date:      $Date$
  Version:   $Revision$


Copyright (c) 1993-1996 Ken Martin, Will Schroeder, Bill Lorensen.

This software is copyrighted by Ken Martin, Will Schroeder and Bill Lorensen.
The following terms apply to all files associated with the software unless
explicitly disclaimed in individual files. This copyright specifically does
not apply to the related textbook "The Visualization Toolkit" ISBN
013199837-4 published by Prentice Hall which is covered by its own copyright.

The authors hereby grant permission to use, copy, and distribute this
software and its documentation for any purpose, provided that existing
copyright notices are retained in all copies and that this notice is included
verbatim in any distributions. Additionally, the authors grant permission to
modify this software and its documentation for any purpose, provided that
such modifications are not distributed without the explicit consent of the
authors and that existing copyright notices are retained in all copies. Some
of the algorithms implemented by this software are patented, observe all
applicable patent law.

IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY FOR
DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY DERIVATIVES THEREOF,
EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE, AND NON-INFRINGEMENT.  THIS SOFTWARE IS PROVIDED ON AN
"AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO PROVIDE
MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.


=========================================================================*/
Lisa Avila's avatar
Lisa Avila committed
41
42
43
44
#include "vtkMultiThreader.h"

// These are the includes necessary for multithreaded rendering on an SGI
// using the sproc() call
Lisa Avila's avatar
Lisa Avila committed
45
#ifdef VTK_USE_SPROC
Lisa Avila's avatar
Lisa Avila committed
46
47
48
49
50
#include <sys/resource.h>
#include <sys/prctl.h>
#include <wait.h>
#endif

Lisa Avila's avatar
Lisa Avila committed
51
#ifdef VTK_USE_PTHREADS
Lisa Avila's avatar
Lisa Avila committed
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#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;

Lisa Avila's avatar
Lisa Avila committed
71
#ifdef VTK_USE_SPROC
Lisa Avila's avatar
Lisa Avila committed
72
73
74
75
76
  // Default the number of threads to be the number of available
  // processors if we are using sproc()
  this->ThreadCount             = prctl( PR_MAXPPROCS );
#endif

Lisa Avila's avatar
Lisa Avila committed
77
#ifdef VTK_USE_PTHREADS
Lisa Avila's avatar
Lisa Avila committed
78
79
80
81
82
  // 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
83
84
85
86
87
88
89
90
91
#ifdef _WIN32
  {
    SYSTEM_INFO sysInfo;
    GetSystemInfo(&sysInfo);
    this->ThreadCount = sysInfo.dwNumberOfProcessors;
  }
#endif

#ifndef _WIN32
Lisa Avila's avatar
Lisa Avila committed
92
93
#ifndef VTK_USE_SPROC
#ifndef VTK_USE_PTHREADS
Lisa Avila's avatar
Lisa Avila committed
94
95
96
97
98
  // If we are not multithreading, the number of threads should
  // always be 1
  this->ThreadCount             = 1;
#endif  
#endif  
Ken Martin's avatar
Ken Martin committed
99
#endif
Lisa Avila's avatar
Lisa Avila committed
100
101
102
103
104
105
106
107
108
109
110
}

// 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.
Ken Martin's avatar
Ken Martin committed
111
void vtkMultiThreader::SetSingleMethod( vtkThreadFunctionType f, 
Lisa Avila's avatar
Lisa Avila committed
112
113
114
115
116
117
118
119
120
121
122
123
					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, 
Ken Martin's avatar
Ken Martin committed
124
					  vtkThreadFunctionType f, void *data )
Lisa Avila's avatar
Lisa Avila committed
125
126
{ 
  // You can only set the method for 0 through ThreadCount-1
Charles Law's avatar
Charles Law committed
127
  if ( index >= this->ThreadCount ) {
Lisa Avila's avatar
Lisa Avila committed
128
    vtkErrorMacro( << "Can't set method " << index << 
Charles Law's avatar
Charles Law committed
129
130
    " with a thread count of " << this->ThreadCount );
    }
Lisa Avila's avatar
Lisa Avila committed
131
132
133
134
135
136
137
138
139
140
141
142
  else
    {
    this->MultipleMethod[index] = f;
    this->MultipleData[index]   = data;
    }
}

// Description:
// Execute the method set as the SingleMethod on ThreadCount threads.
void vtkMultiThreader::SingleMethodExecute()
{
  int                thread_loop;
Ken Martin's avatar
Ken Martin committed
143
144
145
146
147
148

#ifdef _WIN32
  DWORD              threadId;
  HANDLE             process_id[VTK_MAX_THREADS];
#endif

Lisa Avila's avatar
Lisa Avila committed
149
#ifdef VTK_USE_SPROC
Lisa Avila's avatar
Lisa Avila committed
150
151
152
153
  siginfo_t          info_ptr;
  int                process_id[VTK_MAX_THREADS];
#endif

Lisa Avila's avatar
Lisa Avila committed
154
#ifdef VTK_USE_PTHREADS
Lisa Avila's avatar
Lisa Avila committed
155
156
157
158
159
160
161
162
163
164
165
166
  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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#ifdef _WIN32
  // Using CreateThread on a PC
  //
  // We want to use CreateThread 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] = 
      CreateThread(NULL, 0, this->SingleMethod, 
	     ((void *)(&this->ThreadInfoArray[thread_loop])), 0, &threadId);
    if (process_id == NULL)
      {
      vtkErrorMacro("Error in thread creation !!!");
      } 
    }
  
  // 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++ )
    {
    WaitForSingleObject(process_id[thread_loop], INFINITE);
    }

  // close the threads
  for ( thread_loop = 0; thread_loop < this->ThreadCount-1; thread_loop++ )
    {
    CloseHandle(process_id[thread_loop]);
    }
#endif

Lisa Avila's avatar
Lisa Avila committed
210
#ifdef VTK_USE_SPROC
Lisa Avila's avatar
Lisa Avila committed
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
  // 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

Lisa Avila's avatar
Lisa Avila committed
243
#ifdef VTK_USE_PTHREADS
Lisa Avila's avatar
Lisa Avila committed
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
  // 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
282
#ifndef _WIN32
Lisa Avila's avatar
Lisa Avila committed
283
284
#ifndef VTK_USE_SPROC
#ifndef VTK_USE_PTHREADS
Lisa Avila's avatar
Lisa Avila committed
285
286
287
288
289
290
  // 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
Ken Martin's avatar
Ken Martin committed
291
#endif
Lisa Avila's avatar
Lisa Avila committed
292
293
294
295
296
297
}

void vtkMultiThreader::MultipleMethodExecute()
{
  int                thread_loop;

Ken Martin's avatar
Ken Martin committed
298
299
300
301
302
#ifdef _WIN32
  DWORD              threadId;
  HANDLE             process_id[VTK_MAX_THREADS];
#endif

Lisa Avila's avatar
Lisa Avila committed
303
#ifdef VTK_USE_SPROC
Lisa Avila's avatar
Lisa Avila committed
304
305
306
307
  siginfo_t          info_ptr;
  int                process_id[VTK_MAX_THREADS];
#endif

Lisa Avila's avatar
Lisa Avila committed
308
#ifdef VTK_USE_PTHREADS
Lisa Avila's avatar
Lisa Avila committed
309
310
311
312
313
314
315
316
317
318
319
  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;
      }

Ken Martin's avatar
Ken Martin committed
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
  // We are using sproc (on SGIs), pthreads(on Suns), CreateThread
  // on a PC or a single thread (the default)  

#ifdef _WIN32
  // Using CreateThread on a PC
  //
  // We want to use CreateThread 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] = 
      CreateThread(NULL, 0, this->MultipleMethod[thread_loop], 
	     ((void *)(&this->ThreadInfoArray[thread_loop])), 0, &threadId);
    if (process_id == NULL)
      {
      vtkErrorMacro("Error in thread creation !!!");
      } 
    }
  
  // 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]) );
Lisa Avila's avatar
Lisa Avila committed
354

Ken Martin's avatar
Ken Martin committed
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
  // 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++ )
    {
    WaitForSingleObject(process_id[thread_loop], INFINITE);
    }

  // close the threads
  for ( thread_loop = 0; thread_loop < this->ThreadCount-1; thread_loop++ )
    {
    CloseHandle(process_id[thread_loop]);
    }
#endif

Lisa Avila's avatar
Lisa Avila committed
370
#ifdef VTK_USE_SPROC
Lisa Avila's avatar
Lisa Avila committed
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
  // 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

Lisa Avila's avatar
Lisa Avila committed
407
#ifdef VTK_USE_PTHREADS
Lisa Avila's avatar
Lisa Avila committed
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
  // 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
443
#ifndef _WIN32
Lisa Avila's avatar
Lisa Avila committed
444
445
#ifndef VTK_USE_SPROC
#ifndef VTK_USE_PTHREADS
Lisa Avila's avatar
Lisa Avila committed
446
447
448
449
450
451
  // 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
Ken Martin's avatar
Ken Martin committed
452
#endif
Lisa Avila's avatar
Lisa Avila committed
453
454
455
456
457
458
459
460
461
462
}

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

}