我们的任务是使用 openmp 来优化结构不佳的程序。我对编程很陌生,所以我希望任何人都可以了解如何优化这个功能(我需要优化的众多功能之一):
void
entry_type3(F2D *sData, F2D *ones, F2D *quat, F2D **pos, F2D **vel)
{
//Observation
F2D *t;
t = fSetArray(1, 3, 0);
asubsref(t,2) = -9.8;
F2D *accl = fDeepCopyRange(sData, 0, 1, 0, 3);
F2D *gtemp = fMtimes( ones, t);
F2D *gravity = quatRot(gtemp, quat);
fFreeHandle(gtemp);
fFreeHandle(t);
t = fSetArray(3,3,0);
asubsref(t,0) = 1;
asubsref(t,4) = 1;
asubsref(t,8) = 1;
int n = ones->height;
int i;
for(i=0; i<(t->height*t->width); i++)
asubsref(t,i) = asubsref(t,i)/STDDEV_ACCL;
F2D *w = mcl( gravity, accl, t);
generateSample(w, quat, *vel, *pos);
fFreeHandle(t);
//Motion model
t = fMtimes(ones, accl);
fFreeHandle(accl);
accl = fMinus(t, gravity);
fFreeHandle(w);
fFreeHandle(gravity);
fFreeHandle(t);
F2D *is;
#pragma omp parallel sections
{
#pragma omp section
{
F2D *is = quatConj(quat);
F2D *s = quatRot(*vel, is);
fFreeHandle(is);
for(i=0; i<(s->height*s->width); i++)
{
asubsref(s,i) = asubsref(s,i)*acclTimeInterval;
}
is = fPlus(*pos, s);
fFreeHandle(*pos);
*pos = fDeepCopy(is);
fFreeHandle(is);
fFreeHandle(s);
}
/** pos_ above stores: pos+quatRot(vel,quatConj(quat))*acclTimeInterval **/
#pragma omp section
{
F2D *is = quatConj(quat);
F2D *s = quatRot(accl, is);
F2D* t = fDeepCopy(s);
for(i=0; i<(s->height*s->width); i++)
{
asubsref(t,i) = 1/2*asubsref(s,i)*acclTimeInterval*acclTimeInterval;
}
/** t_ above stores: 1/2*quatRot(accl,quatCong(quat))*acclTimeInterval^2 **/
fFreeHandle(s);
fFreeHandle(is);
s = randnWrapper(n,3);
for(i=0; i<(s->height*s->width); i++)
{
asubsref(s,i) = asubsref(s,i) * M_STDDEV_POS;
}
/** s_ above stores: randn(n,3)*M_STDDEV_POS **/
is = fPlus(*pos, t);
fFreeHandle(*pos);
*pos = fPlus(is, s);
fFreeHandle(s);
fFreeHandle(t);
fFreeHandle(is);
}
}
//vel=vel+accl*acclTimeInterval+randn(n,3)*M_STDDEV_VEL;
#pragma omp parallel sections
{
#pragma omp section
{
F2D *t = fDeepCopy(accl);
#pragma omp parallel for
for(i=0; i<(accl->height*accl->width); i++)
{
asubsref(t,i) = asubsref(accl,i) * acclTimeInterval;
}
is = fPlus(*vel, t);
fFreeHandle(accl);
fFreeHandle(t);
}
#pragma omp section
{
F2D *s = randnWrapper(n,3);
#pragma omp parallel for
for(i=0; i<(s->height*s->width); i++)
{
asubsref(s,i) = asubsref(s,i) * M_STDDEV_VEL;
}
fFreeHandle(*vel);
*vel = fPlus(is, s);
fFreeHandle(is);
fFreeHandle(s);
}
}
}
我已经添加了一些 openmp 并行,但它仍然运行得很慢,所以我希望如果你经验丰富的退伍军人可以指出我应该关注哪些地方来提高性能。