d7b08f6dfb5e36460c0093454088d1691e20ac8f
[alexxy/gromacs.git] / src / gromacs / linearalgebra / sparsematrix.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5  * Copyright (c) 2001-2004, The GROMACS development team.
6  * Copyright (c) 2012,2014, by the GROMACS development team, led by
7  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8  * and including many others, as listed in the AUTHORS file in the
9  * top-level source directory and at http://www.gromacs.org.
10  *
11  * GROMACS is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public License
13  * as published by the Free Software Foundation; either version 2.1
14  * of the License, or (at your option) any later version.
15  *
16  * GROMACS is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with GROMACS; if not, see
23  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
25  *
26  * If you want to redistribute modifications to GROMACS, please
27  * consider that scientific software is very special. Version
28  * control is crucial - bugs must be traceable. We will be happy to
29  * consider code for inclusion in the official distribution, but
30  * derived work must not be called official GROMACS. Details are found
31  * in the README & COPYING files - if they are missing, get the
32  * official version at http://www.gromacs.org.
33  *
34  * To help us fund GROMACS development, we humbly ask that you cite
35  * the research papers on the package. Check out http://www.gromacs.org.
36  */
37 #ifndef GMX_LINEARALGEBRA_SPARSEMATRIX_H
38 #define GMX_LINEARALGEBRA_SPARSEMATRIX_H
39
40 #include <stdio.h>
41
42 #include "../utility/basedefinitions.h"
43 #include "../utility/real.h"
44
45 #ifdef __cplusplus
46 extern "C" {
47 #endif
48
49 typedef struct
50     gmx_sparsematrix_entry
51 {
52     int      col;
53     real     value;
54 } gmx_sparsematrix_entry_t;
55
56 /*! \brief Sparse matrix storage format
57  *
58  *  This structure specifies a storage format for a sparse matrix.
59  *  The memory requirements are only proportional to the number
60  *  of nonzero elements, and it provides a reasonably fast way to
61  *  perform matrix-vector multiplications.
62  *
63  *  The data format is very similar to a neighborlist. It is optimized
64  *  for fast access, but it is difficult to add entries. If you are
65  *  constructing a matrix you should either do it in exactly the order
66  *  specified here, or use some other more flexible intermediate structure.
67  *
68  *  The index array is of size nrow+1. All non-zero matrix elements
69  *  on row i are stored in positions index[i] through index[i+1]-1 in
70  *  the arrays column and value. The column array contains the column
71  *  index for each entry, in ascending order, and the corresponding
72  *  position in the value array contains the floating point matrix element.
73  *
74  *  index[nrow] should be equal to the total number of elements stored.
75  *
76  *  Thus, to find the value of matrix element [5,4] you should loop
77  *  over positions index[5] to index[6]-1 in column until you either find
78  *  the value 4, or a higher value (meaning the element was zero).
79  *
80  *  It is fairly easy to construct the matrix on-the-fly if you can do
81  *  it row-by-row.
82  *
83  *  IMPORTANT:
84  *  If compressed_symmetric is set to TRUE, you should only store EITHER the upper OR
85  *  lower triangle (and the diagonal), and the other half is assumed to be
86  *  symmetric. Otherwise, if compressed_symmetric==FALSE, no symmetry is implied and all
87  *  elements should be stored.
88  *
89  *  The symmetry compression saves us a factor 2 both in storage and
90  *  matrix multiplication CPU-time, which can be very useful for huge eigenproblems.
91  *
92  *  If you are unsure, just set compressed_symmetric to FALSE and list all elements. If
93  *  you enable it but still list all elements (both upper and lower triangle) you will be sorry...
94  *
95  *  Internally, the sparse data is stored as a separate list for each row, where the list
96  *  element is a structure with a column and (floating-point) data value. This makes it
97  *  possible, although not completely transparent, to update values in random access order.
98  *  The drawback is that the structure will allocate nrow memory regions.
99  *  The matrix data could be stored in a single contiguous array with indices for each row,
100  *  but then we could only insert elements at the end without copying the entire matrix.
101  *
102  *  After you have
103  *
104  *  In other words: Not perfect, but it works.
105  */
106 typedef struct
107     gmx_sparsematrix
108 {
109     gmx_bool                     compressed_symmetric; /**< Store half elements and assume symmetry. */
110     int                          nrow;                 /**< Number of rows in matrix                 */
111     int *                        ndata;                /**< Number of entries on each row (list)     */
112     int *                        nalloc;               /**< Allocated entry list length for each row */
113     gmx_sparsematrix_entry_t **  data;                 /**< data[i] is a list with entries on row i  */
114 }
115 gmx_sparsematrix_t;
116
117
118 /*! \brief Allocate a new sparse matrix structure
119  *
120  *  The number of rows is used to allocate the index array entry. Obviously you
121  *  can reallocate these later yourself if necessary - this is a
122  *  convenience routine.
123  *
124  *  By default, the compressed_symmetric flag in the structure will
125  *  be FALSE. Set it to TRUE manually if you are only storing either the
126  *  upper or lower half of the matrix.
127  */
128 gmx_sparsematrix_t *
129 gmx_sparsematrix_init            (int                    nrow);
130
131
132 /*! \brief Release all resources used by a sparse matrix structure
133  *
134  *  All arrays in the structure will be freed, and the structure itself.
135  */
136 void
137 gmx_sparsematrix_destroy         (gmx_sparsematrix_t *   A);
138
139
140 /*! \brief Print sparse matrix to a stream.
141  *
142  *  Mainly used for debugging. Be warned that the real sparse matrices used
143  *  in Gromacs runs can be HUGE (think 100,000 rows).
144  */
145 void
146 gmx_sparsematrix_print           (FILE *                 stream,
147                                   gmx_sparsematrix_t *   A);
148
149 /* Adds value at row,col. If the value did not exist
150  * previously it is added, otherwise it is incremented with difference.
151  *
152  * The column sort order might change, so you need to run fix_sparsematrix
153  * once you are done changing the matrix.
154  */
155 real
156 gmx_sparsematrix_value          (gmx_sparsematrix_t *    A,
157                                  int                     row,
158                                  int                     col);
159
160
161 /* Adds value at row,col. If the value did not exist
162  * previously it is added, otherwise it is incremented with difference.
163  *
164  * The column sort order might change, so you need to run fix_sparsematrix
165  * once you are done changing the matrix.
166  */
167 void
168 gmx_sparsematrix_increment_value(gmx_sparsematrix_t *    A,
169                                  int                     row,
170                                  int                     col,
171                                  real                    difference);
172
173
174
175 /*! \brief Sort elements in each column and remove zeros.
176  *
177  *  Sparse matrix access is faster when the elements are stored in
178  *  increasing column order in each row. In some cases previously non-zero
179  *  elements will be zero after adding more data, and this routine also removes
180  *  those entries to reduce the storage requirements.
181  *
182  *  It never hurts to run this routine if you have been updating the matrix...
183  */
184 void
185 gmx_sparsematrix_compress       (gmx_sparsematrix_t *    A);
186
187
188
189 /*! \brief Sparse matrix vector multiplication
190  *
191  * Calculate y = A * x for a sparse matrix A.
192  */
193 void
194 gmx_sparsematrix_vector_multiply(gmx_sparsematrix_t *    A,
195                                  real *                  x,
196                                  real *                  y);
197
198 #ifdef __cplusplus
199 }
200 #endif
201
202
203 #endif