Author Topic: O2 JIT function calls  (Read 6733 times)

0 Members and 1 Guest are viewing this topic.

Charles Pegge

  • Guest
Re: O2 JIT function calls
« Reply #15 on: May 28, 2013, 09:10:52 PM »
Phew! You had me worried there. Yes, your syntax caused a recursion.

here is another example with char strings. You will need to change to \sb22.



X

JRS

  • Guest
Re: O2 JIT function calls
« Reply #16 on: May 28, 2013, 09:41:19 PM »
I'm in BASIC heaven!

Code: [Select]
  include "dllcinc.sb"

  oxy=dllfile("/sb22/modules/oxygen.dll")

  o2_basic = dllproc( oxy, "o2_basic i =(c*source) " )
  o2_exec  = dllproc( oxy, "o2_exec  i =(i call)   " )
  o2_error = dllproc( oxy, "o2_error c*=()         " )
  o2_errno = dllproc( oxy, "o2_errno i =()         " )
  o2_len   = dllproc( oxy, "o2_len   i =()         " )
  o2_mode  = dllproc( oxy, "o2_mode     (i mode)   " )

  dllcall o2_mode,1

' ==============================
  src="""
  extern

  function funA(char* s) as char*
  static char c[256]
  c="Hello "+s
  return c
  end function

  function funB(char*s) as char*
  static char c[256]
  c="Goodbye "+s
  return c
  end function

  function reverse(char*s)
  byte b at (strptr s)
  sys i,d,e,j,t
  e=len s
  d=e>>1
  for i=1 to d
  j=e-i+1
  t=b[i] : b[i]=b[j] : b[j]=t
  next
  end function

  sub finish()
  terminate
  end sub

  function link(sys n) as sys
  select n
  case 0 : return @finish
  case 1 : return @funA
  case 2 : return @funB
  case 3 : return @reverse
  end select
  end function

  end extern
 

  addr link
  """
' ==============================

  function oxygen(src)
  dllcall o2_basic,src
  if (dllcall(o2_errno)<>0) then
    dllprnt dllcall(o2_error)
    a=0
  else
    a=dllcall(o2_exec,0)
  end if
  oxygen=a
  end function
  '
  a=oxygen(src)
  '
  if (a<>0) then
  '
' ==============================
  '
  Hello   = dllproc(a,"Hello   c*=(c*value) ", dllcald(a,1) )
  Goodbye = dllproc(a,"Goodbye c*=(c*value) ", dllcald(a,2) )
  Reverse = dllproc(a,"Reverse    (c*value) ", dllcald(a,3) )
  Finish  = dllproc(a,"Finish     ()        ", dllcald(a,0) )
  '
' ==============================
  '
  print dllcall(hello,"Henry") & "\n"
  print dllcall(Goodbye,"Henry") & "\n"
  s="abcdef"
  print "Reversed " & s & " = "
  dllcall(Reverse,s)
  print s & "\n"
  dllcall(Finish)
  '
  end if
  dllfile
  line input q

C:\SB22\o2call>scriba dllco2_E.sb
Hello Henry
Goodbye Henry
Reversed abcdef = fedcba

C:\SB22\o2call>

Charles Pegge

  • Guest
Re: O2 JIT function calls
« Reply #17 on: May 28, 2013, 09:52:50 PM »
Glad it's working. I must catch some more sleep.

JRS

  • Guest
Re: O2 JIT function calls
« Reply #18 on: May 29, 2013, 11:59:11 PM »
Charles,

I can't seem to get by the defining of the JIT function from the SB side. It GPF's on either function. (mandel or Finish) I can get to just prior defining these functions.

