1919
2020using namespace std ;
2121
22- PopSift::PopSift ( const popsift::Config& config, popsift::Config::ProcessingMode mode, ImageMode imode )
22+ PopSift::PopSift ( const popsift::Config& config, popsift::Config::ProcessingMode mode, ImageMode imode, int device )
2323 : _image_mode( imode )
24+ , _device(device)
2425{
26+ cudaSetDevice (_device);
27+ configure (config);
28+
2529 if ( imode == ByteImages )
2630 {
2731 _pipe._unused .push ( new popsift::Image);
@@ -33,18 +37,19 @@ PopSift::PopSift( const popsift::Config& config, popsift::Config::ProcessingMode
3337 _pipe._unused .push ( new popsift::ImageFloat );
3438 }
3539
36- configure ( config, true );
37-
3840 _pipe._thread_stage1 .reset ( new std::thread ( &PopSift::uploadImages, this ));
3941 if ( mode == popsift::Config::ExtractingMode )
4042 _pipe._thread_stage2 .reset ( new std::thread ( &PopSift::extractDownloadLoop, this ));
4143 else
4244 _pipe._thread_stage2 .reset ( new std::thread ( &PopSift::matchPrepareLoop, this ));
4345}
4446
45- PopSift::PopSift ( ImageMode imode )
47+ PopSift::PopSift ( ImageMode imode, int device )
4648 : _image_mode( imode )
49+ , _device(device)
4750{
51+ cudaSetDevice (_device);
52+
4853 if ( imode == ByteImages )
4954 {
5055 _pipe._unused .push ( new popsift::Image);
@@ -68,16 +73,20 @@ PopSift::~PopSift()
6873 }
6974}
7075
71- bool PopSift::configure ( const popsift::Config& config, bool force )
76+ bool PopSift::configure ( const popsift::Config& config, bool /* force*/ )
7277{
7378 if ( _pipe._pyramid != nullptr ) {
7479 return false ;
7580 }
7681
7782 _config = config;
78-
7983 _config.levels = max ( 2 , config.levels );
8084
85+ return true ;
86+ }
87+
88+ bool PopSift::applyConfiguration (bool force)
89+ {
8190 if ( force || ( _config != _shadow_config ) )
8291 {
8392 popsift::init_filter ( _config,
@@ -131,6 +140,16 @@ bool PopSift::private_init( int w, int h )
131140 return true ;
132141}
133142
143+ bool PopSift::private_uninit ()
144+ {
145+ Pipe& p = _pipe;
146+
147+ delete p._pyramid ;
148+ p._pyramid = nullptr ;
149+
150+ return true ;
151+ }
152+
134153void PopSift::uninit ( )
135154{
136155 if (!_isInit)
@@ -273,6 +292,8 @@ SiftJob* PopSift::enqueue( int w,
273292
274293void PopSift::uploadImages ( )
275294{
295+ cudaSetDevice (_device);
296+
276297 SiftJob* job;
277298 while ( ( job = _pipe._queue_stage1 .pull () ) != nullptr ) {
278299 popsift::ImageBase* img = _pipe._unused .pull ();
@@ -284,10 +305,15 @@ void PopSift::uploadImages( )
284305
285306void PopSift::extractDownloadLoop ( )
286307{
308+ cudaSetDevice (_device);
309+ applyConfiguration (true );
310+
287311 Pipe& p = _pipe;
288312
289313 SiftJob* job;
290314 while ( ( job = p._queue_stage2 .pull () ) != nullptr ) {
315+ applyConfiguration ();
316+
291317 popsift::ImageBase* img = job->getImg ();
292318
293319 private_init ( img->getWidth (), img->getHeight () );
@@ -313,14 +339,21 @@ void PopSift::extractDownloadLoop( )
313339
314340 job->setFeatures ( features );
315341 }
342+
343+ private_uninit ();
316344}
317345
318346void PopSift::matchPrepareLoop ( )
319347{
348+ cudaSetDevice (_device);
349+ applyConfiguration (true );
350+
320351 Pipe& p = _pipe;
321352
322353 SiftJob* job;
323354 while ( ( job = p._queue_stage2 .pull () ) != nullptr ) {
355+ applyConfiguration ();
356+
324357 popsift::ImageBase* img = job->getImg ();
325358
326359 private_init ( img->getWidth (), img->getHeight () );
@@ -336,6 +369,8 @@ void PopSift::matchPrepareLoop( )
336369
337370 job->setFeatures ( features );
338371 }
372+
373+ private_uninit ();
339374}
340375
341376SiftJob::SiftJob ( int w, int h, const unsigned char * imageData )
@@ -445,8 +480,4 @@ void PopSift::Pipe::uninit()
445480 popsift::ImageBase* img = _unused.pull ();
446481 delete img;
447482 }
448-
449- delete _pyramid;
450- _pyramid = nullptr ;
451-
452483}
0 commit comments