-
Notifications
You must be signed in to change notification settings - Fork 0
/
SVD.cpp
328 lines (252 loc) · 12.4 KB
/
SVD.cpp
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
319
320
321
322
323
324
325
326
327
328
//
// SVD.cpp
// GeometricMultiResolutionAnalysis
//
// Created by Mauro Maggioni on 8/9/12.
// Copyright (c) 2012 Mauro Maggioni. All rights reserved.
//
#include <iostream.h>
#include <mach/mach_time.h>
#include "SVD.h"
#define MULTISCALESVDONSETOFPOINTS_AVOIDMEMORYCLASHES 0
//
// Computes svd of covariance matrix of points
//
// IN:
// Pts : a nPts by nDim matrix of points
//
// OUT:
// return : info as returned by dgesvd_ (in particular, 0 is success)
// S : pointer to at least min(nPts,nDim) doubles to contain the s.v.
// V : pointer to min(nDim,nPts)*nDim matrix of rows of V^T , ronak added:A: all N rows of V^T are returned in array VT.
//
int Cov_SVD(double* Pts, unsigned long nPts, unsigned long nDim, double *S, double *V) {
__CLPK_integer info = 0;
//double *V;
if( MIN(nDim,nPts)==0 )
return info;
double* MeanPt = new double[nDim];
// Compute the mean of the points
unsigned long int k, j;
for (j = 0; j < (unsigned long) nDim; j++) {
MeanPt[j] = 0;
for (k = 0; k < (unsigned long) nPts; k++)
MeanPt[j] += Pts[k * nDim + j];
MeanPt[j] /= nPts;
}
// Center the points and normalize by 1/\sqrt{n}
double sqrtnPts = sqrt((double) nPts);
for (k = 0; k < (unsigned long) nPts; k++)
for (j = 0; j < (unsigned long) nDim; j++)
Pts[k * nDim + j] = (Pts[k * nDim + j] - MeanPt[j]) / sqrtnPts;
// Calculate SVD
__CLPK_integer m = (__CLPK_integer)nDim; // rows
__CLPK_integer n = (__CLPK_integer)nPts; // coloumns
__CLPK_integer lapack_workl = 20*MIN(m,n);
uint64_t clock_start = mach_absolute_time();
__CLPK_doublereal *lapack_work = (__CLPK_doublereal*)malloc(lapack_workl*sizeof(__CLPK_doublereal));
// double MemoryAllocation_t = subtractTimes( mach_absolute_time(), clock_start );
// char lapack_param[1] = {'S'}; //the first min(m,n) rows of V**T (the right singular vectors) are returned in the array VT;
char lapack_param[1] = {'A'}; //the first min(m,n) rows of V**T (the right singular vectors) are returned in the array VT;
char lapack_param1[1] = {'n'};
clock_start = mach_absolute_time();
dgesvd_(lapack_param1, lapack_param, &m, &n, Pts, &m, S, NULL, &m, V, &n, lapack_work, &lapack_workl, &info);
// double dgesvd_t = subtractTimes( mach_absolute_time(), clock_start );
// Handle error conditions
if (info)
printf("Could not compute SVD with error %d\n", info);
else {
/* printf("\n Solution is:\n");
for( unsigned int k = 0; k<NUM_VARIABLES; k++ )
printf("%f,", S[k]);
printf("\n");*/
}
free( lapack_work );
return info;
}
int Cov_SVD_withV(double* Pts, unsigned long nPts, unsigned long nDim,double *S, double *V) {
// double *S;
__CLPK_integer info = 0;
if( MIN(nDim,nPts)==0 )
return info;
double* MeanPt = new double[nDim];
// Compute the mean of the points
unsigned long int k, j;
for (j = 0; j < (unsigned long) nDim; j++) {
MeanPt[j] = 0;
for (k = 0; k < (unsigned long) nPts; k++)
MeanPt[j] += Pts[k * nDim + j];
MeanPt[j] /= nPts;
}
// Center the points and normalize by 1/\sqrt{n}
double sqrtnPts = sqrt((double) nPts);
for (k = 0; k < (unsigned long) nPts; k++)
for (j = 0; j < (unsigned long) nDim; j++)
Pts[k * nDim + j] = (Pts[k * nDim + j] - MeanPt[j]) / sqrtnPts;
// Calculate SVD
__CLPK_integer m = (__CLPK_integer)nDim; // rows
__CLPK_integer n = (__CLPK_integer)nPts; // coloumns
__CLPK_integer lapack_workl = 20*MIN(m,n);
uint64_t clock_start = mach_absolute_time();
__CLPK_doublereal *lapack_work = (__CLPK_doublereal*)malloc(lapack_workl*sizeof(__CLPK_doublereal));
// double MemoryAllocation_t = subtractTimes( mach_absolute_time(), clock_start );
char lapack_param[1] = {'A'}; //the first min(m,n) rows of V**T (the right singular vectors) are returned in the array VT;
char lapack_param1[1] = {'n'};
clock_start = mach_absolute_time();
dgesvd_(lapack_param1, lapack_param, &m, &n, Pts, &m, S, NULL, &m, V, &n, lapack_work, &lapack_workl, &info);
// double dgesvd_t = subtractTimes( mach_absolute_time(), clock_start );
// Handle error conditions
if (info)
printf("Could not compute SVD with error %d\n", info);
else {
/* printf("\n Solution is:\n");
for( unsigned int k = 0; k<NUM_VARIABLES; k++ )
printf("%f,", S[k]);
printf("\n");*/
}
free( lapack_work );
return info;
}
int Cov_SVD_withU(double* Pts, unsigned long nPts, unsigned long nDim, double *S, double *U) {
__CLPK_integer info = 0;
if( MIN(nDim,nPts)==0 )
return info;
// whether centering or not make difference......
/* double* MeanPt = new double[nDim];
// Compute the mean of the points
unsigned long int k, j;
for (j = 0; j < (unsigned long) nDim; j++) {
MeanPt[j] = 0;
for (k = 0; k < (unsigned long) nPts; k++)
MeanPt[j] += Pts[k * nDim + j];
MeanPt[j] /= nPts;
}
// Center the points and normalize by 1/\sqrt{n}
double sqrtnPts = sqrt((double) nPts);
for (k = 0; k < (unsigned long) nPts; k++)
for (j = 0; j < (unsigned long) nDim; j++)
Pts[k * nDim + j] = (Pts[k * nDim + j] - MeanPt[j]) / sqrtnPts;*/
// Calculate SVD
__CLPK_integer m = (__CLPK_integer)nDim; // rows
__CLPK_integer n = (__CLPK_integer)nPts; // coloumns
__CLPK_integer lapack_workl = 20*MIN(m,n);
uint64_t clock_start = mach_absolute_time();
__CLPK_doublereal *lapack_work = (__CLPK_doublereal*)malloc(lapack_workl*sizeof(__CLPK_doublereal));
// double MemoryAllocation_t = subtractTimes( mach_absolute_time(), clock_start );
char lapack_param[1] = {'A'}; //the first min(m,n) rows of V**T (the right singular vectors) are returned in the array VT;
char lapack_param1[1] = {'n'};
clock_start = mach_absolute_time();
dgesvd_(lapack_param, lapack_param1, &m, &n, Pts, &m, S, U, &m, NULL, &n, lapack_work, &lapack_workl, &info);
// double dgesvd_t = subtractTimes( mach_absolute_time(), clock_start );
// Handle error conditions
if (info)
printf("Could not compute SVD with error %d\n", info);
else {
/* printf("\n Solution is:\n");
for( unsigned int k = 0; k<NUM_VARIABLES; k++ )
printf("%f,", S[k]);
printf("\n");*/
}
free( lapack_work );
return info;
}
//
// MultiSVDOnSetOfPoints
//
// IN: params pointint to a structure containing the following fields:
//
// dataPts : ANNpointArray with all the points
// nDims : dimension of the ambient space, number of coordinates of each point
// nnIdxs : array of length nnIdxsLen of center points, with the k-th pointer pointing to a list of nnIdxsWidth[k] nearest neighbors of the k-th point
// maxIdxsWidth: maximum number of nearest neighbors of a point, i.e. maximum of nnIdxsWidth
// nThreads : how many threads to use for this computation
//
// OUT:
// return : as returned by Cov_SVD if single threaded (in particular, 0 is success)
// PointsS : nDims*nnIdxsLen matrix of singular values at the nnIdxsLen points requested
// Call parallel SVD computation at the points of the net
//Ronak ADDED: MultiSVDOnSetOfPoints( ¶ms_s ); is only added in Net.cpp and it is not used for our algorithm
//
int MultiSVDOnSetOfPoints( param_MSVD_single *params )
{
int info = 0;
// double *MeanPt=new double[params->nDims];
if ( params->nThreads==1 ) { // Go serial (only one thread)
unsigned long int k,p;
double *PointsForSVD = new double[params->nDims * params->maxIdxsWidth]; // Memory allocation
double *PointsV = new double[params->nDims * params->nnIdxsLen * params->nDims];
for ( k = 0; k<params->nnIdxsLen; k++) { // Loop through the points and compute SVDs
for ( p = 0; p < params->nnIdxsWidth[k]; p++) // Organize points in local neighborhood into matrix
memcpy(&(PointsForSVD[params->nDims*p]), params->dataPts[params->nnIdxs[k][p]], sizeof(double)*params->nDims);
params->timings->startClock("Cov_SVD");
info = Cov_SVD(PointsForSVD, (params->nnIdxsWidth)[k], params->nDims, params->S + k * params->nDims, PointsV); // Compute SVD of local neighborhood
params->timings->endClock("Cov_SVD");
}
delete [] PointsV;
delete [] PointsForSVD;
} else { // Multi-threaded version
unsigned long k, p, pts_per_thread;
long pts_remaining;
pthread_t threads[params->nThreads];
param_MSVD_single params_mt[params->nThreads];
int exit_value[params->nThreads];
char clockname[20];
pts_per_thread = MAX(1,(unsigned long int)(ceil((double)(params->nnIdxsLen)/(double)(params->nThreads)))); // How many pts should be assigned to each thread
pts_remaining = params->nnIdxsLen;
for (k = 0; k < params->nThreads; k++) { // Create thread parameters and prepare threads
if( (!MULTISCALESVDONSETOFPOINTS_AVOIDMEMORYCLASHES) || (k==0) ) {
params_mt[k].dataPts = params->dataPts;
} else {
params_mt[k].dataPts = new ANNpoint[params->nPts];
for( p=0; p < params->nPts; p++) {
params_mt[k].dataPts[p] = new ANNcoord[params->nDims];
memcpy(params_mt[k].dataPts[p], params->dataPts[p],params->nDims * sizeof(ANNcoord)); //size of datatype of ANNcoord
}
}
params_mt[k].nDims = params->nDims;
params_mt[k].nPts = params->nPts;
params_mt[k].nnIdxsLen = MIN( pts_per_thread, pts_remaining );
params_mt[k].nnIdxs = params->nnIdxs+k*pts_per_thread;
params_mt[k].ChosenPtIdxs = params->ChosenPtIdxs+k*pts_per_thread;
params_mt[k].nnIdxsWidth = params->nnIdxsWidth+k*pts_per_thread;
params_mt[k].maxIdxsWidth = params->maxIdxsWidth;
params_mt[k].nThreads = 1;
params_mt[k].S = params->S+k*pts_per_thread*params->nDims;
params_mt[k].id = k;
sprintf(clockname,"Thread %lu",k);
//params->timings->startClock( clockname );
// pthread_create( threads+k, NULL, MultiSVDOnSetOfPoints, (void *) (params_mt + k) ); // Start the k-th thread
pts_remaining -= params_mt[k].nnIdxsLen;
}
for (k = 0; k < params->nThreads; k++) { // Wait for threads
pthread_join(threads[k], (void**) (&(exit_value[k])));
sprintf(clockname,"Thread %lu",k);
params->timings->endClock( clockname );
if( MULTISCALESVDONSETOFPOINTS_AVOIDMEMORYCLASHES && (k>0) ) { // Clean up
for( p=0; p < params->nPts; p++) {
delete [] params_mt[k].dataPts[p];
}
delete [] params_mt[k].dataPts;
}
}
}
return info;
}
void *MultiSVDOnSetOfPoints_multi(void *param) { // Driver for thread function for svd calculation
MultiSVDOnSetOfPoints( (param_MSVD_single *) param );
return 0;
}
unsigned long int max(double* array, unsigned long int len)
{
unsigned long int lMaxIdx = 0;
double lMaxElement = array[0];
for(register unsigned long int k=1; k<len; k++)
{
if( array[k]>lMaxElement )
{
lMaxIdx = k;
lMaxElement = array[k];
}
}
return lMaxIdx;
}