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