Hot!How to use properly the DSP library functions?

Author
thoraz
Starting Member
  • Total Posts : 70
  • Reward points : 0
  • Joined: 2013/06/01 18:37:43
  • Location: 0
  • Status: offline
2017/10/03 17:08:24 (permalink)
0

How to use properly the DSP library functions?

I'm working with the matricial operation and I want to use for my first time the dsplib_dsp.h library. I'm in the Harmony framework. From the XC32 I found the mips_vec_dotp32 function:
int32
mips_vec_dotp32
(
    int32 *indata1,
    int32 *indata2,
    int N,
    int scale
);

where indata1 and indata2 are in Q31 format. I haven't confidence with this format and I don't understand how to use the function. My original basic code for a dot product between vectors is the following 

uint32_t i, N = 100;
double vectorA[100], vectorB[100];
double mul = 0;
 
//...Fill the vectors with double values...
 
for ( i = 0; i < N; i++ )
    mul +=vectorA[i] * vectorB[i];

and for the matrix product

uint32_t i, j, k, N = 100;
double matrixA[100][100], matrixB[100][100], result[100][100];
 
//...Fill the matrix with double values...
 
for(i=0; i<N; ++i)
    for(j=0; j<N; ++j)
        for(k=0; k<N; ++k)
        {
            result[i][j]+=a[i][k]*b[k][j];
         }

Now, how can I the mips_vec_dotp32 function in this code to improve the performance (if there will be an improvement)? Can anyone show me a little example?
#1

