Announcement

Collapse
No announcement yet.

DLL for VB

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • DLL for VB

    I have written an image processing program in VB, but I am interested in seeing if some of the complex calculations can be made to run faster by switching them to a DLL in PB. The common operation in all of the subs or functions of the DLL would be to receive an existing 2D array of single precision numbers from VB, modify the array values, and then pass the array back to VB.

    From my reading of the online help manual it seems that this could be done by manipulating an absolute array overlay in PB. From my current understanding the DLL routines could look something like the following:

    DECLARE Sub ModifyVBMatrix( ByRef first_element as Single, C as Long, R as Long) Export

    Dim I as Long, J as Long
    Dim PB_array(0 to C-1, 0 to R-1) as Single at first_element

    For I= 0 to R-1
    For J=0 to C-1
    PB_array(J,I)= "something complicated"
    Next J
    Next I

    End Sub

    Where first_element is the (0,0) element of the VB array with C columns and R rows.

    The fundamental question is whether or not this is a valid approach? If the approach is valid, will this simple method work, or do I have to use the more elaborate method of calling the API's for manipulating VB SafeArrays in order to get the parameters for dimensioning the PB_array() ?

  • #2
    >The fundamental question is whether or not this is a valid approach?

    Looks good to me assuming you can build the array (small 'a') in VB.

    Not sure if you have R and C in the right order in the REDIM statement but you will find that out quick enough.

    MCM
    Michael Mattias
    Tal Systems (retired)
    Port Washington WI USA
    [email protected]
    http://www.talsystems.com

    Comment


    • #3
      Looks worthwhile.

      A small change. Need to use VarPtr(first_element) to get the pointer to the memory location first element is stored at and initialize the array at this memory location.

      I attached complete example using some functions that take time.
      VB: 7344 PB: 5453 or 25% faster. Seems it could be worthwhile, although not sure how complex your formulas are.

      You will also notice I compare the VB/PB results making sure there is no difference.

      NOTE: You need to run the VB application in compiled exe form. Running in the VB IDE gives a false performance result (It will take a long time) as it is 100% interpreted.

      Also running inside the IDE you need to copy the PB DLL to a path where the IDE can see it. Either Windows\System32 or VB ide directory. You will get a pbcalc not found. I always forget about this.




      Code:
      DECLARE Sub ModifyVBMatrix Alias "ModifyVBMatrix" ( ByRef first_element as Single, C as Long, R as Long) Export
      
      Dim I as Long, J as Long
      Dim PB_array(0 to C-1, 0 to R-1) as Single at varPtr(first_element)
      
      For I= 0 to R-1
       For J=0 to C-1
         PB_array(J,I)= "something complicated"
       Next J
      Next I
      
      End Sub

      Hope this helps

      Brian Chirgwin
      Attached Files

      Comment


      • #4
        Brian is correct, you can also do it this way
        Code:
        DECLARE Sub ModifyVBMatrix( ByVal first_element as DWord, C as Long, R as Long) Export
        
        Dim I as Long, J as Long
        Dim PB_array(0 to C-1, 0 to R-1) as Single at first_element
        Actually if you get comfortable with pointers you don't even need to dim the array
        The following snippet of code passes 3 arrays, 2 of singles and one of UDTs which contain an array. Accepts calls equally from VB or PB.
        Code:
        $COMPILE DLL
        TYPE X_array
            x(7) AS SINGLE
        END TYPE
        SUB Dl_Mult_Reg(BYVAL xv AS DWORD, BYVAL yv AS DWORD, np&, nx&, BYVAL bv AS DWORD) EXPORT
        'nx&=number of x variables
        'np& = number of samples
        'bv& points to an array of singles 1 larger than the number of x values
        'note the array of x variables must always bi in an array of type X_array
        'xvar(np&-1,nx&-1)=x variables
        'yvar(np&-1)=y variables
        DIM xvar AS X_array POINTER
        DIM yvar AS SINGLE POINTER
        DIM b AS SINGLE POINTER
        REDIM ytot(nx&) AS EXT
        REDIM xtot(nx& - 1, nx&) AS EXT    ',0=sum of
        REDIM equ(nx&, nx&, nx& + 1) AS EXT
        xvar = xv
        yvar = yv
        b = bv
        FOR x& = 0 TO np& - 1
            ytot(0) = ytot(0) + @yvar[x&]
            FOR y& = 1 TO nx&
                ytot(y&) = ytot(y&) + @yvar[x&] * @xvar[x&].x( y& - 1)   'sum of y mult by x's
                xtot(y& - 1, 0) = xtot(y& - 1, 0) + @xvar[x&].x( y& - 1)    'sum of x's
                FOR z& = y& TO nx&
                    xtot(y& - 1, z&) = xtot(y& - 1, z&) + @xvar[x&].x( y& - 1) * @xvar[x&].x( z& - 1)
                NEXT
            NEXT
        NEXT

        Comment


        • #5
          DLL test

          Thanks for the fast responses! I bought PB and tried writing a DLL using Brian's suggestion of explicitly using VARPTR. I eventually got it to work on a simple routine that merely rescales a matrix by explitily calling and assigning each element. The PB DLL version worked a little more than 6 times as fast as an interpreted VB version. From my experience wtith VB on simple routines like this one, the VB compiled version typically runs 3 to 3.5 times faster than interpreted. So using PB definitely looks promising for my needs. As I get more experience I'll try the additional suggestions made by John Petty.

          Ken German

          Comment

          Working...
          X