-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathsparse_vector.h
342 lines (264 loc) · 13.8 KB
/
sparse_vector.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
//
// sparse_vector.h
// SLiM
//
// Created by Ben Haller on 4/14/2022.
// Copyright (c) 2022-2025 Philipp Messer. All rights reserved.
// A product of the Messer Lab, http://messerlab.org/slim/
//
// This file is part of SLiM.
//
// SLiM is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
//
// SLiM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along with SLiM. If not, see <http://www.gnu.org/licenses/>.
#ifndef sparse_vector_h
#define sparse_vector_h
#include "slim_globals.h"
#include <vector>
/*
This class provides a sparse vector of distance/strength pairs, for use by the InteractionType code. Each sparse vector entry
contains an interaction distance and strength, kept in separate buffers internally. If a given interaction is not contained
by the sparse vector (because it is beyond the maximum interaction distance), a distance of INFINITY will be returned, with a
strength of 0. A sparse vector contains all of the interaction values *felt* by a given individual (the "receiver"); each column
represents the interactions *exerted* by particular individuals (the "exerters"). This way one can quickly read all of the
interaction strengths felt by a focal receiver individual, which is the typical use case.
*/
// This is the type used to store distances and strengths in SparseVector. It is defined as float, in order both to cut
// down on memory usage, and to maybe increase speed due to vectorization and less bytes going to/from memory. These typedefs
// can be changed to double if float's precision is problematic; everything should just work, although that is not tested.
typedef float sv_value_t;
// This enum designates the type of value being stored by SparseVector. It is used for consistency checking in DEBUG builds.
typedef enum {
kNoData = 0,
kPresences,
kDistances,
kStrengths
} SparseVectorDataType;
class SparseVector
{
// This class has its copy constructor and assignment operator disabled, to prevent accidental copying.
private:
// note that we do not sort by column within the row; we do a linear search for the column
// usually we do not need to identify a particular column, we just want to look at all the values
sv_value_t *values_; // a distance or strength value for each non-empty entry
uint32_t *columns_; // the column indices for the non-empty values in each row
SparseVectorDataType value_type_; // what kind of values we're storing
uint32_t ncols_; // the number of columns; determined at construction time
uint32_t nnz_; // the number of non-zero entries in the sparse vector
uint32_t nnz_capacity_; // the number of non-zero entries allocated for at present
bool finished_; // if true, Finished() has been called and the vector is ready to use
void ResizeToFitMaxNNZ(uint32_t max_nnz);
public:
SparseVector(const SparseVector&) = delete; // no copying
SparseVector& operator=(const SparseVector&) = delete; // no copying
SparseVector(void) = delete; // no null construction
SparseVector(unsigned int p_ncols);
~SparseVector(void);
void Reset(unsigned int p_ncols, SparseVectorDataType data_type); // reset to new dimensions
// Building a sparse vector has to be done in column order, one entry at a time, and then has to be Finished().
// You can supply either distances or strengths; SparseVector does not store both simultaneously. You should
// declare in advance which type of value you intend to store; this is checked when building DEBUG.
void AddEntryPresence(const uint32_t p_column);
void AddEntryDistance(const uint32_t p_column, sv_value_t p_distance);
void AddEntryStrength(const uint32_t p_column, sv_value_t p_strength);
void Finished(void);
inline __attribute__((always_inline)) bool IsFinished() const { return finished_; };
inline __attribute__((always_inline)) uint32_t ColumnCount() const { return ncols_; };
inline __attribute__((always_inline)) SparseVectorDataType DataType(void) const { return value_type_; }
inline __attribute__((always_inline)) void SetDataType(SparseVectorDataType type) { value_type_ = type; }
// Access to the sparse vector's data
void Presences(uint32_t *p_nnz) const;
void Presences(uint32_t *p_nnz, const uint32_t **p_columns) const;
const sv_value_t *Distances(uint32_t *p_nnz) const;
const sv_value_t *Distances(uint32_t *p_nnz, const uint32_t **p_columns) const;
void Distances(uint32_t *p_nnz, uint32_t **p_columns, sv_value_t **p_distances); // non-const
const sv_value_t *Strengths(uint32_t *p_nnz) const;
const sv_value_t *Strengths(uint32_t *p_nnz, const uint32_t **p_columns) const;
void Strengths(uint32_t *p_nnz, uint32_t **p_columns, sv_value_t **p_strengths); // non-const
// Memory usage tallying, for outputUsage()
size_t MemoryUsage(void);
friend std::ostream &operator<<(std::ostream &p_outstream, const SparseVector &p_vector);
};
inline void SparseVector::Reset(unsigned int p_ncols, SparseVectorDataType data_type)
{
#if DEBUG
if (p_ncols == 0)
EIDOS_TERMINATION << "ERROR (SparseVector::Reset): zero-size sparse vector." << EidosTerminate(nullptr);
#endif
ncols_ = p_ncols;
nnz_ = 0;
finished_ = false;
value_type_ = data_type;
ResizeToFitMaxNNZ(ncols_);
}
inline void SparseVector::AddEntryPresence(const uint32_t p_column)
{
#if DEBUG
if (finished_)
EIDOS_TERMINATION << "ERROR (SparseVector::AddEntryPresence): adding entry to sparse vector that is finished." << EidosTerminate(nullptr);
if (p_column >= ncols_)
EIDOS_TERMINATION << "ERROR (SparseVector::AddEntryPresence): adding column beyond the end of the sparse vector." << EidosTerminate(nullptr);
if (value_type_ != SparseVectorDataType::kPresences)
EIDOS_TERMINATION << "ERROR (SparseVector::AddEntryPresence): sparse vector is not specialized for presences." << EidosTerminate(nullptr);
if (nnz_ >= nnz_capacity_)
EIDOS_TERMINATION << "ERROR (SparseVector::AddEntryPresence): insufficient capacity allocated." << EidosTerminate(nullptr);
#endif
// insert the new entry
columns_[nnz_] = p_column;
nnz_++;
}
inline void SparseVector::AddEntryDistance(const uint32_t p_column, sv_value_t p_distance)
{
#if DEBUG
if (finished_)
EIDOS_TERMINATION << "ERROR (SparseVector::AddEntryDistance): adding entry to sparse vector that is finished." << EidosTerminate(nullptr);
if (p_column >= ncols_)
EIDOS_TERMINATION << "ERROR (SparseVector::AddEntryDistance): adding column beyond the end of the sparse vector." << EidosTerminate(nullptr);
if (value_type_ != SparseVectorDataType::kDistances)
EIDOS_TERMINATION << "ERROR (SparseVector::AddEntryDistance): sparse vector is not specialized for distances." << EidosTerminate(nullptr);
if (nnz_ >= nnz_capacity_)
EIDOS_TERMINATION << "ERROR (SparseVector::AddEntryDistance): insufficient capacity allocated." << EidosTerminate(nullptr);
#endif
// insert the new entry
columns_[nnz_] = p_column;
values_[nnz_] = p_distance;
nnz_++;
}
inline void SparseVector::AddEntryStrength(const uint32_t p_column, sv_value_t p_strength)
{
#if DEBUG
if (finished_)
EIDOS_TERMINATION << "ERROR (SparseVector::AddEntryStrength): adding entry to sparse vector that is finished." << EidosTerminate(nullptr);
if (p_column >= ncols_)
EIDOS_TERMINATION << "ERROR (SparseVector::AddEntryStrength): adding column beyond the end of the sparse vector." << EidosTerminate(nullptr);
if (value_type_ != SparseVectorDataType::kStrengths)
EIDOS_TERMINATION << "ERROR (SparseVector::AddEntryStrength): sparse vector is not specialized for strengths." << EidosTerminate(nullptr);
if (nnz_ >= nnz_capacity_)
EIDOS_TERMINATION << "ERROR (SparseVector::AddEntryStrength): insufficient capacity allocated." << EidosTerminate(nullptr);
#endif
// insert the new entry
columns_[nnz_] = p_column;
values_[nnz_] = p_strength;
nnz_++;
}
inline __attribute__((always_inline)) void SparseVector::Finished(void)
{
#if DEBUG
if (finished_)
EIDOS_TERMINATION << "ERROR (SparseVector::Finished): finishing sparse vector that is already finished." << EidosTerminate(nullptr);
#endif
if (value_type_ == SparseVectorDataType::kNoData)
EIDOS_TERMINATION << "ERROR (SparseVector::Finished): sparse vector was never specialized to presences, distances, or strengths." << EidosTerminate(nullptr);
finished_ = true;
}
inline void SparseVector::Presences(uint32_t *p_nnz) const
{
#if DEBUG
// should be done building the vector
if (!finished_)
EIDOS_TERMINATION << "ERROR (SparseVector::Presences): sparse vector is not finished being built." << EidosTerminate(nullptr);
if (value_type_ != SparseVectorDataType::kPresences)
EIDOS_TERMINATION << "ERROR (SparseVector::Presences): sparse vector is not specialized for presences." << EidosTerminate(nullptr);
#endif
*p_nnz = (uint32_t)nnz_; // cast should be safe, the number of entries is 32-bit
}
inline void SparseVector::Presences(uint32_t *p_nnz, const uint32_t **p_columns) const
{
#if DEBUG
// should be done building the vector
if (!finished_)
EIDOS_TERMINATION << "ERROR (SparseVector::Presences): sparse vector is not finished being built." << EidosTerminate(nullptr);
if (value_type_ != SparseVectorDataType::kPresences)
EIDOS_TERMINATION << "ERROR (SparseVector::Presences): sparse vector is not specialized for presences." << EidosTerminate(nullptr);
#endif
*p_nnz = (uint32_t)nnz_; // cast should be safe, the number of entries is 32-bit
*p_columns = columns_;
}
inline const sv_value_t *SparseVector::Distances(uint32_t *p_nnz) const
{
#if DEBUG
// should be done building the vector
if (!finished_)
EIDOS_TERMINATION << "ERROR (SparseVector::Distances): sparse vector is not finished being built." << EidosTerminate(nullptr);
if (value_type_ != SparseVectorDataType::kDistances)
EIDOS_TERMINATION << "ERROR (SparseVector::Distances): sparse vector is not specialized for distances." << EidosTerminate(nullptr);
#endif
// return info; note that a non-null pointer is returned even if count==0
*p_nnz = (uint32_t)nnz_; // cast should be safe, the number of entries is 32-bit
return values_;
}
inline const sv_value_t *SparseVector::Distances(uint32_t *p_nnz, const uint32_t **p_columns) const
{
#if DEBUG
// should be done building the vector
if (!finished_)
EIDOS_TERMINATION << "ERROR (SparseVector::Distances): sparse vector is not finished being built." << EidosTerminate(nullptr);
if (value_type_ != SparseVectorDataType::kDistances)
EIDOS_TERMINATION << "ERROR (SparseVector::Distances): sparse vector is not specialized for distances." << EidosTerminate(nullptr);
#endif
// return info; note that a non-null pointer is returned even if count==0
*p_nnz = (uint32_t)nnz_; // cast should be safe, the number of entries is 32-bit
*p_columns = columns_;
return values_;
}
inline void SparseVector::Distances(uint32_t *p_nnz, uint32_t **p_columns, sv_value_t **p_distances)
{
#if DEBUG
// should be done building the vector
if (!finished_)
EIDOS_TERMINATION << "ERROR (SparseVector::Distances): sparse vector is not finished being built." << EidosTerminate(nullptr);
if (value_type_ != SparseVectorDataType::kDistances)
EIDOS_TERMINATION << "ERROR (SparseVector::Distances): sparse vector is not specialized for distances." << EidosTerminate(nullptr);
#endif
// return info; note that a non-null pointer is returned even if count==0
*p_nnz = (uint32_t)nnz_; // cast should be safe, the number of entries is 32-bit
*p_columns = columns_;
*p_distances = values_;
}
inline const sv_value_t *SparseVector::Strengths(uint32_t *p_nnz) const
{
#if DEBUG
// should be done building the vector
if (!finished_)
EIDOS_TERMINATION << "ERROR (SparseVector::Strengths): sparse vector is not finished being built." << EidosTerminate(nullptr);
if (value_type_ != SparseVectorDataType::kStrengths)
EIDOS_TERMINATION << "ERROR (SparseVector::Strengths): sparse vector is not specialized for strengths." << EidosTerminate(nullptr);
#endif
// return info; note that a non-null pointer is returned even if count==0
*p_nnz = (uint32_t)nnz_; // cast should be safe, the number of entries is 32-bit
return values_;
}
inline const sv_value_t *SparseVector::Strengths(uint32_t *p_nnz, const uint32_t **p_columns) const
{
#if DEBUG
// should be done building the vector
if (!finished_)
EIDOS_TERMINATION << "ERROR (SparseVector::Strengths): sparse vector is not finished being built." << EidosTerminate(nullptr);
if (value_type_ != SparseVectorDataType::kStrengths)
EIDOS_TERMINATION << "ERROR (SparseVector::Strengths): sparse vector is not specialized for strengths." << EidosTerminate(nullptr);
#endif
// return info; note that a non-null pointer is returned even if count==0
*p_nnz = (uint32_t)nnz_; // cast should be safe, the number of entries is 32-bit
*p_columns = columns_;
return values_;
}
inline void SparseVector::Strengths(uint32_t *p_nnz, uint32_t **p_columns, sv_value_t **p_strengths)
{
#if DEBUG
// should be done building the vector
if (!finished_)
EIDOS_TERMINATION << "ERROR (SparseVector::Strengths): sparse vector is not finished being built." << EidosTerminate(nullptr);
if (value_type_ != SparseVectorDataType::kStrengths)
EIDOS_TERMINATION << "ERROR (SparseVector::Strengths): sparse vector is not specialized for strengths." << EidosTerminate(nullptr);
#endif
// return info; note that a non-null pointer is returned even if count==0
*p_nnz = (uint32_t)nnz_; // cast should be safe, the number of entries is 32-bit
*p_columns = columns_;
*p_strengths = values_;
}
std::ostream &operator<<(std::ostream &p_outstream, const SparseVector &p_vector);
#endif /* sparse_vector_h */