6228
|
1 /* |
|
2 |
|
3 Copyright (C) 2006 David Bateman |
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
|
28 #include <algorithm> |
|
29 #include "ov.h" |
|
30 #include "defun-dld.h" |
|
31 #include "error.h" |
|
32 |
|
33 #if defined (HAVE_FFTW3) |
|
34 #include "oct-fftw.h" |
|
35 #endif |
|
36 |
|
37 extern octave_fftw_planner fftw_planner; |
|
38 |
|
39 DEFUN_DLD (fftw, args, , |
|
40 "-*- texinfo -*-\n\ |
|
41 @deftypefn {Loadable Function} {@var{method} =} fftw ('planner')\n\ |
|
42 @deftypefnx {Loadable Function} {} fftw ('planner', @var{method})\n\ |
|
43 @deftypefnx {Loadable Function} {@var{wisdom} =} fftw ('dwisdom')\n\ |
|
44 @deftypefnx {Loadable Function} {@var{wisdom} =} fftw ('dwisdom', @var{wisdom})\n\ |
|
45 \n\ |
|
46 Manage FFTW wisdom data. Wisdom data can be used to significantly\n\ |
|
47 accelerate the calculation of the FFTs but implies a initial cost\n\ |
|
48 in its calculation. The wisdom used by Octave can be imported directly,\n\ |
|
49 usually from a file /etc/fftw/wisdom, or @dfn{fftw} can be used\n\ |
|
50 to import wisdom. For example\n\ |
|
51 \n\ |
|
52 @example\n\ |
|
53 @var{wisdom} = fftw ('dwisdom')\n\ |
|
54 @end example\n\ |
|
55 \n\ |
|
56 will save the existing wisdom used by Octave to the string @var{wisdom}.\n\ |
|
57 This string can then be saved in the usual manner. This existing wisdom\n\ |
|
58 can be reimported as follows\n\ |
|
59 \n\ |
|
60 @example\n\ |
|
61 fftw ('dwisdom', @var{wisdom})\n\ |
|
62 @end example \n\ |
|
63 \n\ |
|
64 If @var{wisdom} is an empty matrix, then the wisdom used is cleared.\n\ |
|
65 \n\ |
|
66 During the calculation of fourier transforms further wisdom is generated.\n\ |
|
67 The fashion in which this wisdom is generated is equally controlled by\n\ |
|
68 the @dfn{fftw} function. There are five different manners in which the\n\ |
|
69 wisdom can be treated, these being\n\ |
|
70 \n\ |
|
71 @table @asis\n\ |
|
72 @item 'estimate'\n\ |
|
73 This specifies that no run-time measurement of the optimal means of\n\ |
|
74 calculating a particular is performed, and a simple heuristic is used\n\ |
|
75 to pick a (probably sub-optimal) plan. The advantage of this method is\n\ |
|
76 that there is little or no overhead in the generation of the plan, which\n\ |
|
77 is appropriate for a fourier transform that will be calculated once.\n\ |
|
78 \n\ |
|
79 @item 'measure'\n\ |
|
80 In this case a range of algorithms to perform the transform is considered\n\ |
|
81 and the best is selected based on their execution time.\n\ |
|
82 \n\ |
|
83 @item 'patient'\n\ |
|
84 This is like 'measure', but a wider range of algorithms is considered.\n\ |
|
85 \n\ |
|
86 @item 'exhasutive'\n\ |
|
87 This is like 'meaure', but all possible algorithms that may be used to\n\ |
|
88 treat the transform are considered.\n\ |
|
89 \n\ |
|
90 @item 'hybrid'\n\ |
|
91 As run-time measurement of the algorithm can be expensive, this is a\n\ |
|
92 compromise where 'measure' is used for transforms upto the size of 8192\n\ |
|
93 and beyond that the 'estimate' method is used.\n\ |
|
94 @end table\n\ |
|
95 \n\ |
|
96 The default method is 'estimate', and the method currently being used can\n\ |
|
97 be probed with\n\ |
|
98 \n\ |
|
99 @example\n\ |
|
100 @var{method} = fftw ('planner')\n\ |
|
101 @end example\n\ |
|
102 \n\ |
|
103 and the method used can be set using\n\ |
|
104 \n\ |
|
105 @example\n\ |
|
106 fftw ('planner', @var{method})\n\ |
|
107 @end example\n\ |
|
108 \n\ |
|
109 Note that calculated wisdom will be lost when restarting Octave. However,\n\ |
|
110 the wisdom data can be reloaded if it is saved to a file as described\n\ |
|
111 above. Also, any system-wide wisdom file that has been found will\n\ |
|
112 also be used. Saved wisdom files should not be used on different\n\ |
|
113 platforms since they will not be efficient and the point of calculating\n\ |
|
114 the wisdom is lost.\n\ |
|
115 @seealso{fft, ifft, fft2, ifft2, fftn, ifftn}\n\ |
|
116 @end deftypefn") |
|
117 { |
|
118 octave_value retval; |
|
119 |
|
120 int nargin = args.length(); |
|
121 |
|
122 if (nargin < 1 || nargin > 2) |
|
123 { |
|
124 print_usage (); |
|
125 return retval; |
|
126 } |
|
127 |
|
128 #if defined (HAVE_FFTW3) |
|
129 if (args(0).is_string ()) |
|
130 { |
|
131 std::string arg0 = args(0).string_value (); |
|
132 |
|
133 if (!error_state) |
|
134 { |
|
135 // Use STL function to convert to lower case |
|
136 std::transform (arg0.begin (), arg0.end (), arg0.begin (), tolower); |
|
137 |
|
138 if (nargin == 2) |
|
139 { |
|
140 std::string arg1 = args(1).string_value (); |
|
141 if (!error_state) |
|
142 { |
|
143 if (arg0 == "planner") |
|
144 { |
|
145 std::transform (arg1.begin (), arg1.end (), |
|
146 arg1.begin (), tolower); |
|
147 octave_fftw_planner::FftwMethod meth; |
|
148 |
|
149 if (arg1 == "estimate") |
|
150 meth = fftw_planner.method |
|
151 (octave_fftw_planner::ESTIMATE); |
|
152 else if (arg1 == "measure") |
|
153 meth = fftw_planner.method |
|
154 (octave_fftw_planner::MEASURE); |
|
155 else if (arg1 == "patient") |
|
156 meth = fftw_planner.method |
|
157 (octave_fftw_planner::PATIENT); |
|
158 else if (arg1 == "exhaustive") |
|
159 meth = fftw_planner.method |
|
160 (octave_fftw_planner::EXHAUSTIVE); |
|
161 else if (arg1 == "hybrid") |
|
162 meth = fftw_planner.method |
|
163 (octave_fftw_planner::HYBRID); |
|
164 else |
|
165 error ("unrecognized planner method"); |
|
166 |
|
167 if (!error_state) |
|
168 { |
|
169 if (meth == octave_fftw_planner::MEASURE) |
|
170 retval = octave_value ("measure"); |
|
171 else if (meth == octave_fftw_planner::PATIENT) |
|
172 retval = octave_value ("patient"); |
|
173 else if (meth == octave_fftw_planner::EXHAUSTIVE) |
|
174 retval = octave_value ("exhaustive"); |
|
175 else if (meth == octave_fftw_planner::HYBRID) |
|
176 retval = octave_value ("hybrid"); |
|
177 else |
|
178 retval = octave_value ("estimate"); |
|
179 } |
|
180 } |
|
181 else if (arg0 == "dwisdom") |
|
182 { |
|
183 char *str = fftw_export_wisdom_to_string (); |
|
184 |
|
185 if (arg1.length() < 1) |
|
186 fftw_forget_wisdom (); |
|
187 else if (! fftw_import_wisdom_from_string (arg1.c_str())) |
|
188 error ("could not import supplied wisdom"); |
|
189 |
|
190 if (!error_state) |
|
191 retval = octave_value (std::string (str)); |
|
192 |
|
193 free (str); |
|
194 } |
|
195 else if (arg0 == "swisdom") |
|
196 error ("single precision wisdom is not supported"); |
|
197 else |
|
198 error ("unrecognized argument"); |
|
199 } |
|
200 } |
|
201 else |
|
202 { |
|
203 if (arg0 == "planner") |
|
204 { |
|
205 octave_fftw_planner::FftwMethod meth = |
|
206 fftw_planner.method (); |
|
207 |
|
208 if (meth == octave_fftw_planner::MEASURE) |
|
209 retval = octave_value ("measure"); |
|
210 else if (meth == octave_fftw_planner::PATIENT) |
|
211 retval = octave_value ("patient"); |
|
212 else if (meth == octave_fftw_planner::EXHAUSTIVE) |
|
213 retval = octave_value ("exhaustive"); |
|
214 else if (meth == octave_fftw_planner::HYBRID) |
|
215 retval = octave_value ("hybrid"); |
|
216 else |
|
217 retval = octave_value ("estimate"); |
|
218 } |
|
219 else if (arg0 == "dwisdom") |
|
220 { |
|
221 char *str = fftw_export_wisdom_to_string (); |
|
222 retval = octave_value (std::string (str)); |
|
223 free (str); |
|
224 } |
|
225 else if (arg0 == "swisdom") |
|
226 error ("single precision wisdom is not supported"); |
|
227 else |
|
228 error ("unrecognized argument"); |
|
229 } |
|
230 } |
|
231 } |
|
232 #else |
|
233 |
|
234 warning ("fftw: this copy of Octave was not configured to use FFTW3"); |
|
235 |
|
236 #endif |
|
237 |
|
238 return retval; |
|
239 } |
|
240 |