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