comparison src/DLD-FUNCTIONS/fftw_wisdom.cc @ 4776:adf8d68d7143 ss-2-1-54

[project @ 2004-02-16 20:32:20 by jwe]
author jwe
date Mon, 16 Feb 2004 20:32:20 +0000
parents
children 02c748eb2ddc
comparison
equal deleted inserted replaced
4775:88b638195bd1 4776:adf8d68d7143
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 #if defined (HAVE_FFTW3)
90
91 int nargin = args.length();
92
93 if (nargin < 1 || nargin > 2)
94 {
95 print_usage ("fftw_wisdom");
96 return retval;
97 }
98
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
111 (Vload_path_dir_path.find_first_of (args(0).string_value ().c_str ()),
112 octave_env::getcwd ());
113
114 if (wisdom.empty () || overwrite)
115 {
116 FILE *ofile = fopen (wisdom.c_str (), "wb");
117 fftw_export_wisdom_to_file (ofile);
118 fclose (ofile);
119 }
120 else
121 {
122 FILE *ifile = fopen (wisdom.c_str (), "r");
123 if (! fftw_import_wisdom_from_file (ifile))
124 error ("fftw_wisdom: can not import wisdom from file");
125 fclose (ifile);
126 }
127
128 }
129 else
130 {
131 Matrix m = args (0).matrix_value ();
132
133 if (error_state)
134 {
135 error ("fftw_wisdom: argument must be a matrix or a string");
136 return retval;
137 }
138
139 std::string name = file_ops::tempnam ("", "oct-");
140
141 if (name.empty ())
142 {
143 error ("fftw_wisdom: can not open temporary file");
144 return retval;
145 }
146
147 OSSTREAM cmd_buf;
148 cmd_buf << Vfftw_wisdom_prog << " -n -o \"" << name << "\"";
149
150 for (int k = 0; k < m.rows (); k++)
151 {
152 bool first = true;
153
154 cmd_buf << " ";
155
156 // Note reversal of dimensions for column major storage in FFTW
157 for (int j = m.columns()-1; j >= 0; j--)
158 if (NINT(m(k,j)) > 0)
159 {
160 if (first)
161 first = false;
162 else
163 cmd_buf << "x";
164 cmd_buf << NINT(m(k,j)) ;
165 }
166 }
167
168 cmd_buf << OSSTREAM_ENDS;
169
170 volatile octave_interrupt_handler old_interrupt_handler
171 = octave_ignore_interrupts ();
172
173 int status = system (OSSTREAM_C_STR (cmd_buf));
174
175 OSSTREAM_FREEZE (cmd_buf);
176
177 octave_set_interrupt_handler (old_interrupt_handler);
178
179 if (WIFEXITED (status))
180 {
181 FILE *ifile = fopen (name.c_str (), "r");
182 if (! fftw_import_wisdom_from_file (ifile))
183 error ("fftw_wisdom: can not import wisdom from temporary file");
184 fclose (ifile);
185 }
186 else
187 error ("fftw_wisdom: error running %s", Vfftw_wisdom_prog.c_str ());
188
189 }
190
191 #else
192
193 error ("fftw_wisdom: this copy of Octave was not configured to use FFTW3");
194
195 #endif
196
197 return retval;
198 }