4776
|
1 /* |
|
2 |
|
3 Copyright (C) 2004 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, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
|
27 #if defined (HAVE_FFTW3) |
|
28 #include <fftw3.h> |
|
29 #endif |
|
30 |
|
31 #include "defaults.h" |
|
32 #include "defun-dld.h" |
|
33 #include "error.h" |
|
34 #include "file-ops.h" |
|
35 #include "gripes.h" |
|
36 #include "lo-mappers.h" |
|
37 #include "lo-sstream.h" |
|
38 #include "oct-env.h" |
|
39 #include "oct-obj.h" |
|
40 #include "sighandlers.h" |
|
41 #include "utils.h" |
|
42 |
|
43 DEFUN_DLD (fftw_wisdom, args, , |
|
44 "-*- texinfo -*-\n\ |
|
45 @deftypefn {Loadable Function} {} fftw_wisdom (@var{file}, @var{ow})\n\ |
|
46 @deftypefnx {Loadable Function} {} fftw_wisdom (@var{n})\n\ |
|
47 \n\ |
|
48 This function is used to manipulate the FFTW wisdom data. It can\n\ |
|
49 be used to load previously stored wisdom from a file, create wisdom\n\ |
|
50 needed by Octave and to save the current wisdom to a file. Wisdom\n\ |
|
51 data can be used to significantly accelerate the calculation of the FFTs,\n\ |
|
52 but is only profitable if the same FFT is called many times due to the\n\ |
|
53 overhead in calculating the wisdom data.\n\ |
|
54 \n\ |
|
55 Called with a single string argument, @code{fftw_wisdom (@var{file})}\n\ |
|
56 will load the wisdom data found in @var{file}. If @var{file} does\n\ |
|
57 not exist, then the current wisdom used by FFTW is saved to this\n\ |
|
58 file. If the flag @var{ow} is non-zero, then even if @var{file}\n\ |
|
59 exists, it will be overwritten.\n\ |
|
60 \n\ |
|
61 It is assumed that each row of @var{n} represents the size of a FFT for\n\ |
|
62 which it is desired to pre-calculate the wisdom needed to accelerate it.\n\ |
|
63 Any value of the matrix that is less than 1, is assumed to indicate an\n\ |
|
64 absent dimension. That is\n\ |
|
65 \n\ |
|
66 @example\n\ |
|
67 fftw_wisdom ([102, 0, 0; 103, 103, 0; 102, 103, 105]);\n\ |
|
68 a = fft (rand (1,102));\n\ |
|
69 b = fft (rand (103,103));\n\ |
|
70 c = fftn (rand ([102, 103, 105]));\n\ |
|
71 @end example\n\ |
|
72 \n\ |
|
73 calculates the wisdom necessary to accelerate the 103, 102x102 and\n\ |
|
74 the 102x103x105 FFTs. Note that calculated wisdom will be lost when\n\ |
|
75 when restarting Octave. However, if it is saved as discussed above, it\n\ |
|
76 can be reloaded. Also, any system-wide wisdom file that has been found\n\ |
|
77 will also be used. Saved wisdom files should not be used on different\n\ |
|
78 platforms since they will not be efficient and the point of calculating\n\ |
|
79 the wisdom is lost.\n\ |
|
80 \n\ |
|
81 Note that the program @code{fftw-wisdom} supplied with FFTW can equally\n\ |
|
82 be used to create a file containing wisdom that can be imported into\n\ |
|
83 Octave.\n\ |
|
84 @end deftypefn\n\ |
|
85 @seealso {fft, ifft, fft2, ifft2, fftn, ifftn}") |
|
86 { |
|
87 octave_value retval; |
|
88 |
|
89 int nargin = args.length(); |
|
90 |
|
91 if (nargin < 1 || nargin > 2) |
|
92 { |
|
93 print_usage ("fftw_wisdom"); |
|
94 return retval; |
|
95 } |
|
96 |
4792
|
97 #if defined (HAVE_FFTW3) |
|
98 |
4776
|
99 if (args(0).is_string ()) |
|
100 { |
|
101 bool overwrite = false; |
|
102 |
|
103 if (nargin != 1) |
|
104 { |
|
105 double dval = args (1).double_value (); |
|
106 if (NINT (dval) != 0) |
|
107 overwrite = true; |
|
108 } |
|
109 |
|
110 std::string wisdom = octave_env::make_absolute |
4787
|
111 (Vload_path_dir_path.find_first_of (args(0).string_value ()), |
4776
|
112 octave_env::getcwd ()); |
|
113 |
4787
|
114 // XXX FIXME XXX -- should probably protect FILE* resources with |
|
115 // auto_ptr or similar... |
|
116 |
4776
|
117 if (wisdom.empty () || overwrite) |
|
118 { |
|
119 FILE *ofile = fopen (wisdom.c_str (), "wb"); |
|
120 fftw_export_wisdom_to_file (ofile); |
|
121 fclose (ofile); |
|
122 } |
|
123 else |
|
124 { |
|
125 FILE *ifile = fopen (wisdom.c_str (), "r"); |
|
126 if (! fftw_import_wisdom_from_file (ifile)) |
|
127 error ("fftw_wisdom: can not import wisdom from file"); |
|
128 fclose (ifile); |
|
129 } |
|
130 |
|
131 } |
|
132 else |
|
133 { |
|
134 Matrix m = args (0).matrix_value (); |
|
135 |
|
136 if (error_state) |
|
137 { |
|
138 error ("fftw_wisdom: argument must be a matrix or a string"); |
|
139 return retval; |
|
140 } |
|
141 |
|
142 std::string name = file_ops::tempnam ("", "oct-"); |
|
143 |
|
144 if (name.empty ()) |
|
145 { |
|
146 error ("fftw_wisdom: can not open temporary file"); |
|
147 return retval; |
|
148 } |
|
149 |
|
150 OSSTREAM cmd_buf; |
|
151 cmd_buf << Vfftw_wisdom_prog << " -n -o \"" << name << "\""; |
|
152 |
|
153 for (int k = 0; k < m.rows (); k++) |
|
154 { |
|
155 bool first = true; |
|
156 |
|
157 cmd_buf << " "; |
|
158 |
|
159 // Note reversal of dimensions for column major storage in FFTW |
|
160 for (int j = m.columns()-1; j >= 0; j--) |
|
161 if (NINT(m(k,j)) > 0) |
|
162 { |
|
163 if (first) |
|
164 first = false; |
|
165 else |
|
166 cmd_buf << "x"; |
|
167 cmd_buf << NINT(m(k,j)) ; |
|
168 } |
|
169 } |
|
170 |
|
171 cmd_buf << OSSTREAM_ENDS; |
|
172 |
|
173 volatile octave_interrupt_handler old_interrupt_handler |
|
174 = octave_ignore_interrupts (); |
|
175 |
|
176 int status = system (OSSTREAM_C_STR (cmd_buf)); |
|
177 |
|
178 OSSTREAM_FREEZE (cmd_buf); |
|
179 |
|
180 octave_set_interrupt_handler (old_interrupt_handler); |
|
181 |
|
182 if (WIFEXITED (status)) |
|
183 { |
|
184 FILE *ifile = fopen (name.c_str (), "r"); |
|
185 if (! fftw_import_wisdom_from_file (ifile)) |
|
186 error ("fftw_wisdom: can not import wisdom from temporary file"); |
|
187 fclose (ifile); |
|
188 } |
|
189 else |
|
190 error ("fftw_wisdom: error running %s", Vfftw_wisdom_prog.c_str ()); |
|
191 |
|
192 } |
|
193 |
|
194 #else |
|
195 |
4787
|
196 warning ("fftw_wisdom: this copy of Octave was not configured to use FFTW3"); |
4776
|
197 |
|
198 #endif |
|
199 |
|
200 return retval; |
|
201 } |