Community
cancel
Showing results for 
Search instead for 
Did you mean: 
shivany
Beginner
255 Views

IPP Matrix functions

Hello,

I'd like to optimize the execution speed of my code using small matrices (3x3) and I tried to use IPP functions but the result seems to be slower than the original code. My program is running on Ubuntu 32-but using a VM (VirtualBox) that can only handle 1 processor on this computer. Here is the function I've tried to optimize :

[cpp]/* Compute an updated rotation matrix given the initial rotation 
* and the correction (w) */
void rot_update(double *R, double *w, double *Rnew)
{
double theta, sinth, costh, n[3];
double nx[9], nxsq[9], Rnew2[9];
double term2[9], term3[9];
double tmp[9], dR[9];

double ident[9] =
{ 1.0, 0.0, 0.0,
0.0, 1.0, 0.0,
0.0, 0.0, 1.0 };

theta = sqrt(w[0] * w[0] + w[1] * w[1] + w[2] * w[2]);

if (theta == 0.0) {
memcpy(Rnew, R, sizeof(double) * 9);
return;
}

n[0] = w[0] / theta;
n[1] = w[1] / theta;
n[2] = w[2] / theta;

nx[0] = 0.0; nx[1] = -n[2]; nx[2] = n[1];
nx[3] = n[2]; nx[4] = 0.0; nx[5] = -n[0];
nx[6] = -n[1]; nx[7] = n[0]; nx[8] = 0.0;

matrix_product33(nx, nx, nxsq);

sinth = sin(theta);
costh = cos(theta);

matrix_scale(3, 3, nx, sinth, term2);
matrix_scale(3, 3, nxsq, 1.0 - costh, term3);

matrix_sum(3, 3, 3, 3, ident, term2, tmp);
matrix_sum(3, 3, 3, 3, tmp, term3, dR);

matrix_product33(dR, R, Rnew2);

}

[/cpp]
______________________________________________________

[cpp]/* Compute an updated rotation matrix given the initial rotation 
 * and the correction (w) */
void rot_update(double *R, double *w, double *Rnew) 
{
    int i, stride0, stride1;
    Ipp32f mtheta, msinth, mcosth, mn[3];
    Ipp32f mat[9], matsq[9], matterm2[9], matterm3[9], mattmp[9], matdR[9], matR[9], matRNew[9];

    Ipp32f matident[9] =
    { 1.0, 0.0, 0.0,
      0.0, 1.0, 0.0,
      0.0, 0.0, 1.0 };

    mtheta = sqrt(w[0] * w[0] + w[1] * w[1] + w[2] * w[2]);

    if (mtheta == 0.0) {
        memcpy(Rnew, R, sizeof(double) * 9);
        return;
    }

    mn[0] = w[0] / mtheta;
    mn[1] = w[1] / mtheta;
    mn[2] = w[2] / mtheta;

    mat[0] = 0.0;   mat[1] = -mn[2];  mat[2] = mn[1];
    mat[3] = mn[2];  mat[4] = 0.0;    mat[5] = -mn[0];
    mat[6] = -mn[1]; mat[7] = mn[0];   mat[8] = 0.0;
    stride0 = 3*sizeof(Ipp32f);
    stride1 = sizeof(Ipp32f);

    ippmMul_mm_32f(mat, stride0, stride1, 3, 3, mat, stride0, stride1, 3, 3, matsq, stride0, stride1);

    msinth = sin(mtheta);
    mcosth = cos(mtheta);

    ippmMul_mc_32f(mat, stride0, stride1,msinth, matterm2, stride0, stride1, 3, 3);

    ippmMul_mc_32f(matsq, stride0, stride1, 1.0-mcosth, matterm3, stride0, stride1, 3, 3);

    ippmAdd_mm_32f(matident, stride0, stride1, matterm2, stride0, stride1, mattmp, stride0, stride1, 3, 3);

    ippmAdd_mm_32f(mattmp, stride0, stride1, matterm3, stride0, stride1, matdR, stride0, stride1, 3, 3);

    for(i=0; i<9; ++i)
         matR = R;

    ippmMul_mm_32f(matdR, stride0, stride1, 3, 3, matR, stride0, stride1, 3, 3, matRNew, stride0, stride1);

    for(i=0; i<9; ++i){
         Rnew = matRNew;
     }
}
[/cpp]

I didn't test it yet on other computers/systems with more processors/cores but I wanted to know if I was misusing the library or if something else in my code was wrong or could be enhanced.
0 Kudos
8 Replies
vrennert
Beginner
255 Views

Have you allowed VirtualBox to pass-through all CPU features and cores?

Regards
Viktor
shivany
Beginner
255 Views

Obviously my processor doesn't support virtualization (Pentium Dual-Core T4200)
mecej4
Black Belt
255 Views

What is the basis of your conclusion that the IPP version is slower? Your example involves such a small number of calculations that other factors (loading a DLL / shared library, for example) may control speed.
shivany
Beginner
255 Views

It's not only a function called once or twice, I actually launched valgrind to generate a call graph with the callgrind tool and it showed that the rate of this function (rot_update) is more important with the IPP code than in the original one even if there are less calls to it.

Here are the call graphs below, the first one without and the second one with IPP as you can guess :


Chao_Y_Intel
Employee
255 Views


Hello,

I think Viktor want to check if all CPU features are enabled in the Virtual Box, it needs make sure the optimized CPU code were actually used. You can check with ippsGetLibVersion() function, please see "Example 3-1 Using the Function ippsGetLibVersion" in the ippsman.pdf manual on this fucntions. If it used some PX code, actually the code did not use optimized function.

3x3 matrix computation is very fast operation, compared with the function call overhead. If possibly, you can combine the matrix multiplication into one function call with "matrix array operation".

Thanks,

Chao

shivany
Beginner
255 Views

Hello,

Here is the output of the printf showed in the 3-1 example :

libippsv8.so.6.1 6.1 build 137.36 6.1.137.827

shivany
Beginner
255 Views

Hi,

I didn't manage to solve the problem yet, does anyone has an idea?

Allan
Chao_Y_Intel
Employee
255 Views

Allan,

do you have your code implementation? We can have a benchmark here to check the performance (please reply with private message if you do not publish the code).

when looking at the code, in order to call the function, it adds some additional computation on data type conversion, it convert the data to single float first, then converting it back.

...

for(i=0; i<9; ++i)

matR = R;

...

for(i=0; i<9; ++i){

Rnew = matRNew;

}

If the matrix is small (fast in function call), such overheard may overcome the benefit of calling IPP functions.

Thanks,

Chao

Reply