ITK  4.6.0
Insight Segmentation and Registration Toolkit
itkFFTWGlobalConfiguration.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef __itkFFTWGlobalConfiguration_h
19 #define __itkFFTWGlobalConfiguration_h
20 
21 #include "itkObject.h"
22 //NOTE: Need to have at least one itk include before
23 // the next defines in order to have ITK_USE_FFTWF,ITK_USE_FFTWD defined
24 #if defined(ITK_USE_FFTWF) || defined(ITK_USE_FFTWD)
25 
26 #include "itkSimpleFastMutexLock.h"
27 
28 #include "itksys/SystemTools.hxx"
29 #include "itksys/SystemInformation.hxx"
30 #include "fftw3.h"
31 #include <algorithm>
32 #include <cctype>
33 
34 //* The fftw utilities help control the various strategies
35 //available for controlling optimizations for the FFTW library.
36 //
37 //Environmental variables:
38 //ITK_FFTW_PLAN_RIGOR - Defines how aggressive the generation of
39 // wisdom should be.
40 //ITK_FFTW_READ_WISDOM_CACHE - Defines if a wisdom file cache should
41 // be read if found. (it is "On" by default)
42 //ITK_FFTW_WRITE_WISDOM_CACHE - Defines if generated wisdom file cache
43 // should be written (it is "Off" by default)
44 //ITK_FFTW_WISDOM_CACHE_BASE - Defines the base directory where the
45 // fftw wisdom cache will be placed,
46 // this is intended to be used with auto-
47 // generated cache file names
48 //ITK_FFTW_WISDOM_CACHE_FILE - Defines the full name of the cache
49 // file to be generated. If this is
50 // set, then ITK_FFTW_WISDOM_CACHE_BASE
51 // is ignored.
52 //
53 // The above behaviors can also be controlled by the application.
54 //
55 
56 namespace itk
57 {
62 #ifdef _WIN32
63 #define FFTWPathSep "\\"
64 #else
65 #define FFTWPathSep "/"
66 #endif
67 
69 {
70  public:
71  //The baseCacheDirectory from which to build the cache hierarchy
72  virtual std::string GenerateWisdomFilename(const std::string baseCacheDirectory) const = 0;
75  private:
76 };
77 
79 {
80  public:
81  ManualWisdomFilenameGenerator(const std::string wfn): m_WisdomFilename(wfn) { };
82  void SetWisdomFilename(const std::string wfn)
83  {
84  this->m_WisdomFilename=wfn;
85  }
86  virtual std::string GenerateWisdomFilename(const std::string itkNotUsed(baseCacheDirectory) ) const ITK_OVERRIDE
87  {
88  return this->m_WisdomFilename;
89  }
90  private:
91  std::string m_WisdomFilename;
92 };
93 
95 {
96  public:
97  virtual std::string GenerateWisdomFilename(const std::string baseCacheDirectory) const ITK_OVERRIDE
98  {
99  return baseCacheDirectory+FFTWPathSep+".itksimple.wisdom";
100  }
101 };
102 
104 {
105  public:
106  virtual std::string GenerateWisdomFilename(const std::string baseCacheDirectory) const ITK_OVERRIDE
107  {
108 
109  itksys::SystemInformation hostInfo;
110  hostInfo.RunOSCheck();
111  return baseCacheDirectory+FFTWPathSep + ".itkwisdomfftw"+FFTWPathSep+".itk_"+hostInfo.GetHostname()+".wisdom";
112  }
113 };
114 
116 {
117 public:
119  m_UseOSName(true),
120  m_UseOSRelease(false),
121  m_UseOSVersion(false),
122  m_UseOSPlatform(true),
123  m_UseOSBitSize(true),
125  m_UseVendorString(true),
126  m_UseVendorID(false),
127  m_UseTypeID(true),
128  m_UseFamilyID(true),
129  m_UseModelID(true),
130  m_UseSteppingCode(true)
131  {}
132 
133  virtual std::string GenerateWisdomFilename(const std::string baseCacheDirectory) const ITK_OVERRIDE
134  {
135  //Now build the hardware string by system interogation
136  itksys::SystemInformation hardwareInfo;
137  hardwareInfo.RunCPUCheck();
138  hardwareInfo.RunOSCheck();
139  hardwareInfo.RunMemoryCheck();
140  std::stringstream OSD("");
141 
142  if( this->m_UseOSName )
143  {
144  OSD << hardwareInfo.GetOSName() << "_";
145  }
146  if( this->m_UseOSRelease )
147  {
148  OSD << hardwareInfo.GetOSRelease() << "_";
149  }
150  if( this->m_UseOSVersion )
151  {
152  OSD << hardwareInfo.GetOSVersion() << "_";
153  }
154  if( this->m_UseOSPlatform )
155  {
156  OSD << hardwareInfo.GetOSPlatform() << "_";
157  }
158  if( this->m_UseOSBitSize )
159  {
160  const char * const bitsizeString=( hardwareInfo.Is64Bits() ) ? "64_":"32_";
161  OSD << bitsizeString;
162  }
163  if( this->m_UseNumberOfProcessors )
164  {
165  OSD << hardwareInfo.GetNumberOfLogicalCPU() <<"x"<< hardwareInfo.GetNumberOfPhysicalCPU() << "_";
166  }
167  if( this->m_UseVendorString )
168  {
169  OSD << hardwareInfo.GetVendorString() << "_";
170  }
171  if( this->m_UseVendorID )
172  {
173  OSD << hardwareInfo.GetVendorID() << "_";
174  }
175  if( this->m_UseTypeID )
176  {
177  OSD << hardwareInfo.GetTypeID() << "_";
178  }
179  if( this->m_UseFamilyID )
180  {
181  OSD << hardwareInfo.GetFamilyID() << "_";
182  }
183  if( this->m_UseModelID )
184  {
185  OSD << hardwareInfo.GetModelID() << "_";
186  }
187  if( this->m_UseSteppingCode )
188  {
189  OSD << hardwareInfo.GetSteppingCode();
190  }
191  OSD << ".wisdom";
192  std::string noSpaceStr=OSD.str();
193  //Now remove spaces
194  noSpaceStr.erase(std::remove_if(noSpaceStr.begin(), noSpaceStr.end(),::isspace), noSpaceStr.end());
195  return baseCacheDirectory + FFTWPathSep + ".itkwisdomfftw" + FFTWPathSep + noSpaceStr;
196  }
197  void SetUseOSName(const bool flag) { this->m_UseOSName=flag; }
198  void SetUseOSRelease(const bool flag) { this->m_UseOSRelease=flag; }
199  void SetUseOSVersion(const bool flag) { this->m_UseOSVersion=flag; }
200  void SetUseOSPlatform(const bool flag) { this->m_UseOSPlatform=flag; }
201  void SetUseOSBitSize(const bool flag) { this->m_UseOSBitSize=flag; }
202  void SetUseNumberOfProcessors(const bool flag) { this->m_UseNumberOfProcessors=flag; }
203  void SetUseVendorString(const bool flag) { this->m_UseVendorString=flag; }
204  void SetUseTypeID(const bool flag) { this->m_UseTypeID=flag; }
205  void SetUseFamilyID(const bool flag) { this->m_UseFamilyID=flag; }
206  void SetUseModelID(const bool flag) { this->m_UseModelID=flag; }
207  void SetUseSteppingCode(const bool flag) { this->m_UseSteppingCode=flag; }
208 
209  bool GetUseOSName() const { return this->m_UseOSName; }
210  bool GetUseOSRelease() const { return this->m_UseOSRelease; }
211  bool GetUseOSVersion() const { return this->m_UseOSVersion; }
212  bool GetUseOSPlatform() const { return this->m_UseOSPlatform; }
213  bool GetUseOSBitSize() const { return this->m_UseOSBitSize; }
214  bool GetUseNumberOfProcessors() const { return this->m_UseNumberOfProcessors; }
215  bool GetUseVendorString() const { return this->m_UseVendorString; }
216  bool GetUseTypeID() const { return this->m_UseTypeID; }
217  bool GetUseFamilyID() const { return this->m_UseFamilyID; }
218  bool GetUseModelID() const { return this->m_UseModelID; }
219  bool GetUseSteppingCode() const { return this->m_UseSteppingCode; }
220 
221 private:
234 };
235 
255 {
256 public:
257 
264 
266  itkTypeMacro(FFTWGlobalConfiguration, Object);
267 
270  {
271  return GetInstance()->m_Lock;
272  }
273 
278  static void SetNewWisdomAvailable( const bool & v )
279  {
280  GetInstance()->m_NewWisdomAvailable = v;
281  }
282  static bool GetNewWisdomAvailable()
283  {
284  return GetInstance()->m_NewWisdomAvailable;
285  }
287 
296  static void SetPlanRigor( const int & v )
297  {
298  // use that method to check the value
299  GetPlanRigorName( v );
300  GetInstance()->m_PlanRigor = v;
301  }
303 
304  static int GetPlanRigor()
305  {
306  return GetInstance()->m_PlanRigor;
307  }
308  static void SetPlanRigor( const std::string & name )
309  {
310  SetPlanRigor( GetPlanRigorValue( name ) );
311  }
312 
314  static int GetPlanRigorValue( const std::string & name );
315 
317  static std::string GetPlanRigorName( const int & value );
318 
326  static void SetReadWisdomCache( const bool & v )
327  {
328  GetInstance()->m_ReadWisdomCache = v;
329  }
330 
331  static bool GetReadWisdomCache()
332  {
333  return GetInstance()->m_ReadWisdomCache;
334  }
335 
343  static void SetWriteWisdomCache( const bool & v )
344  {
345  GetInstance()->m_WriteWisdomCache = v;
346  }
347 
348  static bool GetWriteWisdomCache()
349  {
350  return GetInstance()->m_WriteWisdomCache;
351  }
352 
360  static void SetWisdomCacheBase( const std::string & v )
361  {
362  GetInstance()->m_WisdomCacheBase = v;
363  }
364 
365  static std::string GetWisdomCacheBase()
366  {
367  return GetInstance()->m_WisdomCacheBase;
368  }
369 
383 
397  static std::string GetWisdomFileDefaultBaseName();
398 
400  static bool ImportWisdomFileDouble( const std::string &fname );
401  static bool ExportWisdomFileDouble( const std::string &fname );
403 
405  static bool ImportWisdomFileFloat( const std::string &fname );
406  static bool ExportWisdomFileFloat( const std::string &fname );
408 
410  static bool ImportDefaultWisdomFileDouble();
411  static bool ExportDefaultWisdomFileDouble();
413 
415  static bool ImportDefaultWisdomFileFloat();
416  static bool ExportDefaultWisdomFileFloat();
418 
419 private:
420  FFTWGlobalConfiguration(); //This will process env variables
421  ~FFTWGlobalConfiguration(); //This will write cache file if requested.
422 
424  static Pointer GetInstance();
425 
430  itkFactorylessNewMacro(Self);
431 
432  FFTWGlobalConfiguration(const Self &); //purposely not implemented
433  void operator=(const Self &); //purposely not implemented
434 
437 
443  std::string m_WisdomCacheBase;
444  //m_WriteWisdomCache Controls the behavior of default
445  //wisdom file creation policies.
447 };
448 }
449 #endif
450 #endif
Critical section locking class that can be allocated on the stack.
WisdomFilenameGeneratorBase * m_WisdomFilenameGenerator
static SimpleFastMutexLock & GetLockMutex()
static void SetWriteWisdomCache(const bool &v)
Set the behavior of wisdom file caching.
static std::string GetWisdomFileDefaultBaseName()
static bool ExportDefaultWisdomFileFloat()
static std::string GetPlanRigorName(const int &value)
virtual std::string GenerateWisdomFilename(const std::string baseCacheDirectory) const ITK_OVERRIDE
static bool ImportWisdomFileFloat(const std::string &fname)
static bool ExportWisdomFileFloat(const std::string &fname)
static bool ImportDefaultWisdomFileFloat()
static bool ExportWisdomFileDouble(const std::string &fname)
void operator=(const Self &)
virtual std::string GenerateWisdomFilename(const std::string baseCacheDirectory) const =0
static bool ExportDefaultWisdomFileDouble()
ManualWisdomFilenameGenerator(const std::string wfn)
static void SetReadWisdomCache(const bool &v)
Set the behavior of wisdom file caching.
static Pointer GetInstance()
static SimpleFastMutexLock m_CreationLock
static bool ImportDefaultWisdomFileDouble()
static void SetPlanRigor(const std::string &name)
#define FFTWPathSep
static void SetPlanRigor(const int &v)
Set the behavior of wisdom plan creation.
static void SetNewWisdomAvailable(const bool &v)
virtual std::string GenerateWisdomFilename(const std::string) const ITK_OVERRIDE
void SetWisdomFilename(const std::string wfn)
static void SetWisdomFilenameGenerator(WisdomFilenameGeneratorBase *wfg)
allows application developers to create arbitrary rules for auto-generating cache file names...
virtual std::string GenerateWisdomFilename(const std::string baseCacheDirectory) const ITK_OVERRIDE
static bool ImportWisdomFileDouble(const std::string &fname)
static void SetWisdomCacheBase(const std::string &v)
Define the directory where the wisdom cache will be placed. The environmental variable ITK_FFTW_WISDO...
static int GetPlanRigorValue(const std::string &name)
SmartPointer< const Self > ConstPointer
Base class for most ITK classes.
Definition: itkObject.h:57
virtual std::string GenerateWisdomFilename(const std::string baseCacheDirectory) const ITK_OVERRIDE