This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Slow coefficient-wise input?

Tags: None
(comma "," separated)
sschoen
Registered Member
Posts
4
Karma
0

Slow coefficient-wise input?

Sat Oct 15, 2011 6:36 am
Hi All,

I apologize if this question has been asked and answered, but I have been looking for some time and have found no suitable conclusion. So I had some code using Eigen that seemed to be running oddly slowly. I was able to confine the issue to what seems to be setting elements of a vector in a coefficient wise fashion. Here is the code of the test:

Just writing to doubles:
Code: Select all
double x,y,z;
for(int i = 0;i<50*50*50;i++)
    {
        //if(i%1000==0)
        //  cout << i << endl;
        //cout << x1 << " : " << y1 << " : " << z1 << endl;
        for(int j = i+1;j<50*50*50;j++)
        {
            x = j%50;
            y = j%(50*50)/50;
            z = j/(50*50);
        }
    }


Writing to an Eigen vector:
Code: Select all
Array<int,3,1> m;
   
    for(int i = 0;i<50*50*50;i++)
    {
        for(int j = i+1;j<50*50*50;j++)
        {
            m.coeffRef(0) = j%50;
            m.coeffRef(1) = j%(50*50)/50;
            m.coeffRef(2) = j/(50*50);
        }
    }


Now the latter snippet runs incomparably slowly with respect to the first and this just does not seem like it should be the case. Is there anything that I can do to get around this?

Thanks all.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: Slow coefficient-wise input?

Sat Oct 15, 2011 8:40 am
Make sure you compiled with optimization enabled (-O2)
sschoen
Registered Member
Posts
4
Karma
0

Re: Slow coefficient-wise input?

Sat Oct 15, 2011 8:52 am
Thanks for the idea, but I already am using -O2 (actually -O3, but I just tried -O2 to no avail.)
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: Slow coefficient-wise input?

Sat Oct 15, 2011 11:11 pm
which compiler version? also remark that your small benchmark could easily be too aggressively optimized by the compiler which could totally removes the loops... The best is to check the generated assembly in your real code, e.g., enclose your relevant part with:

EIGEN_ASM_COMMENT("my begin")
EIGEN_ASM_COMMENT("my end")

compile the file with -S -O2 -DNDEBUG

and search for "my begin" in the generated ASM file... If you see "call" instructions that means some functions have not been properly inlined...
sschoen
Registered Member
Posts
4
Karma
0

Re: Slow coefficient-wise input?

Sat Oct 15, 2011 11:30 pm
Thank you very much. Indeed the compiler was optimizing away the loop in the first example. I definitely feel dumb.


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], Yahoo [Bot]