File: cmtkThreadParameterArray.txx

package info (click to toggle)
cmtk 3.3.1p2%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 10,492 kB
  • sloc: cpp: 87,098; ansic: 23,347; sh: 3,896; xml: 1,551; perl: 707; makefile: 332
file content (172 lines) | stat: -rw-r--r-- 5,266 bytes parent folder | download | duplicates (7)
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
41
42
43
44
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
//
//  Copyright 1997-2009 Torsten Rohlfing
//
//  Copyright 2004-2011, 2013 SRI International
//
//  This file is part of the Computational Morphometry Toolkit.
//
//  http://www.nitrc.org/projects/cmtk/
//
//  The Computational Morphometry Toolkit is free software: you can
//  redistribute it and/or modify it under the terms of the GNU General Public
//  License as published by the Free Software Foundation, either version 3 of
//  the License, or (at your option) any later version.
//
//  The Computational Morphometry Toolkit is distributed in the hope that it
//  will be useful, but WITHOUT ANY WARRANTY; without even the implied
//  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License along
//  with the Computational Morphometry Toolkit.  If not, see
//  <http://www.gnu.org/licenses/>.
//
//  $Revision: 4862 $
//
//  $LastChangedDate: 2013-09-23 16:41:36 -0700 (Mon, 23 Sep 2013) $
//
//  $LastChangedBy: torstenrohlfing $
//
*/

#include <System/cmtkConsole.h>

#ifdef _OPENMP
#  include <omp.h>
#endif

namespace
cmtk
{

/** \addtogroup System */
//@{

template<class TClass,class TParam>
void
ThreadParameterArray<TClass,TParam>
::RunInParallelFIFO
( ThreadFunction threadCall, const size_t numberOfThreadsTotal, const size_t firstThreadIdx )
{
#ifdef _OPENMP
  const int nThreadsOMP = std::max<int>( 1, 1+GetNumberOfThreads()-this->m_NumberOfThreads );
  omp_set_num_threads( nThreadsOMP );
#endif

#ifndef CMTK_USE_PTHREADS
  // we're not actually using threads, so simply run everything "by hand".
  for ( size_t threadIdx = 0; threadIdx < numberOfThreadsTotal; ++threadIdx ) 
    {
    this->m_Ptr[0].ThisThreadIndex = firstThreadIdx + threadIdx;
    threadCall( this->m_Ptr );
    }
#else
  if ( this->m_NumberOfThreads == 1 )
    {
    for ( size_t threadIdx = 0; threadIdx < numberOfThreadsTotal; ++threadIdx ) 
      {
      this->m_Ptr[0].ThisThreadIndex = firstThreadIdx + threadIdx;
      threadCall( this->m_Ptr );
      }
    }
  else
    {
#ifdef _MSC_VER
#else
    pthread_attr_t attr;
    pthread_attr_init(&attr);
    pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
#endif // #ifdef _MSC_VER
      
    /* Initialization phase -- start first batch of parallel threads. */
    size_t threadIdx = 0;
    for ( ; (threadIdx < this->m_NumberOfThreads) && (threadIdx < numberOfThreadsTotal); ++threadIdx ) 
      {
      this->m_Ptr[threadIdx].ThisThreadIndex = firstThreadIdx + threadIdx;
#ifdef _MSC_VER
      this->m_Ptr[threadIdx].m_Handle = CreateThread( NULL /*default security attributes*/, 0 /*use default stack size*/, (LPTHREAD_START_ROUTINE) threadCall, 
						      &this->m_Ptr[threadIdx], 0 /*use default creation flags*/, &this->m_Ptr[threadIdx].m_ThreadID );
      if ( this->m_Ptr[threadIdx].m_Handle == NULL )
	{
	fprintf( stderr, "Creation of thread #%d failed.\n", (int)threadIdx );
	exit( 1 );
	}
#else
      int status = pthread_create( &this->m_Ptr[threadIdx].m_ThreadID, &attr, threadCall, &this->m_Ptr[threadIdx] );	
      if ( status ) 
	{
	fprintf( stderr, "Creation of thread #%d failed with status %d.\n", (int)threadIdx, (int)status );
	exit( 1 );
	}
#endif
      }
      
    /* Sustained phase -- start new thread whenever oldest one completes. */
    size_t nextThreadToJoin = 0;
    while ( threadIdx < numberOfThreadsTotal )
      {
#ifndef _MSC_VER
      void *resultThread;
#endif
      if ( this->m_Ptr[threadIdx].m_ThreadID ) 
	{
#ifdef _MSC_VER
#else
	pthread_join( this->m_Ptr[threadIdx].m_ThreadID, &resultThread );
#endif
	}
	
      this->m_Ptr[nextThreadToJoin].ThisThreadIndex = firstThreadIdx + threadIdx;
	
#ifdef _MSC_VER
      this->m_Ptr[nextThreadToJoin].m_Handle = CreateThread( NULL /*default security attributes*/, 0 /*use default stack size*/, (LPTHREAD_START_ROUTINE) threadCall, 
							     &this->m_Ptr[nextThreadToJoin],0 /*use default creation flags*/, &this->m_Ptr[nextThreadToJoin].m_ThreadID );
      if ( this->m_Ptr[nextThreadToJoin].m_Handle == NULL) 
	{
	fprintf( stderr, "Creation of thread #%d failed.\n", (int)threadIdx );
	exit( 1 );
	}
#else
      int status = pthread_create( &this->m_Ptr[nextThreadToJoin].m_ThreadID, &attr, threadCall, &this->m_Ptr[nextThreadToJoin] );
      if ( status ) 
	{
	fprintf( stderr, "Creation of thread #%d failed with status %d.\n", (int)threadIdx, (int)status );
	exit( 1 );
	}
#endif	

      ++threadIdx;
      nextThreadToJoin = (nextThreadToJoin + 1) % this->m_NumberOfThreads;
      }

    /* Cleanup phase -- Collect remaining thread results. */
    for ( threadIdx = 0; (threadIdx < this->m_NumberOfThreads) && (threadIdx < numberOfThreadsTotal); ++threadIdx ) 
      {
#ifndef _MSC_VER
      void *resultThread;
#endif
      if ( this->m_Ptr[nextThreadToJoin].m_ThreadID ) 
	{
#ifdef _MSC_VER
#else
	pthread_join( this->m_Ptr[nextThreadToJoin].m_ThreadID, &resultThread );
#endif
	}
	
      nextThreadToJoin = (nextThreadToJoin + 1) % this->m_NumberOfThreads;
      }
      
#ifdef _MSC_VER
#else
    pthread_attr_destroy(&attr);
#endif
    }
#endif // #ifndef CMTK_USE_PTHREADS

#ifdef _OPENMP
  omp_set_num_threads( GetNumberOfThreads() );
#endif
}

} // namespace cmtk