6 Replies Related Threads

    Paul Bjork
    Super Member
    • Total Posts : 204
    • Reward points : 0
    • Joined: 2011/09/21 14:08:10
    • Location: 0
    • Status: offline
    Re: How to use properly the DSP library functions? 2017/10/04 08:28:33 (permalink)
    0
    Unfortunately, I believe that this mips function is for the mips fixed point multiplier hardware, not floating point (double) like it appears that your original code was.  So it won't help you at all.  
     
    Are you using a controller with an FPU?  There are other libraries out there that will help you with a vector multiply on an FPU.
    #2
    thoraz
    Starting Member
    • Total Posts : 70
    • Reward points : 0
    • Joined: 2013/06/01 18:37:43
    • Location: 0
    • Status: offline
    Re: How to use properly the DSP library functions? 2017/10/04 17:02:40 (permalink)
    +1 (1)
    I have seen that all the mips function accept inputs in Q format so I have supposed that there was some kind of conversion from "normal" data like float/double in the fixed point format Q.
    Anyway, I'm using an PIC32MZ0512EFE64 which implements an FPU. You can tell me that there are other libraryes that will help me? Can you link me these?
     
     
    #3
    LostInSpace
    Senior Member
    • Total Posts : 111
    • Reward points : 0
    • Joined: 2016/03/11 22:47:59
    • Location: 0
    • Status: offline
    Re: How to use properly the DSP library functions? 2017/10/07 19:47:54 (permalink)
    +2 (2)
    All the Microchip supplied DSP functions contain sample calling code in the help files. I have found these very useful in figuring out how exactly to call the functions.
     
    Because of speed and size almost all DSP code for microprocessors is written in fixed point format. The format most common in the DSP libraries is Q15 and Q31 (for 16 and 32 bit number widths respectively).
     
    Q format is just a way of thinking about how to change a signed int into a fixed point representation. As another poster has suggested it is just a mental exercise.
     
    When I learned about Q formats I just did a search for "Q number format" and "fixed point format" numbering systems and I found many tutorials.
     
    If you really want to use floating point, then you will have to write your own functions - again there are many tutorials on the web on how to calculate everything.
     
    HTH
     
    post edited by LostInSpace - 2017/10/07 19:51:15
    #4
    simong123
    Lab Member No. 003
    • Total Posts : 1144
    • Reward points : 0
    • Joined: 2012/02/07 18:21:03
    • Location: Future Gadget Lab (UK Branch)
    • Status: offline
    Re: How to use properly the DSP library functions? 2017/10/07 20:31:44 (permalink)
    +2 (2)
    On an 'EF there is little benefit to using the dsp functions for floats. The FPU has a throughput of 1 single precision FMA/clock. You'd spend more time converting to/from Q31. At optimisation level -O1 or greater the compiler should produce reasonably decent code.
    You could search for my previous posts on the EF FPU, I think I posted a hand optimised dot product assembly code (maybe only for 1-dimensional vectors, I cannot remember, but you should get the general idea).
     
    Edit:-
    Found the library I wrote. Does vector dot product and n x n matrix multiply.
    post edited by simong123 - 2017/10/07 20:33:52

    Attachment(s)

    Attachments are not available: Download requirements not met
    #5
    thoraz
    Starting Member
    • Total Posts : 70
    • Reward points : 0
    • Joined: 2013/06/01 18:37:43
    • Location: 0
    • Status: offline
    Re: How to use properly the DSP library functions? 2017/10/08 10:04:13 (permalink)
    0
    simong123
    On an 'EF there is little benefit to using the dsp functions for floats. The FPU has a throughput of 1 single precision FMA/clock. You'd spend more time converting to/from Q31. At optimisation level -O1 or greater the compiler should produce reasonably decent code.
    You could search for my previous posts on the EF FPU, I think I posted a hand optimised dot product assembly code (maybe only for 1-dimensional vectors, I cannot remember, but you should get the general idea).
     
    Edit:-
    Found the library I wrote. Does vector dot product and n x n matrix multiply.



    Great! I am very grateful to you. Thanks!
    #6
    thoraz
    Starting Member
    • Total Posts : 70
    • Reward points : 0
    • Joined: 2013/06/01 18:37:43
    • Location: 0
    • Status: offline
    Re: How to use properly the DSP library functions? 2017/11/09 05:56:01 (permalink)
    0
    simong123
    On an 'EF there is little benefit to using the dsp functions for floats. The FPU has a throughput of 1 single precision FMA/clock. You'd spend more time converting to/from Q31. At optimisation level -O1 or greater the compiler should produce reasonably decent code.
    You could search for my previous posts on the EF FPU, I think I posted a hand optimised dot product assembly code (maybe only for 1-dimensional vectors, I cannot remember, but you should get the general idea).
     
    Edit:-
    Found the library I wrote. Does vector dot product and n x n matrix multiply.




    Hi simong123!
    After one month I'm trying to use your library and I have some trouble! I say I'm not an expert in assembly language so I don't understand deeply your vectorial functions.
    Anyway, I'm testing the library on my system which mounts a PIC32MZ0512EFE64, as you know already. In my first test the code was
     
    VEC3 Vect1 = { 1.1, 2.2, 3.3 };
    VEC3 Vect2 = { 1.1, 2.2, 3.3 };
    _VectorType dotProductRes;
    printf( "\nVector1 = %f, %f, %f\n", Vect1.d[0], Vect1.d[1], Vect1.d[2] );
    printf( "\nVector2 = %f, %f, %f\n", Vect2.d[0], Vect2.d[1], Vect1.d[2] );
    dDotProduct( Vect1.d, Vect2.d, &dotProductRes, 3 );
    printf( "\nDot Product = %f\n", dotProductRes );

     
    but in this case the whole system freezes, so I tryed to print only the source vectors and cut away the dDotProduct function (the last two rows of the code above) to see if the vector initializzation was right: yes, it was. The print out is the following.
     
    Vector1 = 1.100000, 2.200000, 3.300000
    Vector2 = 1.100000, 2.200000, 3.300000

     
    The second test was to create my 6-dim vector starting from your and try the dot product again:
     
    typedef union
    {
        struct{
        _VectorType i1;
        _VectorType i2;
        _VectorType i3;
        _VectorType i4;
        _VectorType i5;
        _VectorType i6;
        };
        _VectorType d[6];
    } VEC6 __attribute__ ((aligned(8))) ;
     
    VEC6 Vect1 = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6 };
    VEC6 Vect2 = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6 };
    _VectorType dotProductRes;
    printf( "\nVector1 = %f, %f, %f, %f, %f, %f\n", Vect1.d[0], Vect1.d[1], Vect1.d[2], Vect1.d[3], Vect1.d[4], Vect1.d[5] );
    printf( "\nVector2 = %f, %f, %f, %f, %f, %f\n", Vect2.d[0], Vect2.d[1], Vect1.d[2], Vect2.d[3], Vect2.d[4], Vect1.d[5] );
    dDotProduct( Vect1.d, Vect2.d, &dotProductRes, 6 );
    printf( "\nDot Product = %f\n", dotProductRes );

     
    The output of this test is
     
    Vector1 = 1.100000, 2.200000, 3.300000, 4.400000, 5.500000, 6.600000
    Vector2 = 1.100000, 2.200000, 3.300000, 4.400000, 5.500000, 6.600000
    Dot Product = 0.000000

     
    Now the system doesn't freeze but the result of the function is 0!
    The last test confused me. I used my 6-dim vector type define but used only three values and in the dDotProduct function I gave 3 in the lenght field.
     
    VEC6 Vect1 = { 1.1, 2.2, 3.3 };
    VEC6 Vect2 = { 1.1, 2.2, 3.3 };
    VectorType dotProductRes;
    printf( "\nVector1 = %f, %f, %f\n", Vect1.d[0], Vect1.d[1], Vect1.d[2] );
    printf( "\nVector2 = %f, %f, %f\n", Vect2.d[0], Vect2.d[1], Vect1.d[2] );
    dDotProduct( Vect1.d, Vect2.d, &dotProductRes, 3 );
    printf( "\nDot Product = %f\n", dotProductRes );

     
    This is the output
     
    Vector1 = 1.100000, 2.200000, 3.300000
    Vector2 = 1.100000, 2.200000, 3.300000
    Dot Product = -429496729500000000.000000

     
    The result is -429496729500000000.000000!
     
    What I'm wrong? Am I using the library in the right way? Are the library tested?
    #7
    Jump to:
    © 2017 APG vNext Commercial Version 4.5