Code: OxygenBasic
  1. ' JAPI 2.0 DLLC JIT
  2.  
  3. include "dllcinc.sb"
  4.  
  5. oxy = dllfile("/sb22/modules/oxygen.dll")
  6.  
  7. o2_basic = dllproc( oxy, "o2_basic i =(c*source) " )
  8. o2_exec  = dllproc( oxy, "o2_exec  i =(i call)   " )
  9. o2_error = dllproc( oxy, "o2_error c*=()         " )
  10. o2_errno = dllproc( oxy, "o2_errno i =()         " )
  11. o2_len   = dllproc( oxy, "o2_len   i =()         " )
  12. o2_mode  = dllproc( oxy, "o2_mode     (i mode)   " )
  13.  
  14. dllcall(o2_mode,1)
  15.  
  16. src = """
  17. extern
  18. function mandel(float zre,zim,sys maxiter) as sys
  19.     float mx,my,betrag
  20.     sys iter
  21.  
  22.     while iter < maxiter and betrag < 4.0
  23.         iter = iter+1
  24.         tmp = mx*mx-my*my+zre
  25.         my = 2*mx*my+zim
  26.         mx = tmp
  27.         betrag = (mx*mx + my*my)
  28.     wend
  29.     return iter
  30. end function
  31.  
  32. sub finish()
  33.   terminate
  34. end sub
  35.  
  36. function link(sys n) as sys
  37.   select n
  38.     case 0
  39.       return @finish
  40.     case 1
  41.       return @mandel
  42.   end select
  43. end function
  44.  
  45. end extern
  46.  
  47. addr link
  48. """
  49.  
  50. dllcall(o2_basic, src)
  51. dfn = dllcall(o2_exec,0)
  52. mandel = dllproc(dfn,"mandel i = (f zre, f zim, i maxiter)", dllcald(dfn, 1))
  53. Finish = dllproc(dfn,"Finish ()", dllcald(dfn, 0))
  54.  
  55. japi = dllfile("japi.dll")
  56.  
  57. j_start = dllproc(japi, "j_start i = ()")
  58. j_frame = dllproc(japi, "j_frame i = (c * label)")
  59. j_menubar = dllproc(japi, "j_menubar i = ( i obj)")
  60. j_menu = dllproc(japi, "j_menu i = (i obj, c *label)")
  61. j_menuitem = dllproc(japi, "j_menuitem i = (i obj, c *label)")
  62. j_canvas = dllproc(japi, "j_canvas i = (i obj, i width , i height)")
  63. j_setpos = dllproc(japi, "j_setpos (i obj, i xpos, i ypos)")
  64. j_pack = dllproc(japi, "j_pack (i obj)")
  65. j_show = dllproc(japi, "j_show (i obj)")
  66. j_getaction = dllproc(japi, "j_getaction i = ()")
  67. j_nextaction = dllproc(japi, "j_nextaction i = ()")
  68. j_setcolor = dllproc(japi, "j_setcolor (i obj, i r, i g, i b)")
  69. j_drawpixel = dllproc(japi, "j_drawpixel (i obj, i x, i y)")
  70. j_quit = dllproc(japi, "j_quit ()")
  71.  
  72. CONST J_TRUE = 1
  73. CONST J_FALSE = 0
  74.  
  75.  
  76. xstart = -1.8
  77. xend   =  0.8
  78. ystart = -1.0
  79. yend   =  1.0
  80.  
  81. hoehe  = 240
  82. breite = 320
  83.  
  84. if (dllcall(j_start) = J_FALSE) then
  85.   print("JAPI interface failed to start.\n")
  86.   end
  87. end if
  88.  
  89. jframe  = dllcall(j_frame,"JAPI 2.0 DLLC")
  90. menubar = dllcall(j_menubar,jframe)
  91. jfile   = dllcall(j_menu,menubar,"File")
  92. calc    = dllcall(j_menu,menubar,"Calc")
  93. quit    = dllcall(j_menuitem,jfile,"Quit")
  94. start   = dllcall(j_menuitem,calc,"Start")
  95. jstop   = dllcall(j_menuitem,calc,"Stop")
  96.  
  97. canvas  = dllcall(j_canvas,jframe,breite,hoehe)
  98. dllcall(j_setpos,canvas,10,60)
  99. dllcall(j_pack,jframe)
  100. dllcall(j_show,jframe)
  101.  
  102. obj = 0
  103. do_work = 0
  104.  
  105. while((obj <> jframe) and (obj <> quit))
  106.     if(do_work = 1) then
  107.         obj = dllcall(j_getaction)
  108.     else
  109.         obj = dllcall(j_nextaction)
  110.     end if      
  111.  
  112.     if(obj = start) then
  113.         x = -1
  114.         y = -1
  115.         do_work = 1
  116.     end if
  117.  
  118.     if(obj = jstop) then
  119.         do_work = 0
  120.     end if
  121.    
  122.     if(do_work = 1) then
  123.         x = (x+1) % breite
  124.        if(x = 0) then
  125.             y = (y+1) % hoehe
  126.        end if
  127.        if((x = breite-1) and (y = hoehe-1)) then
  128.             do_work = 0
  129.        else
  130.             zre = xstart + x*(xend-xstart)/breite
  131.             zim = ystart + y*(yend-ystart)/hoehe
  132.             it = dllcall(mandel,zre,zim,512)
  133.             dllcall(j_setcolor,canvas,it*11,it*13,it*17)
  134.             dllcall(j_drawpixel,canvas,x,y)
  135.        end if
  136.     end if
  137. wend
  138.  
  139. dllcall(Finish)
  140. dllcall(j_quit)
  141. dllfile
  142.  
« Last Edit: May 30, 2013, 07:32:21 PM by JRS »

Charles Pegge

  • Guest
Re: O2 JIT function calls
« Reply #19 on: May 30, 2013, 07:28:14 AM »
Hi John,

