Reply to topic

Fast updating a existent sparsematrix?

medic123
Registered Member
Posts
25
Karma
0
OS
Hi,

I use two sparsematrices and two dense vectors that have to be updated frequently during an iterative solving process. The matrices are very sparse, it exists indeed only one Element != 0 per column, wich position stays the same, but the value has to be updated. Currently I use the CoeffRef method to search for the specific Element position, give the References to a method of my own and assign a new value to the element. Three values at once are calculated by a relativ complicated (big therms, exp functions...) mathematical function and then assigned to the references taken as parameter. Looks like this (eg in a for-loop):

Code: Select all
foo1[i]->calculateNewValues(DenseVector1.coeffRef(i), SparseMatrix1.coeffRef(row,col1), SparseMatrix1.coeffRef(row,col2), double1, double2, double3); 
foo2[j]->calculateNewValues(DenseVector2.coeffRef(i), SparseMatrix2.coeffRef(row,col1), SparseMatrix2.coeffRef(row,col2), double4, double5, double6); 


But coeffRef method is usually not a fast way to fill a sparsematrix . Is it ok for filling the dense vectros? . Can you imaging a faster way to do the same?
I see 2 more possibilities:

1) Create a new matrix in every iteration-step and fill them with a triplet list wich has been updated.
2) Somehow iterate over the inner vectors of the sm in order to put new values to the entries.

Some suggestions?

greetings medic
User avatar ggael
Moderator
Posts
2195
Karma
15
OS
For dense objects, using coeffRef is perfectly fine. For sparse matrices, if there is only 1 non zero per inner vector, and that the sparcity pattern does not change, coeffRef is a O(1) operation, so very fast!

Note that you should bench in release mode only, i.e., with optimizations enabled, and with -DNDEBUG.
medic123
Registered Member
Posts
25
Karma
0
OS
Thank you for your expert-knowledge, nice to know that I must not do something fancy and eigen is just fast. :-)

I want to leave this topic open until I tested the whole algorithm within this this week, just in case... I will post if everything worked fine and the thread is solved.

Greeting, medic
medic123
Registered Member
Posts
25
Karma
0
OS
Works fine and is as fast as manually iterating over the inner vectors. Solved...

 
Reply to topic

Bookmarks



Who is online

Registered users: Baidu [Spider], Bing [Bot], Dimitrios, drnn1076, Exabot [Bot], Google [Bot], google01103, Hans, Horus, koriun, Majestic-12 [Bot], pbCyanide, pedrorodriguez, random_fan, scummos, SeaJey, Sogatori, Sogou [Bot], TheraHedwig, vascobasque, whatthefunk, Yahoo [Bot], yurchor, z-uo