Skip to content

Commit

Permalink
regen ker coeffs .h without nc200() etc func; use .size() instead, mu…
Browse files Browse the repository at this point in the history
…ch neater
  • Loading branch information
ahbarnett committed Jul 22, 2024
1 parent 20b6265 commit 2e88b76
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 99 deletions.
24 changes: 7 additions & 17 deletions devel/gen_all_horner_cpp_header.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
% version including low upsampfac, 6/17/18.
% Ludvig put in w=4n padding, 1/31/20. Mystery about why d was bigger 2/6/20.
% C++ header using constexpr of Barbone, replacing *_C_code.m. Barnett 7/16/24.
% simplified to kill nc200() etc functions, Barbone + Barnett 7/22/24.
clear
%opts = struct(); % not needed yet

Expand All @@ -20,34 +21,23 @@
fid = fopen('../src/ker_lowupsampfac_horner_allw_loop_constexpr.h','w');
end
fwrite(fid,sprintf('// Header of static arrays of monomial coeffs of spreading kernel function in each\n'));
fwrite(fid,sprintf('// fine-grid interval. Generated by gen_all_horner_cpp_header.m in finufft/devel\n'));
fwrite(fid,sprintf('// fine-grid interval. Generated by gen_all_horner_cpp_header.m in devel/\n'));
fwrite(fid,sprintf('// Authors: Alex Barnett, Ludvig af Klinteberg, Marco Barbone & Libin Lu.\n// (C) 2018--2024 The Simons Foundation, Inc.\n'));
fwrite(fid,sprintf('#include <array>\n\n'));

usf_tag = sprintf('%d',100*upsampfac); % follow Barbone convention: 200 or 125
fwrite(fid,sprintf('template<uint8_t w> constexpr auto nc%s() noexcept {\n',usf_tag));
fwrite(fid,' constexpr uint8_t ncs[] = {'); % array of ncoeffs for ws
for w=ws
[d,~] = get_degree_and_beta(w, upsampfac);
nc = d+1;
fwrite(fid,sprintf('%d, ',nc));
end
fwrite(fid,sprintf('};\n return ncs[w-%d];\n}\n\n',ws(1))); % finish func code
% (must be a func since also called from spreadinterp.cpp)

fwrite(fid,sprintf('template<class T, uint8_t w>\nconstexpr std::array<std::array<T, w>, nc%s<w>()> get_horner_coeffs_%s() noexcept {\n',usf_tag,usf_tag));
fwrite(fid,sprintf(' constexpr auto nc = nc%s<w>();\n',usf_tag)); % use func
usf_tag = sprintf('%d',100*upsampfac); % string, follow Barbone convention: 200 or 125
fwrite(fid,sprintf('template<class T, uint8_t w>\nconstexpr auto get_horner_coeffs_%s() noexcept {\n',usf_tag));

for j=1:numel(ws)
w = ws(j);
[d,beta] = get_degree_and_beta(w,upsampfac);
fprintf('w=%d\td=%d\tbeta=%.3g\n',w,d,beta);
str = gen_ker_horner_loop_cpp_code(w,d,beta); % code strings for this w
str = gen_ker_horner_loop_cpp_code(w,d,beta); % code strings for this w (nc hardcoded)

if j==1 % write switch statement
fwrite(fid,sprintf(' if constexpr (w==%d) {\n',w));
fwrite(fid,sprintf(' if constexpr (w == %d) {\n',w));
else
fwrite(fid,sprintf(' } else if constexpr (w==%d) {\n',w));
fwrite(fid,sprintf(' } else if constexpr (w == %d) {\n',w));
end
for i=1:numel(str); fwrite(fid,[' ',str{i}]); end % format 4 extra spaces
end
Expand Down
12 changes: 7 additions & 5 deletions devel/gen_ker_horner_loop_cpp_code.m
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,24 @@

% Ludvig af Klinteberg 4/25/18, based on Barnett 4/23/18. Ludvig wpad 1/31/20.
% Barnett redo for Barbone templated arrays, no wpad, 7/16/24.
% hardcoded outer nc array size, 7/22/24.

if nargin==0, test_gen_ker_horner_loop_cpp_code; return; end
if nargin<4, o=[]; end

C = ker_ppval_coeff_mat(w,d,be,o);
if d==0, d = size(C,2)-1; end
str = cell(d+3,1); % nc = d+1, plus one start and one close-paren line
nc = d+1;
str = cell(nc+2,1); % one coeff per line + one return and one close-paren line
% code to open the templated array... why two {{? (some C++ ambiguity thing)
str{1} = sprintf(' return std::array<std::array<T, w>, nc> {{\n');
str{1} = sprintf(' return std::array<std::array<T, w>, %d> {{\n', nc);
for n=1:d+1 % loop over poly coeff powers 0,1,..,d
% sprintf implicitly loops over fine-grid interpolation intervals 1:w...
coeffrow = sprintf('%.16E, ', C(n,:));
coeffrow = coeffrow(1:end-2); % kill trailing comma even though allowed in C++
str{d+3-n} = sprintf(' {%s},\n', coeffrow); % leaves outer trailing comma
coeffrow = coeffrow(1:end-2); % easy kill trailing comma (but allowed in C++)
str{d+3-n} = sprintf(' {%s},\n', coeffrow); % leave outer trailing comma
end
str{d+3} = sprintf(' }};\n'); % terminate the array why two }}?
str{nc+2} = sprintf(' }};\n'); % terminate the array (two braces)


%%%%%%%%
Expand Down
Loading

0 comments on commit 2e88b76

Please sign in to comment.