Something very strange. I got it to work eventually by indenting the o2 source lines by 2 spaces. I will have to see what is happening to the source string as it enters the compiler.

Anyway, I will add the one attached here to the ScriptBasic examples.


X

JRS

  • Guest
Re: O2 JIT function calls
« Reply #20 on: May 30, 2013, 07:43:54 AM »
I indented my version that didn't work and it now it does.

Can't believe the difference between calling the O2 mandel() function and calling it in SB.

Great job, couldn't be happier!

JRS

  • Guest
Re: O2 JIT function calls
« Reply #21 on: May 30, 2013, 08:31:26 AM »
ScriptBasic
C:\SB22\japi_dllc>scriba mandel_dllc.sb
56.9223

C:\SB22\japi_dllc>

ScriptBasic JIT

C:\SB22\japi_dllc>scriba mandel_dllc2.sb
17.608

C:\SB22\japi_dllc>

If my math is correct, that's 309% faster than stock SB.

« Last Edit: May 30, 2013, 08:44:06 AM by JRS »

Charles Pegge

  • Guest
Re: O2 JIT function calls
« Reply #22 on: May 30, 2013, 09:08:42 AM »
I've fixed the source problem. It was chr(10) line terminators again. Oxygen should now be able to handle these and Mac line terminators chr(13) as well as MS lines chr(13,10) and any combination of them.

Posted the updates:

http://www.oxygenbasic.org/forum/index.php?topic=749.0

JRS

  • Guest
Re: O2 JIT function calls
« Reply #23 on: May 30, 2013, 09:43:53 AM »
Quote
I've fixed the source problem. It was chr(10) line terminators again.

Sorry Charles. The downside of working with a Linux user.

The blame may be SB itself as multi-line strings may use CHR(10) no matter what platform it's running under. Lets blame Peter Verhas. I haven't abused him in awhile.  ;D

Update

After giving this some thought, I agree with Peter's direction at the time as SB was primary targeted at web scripting and all web servers typically use CHR(10) as HTML line termination.
« Last Edit: May 31, 2013, 12:38:24 PM by JRS »

JRS

  • Guest
Re: O2 JIT function calls
« Reply #24 on: May 31, 2013, 11:18:22 PM »
I converted the JAPI Mandelbrot Set to Simple Window to compare the speed of the two libraries.



Code: OxygenBasic
  1. ' Simple Window DLLC JIT
  2.  
  3. include "dllcinc.sb"
  4.  
  5. oxy = dllfile("/sb22/modules/oxygen.dll")
  6.  
  7. o2_basic = dllproc( oxy, "o2_basic i =(c*source) " )
  8. o2_exec  = dllproc( oxy, "o2_exec  i =(i call)   " )
  9. o2_error = dllproc( oxy, "o2_error c*=()         " )
  10. o2_errno = dllproc( oxy, "o2_errno i =()         " )
  11. o2_len   = dllproc( oxy, "o2_len   i =()         " )
  12. o2_mode  = dllproc( oxy, "o2_mode     (i mode)   " )
  13.  
  14. dllcall(o2_mode,1)
  15.  
  16. src = """
  17. extern
  18. function mandel(float zre,zim,sys maxiter) as sys
  19.     float mx,my,betrag
  20.     sys iter
  21.  
  22.     while iter < maxiter and betrag < 4.0
  23.         iter = iter+1
  24.         tmp = mx*mx-my*my+zre
  25.         my = 2*mx*my+zim
  26.         mx = tmp
  27.         betrag = (mx*mx + my*my)
  28.     wend
  29.     return iter
  30. end function
  31.  
  32. sub finish()
  33.   terminate
  34. end sub
  35.  
  36. function link(sys n) as sys
  37.   select n
  38.     case 0
  39.       return @finish
  40.     case 1
  41.       return @mandel
  42.   end select
  43. end function
  44.  
  45. end extern
  46.  
  47. addr link
  48. """
  49.  
  50. dllcall(o2_basic, src)
  51. dfn = dllcall(o2_exec,0)
  52. mandel = dllproc(dfn,"mandel i = (f zre, f zim, i maxiter)", dllcald(dfn, 1))
  53. finish = dllproc(dfn,"finish ()", dllcald(dfn, 0))
  54.  
  55. sw = dllfile("sw.dll")
  56.  
  57. Window = dllproc(sw, "Window i = (i width, i height, i mode)")
  58. SetCaption = DLLC_Proc(sw, "SetCaption i = (c *capText)")
  59. SetPixel = dllproc(sw, "SetPixel i = (i xPos, i yPos, i color)")
  60. RGB = dllproc(sw, "RGB i = (i rValue, i gValue, i bValue)")
  61. Redraw = DLLC_Proc(sw, "Redraw ( )")
  62. WaitKey = dllproc(sw, "WaitKey i = ( )")
  63. Quit = DLLC_Proc(sw, "Quit i = ( )")
  64.  
  65.  
  66. xstart = -1.8
  67. xend   =  0.8
  68. ystart = -1.0
  69. yend   =  1.0
  70.  
  71. hoehe  = 240
  72. breite = 320
  73.  
  74. dllcall(Window,320,240,1)
  75. dllcall(SetCaption,"SB SW DLLC JIT")
  76.  
  77. st = dllsecs()
  78.  
  79. x = -1
  80. y = -1
  81.  
  82. REPEAT
  83.   x = (x+1) % breite
  84.   if(x = 0) then y = (y+1) % hoehe
  85.   zre = xstart + x*(xend-xstart)/breite
  86.   zim = ystart + y*(yend-ystart)/hoehe
  87.   it = dllcall(mandel,zre,zim,512)
  88.   dllcall(SetPixel,x, y, dllcall(RGB,it*11,it*13,it*17))
  89.   dllcall(Redraw)
  90. UNTIL ((x = breite-1) and (y = hoehe-1))
  91. PRINT format("%g",dllsecs() - st),"\n"
  92. dllcall(WaitKey)
  93.  
  94. dllcall(Finish)
  95. dllcall(Quit)
  96. dllfile
  97.  

