/
randomindex.cc
230 lines (204 loc) · 7.27 KB
/
randomindex.cc
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
/*
* Random indexing of n-dimensional arrays, v 1.0
*
* URL: github.com/fresan/nri (released under The MIT License).
*
* Copyright Fredrik Sandin (2010-2016), fredrik.sandin@gmail.com
*/
#include <iostream>
using namespace std;
#include <cstdlib>
#include "randomindex.h"
MTRand RandomIndex::mtrand_(0x12345); // Constant seed so that experiments can be repeated
RandomIndex::RandomIndex(unsigned dims, unsigned *datarange, unsigned *numrind)
: dims_(dims), datasat_(0), datanumel_(1), distnumel_(1), ritables_(NULL), riunroll_(NULL) {
assert(__MIN(RIIndex) == 0); // Better be unsigned
assert(__MIN(RIData) < 0); // Must be signed
assert(dims > 0);
unsigned i,j;
ritables_ = new RITable* [dims];
riunroll_ = new unsigned [dims];
for(i=0; i<dims; i++) {
ritables_[i] = new RITable(datarange[i], numrind[i]);
datanumel_ *= datarange[i];
distnumel_ *= numrind[i];
riunroll_[i] = 1;
// Lookup table for loop unrolling, row-major ordering
for(j=i+1; j<dims_; j++) {
riunroll_[i] *= numrind[j];
}
}
}
RandomIndex::~RandomIndex() {
if(ritables_ != NULL) {
for(unsigned i=0; i<dims(); i++) {
delete ritables_[i];
}
delete[] ritables_;
delete[] riunroll_;
}
}
unsigned RandomIndex::setrange(unsigned dim, unsigned range) {
assert(dim < dims_);
return ritables_[dim]->rows(range);
}
unsigned RandomIndex::indexsize(void) const {
unsigned i, s=0;
for(i=0; i<dims_; i++) {
s += ritables_[i]->size();
}
return s;
}
void RandomIndex::encode(RIData *data, const unsigned *ind, RIData weight) {
if(weight==0) return; // Nothing to do, return early
unsigned i, *coli = new unsigned[dims_];
for(unsigned j=0; j<distnumel_; j++) { // Unrolled loop, row-major ordering
RIData w = weight;
unsigned temp=j, dataindex=0;
for(i=0; i<dims_; i++) {
coli[i] = temp / riunroll_[i];
temp -= coli[i] * riunroll_[i];
if(coli[i] >= ritables_[i]->numpositive()) w = -w;
coli[i] = ritables_[i]->item(ind[i], coli[i]);
dataindex = coli[i] + ritables_[i]->datarange()*dataindex;
}
long v = data[dataindex] + w;
if(v >= __MIN(RIData) && v <= __MAX(RIData)) {
data[dataindex] = v;
} else { // Saturation logic
datasat_ ++;
if(v < __MIN(RIData)) {
data[dataindex] = __MIN(RIData);
} else {
data[dataindex] = __MAX(RIData);
}
}
}
delete[] coli;
}
double RandomIndex::decode(const RIData *data, const unsigned *ind) const {
double weight = 0;
unsigned i, *coli = new unsigned[dims_];
for(unsigned j=0; j<distnumel_; j++) { // Unrolled loop, row-major ordering
RIData s = 1;
unsigned temp=j, dataindex=0;
for(i=0; i<dims_; i++) {
coli[i] = temp / riunroll_[i];
temp -= coli[i] * riunroll_[i];
if(coli[i] >= ritables_[i]->numpositive()) s = -s;
coli[i] = ritables_[i]->item(ind[i], coli[i]);
dataindex = coli[i] + ritables_[i]->datarange()*dataindex;
}
weight += s*data[dataindex];
}
delete[] coli;
return weight / distnumel_;
}
double RandomIndex::cosa(const RIData *d1, const unsigned *i1,
const RIData *d2, const unsigned *i2) const {
unsigned i, j, dnumel = 1;
unsigned *coli1 = new unsigned[dims_];
unsigned *coli2 = new unsigned[dims_];
unsigned *unroll = new unsigned[dims_];
double weight = 0, norm1 = 0, norm2 = 0;
for(i=0; i<dims_; i++) {
if(i1[i]==Average || i2[i]==Average) {
assert(i1[i] == i2[i]);
dnumel *= ritables_[i]->datarange();
} else {
dnumel *= ritables_[i]->cols();
}
// Unrolling lookup table, row-major order
unroll[i] = 1;
for(j=i+1; j<dims_; j++) {
if(i1[j] == Average) {
unroll[i] *= ritables_[j]->datarange();
} else {
unroll[i] *= ritables_[j]->cols();
}
}
}
for(j=0; j<dnumel; j++) { // Unrolled loop, row-major order
unsigned temp=j, dind1=0, dind2=0;
for(i=0; i<dims_; i++) {
coli1[i] = coli2[i] = temp / unroll[i];
temp -= coli1[i] * unroll[i];
if(i1[i] != Average) { // Get random index
coli1[i] = ritables_[i]->item(i1[i], coli1[i]);
coli2[i] = ritables_[i]->item(i2[i], coli2[i]);
}
dind1 = coli1[i] + ritables_[i]->datarange()*dind1;
dind2 = coli2[i] + ritables_[i]->datarange()*dind2;
}
weight += d1[dind1]*d2[dind2]; // RI signs cancel
norm1 += d1[dind1]*d1[dind1];
norm2 += d2[dind2]*d2[dind2];
}
delete[] coli1;
delete[] coli2;
delete[] unroll;
return weight / sqrt(norm1*norm2);
}
unsigned long RandomIndex::saturation(void) const {
return datasat_;
}
RandomIndex::RITable::RITable(unsigned datarange, unsigned cols)
: datarange_(datarange), cols_(cols), rows_(0), table_(NULL) {
assert(datarange_ <= __MAX(RIIndex)); // Range of RIIndex type
assert(!(cols_ & 0x1)); // Even number of cols
assert(cols_ < datarange_); // Necessary for unique indices
}
RandomIndex::RITable::~RITable() {
if(table_ != NULL) {
free(table_);
table_ = NULL;
}
}
unsigned RandomIndex::RITable::rows(unsigned n) {
unsigned i0=rows_, i, j, k, upper=datarange_-1;
if(n > rows_) { // Add rows to table?
if(table_ != NULL) {
table_ = (RIIndex *)realloc(table_, n*cols_*sizeof(RIIndex));
} else {
table_ = (RIIndex *)malloc(n*cols_*sizeof(RIIndex));
}
assert(table_ != NULL);
rows_ = n;
for(i=i0; i<rows_; i++) { // For each new row
for(j=0; j<cols_; j++) { // ... and for each random index
RIIndex rnd;
while(true) { // Generate unique index in [0,upper]
rnd = RandomIndex::mtrand_.randInt(upper);
for(k=0; k<j; k++) {
if(item(i,k) == rnd) k=cols_;
}
if(k<cols_) break;
} // Linear search, short list
item(i,j) = rnd;
}
}
}
return rows_;
}
/*
Changelog
v1.0 First release on http://github.com/fresan/nri
v0.4.1
- Corrected a bug in RandomIndex::RITable::rows()
v0.4.0
- Rewrite of code.
A RandomIndex class is defined that only specifies the random indexing
tables and methods needed to encode, decode and approximate inner products
given data arrays that are defined by the user. This way it becomes possible
to combine explicit and random indexing without hardcoding of special cases.
This design simplifies the code and makes it more flexible and easy to use.
- File I/O has not been ported/implemented in this version, nor templates
or the Matlab/MEX interface.
v0.3.1
- Updated an assert statement in stateSize() that was incompatible
with one-way indexing.
v0.3.0
- Made randinit() compatible with one-way indexing. This special
case was omitted in the initial implementation.
Early developments not documented.
EOF */