Beingamanforever

pdist2 full benchmarking script

Jan 27th, 2026
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Octave 3.16 KB | None | 0 0
  1. function bench_pdist2_full ()
  2.   N_vals = [50, 100, 200, 500, 1000, 2000, 3000, 4000, 5000];
  3.   P = 10;
  4.   n_runs = 7;
  5.   n_warmup = 3;
  6.  
  7.   n_sizes = numel (N_vals);
  8.   res = zeros (n_sizes, 6);
  9.  
  10.   for idx = 1:n_sizes
  11.     N = N_vals(idx);
  12.     rand ("seed", 12345);
  13.     X = rand (N, P);
  14.     Y = rand (N, P);
  15.  
  16.     [t_orig, std_orig] = run_timed (@() pdist2_orig (X, Y), n_runs, n_warmup);
  17.     [t_blocked, std_blocked] = run_timed (@() pdist2_blocked (X, Y), n_runs, n_warmup);
  18.  
  19.     speedup = t_orig / t_blocked;
  20.     res(idx, :) = [N, t_orig, std_orig, t_blocked, std_blocked, speedup];
  21.  
  22.     clear X Y;
  23.   endfor
  24.  
  25.   printf ("\npdist2: Original (meshgrid) vs Blocked (row-wise)\n");
  26.   printf ("Config: P=%d, runs=%d, warmup=%d\n", P, n_runs, n_warmup);
  27.   printf ("%s\n", repmat ("-", 1, 75));
  28.   printf ("%-6s %12s %12s %12s %12s %10s\n", "N", "t_orig", "std_orig", "t_blocked", "std_blocked", "speedup");
  29.   printf ("%s\n", repmat ("-", 1, 75));
  30.   for i = 1:n_sizes
  31.     printf ("%-6d %12.6f %12.6f %12.6f %12.6f %10.2fx\n", ...
  32.             res(i,1), res(i,2), res(i,3), res(i,4), res(i,5), res(i,6));
  33.   endfor
  34.   printf ("%s\n\n", repmat ("-", 1, 75));
  35.  
  36.   fid = fopen ("bench_pdist2_full.csv", "w");
  37.   fprintf (fid, "N,t_orig,std_orig,t_blocked,std_blocked,speedup\n");
  38.   for i = 1:n_sizes
  39.     fprintf (fid, "%d,%.6f,%.6f,%.6f,%.6f,%.4f\n", ...
  40.              res(i,1), res(i,2), res(i,3), res(i,4), res(i,5), res(i,6));
  41.   endfor
  42.   fclose (fid);
  43.   printf ("Saved: bench_pdist2_full.csv\n");
  44.  
  45.   h = figure ("visible", "off");
  46.   loglog (N_vals, res(:,2), "ro-", "linewidth", 2, "markersize", 8, "markerfacecolor", "r");
  47.   hold on;
  48.   loglog (N_vals, res(:,4), "bs-", "linewidth", 2, "markersize", 8, "markerfacecolor", "b");
  49.   hold off;
  50.   xlabel ("N"); ylabel ("Time (s)"); title ("pdist2: Original vs Blocked");
  51.   legend ({"Original (meshgrid)", "Blocked (row-wise)"}, "location", "northwest");
  52.   grid on;
  53.   print (h, "bench_pdist2_full.png", "-dpng", "-r150");
  54.   close (h);
  55.   printf ("Saved: bench_pdist2_full.png\n");
  56.  
  57.   h2 = figure ("visible", "off");
  58.   semilogx (N_vals, res(:,6), "ko-", "linewidth", 2, "markersize", 8, "markerfacecolor", "g");
  59.   hold on;
  60.   plot ([N_vals(1), N_vals(end)], [1, 1], "r--", "linewidth", 1);
  61.   hold off;
  62.   xlabel ("N"); ylabel ("Speedup (t_orig / t_blocked)");
  63.   title ("pdist2: Speedup from Blocked Implementation");
  64.   grid on;
  65.   ylim ([0, max(res(:,6)) * 1.1]);
  66.   print (h2, "bench_pdist2_speedup.png", "-dpng", "-r150");
  67.   close (h2);
  68.   printf ("Saved: bench_pdist2_speedup.png\n");
  69. endfunction
  70.  
  71. function [t_med, t_std] = run_timed (fn, n_runs, n_warmup)
  72.   for w = 1:n_warmup
  73.     fn ();
  74.   endfor
  75.   times = zeros (n_runs, 1);
  76.   for r = 1:n_runs
  77.     tic;
  78.     fn ();
  79.     times(r) = toc;
  80.   endfor
  81.   t_med = median (times);
  82.   t_std = std (times);
  83. endfunction
  84.  
  85. function D = pdist2_orig (X, Y)
  86.   [ix, iy] = meshgrid (1:rows (X), 1:rows (Y));
  87.   D = sqrt (sum ((X(ix(:),:) - Y(iy(:),:)) .^ 2, 2));
  88.   D = reshape (D, rows (Y), rows (X))';
  89. endfunction
  90.  
  91. function D = pdist2_blocked (X, Y)
  92.   M = rows (X);
  93.   N = rows (Y);
  94.   D = zeros (M, N);
  95.   for i = 1:M
  96.     D(i, :) = sqrt (sum ((X(i, :) - Y) .^ 2, 2))';
  97.   endfor
  98. endfunction
  99.  
Advertisement
Add Comment
Please, Sign In to add comment