Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement a huge matrix in C

Tags:

c

memory

matrix

I'm writing a program for a numerical simulation in C. Part of the simulation are spatially fixed nodes that have some float value to each other node. It is like a directed graph. However, if two nodes are too far away, (farther than some cut-off length a) this value is 0.

To represent all these "correlations" or float values, I tried to use a 2D array, but since I have 100.000 and more nodes, that would correspond to 40GB memory or so.

Now, I am trying to think of different solustions for that problem. I don't want to save all these values on the harddisk. I also don't want to calculate them on the fly. One idea was some sort of sparse matrix, like the one one can use in Matlab.

Do you have any other ideas, how to store these values?

I am new to C, so please don't expect too much experience.

Thanks and best regards, Jan Oliver

like image 940
janoliver Avatar asked Feb 09 '11 07:02

janoliver


3 Answers

How many nodes, on average, are within the cutoff distance for a given node determines your memory requirement and tells you whether you need to page to disk. The solution taking the least memory is probably a hash table that maps a pair of nodes to a distance. Since the distance is the same each way, you only need to enter it into the hash table once for the pair -- put the two node numbers in numerical order and then combine them to form a hash key. You could use the Posix hsearch/hcreate/hdestroy functions for the hash table, although they are less than ideal.

like image 54
Jim Balter Avatar answered Nov 01 '22 11:11

Jim Balter


A sparse matrix approach sounds ideal for this. The Wikipedia article on sparse matrices discusses several approaches to implementation.

like image 42
Ted Hopp Avatar answered Nov 01 '22 10:11

Ted Hopp


A sparse adjacency matrix is one idea, or you could use an adjacency list, allowing your to only store the edges which are closer than your cutoff value.

like image 26
Jim Brissom Avatar answered Nov 01 '22 11:11

Jim Brissom