C:\SB22\japi_dllc>scriba swmandeldllc.sb
5.57576

C:\SB22\japi_dllc>

If I remove the Redraw() function and wait until the it's done to refresh the screen.

C:\SB22\japi_dllc>scriba swmandeldllc.sb
1.05691

C:\SB22\japi_dllc>
« Last Edit: June 01, 2013, 12:33:54 AM by JRS »

JRS

  • Guest
Re: O2 JIT function calls
« Reply #25 on: June 01, 2013, 12:03:32 AM »
This is stock SB with no JIT assist.

Code: OxygenBasic
  1. ' Simple Window DLLC
  2.  
  3. include "dllcinc.sb"
  4.  
  5. sw = dllfile("sw.dll")
  6.  
  7. Window = dllproc(sw, "Window i = (i width, i height, i mode)")
  8. SetCaption = DLLC_Proc(sw, "SetCaption i = (c *capText)")
  9. SetPixel = dllproc(sw, "SetPixel i = (i xPos, i yPos, i color)")
  10. RGB = dllproc(sw, "RGB i = (i rValue, i gValue, i bValue)")
  11. Redraw = DLLC_Proc(sw, "Redraw ( )")
  12. WaitKey = dllproc(sw, "WaitKey i = ( )")
  13. Quit = DLLC_Proc(sw, "Quit i = ( )")
  14.  
  15. function mandel(zre,zim,maxiter)
  16.     mx = 0.0
  17.     my = 0.0
  18.     iter=0
  19.     betrag=0.0
  20.  
  21.     while ((iter < maxiter) and (betrag < 4.0))
  22.         iter = iter+1
  23.         tmp = mx*mx-my*my+zre
  24.         my = 2*mx*my+zim
  25.         mx = tmp
  26.         betrag = (mx*mx + my*my)
  27.     wend
  28.     mandel=iter
  29. end function
  30.  
  31. xstart = -1.8
  32. xend   =  0.8
  33. ystart = -1.0
  34. yend   =  1.0
  35.  
  36. hoehe  = 240
  37. breite = 320
  38.  
  39. dllcall(Window,320,240,1)
  40. dllcall(SetCaption,"SB SW DLLC")
  41.  
  42. st = dllsecs()
  43.  
  44. x = -1
  45. y = -1
  46.  
  47. REPEAT
  48.   x = (x+1) % breite
  49.   if(x = 0) then y = (y+1) % hoehe
  50.   zre = xstart + x*(xend-xstart)/breite
  51.   zim = ystart + y*(yend-ystart)/hoehe
  52.   it = mandel(zre,zim,512)
  53.   dllcall(SetPixel,x, y, dllcall(RGB,it*11,it*13,it*17))
  54. UNTIL ((x = breite-1) and (y = hoehe-1))
  55. dllcall(Redraw)
  56. PRINT format("%g",dllsecs() - st),"\n"
  57. dllcall(WaitKey)
  58.  
  59. dllcall(Quit)
  60. dllfile
  61.  

With Redraw() in the loop.

C:\SB22\japi_dllc>scriba swnojit.sb
45.0972

C:\SB22\japi_dllc>

Wait until the end to display the results.

C:\SB22\japi_dllc>scriba swnojit.sb
39.637

C:\SB22\japi_dllc>
« Last Edit: June 01, 2013, 12:44:41 AM by JRS »