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

Possible Alignment Problem - High Misalign Access Rate

Tags: None
(comma "," separated)
a1re
Registered Member
Posts
16
Karma
0
Hi,

I currently have a program that uses Eigen. It runs without error, and I have no alignment assertions from Eigen. However, when I do profiling using AMD CodeAnalyst, it has a misalign access rate of over 80%. I can trace it to
Code: Select all
Eigen::internal::assign_impl<Eigen::Map<Eigen::Array<double,-1,1,0,-1,1>,0,Eigen::Stride<0,0>>,
Eigen::CwiseBinaryOp<Eigen::internal::scalar_product_op<double,double>,
Eigen::Map<Eigen::Array<double,-1,1,0,-1,1>,0,Eigen::Stride<0,0> > const ,Eigen::CwiseUnaryO


in \eigen\src\core\assign.h . It occurs at line 316, which is
Code: Select all
dst.copyCoeff(i, src);


Now, this may be my fault, so I'll give a short breakdown of my program.

Code: Select all
struct my_struct{
   Eigen::ArrayXd v1, v2, v3, v4, v5;
   Eigen::ArrayXi v6;
   double a, b, c;
};

void init_struct(my_struct* ms, int val){
    //Initialize struct here using .resize(val) on Eigen objects
}

void compute(std::vector<my_struct>* vec_structs, my_struct* ms){
    // Perform operations on the vector of structs here
    // Mostly coefficient-wise operations
    // for example,
    // vec_structs->at(0).v1 +=
    //          vec_structs->at(0).v2*log(vec_structs->at(0).v3)
}
int main(){
        my_struct* single_struct = new my_struct;
        std::vector<my_struct> vec_structs;
        vec_structs.resize(300);
        // Init structs here
        compute(&vec_structs, single_struct);
        return (0);
}


Edit: If I use std::vectors in my struct instead of Eigen objects and I check alignment of the structs in the vector, they are all aligned. But using Eigen objects, every other struct in the vector is aligned, while the others are not. This also occurs if I look at the members in my_struct. With Eigen objects, every other member is aligned in my_struct, but changing them to std::vector, all are aligned. To check alignment, I just do

Code: Select all
   if (((uintptr_t)ptr % 16u) == 0)
   {
      // it is aligned
   }



Any help is appreciated. I am using MSVC++ 2010. I've also tried changing the 'struct member alignment' but I'm still having a high misalign access rate. Thank You.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
on WIN64 we directly call malloc which already returns aligned buffers. However, it might likely be the case that CodeAnalyst replaces the standard malloc by its own and that this later does not return aligned buffers. You can check by editing the file Eigen/src/Core/util/Memory.h to remove the line 62:

|| defined(_WIN64) \
a1re
Registered Member
Posts
16
Karma
0
Actually, I believe that CodeAnalyst does not modify/replace anything. I just compile and run the program using VC++ under Win7 32 bit, and this profiler monitors it.

I still tried removing the line you mention, but it doesn't help.
a1re
Registered Member
Posts
16
Karma
0
Ok, I've compiled the same code in Linux 64 bit using both the GCC and Intel C++ compilers, and my pointers are aligned in the sense of

Code: Select all
   if (((uintptr_t)ptr % 16u) == 0)
   {
      // it is aligned
   }


But as said above, on Windows, when I compile my code in Visual Studio 2010 32 bit, I find that some pointers are not aligned. I've also tried Code::Blocks with the GCC compiler on Windows 32 bit and some pointers are still not aligned.

In summary, on Linux 64 bit, I get aligned pointers, but on Windows 32 bit, I get some pointers not being aligned.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
I don't get it, in your first email you said "It runs without error", so I concluded you see unaligned errors only when running it inside code analyst...

In order to get assertions from Eigen, you must not define NDEBUG at the compilation stage.

On WIN32, we use _mm_malloc(size, 16); to allocated aligned buffers is SSE is enabled, and _aligned_malloc(size, 16); otherwise. I don't see why they would failed in your case.

To be sure, you could edit the aligned_malloc function in Memory.h line 197 to enforce the use of handmade_aligned_malloc, this one is guaranteed to success. If not then the problem is somewhere else....
a1re
Registered Member
Posts
16
Karma
0
Ok, so overall, the code runs without error, whether I was using codeanalyst or not. I don't get any assertions from Eigen. I was just saying that when I used code analyst to monitor the program during runtime, code analyst reported a high misalign access rate afterwards(note that the program itself ran without error).

And also, when I check the pointers to the structs (and their members) to see if they are aligned, I only get that some are aligned. (This part is separate from code analyst, and I just insert the abovementioned code to check various pointers). For example, if in my main method given in the first post, I insert the code

Code: Select all
   if (((uintptr_t)(&single_struct->v2) % 16u) == 0)
   {
      std::cout << "Aligned" << std::endl;
   }
   else std::cout << "Not Aligned" << std::endl;

it outputs "Not Aligned".

Overall, there are no assertion errors. I was just wondering why there was a high misalign access rate from the Eigen method quoted in my first post.

But if you think that it's something that I should not worry about, then I'll leave it as is.

By the way, thank you very much for your help and your suggestions.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
if (((uintptr_t)(&single_struct->v2) % 16u) == 0)


you are checking the alignment of the vector object itself, and for dynamic sized objects we don't care. What is important is that the data are aligned, e.g.:

if (((uintptr_t)(single_struct->v2.data()) % 16u) == 0)
a1re
Registered Member
Posts
16
Karma
0
Ok, I didn't realize that. Thank you for your help.


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], q.ignora, watchstar