Community
cancel
Showing results for
Did you mean: 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;    double nx, nxsq, Rnew2;    double term2, term3;    double tmp, dR;    double ident = 	{ 1.0, 0.0, 0.0,	  0.0, 1.0, 0.0,	  0.0, 0.0, 1.0 };    theta = sqrt(w * w + w * w + w * w);    if (theta == 0.0) {	memcpy(Rnew, R, sizeof(double) * 9);	return;    }    n = w / theta;    n = w / theta;    n = w / theta;    nx = 0.0;   nx = -n;  nx = n;    nx = n;  nx = 0.0;    nx = -n;    nx = -n; nx = n;   nx = 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;
Ipp32f mat, matsq, matterm2, matterm3, mattmp, matdR, matR, matRNew;

Ipp32f matident =
{ 1.0, 0.0, 0.0,
0.0, 1.0, 0.0,
0.0, 0.0, 1.0 };

mtheta = sqrt(w * w + w * w + w * w);

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

mn = w / mtheta;
mn = w / mtheta;
mn = w / mtheta;

mat = 0.0;   mat = -mn;  mat = mn;
mat = mn;  mat = 0.0;    mat = -mn;
mat = -mn; mat = mn;   mat = 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.
8 Replies Beginner
255 Views
Have you allowed VirtualBox to pass-through all CPU features and cores?

Regards
Viktor Beginner
255 Views
Obviously my processor doesn't support virtualization (Pentium Dual-Core T4200) 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. 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 :  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 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 Beginner
255 Views
Hi,

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

Allan 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 