Debian Codegraph @

src/thirdparty/PoissonRecon/PointPartitionClientServer.inl

raw
1 /*
2 Copyright (c) 2023, Michael Kazhdan
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< typename Real , unsigned int Dim >
30 size_t _SampleCount( std::string in , std::vector< PlyProperty > &auxProperties )
31 {
32 char *ext = GetFileExtension( in.c_str() );
33 if( strcasecmp( ext , "ply" ) ) MK_THROW( "Only .ply files supported: " , in );
34 delete[] ext;
35
36 size_t vNum;
37 typedef VertexFactory::Factory< Real , typename VertexFactory::PositionFactory< Real , Dim > , typename VertexFactory::NormalFactory< Real , Dim > > Factory;
38 Factory factory;
39 bool *readFlags = new bool[ factory.plyReadNum() ];
40 int fileType = PLY::ReadVertexHeader( in , factory , readFlags , auxProperties , vNum );
41 if( fileType==PLY_ASCII ) MK_THROW( "Point set must be in binary format" );
42 if( !factory.template plyValidReadProperties<0>( readFlags ) ) MK_THROW( "Ply file does not contain positions" );
43 if( !factory.template plyValidReadProperties<1>( readFlags ) ) MK_THROW( "Ply file does not contain normals" );
44 delete[] readFlags;
45 return vNum;
46 }
47
48 template< typename Factory , typename VertexFunctor /* = std::function< void ( Factory::VertexType ) > */ >
49 void _ProcessPLY( std::string in , std::pair< size_t , size_t > range , const Factory &factory , VertexFunctor vf )
50 {
51 using Vertex = typename Factory::VertexType;
52 std::vector< std::string > elist = { std::string( "vertex" ) };
53
54 float version;
55 int file_type;
56
57 PlyFile *ply = PlyFile::Read( in , elist , file_type , version );
58 if( !ply ) MK_THROW( "Could not create ply file for reading: " , in );
59 if( file_type==PLY_ASCII ) MK_THROW( "Only binary file type supported" );
60
61 size_t vCount;
62 std::vector< PlyProperty > plist = ply->get_element_description( std::string( "vertex" ) , vCount );
63 if( !plist.size() ) MK_THROW( "Could not read element properties: vertex" );
64 if( range.second==-1 ) range.second = vCount;
65 if( range.first>=range.second ) MK_THROW( "Bad Range: [ " , range.first , " , " , range.second , " )" );
66 if( range.second>vCount )
67 {
68 MK_WARN( "Max range too large, resetting" );
69 range.second = vCount;
70 }
71
72 size_t leftToReadCount = range.second - range.first;
73 size_t vSize = 0;
74 if constexpr( Factory::IsStaticallyAllocated() ) vSize = sizeof( Vertex );
75 else vSize = factory.bufferSize();
76
77 for( unsigned int i=0 ; i<factory.plyReadNum() ; i++)
78 {
79 PlyProperty prop;
80 if constexpr( Factory::IsStaticallyAllocated() ) prop = factory.plyStaticReadProperty(i);
81 else prop = factory.plyReadProperty(i);
82 ply->get_property( std::string( "vertex" ) , &prop );
83 }
84
85 size_t sizeOnDisk = 0;
86 for( unsigned int i=0 ; i<plist.size() ; i++ ) sizeOnDisk += ply_type_size[ plist[i].external_type ];
87
88 #if defined( _WIN32 ) || defined( _WIN64 )
89 _fseeki64( ply->fp , sizeOnDisk * range.first , SEEK_CUR );
90 #else // !_WIN32 && !_WIN64
91 fseek( ply->fp , sizeOnDisk * range.first , SEEK_CUR );
92 #endif // _WIN32 || _WIN64
93
94 Vertex vertex = factory();
95 Pointer( char ) buffer = NewPointer< char >( factory.bufferSize() );
96 for( size_t i=range.first ; i<range.second ; i++ )
97 {
98 if constexpr( Factory::IsStaticallyAllocated() ) ply->get_element( (void *)&vertex );
99 else
100 {
101 ply->get_element( PointerAddress( buffer ) );
102 factory.fromBuffer( buffer , vertex );
103 }
104 vf( vertex );
105 }
106 DeletePointer( buffer );
107
108 delete ply;
109 }
110
111 template< typename Real , unsigned int Dim , typename Factory >
112 void _MergeSlabs( std::string inDir , std::string outDir , std::string header , unsigned int clientCount , std::pair< unsigned int , unsigned int > slabRange , unsigned int slabs , unsigned int filesPerDir , const Factory &factory , size_t bufferSize )
113 {
114 using Vertex = typename Factory::VertexType;
115
116 Vertex v = factory();
117
118 for( unsigned int s=slabRange.first ; s<slabRange.second ; s++ )
119 {
120 std::string outFileName = PointPartition::FileName( outDir , header , s , slabs , filesPerDir );
121 PointPartition::BufferedBinaryOutputDataStream< Factory > outStream( outFileName.c_str() , factory , bufferSize );
122 for( unsigned int c=0 ; c<clientCount ; c++ )
123 {
124 std::string inFileName = PointPartition::FileName( inDir , header , c , s , slabs , filesPerDir );
125 PointPartition::BufferedBinaryInputDataStream< Factory > inStream( inFileName.c_str() , factory , bufferSize );
126 while( inStream.read( v ) ) outStream.write( v );
127 }
128 }
129 }
130
131 template< typename Real , unsigned int Dim , typename Factory >
132 std::vector< size_t > _PartitionIntoSlabs( std::string in , std::string dir , std::string header , unsigned int clientIndex , std::pair< size_t , size_t > range , unsigned int slabs , unsigned int filesPerDir , XForm< Real , Dim+1 > xForm , const Factory &factory , size_t bufferSize )
133 {
134 using Vertex = typename Factory::VertexType;
135 using _XForm = typename Factory::Transform;
136
137 _XForm _xForm(xForm);
138 std::vector< size_t > slabSizes( slabs , 0 );
139
140 using OutputPointStream = OutputDataStream< Vertex >;
141
142 std::vector< OutputPointStream * > outStreams( slabs );
143 for( unsigned int s=0 ; s<slabs ; s++ )
144 {
145 std::string fileName = PointPartition::FileName( dir , header , clientIndex , s , slabs , filesPerDir );
146 outStreams[s] = new PointPartition::BufferedBinaryOutputDataStream< Factory >( fileName.c_str() , factory , bufferSize );
147 }
148
149 size_t outOfRangeCount = 0;
150 auto vertexFunctor = [&]( Vertex v )
151 {
152 _xForm.inPlace( v );
153 Point< Real , Dim > p = v.template get<0>();
154 int slab = (int)floor( p[Dim-1] * slabs );
155 if( slab>=0 && slab<(int)slabs )
156 {
157 outStreams[slab]->write( v );
158 slabSizes[slab]++;
159 }
160 else outOfRangeCount++;
161 };
162 _ProcessPLY( in , range , factory , vertexFunctor );
163 for( unsigned int i=0 ; i<slabs ; i++ ) delete outStreams[i];
164 if( outOfRangeCount ) MK_WARN( "Out of range count: " , outOfRangeCount );
165 return slabSizes;
166 }
167
168 template< typename Real , unsigned int Dim , typename Factory , bool ExtendedAxes=true >
169 PointExtent::Extent< Real , Dim , ExtendedAxes > _GetExtent( std::string in , std::pair< size_t , size_t > range , const Factory &factory )
170 {
171 using Vertex = typename Factory::VertexType;
172 PointExtent::Extent< Real , Dim , ExtendedAxes > extent;
173 _ProcessPLY( in , range , factory , [&]( const Vertex &vertex ){ extent.add( vertex.template get<0>() ); } );
174 return extent;
175 }
176
177 template< typename Real , unsigned int Dim >
178 std::vector< PlyProperty > _GetUnprocessedProperties( std::string in )
179 {
180 char *ext = GetFileExtension( in.c_str() );
181 if( strcasecmp( ext , "ply" ) ) MK_THROW( "Expected .ply file" );
182 delete[] ext;
183
184 std::vector< PlyProperty > unprocessedProperties;
185 {
186 typedef VertexFactory::Factory< Real , typename VertexFactory::PositionFactory< Real , Dim > , typename VertexFactory::NormalFactory< Real , Dim > > Factory;
187 Factory factory;
188 bool *readFlags = new bool[ factory.plyReadNum() ];
189 PLY::ReadVertexHeader( in , factory , readFlags , unprocessedProperties );
190 if( !factory.template plyValidReadProperties<0>( readFlags ) ) MK_THROW( "Ply file does not contain positions" );
191 if( !factory.template plyValidReadProperties<1>( readFlags ) ) MK_THROW( "Ply file does not contain normals" );
192 delete[] readFlags;
193 }
194 return unprocessedProperties;
195 }
196
197 ////////////
198 // Server //
199 ////////////
200
201 template< typename Real , unsigned int Dim >
202 std::pair< PointPartition::PointSetInfo< Real , Dim > , PointPartition::Partition > RunServer
203 (
204 std::vector< Socket > &clientSockets ,
205 ClientPartitionInfo< Real > clientPartitionInfo ,
206 bool loadBalance
207 )
208 {
209 Timer timer;
210 clientPartitionInfo.clientCount = ( unsigned int )clientSockets.size();
211 for( unsigned int c=0 ; c<clientSockets.size() ; c++ )
212 {
213 SocketStream clientSocketStream( clientSockets[c] );
214 clientPartitionInfo.write( clientSocketStream );
215 }
216 for( unsigned int c=0 ; c<clientSockets.size() ; c++ ) SocketStream( clientSockets[c] ).write( c );
217
218 PointPartition::PointSetInfo< Real , Dim > pointSetInfo( clientPartitionInfo.slabs );
219 pointSetInfo.header = clientPartitionInfo.outHeader;
220 pointSetInfo.filesPerDir = clientPartitionInfo.filesPerDir;
221
222 // Create the directory for the slabs
223 PointPartition::CreatePointSlabDirs( PointPartition::FileDir( clientPartitionInfo.outDir , clientPartitionInfo.outHeader ) , clientPartitionInfo.slabs , clientPartitionInfo.filesPerDir );
224
225 /////////////
226 // Phase 1 //
227 /////////////
228 // Get the number of samples
229 size_t sampleCount = _SampleCount< Real , Dim >( clientPartitionInfo.in , pointSetInfo.auxiliaryProperties );
230 if( clientPartitionInfo.verbose ) std::cout << "Samples: " << sampleCount << std::endl;
231
232 // Send the partitions to the clients
233 for( unsigned int c=0 ; c<clientSockets.size() ; c++ )
234 {
235 size_t start = ( sampleCount * c )/clientSockets.size();
236 size_t end = ( sampleCount * (c+1) )/clientSockets.size();
237 if( clientPartitionInfo.verbose ) std::cout << "[ " << start << " , " << end << " )" << std::endl;
238 SocketStream( clientSockets[c] ).write( std::make_pair( start , end ) );
239 }
240
241 /////////////
242 // Phase 2 //
243 /////////////
244 // Merge the clients' extents and get the direction of maximal extent
245 PointExtent::Extent< Real , Dim > e;
246 for( unsigned int c=0 ; c<clientSockets.size() ; c++ )
247 {
248 PointExtent::Extent< Real , Dim > _e;
249 SocketStream( clientSockets[c] ).read( _e );
250 e = e + _e;
251 }
252 pointSetInfo.modelToUnitCube = PointExtent::GetXForm( e , clientPartitionInfo.scale , clientPartitionInfo.sliceDir );
253
254 // Send the transformation to the clients
255 for( unsigned int c=0 ; c<clientSockets.size() ; c++ ) SocketStream( clientSockets[c] ).write( pointSetInfo.modelToUnitCube );
256
257 /////////////
258 // Phase 3 //
259 /////////////
260 pointSetInfo.pointsPerSlab.resize( clientPartitionInfo.slabs , 0 );
261 for( unsigned int c=0 ; c<clientSockets.size() ; c++ )
262 {
263 std::vector< size_t > slabSizes;
264 SocketStream( clientSockets[c] ).read( slabSizes );
265 if( slabSizes.size()!=clientPartitionInfo.slabs ) MK_THROW( "Unexpected number of slabs: " , slabSizes.size() , " != " , clientPartitionInfo.slabs );
266 for( unsigned int i=0 ; i<clientPartitionInfo.slabs ; i++ ) pointSetInfo.pointsPerSlab[i] += slabSizes[i];
267 }
268 if( clientPartitionInfo.verbose )
269 {
270 std::cout << "Partitions:" << std::endl;
271 for( unsigned int i=0 ; i<pointSetInfo.pointsPerSlab.size() ; i++ ) std::cout << "\t" << i << "] " << pointSetInfo.pointsPerSlab[i] << std::endl;
272 }
273
274 // Compute the assignment of slabs to clients
275 PointPartition::Partition pointPartition( (unsigned int)clientSockets.size() , pointSetInfo.pointsPerSlab );
276 if( loadBalance ) pointPartition.optimize( true );
277 for( unsigned int c=0 ; c<clientSockets.size() ; c++ )
278 {
279 std::pair< unsigned int , unsigned int > range = pointPartition.range( c );
280 if( clientPartitionInfo.verbose ) std::cout << "Range[ " << c << " ]: [ " << range.first << " , " << range.second << " )" << std::endl;
281 SocketStream( clientSockets[c] ).write( range );
282 }
283
284 // Check that the clients are done
285 for( unsigned int c=0 ; c<clientSockets.size() ; c++ )
286 {
287 unsigned int done;
288 SocketStream( clientSockets[c] ).read( done );
289 }
290
291 // Let the clients know that everybody else is done so they can clean up the temporary files
292 for( unsigned int c=0 ; c<clientSockets.size() ; c++ )
293 {
294 unsigned int done = 1;
295 SocketStream( clientSockets[c] ).write( done );
296 }
297
298 return std::make_pair( pointSetInfo , pointPartition );
299 }
300
301
302 ////////////
303 // Client //
304 ////////////
305
306 template< typename Real , unsigned int Dim , typename Factory >
307 void _RunClients
308 (
309 ClientPartitionInfo< Real > clientPartitionInfo ,
310 const Factory &factory ,
311 std::vector< Socket > &serverSockets
312 )
313 {
314 std::vector< unsigned int > clientIndices( serverSockets.size() );
315
316 for( unsigned int c=0 ; c<serverSockets.size() ; c++ ) SocketStream( serverSockets[c] ).read( clientIndices[c] );
317
318 int maxFiles = 2*clientPartitionInfo.slabs;
319 #ifdef _WIN32
320 if( _setmaxstdio( maxFiles )!=maxFiles ) MK_THROW( "Could not set max file handles: " , maxFiles );
321 #else // !_WIN32
322 struct rlimit rl;
323 getrlimit( RLIMIT_NOFILE , &rl );
324 rl.rlim_cur = maxFiles+3;
325 setrlimit( RLIMIT_NOFILE , &rl );
326 #endif // _WIN32
327
328 for( unsigned int i=0 ; i<serverSockets.size() ; i++ ) PointPartition::CreatePointSlabDirs( PointPartition::FileDir( clientPartitionInfo.tempDir , clientPartitionInfo.outHeader , clientIndices[i] ) , clientPartitionInfo.slabs , clientPartitionInfo.filesPerDir );
329
330 /////////////
331 // Phase 1 //
332 /////////////
333 std::vector< std::pair< size_t , size_t > > ranges( serverSockets.size() );
334 for( unsigned int i=0 ; i<serverSockets.size() ; i++ )
335 {
336 // Get the client's range
337 SocketStream( serverSockets[i] ).read( ranges[i] );
338 if( clientPartitionInfo.verbose ) std::cout << "Got range: " << MemoryInfo::PeakMemoryUsageMB() << " (MB)" << std::endl;
339 // Get the extent and send to the client
340 PointExtent::Extent< Real , Dim > e = _GetExtent< Real , Dim >( clientPartitionInfo.in , ranges[i] , factory );
341 SocketStream( serverSockets[i] ).write( e );
342 if( clientPartitionInfo.verbose ) std::cout << "Sent extent: " << MemoryInfo::PeakMemoryUsageMB() << " (MB)" << std::endl;
343 }
344
345 /////////////
346 // Phase 2 //
347 /////////////
348 for( unsigned int i=0 ; i<serverSockets.size() ; i++ )
349 {
350 XForm< Real , Dim+1 > xForm;
351 SocketStream( serverSockets[i] ).read( xForm );
352 if( clientPartitionInfo.verbose ) std::cout << "Got transform: " << MemoryInfo::PeakMemoryUsageMB() << " (MB)" << std::endl;
353
354 PointPartition::CreatePointSlabDirs( PointPartition::FileDir( clientPartitionInfo.tempDir , clientPartitionInfo.outHeader , clientIndices[i] ) , clientPartitionInfo.slabs , clientPartitionInfo.filesPerDir );
355 std::vector< size_t > slabSizes = _PartitionIntoSlabs< Real , Dim >( clientPartitionInfo.in , clientPartitionInfo.tempDir , clientPartitionInfo.outHeader , clientIndices[i] , ranges[i] , clientPartitionInfo.slabs , clientPartitionInfo.filesPerDir , xForm , factory , clientPartitionInfo.bufferSize );
356 SocketStream( serverSockets[i] ).write( slabSizes );
357 if( clientPartitionInfo.verbose ) std::cout << "Wrote slab sizes: " << MemoryInfo::PeakMemoryUsageMB() << " (MB)" << std::endl;
358 }
359
360 /////////////
361 // Phase 3 //
362 /////////////
363 for( unsigned int i=0 ; i<serverSockets.size() ; i++ )
364 {
365 std::pair< unsigned int , unsigned int > slabRange;
366 SocketStream( serverSockets[i] ).read( slabRange );
367 if( clientPartitionInfo.verbose ) std::cout << "Slab range: [ " << slabRange.first << " , " << slabRange.second << " )" << std::endl;
368 _MergeSlabs< Real , Dim >( clientPartitionInfo.tempDir , clientPartitionInfo.outDir , clientPartitionInfo.outHeader , clientPartitionInfo.clientCount , slabRange , clientPartitionInfo.slabs , clientPartitionInfo.filesPerDir , factory , clientPartitionInfo.bufferSize );
369 if( clientPartitionInfo.verbose ) std::cout << "Merged slabs: " << MemoryInfo::PeakMemoryUsageMB() << " (MB)" << std::endl;
370
371 // Notify the server that you're done
372 unsigned int done = 1;
373 SocketStream( serverSockets[i] ).write( done );
374 }
375
376 for( unsigned int i=0 ; i<serverSockets.size() ; i++ )
377 {
378 // Confirm that all the other clients are done and clean up
379 unsigned int done = 1;
380 SocketStream( serverSockets[i] ).read( done );
381
382 PointPartition::RemovePointSlabDirs( PointPartition::FileDir( clientPartitionInfo.tempDir , clientPartitionInfo.outHeader , clientIndices[i] ) );
383 }
384 }
385
386 template< typename Real , unsigned int Dim >
387 void RunClients( std::vector< Socket > &serverSockets )
388 {
389 ClientPartitionInfo< Real > clientPartitionInfo;
390 for( unsigned int i=0 ; i<serverSockets.size() ; i++ )
391 {
392 SocketStream serverSocketStream( serverSockets[i] );
393 clientPartitionInfo = ClientPartitionInfo< Real >( serverSocketStream );
394 }
395 std::vector< PlyProperty > unprocessedProperties = _GetUnprocessedProperties< Real , Dim >( clientPartitionInfo.in );
396
397 if( unprocessedProperties.size() )
398 {
399 typedef VertexFactory::Factory< Real , VertexFactory::PositionFactory< Real , Dim > , VertexFactory::NormalFactory< Real , Dim > , VertexFactory::DynamicFactory< Real > > Factory;
400 VertexFactory::PositionFactory< Real , Dim > vFactory;
401 VertexFactory::NormalFactory< Real , Dim > nFactory;
402 VertexFactory::DynamicFactory< Real > dFactory( unprocessedProperties );
403 Factory factory( vFactory , nFactory , dFactory );
404 _RunClients< Real , Dim >( clientPartitionInfo , factory , serverSockets );
405 }
406 else
407 {
408 typedef VertexFactory::Factory< Real , VertexFactory::PositionFactory< Real , Dim > , VertexFactory::NormalFactory< Real , Dim > > Factory;
409 Factory factory;
410 _RunClients< Real , Dim >( clientPartitionInfo , factory , serverSockets );
411 }
412 }
413
414 /////////////////////////
415 // ClientPartitionInfo //
416 /////////////////////////
417 template< typename Real >
418 ClientPartitionInfo< Real >::ClientPartitionInfo( void ) : scale((Real)1.1) , sliceDir(-1) , verbose(false) , slabs(0) , filesPerDir(-1) , bufferSize(BUFFER_IO) , clientCount(0) {}
419
420 template< typename Real >
421 ClientPartitionInfo< Real >::ClientPartitionInfo( BinaryStream &stream )
422 {
423 auto ReadBool = [&]( bool &b )
424 {
425 char _b;
426 if( !stream.read( _b ) ) return false;
427 b = _b!=0;
428 return true;
429 };
430 if( !stream.read( in ) ) MK_THROW( "Failed to read in" );
431 if( !stream.read( tempDir ) ) MK_THROW( "Failed to read temp dir" );
432 if( !stream.read( outDir ) ) MK_THROW( "Failed to read out dir" );
433 if( !stream.read( outHeader ) ) MK_THROW( "Failed to read out header" );
434 if( !stream.read( slabs ) ) MK_THROW( "Failed to read slabs" );
435 if( !stream.read( filesPerDir ) ) MK_THROW( "Failed to read files per dir" );
436 if( !stream.read( bufferSize ) ) MK_THROW( "Failed to read buffer size" );
437 if( !stream.read( scale ) ) MK_THROW( "Failed to read scale" );
438 if( !stream.read( clientCount ) ) MK_THROW( "Failed to read client count" );
439 if( !stream.read( sliceDir ) ) MK_THROW( "Failed to read slice direction" );
440 if( !ReadBool( verbose ) ) MK_THROW( "Failed to read verbose flag" );
441 }
442
443 template< typename Real >
444 void ClientPartitionInfo< Real >::write( BinaryStream &stream ) const
445 {
446 auto WriteBool = [&]( bool b )
447 {
448 char _b = b ? 1 : 0;
449 stream.write( _b );
450 };
451 stream.write( in );
452 stream.write( tempDir );
453 stream.write( outDir );
454 stream.write( outHeader );
455 stream.write( slabs );
456 stream.write( filesPerDir );
457 stream.write( bufferSize );
458 stream.write( scale );
459 stream.write( clientCount );
460 stream.write( sliceDir );
461 WriteBool( verbose );
462 }