bugGNU Octave - Bugs: bug #64840, VM __vm_enable__ seems to have not...

 
 

bug #64840: VM __vm_enable__ seems to have not negligible overhead losses (~5%) in certain situations

Submitter:  None
Submitted:  Mon 30 Oct 2023 03:31:42 AM UTC
   
 
Category:  None Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Performance
Status:  None Assigned to:  None
Originator Name:  Originator Email:  -email is unavailable-
Open/Closed:  * Open Release:  * dev
Operating System:  * GNU/Linux Fixed Release:  None
Planned Release:  None
* Mandatory Fields

Add a New Comment Rich Markup
   

Tue 31 Oct 2023 10:10:51 PM UTC, comment #1: 

I looked through quadv I think it is the calls to feval() that causes the slowdown from using auto-compilation. feval() is C++ code and new vm is allocated when feval() in turn calls the bytecode for 'sin(x)'.
Allocating a vm is not that much work, but it slows down a bit.

Chaning feval() to calling the handle directly makes auto-compilation somewhat faster than just compiling quadv and its subfunctions.

Making the runtime:
Elapsed time is 4.86418 seconds.
Elapsed time is 2.4246 seconds.
Elapsed time is 2.82154 seconds.

I guess feval could be a m-file instead that calls some __feval__
on string arguments, or something.


# HG changeset patch
# User Petter
# Date 1698789394 -3600
#      Tue Oct 31 22:56:34 2023 +0100
# Node ID 29bee433180c18d79ecb4aa524592461e422ee5f
# Parent  90f4180748f93d133fedb90b8b66f52701acae61
wip quadv call handles directly not a patch for submitting

diff -r 90f4180748f9 -r 29bee433180c scripts/general/quadv.m
--- a/scripts/general/quadv.m        Tue Oct 31 20:36:43 2023 +0100
+++ b/scripts/general/quadv.m        Tue Oct 31 22:56:34 2023 +0100
@@ -76,6 +76,10 @@
     print_usage ();
   endif

+  if ischar (f)
+    f = @(varargin) feval (f, varargin);
+  end
+
   if (isa (a, "single") || isa (b, "single"))
     eps = eps ("single");
   else
@@ -103,23 +107,24 @@
   c2 = (a + b)  / 2;
   c3 = (b2 + b) / 2;

-  fa  = feval (f, a,  varargin{:});
-  fc1 = feval (f, c1, varargin{:});
-  fb1 = feval (f, b1, varargin{:});
-  fc2 = feval (f, c2, varargin{:});
-  fb2 = feval (f, b2, varargin{:});
-  fc3 = feval (f, c3, varargin{:});
-  fb  = feval (f, b,  varargin{:});
+  fa  = f (a,  varargin{:});
+  fc1 = f (c1, varargin{:});
+  fb1 = f (b1, varargin{:});
+  fc2 = f (c2, varargin{:});
+  fb2 = f (b2, varargin{:});
+  fc3 = f (c3, varargin{:});
+  fb  = f (b,  varargin{:});
+
   nfev = 7;

   ## NOTE: If there are edge singularities, move edge point by eps*(b-a) as
   ## discussed in Shampine paper used to implement quadgk.
   if (any (isinf (fa(:))))
-    fa = feval (f, a + eps * (b-a), varargin{:});
+    fa = f (a + eps * (b-a), varargin{:});
     nfev++;
   endif
   if (any (isinf (fb(:))))
-    fb = feval (f, b - eps * (b-a), varargin{:});
+    fb = f (b - eps * (b-a), varargin{:});
     nfev++;
   endif

@@ -169,8 +174,8 @@

   d = (a + c) / 2;
   e = (c + b) / 2;
-  fd = feval (f, d, varargin{:});
-  fe = feval (f, e, varargin{:});
+  fd = f (d, varargin{:});
+  fe = f (e, varargin{:});
   nfev += 2;
   q1 = (c - a) / 6 * (fa + 4*fd + fc);
   q2 = (b - c) / 6 * (fc + 4*fe + fb);


Petter <petter>
Mon 30 Oct 2023 03:31:42 AM UTC, original submission:  

The performance of code executed using the VM and using vm_enabled flag = true seems to cause (in certain situations) a non negligible overhead:

In the example below the VM is faster than the hierarchical interpreter, but ~5% slower compared to code which uses the VM (only) for the pre-compiled key function and sets vm_enabled = false.

Whilst some overhead is expected and unavoidable, there seem to be room for improvement with the noted 5% performance drop.
 


__vm_enable__(0);
clear test2; clear quadv;
tic;
for i=1:10000
  y = test2(0, 2*pi);
endfor
toc;


__vm_enable__(1);
clear test2; clear quadv;
tic;
for i=1:10000
  y = test2(0, 2*pi);
endfor
toc;

__vm_enable__(0);
clear test2; clear quadv;
__vm_compile__("quadv");
tic;
for i=1:10000
  y = test2(0, 2*pi);
endfor
toc;



function ret = test2(a,b);
  ret = quadv(@(x) sin(x), a, b);
endfunction


results in

Elapsed time is 5.68666 seconds.
Elapsed time is 3.71263 seconds.
Elapsed time is 3.50177 seconds.
>> 1-3.50177/3.71263
ans = 0.056795



Note: Interesting is that when in addition to quadv, test2 is also pre-compiled and vm_enabled = false, the overall performance becomes (a little ~2%) slower compared to vm_enabled flag = true.


Anonymous

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

No files currently attached

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by petter (Posted a comment)
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only project members can vote.

     

    No changes have been made to this item

    Back to the top

    Powered by Savane 3.12