Debian Codegraph @

src/thirdparty/PoissonRecon/SparseMatrixInterface.inl

raw
1 /*
2 Copyright (c) 2006, Michael Kazhdan and Matthew Bolitho
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
7
8 Redistributions of source code must retain the above copyright notice, this list of
9 conditions and the following disclaimer. Redistributions in binary form must reproduce
10 the above copyright notice, this list of conditions and the following disclaimer
11 in the documentation and/or other materials provided with the distribution.
12
13 Neither the name of the Johns Hopkins University nor the names of its contributors
14 may be used to endorse or promote products derived from this software without specific
15 prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
18 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO THE IMPLIED WARRANTIES
19 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
20 SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
22 TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
26 DAMAGE.
27 */
28
29 template< class T , class const_iterator > size_t SparseMatrixInterface< T , const_iterator >::entries( void ) const
30 {
31 size_t entries = 0;
32 for( size_t i=0 ; i<rows() ; i++ ) entries += rowSize( i );
33 return entries;
34 }
35 template< class T , class const_iterator > double SparseMatrixInterface< T , const_iterator >::squareNorm( void ) const
36 {
37 double n=0;
38 for( size_t i=0 ; i<rows() ; i++ )
39 {
40 const_iterator e = end( i );
41 for( const_iterator iter = begin( i ) ; iter!=e ; iter++ ) n += iter->Value * iter->Value;
42 }
43 return n;
44
45 }
46 template< class T , class const_iterator > double SparseMatrixInterface< T , const_iterator >::squareASymmetricNorm( void ) const
47 {
48 double n=0;
49 for( size_t i=0 ; i<rows() ; i++ )
50 {
51 const_iterator e = end( i );
52 for( const_iterator iter1 = begin( i ) ; iter1!=e ; iter1++ )
53 {
54 size_t j = iter1->N;
55 const_iterator e = end( j );
56 double value = 0;
57 for( const_iterator iter2 = begin( j ) ; iter2!=e ; iter2++ )
58 {
59 size_t k = iter2->N;
60 if( k==i ) value += iter2->Value;
61 }
62 n += (iter1->Value-value) * (iter1->Value-value);
63 }
64 }
65 return n;
66 }
67 template< class T , class const_iterator > double SparseMatrixInterface< T , const_iterator >::squareASymmetricNorm( size_t &idx1 , size_t &idx2 ) const
68 {
69 double n=0;
70 double max=0;
71 for( size_t i=0 ; i<rows() ; i++ )
72 {
73 const_iterator e = end( i );
74 for( const_iterator iter = begin( i ) ; iter!=e ; iter++ )
75 {
76 size_t j = iter->N;
77 const_iterator e = end( j );
78 double value = 0;
79 for( const_iterator iter2 = begin( j ) ; iter2!=e ; iter2++ )
80 {
81 size_t k = iter2->N;
82 if( k==i ) value += iter2->Value;
83 }
84 double temp = (iter->Value-value) * (iter->Value-value);
85 n += temp;
86 if( temp>=max ) idx1 = i , idx2 = j , max=temp;
87 }
88 }
89 return n;
90 }
91 template< class T , class const_iterator >
92 template< class T2 >
93 void SparseMatrixInterface< T , const_iterator >::multiply( ConstPointer( T2 ) In , Pointer( T2 ) Out , char multiplyFlag ) const
94 {
95 ConstPointer( T2 ) in = In;
96 ThreadPool::ParallelFor( 0 , rows() , [&]( unsigned int , size_t i )
97 {
98 T2 temp;
99 memset( &temp , 0 , sizeof(T2) );
100 ConstPointer( T2 ) _in = in;
101 const_iterator e = end( i );
102 for( const_iterator iter = begin( i ) ; iter!=e ; iter++ ) temp += (T2)( _in[ iter->N ] * iter->Value );
103 if( multiplyFlag & MULTIPLY_NEGATE ) temp = -temp;
104 if( multiplyFlag & MULTIPLY_ADD ) Out[i] += temp;
105 else Out[i] = temp;
106 }
107 );
108 }
109 template< class T , class const_iterator >
110 template< class T2 >
111 void SparseMatrixInterface< T , const_iterator >::multiplyScaled( T scale , ConstPointer( T2 ) In , Pointer( T2 ) Out , char multiplyFlag ) const
112 {
113 ConstPointer( T2 ) in = In;
114 ThreadPool::ParallelFor( 0 , rows() , [&]( unsigned int , size_t i )
115 {
116 T2 temp;
117 memset( &temp , 0 , sizeof(T2) );
118 ConstPointer( T2 ) _in = in;
119 const_iterator e = end( i );
120 for( const_iterator iter = begin( i ) ; iter!=e ; iter++ ) temp += _in[ iter->N ] * iter->Value;
121 temp *= scale;
122 if( multiplyFlag & MULTIPLY_NEGATE ) temp = -temp;
123 if( multiplyFlag & MULTIPLY_ADD ) Out[i] += temp;
124 else Out[i] = temp;
125 }
126 );
127 }
128
129 template< class T , class const_iterator >
130 void SparseMatrixInterface< T , const_iterator >::setDiagonal( Pointer( T ) diagonal ) const
131 {
132 ThreadPool::ParallelFor( 0 , rows() , [&]( unsigned int , size_t i )
133 {
134 diagonal[i] = (T)0;
135 const_iterator e = end( i );
136 for( const_iterator iter = begin( i ) ; iter!=e ; iter++ ) if( iter->N==i ) diagonal[i] += iter->Value;
137 }
138 );
139 }
140
141 template< class T , class const_iterator >
142 void SparseMatrixInterface< T , const_iterator >::setDiagonalR( Pointer( T ) diagonal ) const
143 {
144 ThreadPool::ParallelFor( 0 , rows() , [&]( unsigned int , size_t i )
145 {
146 diagonal[i] = (T)0;
147 const_iterator e = end( i );
148 for( const_iterator iter = begin( i ) ; iter!=e ; iter++ ) if( iter->N==i ) diagonal[i] += iter->Value;
149 if( diagonal[i] ) diagonal[i] = (T)( 1./diagonal[i] );
150 }
151 );
152 }
153
154 template< class T , class const_iterator >
155 template< class T2 >
156 void SparseMatrixInterface< T , const_iterator >::jacobiIteration( ConstPointer( T ) diagonal , ConstPointer( T2 ) b , ConstPointer( T2 ) in , Pointer( T2 ) out , bool dReciprocal ) const
157 {
158 multiply( in , out );
159 if( dReciprocal )
160 ThreadPool::ParallelFor( 0 , rows() , [&]( unsigned int , size_t i ){ out[i] = in[i] + ( b[i] - out[i] ) * diagonal[i]; } );
161 else
162 ThreadPool::ParallelFor( 0 , rows() , [&]( unsigned int , size_t i ){ out[i] = in[i] + ( b[i] - out[i] ) / diagonal[i]; } );
163 }
164 template< class T , class const_iterator >
165 template< class T2 >
166 void SparseMatrixInterface< T , const_iterator >::gsIteration( ConstPointer( T ) diagonal , ConstPointer( T2 ) b , Pointer( T2 ) x , bool forward , bool dReciprocal ) const
167 {
168 if( dReciprocal )
169 {
170 auto Iterate = [&]( long long j )
171 {
172 T2 _b = b[j];
173 const_iterator e = end( j );
174 for( const_iterator iter = begin( j ) ; iter!=e ; iter++ ) _b -= x[iter->N] * iter->Value;
175 x[j] += _b * diagonal[j];
176 };
177 if( forward ) for( long long j=0 ; j<(long long)rows() ; j++ ){ Iterate( j ); }
178 else for( long long j=(long long)rows()-1 ; j>=0 ; j-- ){ Iterate( j ); }
179 }
180 else
181 {
182 auto Iterate = [&]( long long j )
183 {
184 T2 _b = b[j];
185 const_iterator e = end( j );
186 for( const_iterator iter = begin( j ) ; iter!=e ; iter++ ) _b -= x[iter->N] * iter->Value;
187 x[j] += _b / diagonal[j];
188 };
189 if( forward ) for( long long j=0 ; j<(long long)rows() ; j++ ){ Iterate( j ); }
190 else for( long long j=(long long)rows()-1 ; j>=0 ; j-- ){ Iterate( j ); }
191 }
192 }
193 template< class T , class const_iterator >
194 template< class T2 >
195 void SparseMatrixInterface< T , const_iterator >::gsIteration( const std::vector< size_t >& multiColorIndices , ConstPointer( T ) diagonal , ConstPointer( T2 ) b , Pointer( T2 ) x , bool dReciprocal ) const
196 {
197 if( dReciprocal )
198 ThreadPool::ParallelFor( 0 , multiColorIndices.size() , [&]( unsigned int , size_t j )
199 {
200 size_t jj = multiColorIndices[j];
201 T2 _b = b[jj];
202 const_iterator e = end( jj );
203 for( const_iterator iter = begin( jj ) ; iter!=e ; iter++ ) _b -= x[iter->N] * iter->Value;
204 x[jj] += _b * diagonal[jj];
205 }
206 );
207 else
208 ThreadPool::ParallelFor( 0 , multiColorIndices.size() , [&]( unsigned int , size_t j )
209 {
210 size_t jj = multiColorIndices[j];
211 T2 _b = b[jj];
212 const_iterator e = end( jj );
213 for( const_iterator iter = begin( jj ) ; iter!=e ; iter++ ) _b -= x[iter->N] * iter->Value;
214 x[jj] += _b / diagonal[jj];
215 }
216 );
217 }
218
219 template< class T , class const_iterator >
220 template< class T2 >
221 void SparseMatrixInterface< T , const_iterator >::gsIteration( const std::vector< std::vector< size_t > >& multiColorIndices , ConstPointer( T ) diagonal , ConstPointer( T2 ) b , Pointer( T2 ) x , bool forward , bool dReciprocal ) const
222 {
223 if( dReciprocal )
224 {
225 auto Iterate = [&]( const std::vector< size_t > &indices )
226 {
227 ThreadPool::ParallelFor( 0 , indices.size() , [&]( unsigned int , size_t k )
228 {
229 size_t jj = indices[k];
230 T2 _b = b[jj];
231 const_iterator e = end( jj );
232 for( const_iterator iter = begin( jj ) ; iter!=e ; iter++ ) _b -= x[iter->N] * iter->Value;
233 x[jj] += _b * diagonal[jj];
234 }
235 );
236 };
237 if( forward ) for( size_t j=0 ; j<multiColorIndices.size() ; j++ ){ Iterate( multiColorIndices[j] ); }
238 else for( long long j=(long long)multiColorIndices.size()-1 ; j>=0 ; j-- ){ Iterate( multiColorIndices[j] ); }
239 }
240 else
241 {
242 auto Iterate = [&]( const std::vector< size_t > &indices )
243 {
244 ThreadPool::ParallelFor( 0 , indices.size() , [&]( unsigned int , size_t k )
245 {
246 size_t jj = indices[k];
247 T2 _b = b[jj];
248 const_iterator e = end( jj );
249 for( const_iterator iter = begin( jj ) ; iter!=e ; iter++ ) _b -= x[iter->N] * iter->Value;
250 x[jj] += _b / diagonal[jj];
251 }
252 );
253 };
254 if( forward ) for( size_t j=0 ; j<multiColorIndices.size() ; j++ ){ Iterate( multiColorIndices[j] ); }
255 else for( long long j=(long long)multiColorIndices.size()-1 ; j>=0 ; j-- ){ Iterate( multiColorIndices[j] ); }
256 }
257 }
258 template< class SPDFunctor , class T , typename Real , class TDotTFunctor > size_t SolveCG( const SPDFunctor& M , size_t dim , ConstPointer( T ) b , size_t iters , Pointer( T ) x , double eps , TDotTFunctor Dot )
259 {
260 std::vector< Real > scratch( ThreadPool::NumThreads() , 0 );
261 eps *= eps;
262 Pointer( T ) r = AllocPointer< T >( dim );
263 Pointer( T ) d = AllocPointer< T >( dim );
264 Pointer( T ) q = AllocPointer< T >( dim );
265
266 Real delta_new = 0 , delta_0;
267 M( ( ConstPointer( T ) )x , r );
268 ThreadPool::ParallelFor( 0 , dim , [&]( unsigned int thread , size_t i ){ d[i] = r[i] = b[i] - r[i] , scratch[thread] += Dot( r[i] , r[i] ); } );
269 for( unsigned int t=0 ; t<ThreadPool::NumThreads() ; t++ ){ delta_new += scratch[t] ; scratch[t] = 0; }
270
271 delta_0 = delta_new;
272 if( delta_new<=eps )
273 {
274 FreePointer( r );
275 FreePointer( d );
276 FreePointer( q );
277 return 0;
278 }
279 size_t ii;
280 for( ii=0 ; ii<iters && delta_new>eps*delta_0 ; ii++ )
281 {
282 M( ( ConstPointer( T ) )d , q );
283 Real dDotQ = 0;
284 ThreadPool::ParallelFor( 0 , dim , [&]( unsigned int thread , size_t i ){ scratch[thread] += Dot( d[i] , q[i] ); } );
285 for( unsigned int t=0 ; t<ThreadPool::NumThreads() ; t++ ){ dDotQ += scratch[t] ; scratch[t] = 0; }
286 if( !dDotQ ) break;
287
288 Real alpha = delta_new / dDotQ;
289 Real delta_old = delta_new;
290 delta_new = 0;
291 if( (ii%50)==(50-1) )
292 {
293 ThreadPool::ParallelFor( 0 , dim , [&]( unsigned int , size_t i ){ x[i] += (T)( d[i] * alpha ); } );
294 M( ( ConstPointer( T ) )x , r );
295 ThreadPool::ParallelFor( 0 , dim , [&]( unsigned int thread , size_t i ){ r[i] = b[i] - r[i] , scratch[thread] += Dot( r[i] , r[i] ) , x[i] += (T)( d[i] * alpha ); } );
296 for( unsigned int t=0 ; t<ThreadPool::NumThreads() ; t++ ){ delta_new += scratch[t] ; scratch[t] = 0; }
297 }
298 else
299 {
300 ThreadPool::ParallelFor( 0 , dim , [&]( unsigned int thread , size_t i ){ r[i] -=(T)( q[i] * alpha ) , scratch[thread] += Dot( r[i] , r[i] ) , x[i] += (T)( d[i] * alpha ); } );
301 for( unsigned int t=0 ; t<ThreadPool::NumThreads() ; t++ ){ delta_new += scratch[t] ; scratch[t] = 0; }
302 }
303
304 Real beta = delta_new / delta_old;
305 ThreadPool::ParallelFor( 0 , dim , [&]( unsigned int , size_t i ){ d[i] = r[i] + (T)( d[i] * beta ); } );
306 }
307 FreePointer( r );
308 FreePointer( d );
309 FreePointer( q );
310 return ii;
311 }
312 template< class SPDFunctor , class Preconditioner , class T , typename Real , class TDotTFunctor > size_t SolveCG( const SPDFunctor& M , const Preconditioner& P , size_t dim , ConstPointer( T ) b , size_t iters , Pointer( T ) x , double eps , TDotTFunctor Dot )
313 {
314 std::vector< Real > scratch( ThreadPool::NumThreads() , 0 );
315 eps *= eps;
316 Pointer( T ) r = AllocPointer< T >( dim );
317 Pointer( T ) d = AllocPointer< T >( dim );
318 Pointer( T ) q = AllocPointer< T >( dim );
319 Pointer( T ) Pb = AllocPointer< T >( dim );
320 Pointer( T ) temp = AllocPointer< T >( dim );
321
322 auto PM = [&] ( ConstPointer(T) x , Pointer(T) y )
323 {
324 M( x , temp );
325 P( ( ConstPointer(T) )temp , y );
326 };
327
328 Real delta_new = 0 , delta_0;
329 P( b , Pb );
330 PM( ( ConstPointer( T ) )x , r );
331 ThreadPool::ParallelFor( 0 , dim , [&]( unsigned int thread , size_t i ){ d[i] = r[i] = Pb[i] - r[i] , scratch[thread] += Dot( r[i] , r[i] ); } );
332 for( unsigned int t=0 ; t<ThreadPool::NumThreads() ; t++ ){ delta_new += scratch[t] ; scratch[t] = 0; }
333
334 delta_0 = delta_new;
335 if( delta_new<=eps )
336 {
337 FreePointer( Pb );
338 FreePointer( r );
339 FreePointer( d );
340 FreePointer( q );
341 FreePointer( temp );
342 return 0;
343 }
344 size_t ii;
345 for( ii=0 ; ii<iters && delta_new>eps*delta_0 ; ii++ )
346 {
347 PM( ( ConstPointer( T ) )d , q );
348 Real dDotQ = 0;
349 ThreadPool::ParallelFor( 0 , dim , [&]( unsigned int thread , size_t i ){ scratch[thread] += Dot( d[i] , q[i] ); } );
350 for( unsigned int t=0 ; t<ThreadPool::NumThreads() ; t++ ){ dDotQ += scratch[t] ; scratch[t] = 0; }
351 if( !dDotQ ) break;
352
353 Real alpha = delta_new / dDotQ;
354 Real delta_old = delta_new;
355 delta_new = 0;
356 if( (ii%50)==(50-1) )
357 {
358 ThreadPool::ParallelFor( 0 , dim , [&]( unsigned int , size_t i ){ x[i] += (T)( d[i] * alpha ); } );
359 PM( ( ConstPointer( T ) )x , r );
360 ThreadPool::ParallelFor( 0 , dim , [&]( unsigned int thread , size_t i ){ r[i] = Pb[i] - r[i] , scratch[thread] += Dot( r[i] , r[i] ) , x[i] += (T)( d[i] * alpha ); } );
361 for( unsigned int t=0 ; t<ThreadPool::NumThreads() ; t++ ) delta_new += scratch[t];
362 }
363 else
364 {
365 ThreadPool::ParallelFor( 0 , dim , [&]( unsigned int thread , size_t i ){ r[i] -=(T)( q[i] * alpha ) , scratch[thread] += Dot( r[i] , r[i] ) , x[i] += (T)( d[i] * alpha ); } );
366 for( unsigned int t=0 ; t<ThreadPool::NumThreads() ; t++ ) delta_new += scratch[t];
367 }
368
369 Real beta = delta_new / delta_old;
370 ThreadPool::ParallelFor( 0 , dim , [&]( unsigned int , size_t i ){ d[i] = r[i] + (T)( d[i] * beta ); } );
371 }
372 FreePointer( Pb );
373 FreePointer( r );
374 FreePointer( d );
375 FreePointer( q );
376 FreePointer( temp );
377 return ii;
378 }