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

Problems with eigen inside a dll

Tags: None
(comma "," separated)
kgoedde
Registered Member
Posts
6
Karma
0

Problems with eigen inside a dll

Thu Feb 06, 2014 12:27 pm
Hello,

At work I try to update our simulation lib to use eigen. So I switched the Solver for sparse linear systems to the SparseLU solver from eigen. First I used a test programm, it worked perfectly and very fast. then i puut it in our dll (the same code no changes, just that the matrix didnt come from a test file, instead it comes from the programm, which uses the simulation dll). Suddenly I get Access Violation Errors in GeneralBlockPanelKernel.h:

Code: Select all
EIGEN_STRONG_INLINE void unpackRhs(DenseIndex n, const RhsScalar* rhs, RhsScalar* b)
  {
    for(DenseIndex k=0; k<n; k++)
      pstore1<RhsPacket>(&b[k*RhsPacketSize], rhs[k]);
  }


So I looked at the position with the Debugger and everything seems to be in order (at least for the debugging tool from visual studio 2013). So i controlled the input, the matrix and everything else. And if i protocol step by step in short:

Code: Select all
SparseMatrix<double> matrix;
typedef Eigen::Triplet < double > T;
std::vector<T, Eigen::aligned_allocator<T>> triplets;
SparseLU<SparseMatrix<double>> solver;
VectorXd rhs, solution;

this->matrix.resize(this->rows, this->cols);
matrix.reserve(VectorXi::Constant(rows, 12));

this->rhs.resize(rows);
this->solution.resize(cols);

this->matrix.makeCompressed();

this->solver.analyzePattern(matrix);

this->solver.factorize(this->matrix);

this->solver._solve(this->rhs, this->solution);


I get the following info:

- Matrix get initialised successfully
- Matrix get compressed successfully
- analyzePattern and factorize are successfull

and by solve I get the Access Violation. Has somebody an Idea what I do wrong? Or is it a problem, because eigen is inside a dll?

Thanks in Advance,

Kai Gödde
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
First, make sure that both the DLL and your application are compiled with the same flags.

Second, could you show the values of b (the address, not the values pointed by b), k and n? It might be that b is not aligned. It would also be interesting to test with EIGEN_DONT_VECTORIZE predefined for both the DLL and your program.
kgoedde
Registered Member
Posts
6
Karma
0
Thank you for your fast answer.

- If I compile with EIGEN_DONT_VECTORIZE it just works. But how much speed did I loose than?

- If i dont use the compiler flag, than

b = 0x068bb62c
k = 0
n = 2
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: Problems with eigen inside a dll

Fri Feb 07, 2014 12:14 pm
Alright, so the problem is that b is not aligned. Maybe the stack is not aligned and so alloca does not return aligned memory within DLLs. You can verify this guess by adding:

#undef EIGEN_ALLOCA

right after it has been defined in Eigen/src/Core/util/Memory.h
kgoedde
Registered Member
Posts
6
Karma
0

Re: Problems with eigen inside a dll

Fri Feb 07, 2014 12:31 pm
Thanks,

If I use the flag

#undef EIGEN_ALLOCA

it works fine, but it is really slow. How can I fix that?
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
In the Memory.h file, you can modify the lines:
Code: Select all
  #ifdef __arm__
    #define EIGEN_ALIGNED_ALLOCA(SIZE) reinterpret_cast<void*>((reinterpret_cast<size_t>(EIGEN_ALLOCA(SIZE+16)) & ~(size_t(15))) + 16)
  #else
    #define EIGEN_ALIGNED_ALLOCA EIGEN_ALLOCA
  #endif

to
Code: Select all
  #if defined(__arm__) || defined(_MSC_VER)
    #define EIGEN_ALIGNED_ALLOCA(SIZE) reinterpret_cast<void*>((reinterpret_cast<size_t>(EIGEN_ALLOCA(SIZE+16)) & ~(size_t(15))) + 16)
  #else
    #define EIGEN_ALIGNED_ALLOCA EIGEN_ALLOCA
  #endif

If that work, we'll apply the fix upstream.
kgoedde
Registered Member
Posts
6
Karma
0

Re: Problems with eigen inside a dll

Mon Feb 10, 2014 10:17 am
Thank you :) :) :) :)

Now it works fine (but slow, because i have created some redundancies, which i now have to clean up). SO now i just have to show my superior how much faster we can get

Kind regards,

Kai Gödde
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: Problems with eigen inside a dll

Thu Feb 13, 2014 11:06 pm
https://bitbucket.org/eigen/eigen/commits/95bf819b5849/
Changeset: 95bf819b5849
User: ggael
Date: 2014-02-14 00:04:38
Summary: alloca is not necessarily alligned on windows


Bookmarks



Who is online

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