[Index for tmp_for_tar/misc] [Return to Master Index]

test_loop_add

(tmp_for_tar/misc/test_loop_add.m)


Function Synopsis

c = loop_add_naive (a,i,b)

Help text


 Last modified: April 2001



Listing of function file tmp_for_tar/misc/test_loop_add.m




## Author:        Etienne Grossmann  <etienne@isr.ist.utl.pt>
## Last modified: April 2001

ok = 1;
cnt = 0;
printf ("test_loop_add :\n");




function c = loop_add_naive (a,i,b)
  c=a;
  for j=1:rows(i(:)), c(i(j))+=b(j); end
end

				# Test for correctness
a = 0;
b = ones (3,4);
i = ones (3,4);

c1 = loop_add (a,i,b);
c2 = 12;

cnt++;
if any (abs (c1-c2) > sqrt (eps)), 
  printf ("not ok %i\n",cnt); keyboard 
else    
  printf ("ok %i\n",cnt); 
end

c1 = loop_add (a,i);

cnt++;
if any (abs (c1-c2) > sqrt (eps)), 
  printf ("not ok %i\n",cnt); keyboard 
else    
  printf ("ok %i\n",cnt); 
end

c1 = loop_add (a,ones(7,1),1:7);
c2 = 7*8/2 ;

cnt++;
if any (abs (c1-c2) > sqrt (eps)), 
  printf ("not ok %i\n",cnt); keyboard 
else    
  printf ("ok %i\n",cnt); 
end



				# Test for speed and correctness
sizes = [1:5,10:5:30,40:20:100,150:100:650];
ncalls = floor (max (20./(1+0.1*sizes),2));
t1 = t2 = zeros(1, columns (sizes));

ok0 = 1;
for k = 1:columns (sizes),
  
  N = sizes(k);

  a = randn(1,N);
  i = floor(rand(1,2*N)*N)+1 ;
  b = randn(size (i));

  
  ##  for dummy = 1:ncalls(k), 
  ##      mytic (); c1 = loop_add (a,i,b); t1(k) += mytic (); 
  ##      mytic (); c2 = loop_add_naive (a,i,b); t2(k) += mytic (); 
  ##    end
  mytic (); for dummy = 1:ncalls(k), c1 = loop_add (a,i,b); end; t1(k) += mytic (); 
  mytic (); for dummy = 1:ncalls(k), c2 = loop_add_naive (a,i,b); end; t2(k) += mytic (); 
  
  if any (abs (c1-c2) > sqrt (eps)),
    printf ("test_loop_add : Whoa!! There's a bug!!\n");
    keyboard
    ok = 0;
  end
end

cnt++ ;
if ok0, printf ("ok %i\n",cnt); else printf ("not ok %i\n",cnt); end


t1 = t1./ncalls;
t2 = t2./ncalls;

gset ("title",  "'CPU time : loop_add (green), plain loop (red)'")
gset ("xlabel", "'Number of added elements'");
plot (2*sizes, t1, "g", 2*sizes, t2, "r");



Produced by oct2html on Sat Apr 28 21:14:54 2001
Cross-Directory links are: ON