ITK  4.2.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 USE_FFTWF,USE_FFTWD defined
24 #if defined(USE_FFTWF) || defined(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
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
98  {
99  return baseCacheDirectory+FFTWPathSep+".itksimple.wisdom";
100  }
101 };
102 
104 {
105  public:
106  virtual std::string GenerateWisdomFilename(const std::string baseCacheDirectory) const
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),
124  m_UseNumberOfProcessors(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
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 private:
233 };
234 
253 class ITK_EXPORT FFTWGlobalConfiguration: public Object
254 {
255 public:
256 
262 
264  itkTypeMacro(FFTWGlobalConfiguration, Object);
265 
269  static void Lock();
270  static void Unlock();
272 
277  static void SetNewWisdomAvailable( const bool & v )
278  {
279  GetInstance()->m_NewWisdomAvailable = v;
280  }
281  static bool GetNewWisdomAvailable()
282  {
283  return GetInstance()->m_NewWisdomAvailable;
284  }
286 
295  static void SetPlanRigor( const int & v )
296  {
297  // use that method to check the value
298  GetPlanRigorName( v );
299  GetInstance()->m_PlanRigor = v;
300  }
302 
303  static int GetPlanRigor()
304  {
305  return GetInstance()->m_PlanRigor;
306  }
307  static void SetPlanRigor( const std::string & name )
308  {
309  SetPlanRigor( GetPlanRigorValue( name ) );
310  }
311 
313  static int GetPlanRigorValue( const std::string & name );
314 
316  static std::string GetPlanRigorName( const int & value );
317 
325  static void SetReadWisdomCache( const bool & v )
326  {
327  GetInstance()->m_ReadWisdomCache = v;
328  }
329 
330  static bool GetReadWisdomCache()
331  {
332  return GetInstance()->m_ReadWisdomCache;
333  }
334 
342  static void SetWriteWisdomCache( const bool & v )
343  {
344  GetInstance()->m_WriteWisdomCache = v;
345  }
346 
347  static bool GetWriteWisdomCache()
348  {
349  return GetInstance()->m_WriteWisdomCache;
350  }
351 
359  static void SetWisdomCacheBase( const std::string & v )
360  {
361  GetInstance()->m_WisdomCacheBase = v;
362  }
363 
364  static std::string GetWisdomCacheBase()
365  {
366  return GetInstance()->m_WisdomCacheBase;
367  }
368 
381  static void SetWisdomFilenameGenerator( WisdomFilenameGeneratorBase *wfg);
382 
396  static std::string GetWisdomFileDefaultBaseName();
397 
399  static bool ImportWisdomFileDouble( const std::string &fname );
400  static bool ExportWisdomFileDouble( const std::string &fname );
402 
404  static bool ImportWisdomFileFloat( const std::string &fname );
405  static bool ExportWisdomFileFloat( const std::string &fname );
407 
409  static bool ImportDefaultWisdomFileDouble();
410  static bool ExportDefaultWisdomFileDouble();
412 
414  static bool ImportDefaultWisdomFileFloat();
415  static bool ExportDefaultWisdomFileFloat();
417 
418 private:
419  FFTWGlobalConfiguration(); //This will process env variables
420  ~FFTWGlobalConfiguration(); //This will write cache file if requested.
421 
423  static Pointer GetInstance();
424 
429  itkFactorylessNewMacro(Self);
430 
431  FFTWGlobalConfiguration(const Self &); //purposely not implemented
432  void operator=(const Self &); //purposely not implemented
433 
436 
442  std::string m_WisdomCacheBase;
443  //m_WriteWisdomCache Controls the behavior of default
444  //wisdom file creation policies.
446 };
447 }
448 #endif
449 #endif
450