Sign in to follow this  
Carolina

Metaprogramming nightmare

Recommended Posts

Hi there! I've been assigned to code a program for sparse matrix and vector multiplication using metaprogramming, that is, recursive templated structures. It turns out that my code works...only for full matrixes. It has to be very quick since it is for scientific computing. Has anyone any idea on how to do this stuff? Thanks, Carol

Share this post


Link to post
Share on other sites
You really should not post homework/assignments here and expect others to do them for you.

However, if you have specific code-related questions that shows you have been working on it yourself, we may be able to help you.

For instance, if there's a specific part of your code giving you problems, post it, and someone may be able to help you. Don't just ask if someone else can do the entire thing.

Share this post


Link to post
Share on other sites
Hey,I just wanted to know if someone had any idea =)
Here is my code:

template<int dim>
struct SparseDot
{
static double Get(int i,int index,const Array<1,vector<Member>,Brick>& matrix,
const Vector<N,double>& vect)
{
return SparseDot<dim / 2> :: Get(i,index,matrix,vect) +
SparseDot<dim - (dim / 2)> :: Get(i,index + (dim / 2),matrix,vect);
}
};

template<>
struct SparseDot<1>
{
static double Get(int i,int index,const Array<1,vector<Member>,Brick>& matrix,
const Vector<N,double>& vect)
{

return (matrix(i))[index].value * vect((matrix(i))[index].col);

}
};

This one works well if the matrix is full(I am using a library named POOMA in conjunction with STL. The matrix is a POOMA Array of STL vectors, and the vector is a POOMA vector). On another function I iterate over the matrix lines, and then the code shown above does a vector vector multiplication(since each matrix line can be considered a vector).
The problem is, the templated recursion uses a defined dimension(for instance, a #define N 1000) but when the matrix is sparse, obviously this results in Segmentation Fault.
And now I stuck. Anybody help?

Thanks,
Carol

Share this post


Link to post
Share on other sites
I've worked on some sparse matrices for school, and it basically used linked lists for vectors and a linked list of linked lists for a matrix. More information about that should be all over the web and in almost any linear algebra text. As for implementing it using templates, that's more difficult. I assume it'll look something like the normal version.

tj963

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this