~ubuntu-branches/ubuntu/precise/insighttoolkit/precise

« back to all changes in this revision

Viewing changes to Code/Common/itkMultiThreader.cxx

  • Committer: Bazaar Package Importer
  • Author(s): Steve M. Robbins
  • Date: 2008-12-19 20:16:49 UTC
  • mfrom: (1.2.1 upstream) (4.1.1 sid)
  • Revision ID: james.westby@ubuntu.com-20081219201649-drt97guwl2ryt0cn

* New upstream version.
  - patches/nifti-versioning.patch: Remove.  Applied upstream.
  - control:
  - rules: Update version numbers, package names.

* control: Build-depend on uuid-dev (gdcm uses it).

* copyright: Update download URL.

* rules: Adhere to parallel=N in DEB_BUILD_OPTIONS by setting MAKEFLAGS.

* compat: Set to 7.
* control: Update build-dep on debhelper to version >= 7.

* CMakeCache.txt.debian: Set CMAKE_BUILD_TYPE to "RELEASE" so that we
  build with -O3 (not -O2), necessary to optimize the templated code.

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
  Program:   Insight Segmentation & Registration Toolkit
4
4
  Module:    $RCSfile: itkMultiThreader.cxx,v $
5
5
  Language:  C++
6
 
  Date:      $Date: 2007-09-16 15:24:14 $
7
 
  Version:   $Revision: 1.40 $
 
6
  Date:      $Date: 2008-05-26 00:24:36 $
 
7
  Version:   $Revision: 1.44 $
8
8
 
9
9
  Copyright (c) Insight Software Consortium. All rights reserved.
10
10
  See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
20
20
 
21
21
#include "itkMultiThreader.h"
22
22
#include "itkObjectFactory.h"
 
23
#include "itksys/SystemTools.hxx"
 
24
#include <vcl_cstdlib.h>
 
25
 
23
26
#ifndef _WIN32
24
27
#include <unistd.h>
25
28
#endif
 
29
#ifdef _MSC_VER
 
30
#pragma warning ( disable : 4786 )
 
31
#endif
26
32
#ifdef _WIN32
27
 
#pragma warning ( disable : 4786 )
28
33
#include "itkWindows.h"
29
34
#include <process.h>
30
35
#endif
78
83
 
79
84
int MultiThreader::GetGlobalDefaultNumberOfThreads()
80
85
{
81
 
  if (m_GlobalDefaultNumberOfThreads == 0)
 
86
  // if default number has been set then don't try to update it; just
 
87
  // return the value
 
88
  if (m_GlobalDefaultNumberOfThreads != 0)
 
89
    {
 
90
    return m_GlobalDefaultNumberOfThreads;
 
91
    }
 
92
 
 
93
  // first, check for enviornment variable
 
94
  itksys_stl::string itkGlobalDefaultNumberOfThreadsEnv = "0";
 
95
  if (itksys::SystemTools::GetEnv("ITK_GLOBAL_DEFAULT_NUMBER_OF_THREADS",
 
96
                                  itkGlobalDefaultNumberOfThreadsEnv))
 
97
    {
 
98
    m_GlobalDefaultNumberOfThreads = 
 
99
      atoi(itkGlobalDefaultNumberOfThreadsEnv.c_str());
 
100
    }
 
101
 
 
102
  // otherwise, set number of threads based on system information
 
103
  if (m_GlobalDefaultNumberOfThreads <= 0)
82
104
    {
83
105
    int num;
84
106
#ifdef ITK_USE_SPROC
121
143
#endif
122
144
 
123
145
#ifdef __APPLE__
124
 
    // Use sysctl() to determine the number of CPUs.  This is prefered
 
146
    // Determine the number of CPU cores. Prefer sysctlbyname()
125
147
    // over MPProcessors() because it doesn't require CoreServices
126
 
    // (which is only available in 32bit on Mac OS X 10.4)
127
 
    int mib[2] = {CTL_HW, HW_NCPU};
 
148
    // (which is only available in 32bit on Mac OS X 10.4).
 
149
    // hw.logicalcpu takes into account cores/CPUs that are
 
150
    // disabled because of power management.
128
151
    size_t dataLen = sizeof(int); // 'num' is an 'int'
129
 
    int result = sysctl(mib, 2, &num, &dataLen, NULL, 0);
 
152
    int result = sysctlbyname ("hw.logicalcpu", &num, &dataLen, NULL, 0);
130
153
    if (result == -1)
131
154
      {
132
155
      num = 1;
133
156
      }
134
157
#endif
135
158
 
136
 
    // Limit the number of threads
137
 
    if (num > ITK_MAX_THREADS)
138
 
      {
139
 
      num = ITK_MAX_THREADS;
140
 
      }
141
159
    m_GlobalDefaultNumberOfThreads = num;
142
160
    }
 
161
 
 
162
  // limit the number of threads to ITK_MAX_THREADS
 
163
  if (m_GlobalDefaultNumberOfThreads > ITK_MAX_THREADS)
 
164
    {
 
165
    m_GlobalDefaultNumberOfThreads = ITK_MAX_THREADS;
 
166
    }
143
167
  
144
168
  return m_GlobalDefaultNumberOfThreads;
145
169
}
238
262
// Execute the method set as the SingleMethod on NumberOfThreads threads.
239
263
void MultiThreader::SingleMethodExecute()
240
264
{
241
 
  int thread_loop;
 
265
  int thread_loop = 0;
242
266
  ThreadProcessIDType process_id[ITK_MAX_THREADS];
243
267
  
244
268
  if ( !m_SingleMethod)
646
670
  if ( id >= ITK_MAX_THREADS )
647
671
    {
648
672
    itkExceptionMacro( << "You have too many active threads!" );
649
 
    return -1;
650
673
    }
651
674
  
652
675
  m_SpawnedThreadInfoArray[id].UserData        = UserData;