File: fftwmanager.h

package info (click to toggle)
wsclean 2.6-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,916 kB
  • sloc: cpp: 30,108; ansic: 231; python: 173; makefile: 9
file content (93 lines) | stat: -rw-r--r-- 2,067 bytes parent folder | download | duplicates (2)
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
#ifndef FFTW_MULTI_THREAD_ENABLER_H
#define FFTW_MULTI_THREAD_ENABLER_H

#include <cstring>

#include <mutex>

/**
 * Used to initialize and enable fftw's multithreading. While
 * an instance of this class exists, multi-threading is enabled. When the
 * class is destructed, multi-threading is again disabled.
 * 
 * To make the FFTs in for example the @ref FFTConvolver or @ref FFTResampler
 * multi-threaded, it is enough to construct an instance of this class and keep
 * it until done.
 * For example:
 * 
 * @code
 * void doMultiThreadedFFTActions
 * {
 *   FFTWManager fftwMT;
 *   FFTWManager::ThreadingScope fftwScope(fftwMT);
 * 
 *   FFTResampler resampler(..);
 *   ...
 * }
 * @endcode
 */
class FFTWManager
{
public:
	class ThreadingScope
	{
	public:
		ThreadingScope(FFTWManager& manager) :
			_manager(manager)
		{
			manager.IncreaseMultiThreadUsers();
		}
		~ThreadingScope()
		{
			_manager.DecreaseMultiThreadUsers();
		}
	private:
		FFTWManager& _manager;
		ThreadingScope(const ThreadingScope&) = delete;
		ThreadingScope* operator=(const ThreadingScope&) = delete;
	};
	
	/**
	 * Constructor that sets FFTW to use multiple threads.
	 * This will set FFTW to use as many threads as there are cores in the 
	 * system.
	 */
	explicit FFTWManager(bool verbose = false);
	
	/**
	 * Constructor that sets FFTW to use multiple threads.
	 * This will set FFTW to use the given number of threads.
	 */
	explicit FFTWManager(size_t nThreads, bool verbose = false);
	
	~FFTWManager();
	
	void IncreaseMultiThreadUsers()
	{
		std::lock_guard<std::mutex> lock(_mutex);
		if(_multiThreadEnabledDepth == 0)
			activateMultipleThreads();
		++_multiThreadEnabledDepth;
	}
	
	void DecreaseMultiThreadUsers()
	{
		std::lock_guard<std::mutex> lock(_mutex);
		--_multiThreadEnabledDepth;
		if(_multiThreadEnabledDepth == 0)
			endMultipleThreads();
	}
	
	std::mutex& Mutex() { return _mutex; }
	
private:
	void activateMultipleThreads();
	void endMultipleThreads();
	
	int _multiThreadEnabledDepth;
	bool _verbose;
	size_t _nThreads;
	std::mutex _mutex;
};

#endif