Skip to content

Commit

Permalink
Replace exit() call with thrown exceptions
Browse files Browse the repository at this point in the history
Address alicevision#139

Previously, exit() function was called when popsift encountered errors
(e.g. out of memory). This error handling made graceful error handling
within the application using popsift difficult.
This commit replaces all the exit() call with throwing a runtime error
instead.
  • Loading branch information
Azhng committed Aug 29, 2022
1 parent 4c22d41 commit c652202
Show file tree
Hide file tree
Showing 9 changed files with 169 additions and 122 deletions.
99 changes: 50 additions & 49 deletions src/popsift/common/debug_macros.cu
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ void pop_check_last_error( const char* file, size_t line )
{
cudaError_t err = cudaGetLastError( );
if( err != cudaSuccess ) {
std::cerr << __FILE__ << ":" << __LINE__ << std::endl
<< " called from " << file << ":" << line << std::endl
<< " cudaGetLastError failed: " << cudaGetErrorString(err) << std::endl;
exit( -__LINE__ );
std::stringstream ss;
ss << __FILE__ << ":" << __LINE__ << std::endl
<< " called from " << file << ":" << line << std::endl
<< " cudaGetLastError failed: " << cudaGetErrorString(err);
throw std::runtime_error{ss.str()};
}
}

Expand All @@ -35,9 +36,9 @@ void malloc_dev( void** ptr, int sz,
cudaError_t err;
err = cudaMalloc( ptr, sz );
if( err != cudaSuccess ) {
std::cerr << file << ":" << line << std::endl
<< " cudaMalloc failed: " << cudaGetErrorString(err) << std::endl;
exit( -__LINE__ );
std::stringstream ss;
ss << file << ":" << line << std::endl << " cudaMalloc failed: " << cudaGetErrorString(err);
throw std::runtime_error{ss.str()};
}
#ifdef DEBUG_INIT_DEVICE_ALLOCATIONS
popsift::cuda::memset_sync( *ptr, 0, sz, file, line );
Expand All @@ -52,9 +53,9 @@ void malloc_hst( void** ptr, int sz,
cudaError_t err;
err = cudaMallocHost( ptr, sz );
if( err != cudaSuccess ) {
std::cerr << file << ":" << line << std::endl
<< " cudaMallocHost failed: " << cudaGetErrorString(err) << std::endl;
exit( -__LINE__ );
std::stringstream ss;
ss << file << ":" << line << std::endl << " cudaMallocHost failed: " << cudaGetErrorString(err);
throw std::runtime_error{ss.str()};
}
#ifdef DEBUG_INIT_DEVICE_ALLOCATIONS
memset( *ptr, 0, sz );
Expand All @@ -74,14 +75,14 @@ void memcpy_async( void* dst, const void* src, size_t sz,
cudaError_t err;
err = cudaMemcpyAsync( dst, src, sz, type, stream );
if( err != cudaSuccess ) {
cerr << file << ":" << line << endl
<< " " << "Failed to copy "
<< (type==cudaMemcpyHostToDevice?"host-to-device":"device-to-host")
<< ": ";
cerr << cudaGetErrorString(err) << endl;
cerr << " src ptr=" << hex << (size_t)src << dec << endl
<< " dst ptr=" << hex << (size_t)dst << dec << endl;
exit( -__LINE__ );
std::stringstream ss;
ss << file << ":" << line << endl
<< " "
<< "Failed to copy " << (type == cudaMemcpyHostToDevice ? "host-to-device" : "device-to-host") << ": ";
ss << cudaGetErrorString(err) << endl;
ss << " src ptr=" << hex << (size_t)src << dec << endl
<< " dst ptr=" << hex << (size_t)dst << dec << endl;
throw std::runtime_error{ss.str()};
}
POP_CUDA_FATAL_TEST( err, "Failed to copy host-to-device: " );
}
Expand All @@ -95,13 +96,13 @@ void memcpy_sync( void* dst, const void* src, size_t sz, cudaMemcpyKind type, co
cudaError_t err;
err = cudaMemcpy( dst, src, sz, type );
if( err != cudaSuccess ) {
cerr << " " << "Failed to copy "
<< (type==cudaMemcpyHostToDevice?"host-to-device":"device-to-host")
<< ": ";
cerr << cudaGetErrorString(err) << endl;
cerr << " src ptr=" << hex << (size_t)src << dec << endl
<< " dst ptr=" << hex << (size_t)dst << dec << endl;
exit( -__LINE__ );
std::stringstream ss;
ss << " "
<< "Failed to copy " << (type == cudaMemcpyHostToDevice ? "host-to-device" : "device-to-host") << ": ";
ss << cudaGetErrorString(err) << endl;
ss << " src ptr=" << hex << (size_t)src << dec << endl
<< " dst ptr=" << hex << (size_t)dst << dec << endl;
throw std::runtime_error{ss.str()};
}
POP_CUDA_FATAL_TEST( err, "Failed to copy host-to-device: " );
}
Expand All @@ -111,9 +112,9 @@ void memset_async( void* ptr, int value, size_t bytes, cudaStream_t stream, cons
cudaError_t err;
err = cudaMemsetAsync( ptr, value, bytes, stream );
if( err != cudaSuccess ) {
std::cerr << file << ":" << line << std::endl
<< " cudaMemsetAsync failed: " << cudaGetErrorString(err) << std::endl;
exit( -__LINE__ );
std::stringstream ss;
ss << file << ":" << line << std::endl << " cudaMemsetAsync failed: " << cudaGetErrorString(err);
throw std::runtime_error{ss.str()};
}
}

Expand All @@ -122,9 +123,9 @@ void memset_sync( void* ptr, int value, size_t bytes, const char* file, size_t l
cudaError_t err;
err = cudaMemset( ptr, value, bytes );
if( err != cudaSuccess ) {
std::cerr << file << ":" << line << std::endl
<< " cudaMemset failed: " << cudaGetErrorString(err) << std::endl;
exit( -__LINE__ );
std::stringstream ss;
ss << file << ":" << line << std::endl << " cudaMemset failed: " << cudaGetErrorString(err);
throw std::runtime_error{ss.str()};
}
}
} }
Expand All @@ -136,9 +137,9 @@ cudaStream_t stream_create( const char* file, size_t line )
cudaError_t err;
err = cudaStreamCreate( &stream );
if( err != cudaSuccess ) {
std::cerr << file << ":" << line << std::endl
<< " cudaStreamCreate failed: " << cudaGetErrorString(err) << std::endl;
exit( -__LINE__ );
std::stringstream ss;
ss << file << ":" << line << std::endl << " cudaStreamCreate failed: " << cudaGetErrorString(err);
throw std::runtime_error{ss.str()};
}
return stream;
}
Expand All @@ -147,9 +148,9 @@ void stream_destroy( cudaStream_t s, const char* file, size_t line )
cudaError_t err;
err = cudaStreamDestroy( s );
if( err != cudaSuccess ) {
std::cerr << file << ":" << line << std::endl
<< " cudaStreamDestroy failed: " << cudaGetErrorString(err) << std::endl;
exit( -__LINE__ );
std::stringstream ss;
ss << file << ":" << line << std::endl << " cudaStreamDestroy failed: " << cudaGetErrorString(err);
throw std::runtime_error{ss.str()};
}
}
cudaEvent_t event_create( const char* file, size_t line )
Expand All @@ -158,9 +159,9 @@ cudaEvent_t event_create( const char* file, size_t line )
cudaError_t err;
err = cudaEventCreate( &ev );
if( err != cudaSuccess ) {
std::cerr << file << ":" << line << std::endl
<< " cudaEventCreate failed: " << cudaGetErrorString(err) << std::endl;
exit( -__LINE__ );
std::stringstream ss;
ss << file << ":" << line << std::endl << " cudaEventCreate failed: " << cudaGetErrorString(err);
throw std::runtime_error{ss.str()};
}
return ev;
}
Expand All @@ -169,29 +170,29 @@ void event_destroy( cudaEvent_t ev, const char* file, size_t line )
cudaError_t err;
err = cudaEventDestroy( ev );
if( err != cudaSuccess ) {
std::cerr << file << ":" << line << std::endl
<< " cudaEventDestroy failed: " << cudaGetErrorString(err) << std::endl;
exit( -__LINE__ );
std::stringstream ss;
ss << file << ":" << line << std::endl << " cudaEventDestroy failed: " << cudaGetErrorString(err);
throw std::runtime_error{ss.str()};
}
}
void event_record( cudaEvent_t ev, cudaStream_t s, const char* file, size_t line )
{
cudaError_t err;
err = cudaEventRecord( ev, s );
if( err != cudaSuccess ) {
std::cerr << file << ":" << line << std::endl
<< " cudaEventRecord failed: " << cudaGetErrorString(err) << std::endl;
exit( -__LINE__ );
std::stringstream ss;
ss << file << ":" << line << std::endl << " cudaEventRecord failed: " << cudaGetErrorString(err);
throw std::runtime_error{ss.str()};
}
}
void event_wait( cudaEvent_t ev, cudaStream_t s, const char* file, size_t line )
{
cudaError_t err;
err = cudaStreamWaitEvent( s, ev, 0 );
if( err != cudaSuccess ) {
std::cerr << file << ":" << line << std::endl
<< " cudaStreamWaitEvent failed: " << cudaGetErrorString(err) << std::endl;
exit( -__LINE__ );
std::stringstream ss;
ss << file << ":" << line << std::endl << " cudaStreamWaitEvent failed: " << cudaGetErrorString(err);
throw std::runtime_error{ss.str()};
}
}

Expand Down
27 changes: 17 additions & 10 deletions src/popsift/common/debug_macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <stdexcept>
#include <string>

// synchronize device and check for an error
Expand Down Expand Up @@ -117,14 +118,18 @@ class BriefDuration
};
};

#define POP_FATAL(s) { \
std::cerr << __FILE__ << ":" << __LINE__ << std::endl << " " << s << std::endl; \
exit( -__LINE__ ); \
#define POP_FATAL(s) \
{ \
std::stringstream ss; \
ss << __FILE__ << ":" << __LINE__ << std::endl << " " << s; \
throw std::runtime_error{ss.str()}; \
}

#define POP_FATAL_FL(s,file,line) { \
std::cerr << file << ":" << line << std::endl << " " << s << std::endl; \
exit( -__LINE__ ); \
#define POP_FATAL_FL(s, file, line) \
{ \
std::stringstream ss; \
ss << file << ":" << line << std::endl << " " << s << std::endl; \
throw std::runtime_error{ss.str()}; \
}

#define POP_CHECK_NON_NULL(ptr,s) if( ptr == 0 ) { POP_FATAL_FL(s,__FILE__,__LINE__); }
Expand All @@ -147,10 +152,12 @@ class BriefDuration
std::cerr << __FILE__ << ":" << __LINE__ << std::endl; \
std::cerr << " WARNING: " << s << cudaGetErrorString(err) << std::endl; \
}
#define POP_CUDA_FATAL(err,s) { \
std::cerr << __FILE__ << ":" << __LINE__ << std::endl; \
std::cerr << " " << s << cudaGetErrorString(err) << std::endl; \
exit( -__LINE__ ); \
#define POP_CUDA_FATAL(err,s) \
{ \
std::stringstream ss; \
ss << __FILE__ << ":" << __LINE__ << std::endl; \
ss << " " << s << cudaGetErrorString(err) << std::endl; \
throw std::runtime_error{ss.str()}; \
}
#define POP_CUDA_FATAL_TEST(err,s) if( err != cudaSuccess ) { POP_CUDA_FATAL(err,s); }

Expand Down
18 changes: 10 additions & 8 deletions src/popsift/common/plane_2d.cu
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <sstream>
#ifndef _WIN32
#include <unistd.h>
#else
Expand Down Expand Up @@ -65,11 +66,12 @@ void* PlaneBase::allocHost2D( int w, int h, int elemSize, PlaneMapMode m )
#else
const char *buf = strerror(errno);
#endif
cerr << __FILE__ << ":" << __LINE__ << endl
<< " Failed to allocate " << sz << " bytes of unaligned host memory." << endl
<< " Cause: " << buf << endl;
exit( -1 );
} else if( m == PageAligned ) {
stringstream ss;
ss << __FILE__ << ":" << __LINE__ << endl
<< " Failed to allocate " << sz << " bytes of unaligned host memory." << endl
<< " Cause: " << buf;
throw std::runtime_error{ss.str()};
} else if(m == PageAligned) {
void* ptr = memalign(getPageSize(), sz);
if(ptr)
return ptr;
Expand All @@ -93,9 +95,9 @@ void* PlaneBase::allocHost2D( int w, int h, int elemSize, PlaneMapMode m )
POP_CUDA_FATAL_TEST( err, "Failed to allocate aligned and pinned host memory: " );
return ptr;
} else {
cerr << __FILE__ << ":" << __LINE__ << endl
<< " Alignment not correctly specified in host plane allocation" << endl;
exit( -1 );
stringstream ss;
ss << __FILE__ << ":" << __LINE__ << endl << " Alignment not correctly specified in host plane allocation";
throw std::runtime_error{ss.str()};
}
}

Expand Down
16 changes: 10 additions & 6 deletions src/popsift/common/plane_2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -407,14 +407,18 @@ __host__
inline void Plane2D<T>::memcpyToDevice( Plane2D<T>& devPlane, cudaStream_t stream )
{
if( devPlane._cols != this->_cols ) {
std::cerr << __FILE__ << ":" << __LINE__ << std::endl
<< " Error: source columns (" << this->_cols << ") and dest columns (" << devPlane._cols << ") must be identical" << std::endl;
exit( -1 );
std::stringstream ss;
ss << __FILE__ << ":" << __LINE__ << std::endl
<< " Error: source columns (" << this->_cols << ") and dest columns (" << devPlane._cols
<< ") must be identical";
throw std::runtime_error{ss.str()};
}
if( devPlane._rows != this->_rows ) {
std::cerr << __FILE__ << ":" << __LINE__ << std::endl
<< " Error: source rows (" << this->_rows << ") and dest rows (" << devPlane._rows << ") must be identical" << std::endl;
exit( -1 );
std::stringstream ss;
ss << __FILE__ << ":" << __LINE__ << std::endl
<< " Error: source rows (" << this->_rows << ") and dest rows (" << devPlane._rows
<< ") must be identical";
throw std::runtime_error{ss.str()};
}
PitchPlane2D<T>::memcpyToDevice( devPlane, this->_cols, this->_rows, stream );
}
Expand Down
25 changes: 14 additions & 11 deletions src/popsift/features.cu
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <sstream>

using namespace std;

Expand Down Expand Up @@ -61,19 +62,21 @@ void FeaturesHost::reset( int num_ext, int num_ori )

_ext = (Feature*)memalign( getPageSize(), num_ext * sizeof(Feature) );
if( _ext == nullptr ) {
cerr << __FILE__ << ":" << __LINE__ << " Runtime error:" << endl
<< " Failed to (re)allocate memory for downloading " << num_ext << " features" << endl;
if( errno == EINVAL ) cerr << " Alignment is not a power of two." << endl;
if( errno == ENOMEM ) cerr << " Not enough memory." << endl;
exit( -1 );
std::stringstream ss;
ss << __FILE__ << ":" << __LINE__ << " Runtime error:" << endl
<< " Failed to (re)allocate memory for downloading " << num_ext << " features" << endl;
if(errno == EINVAL) ss << " Alignment is not a power of two.";
if(errno == ENOMEM) ss << " Not enough memory.";
throw std::runtime_error{ss.str()};
}
_ori = (Descriptor*)memalign( getPageSize(), num_ori * sizeof(Descriptor) );
if( _ori == nullptr ) {
cerr << __FILE__ << ":" << __LINE__ << " Runtime error:" << endl
<< " Failed to (re)allocate memory for downloading " << num_ori << " descriptors" << endl;
if( errno == EINVAL ) cerr << " Alignment is not a power of two." << endl;
if( errno == ENOMEM ) cerr << " Not enough memory." << endl;
exit( -1 );
if(_ori == nullptr) {
std::stringstream ss;
ss << __FILE__ << ":" << __LINE__ << " Runtime error:" << endl
<< " Failed to (re)allocate memory for downloading " << num_ori << " descriptors" << endl;
if(errno == EINVAL) ss << " Alignment is not a power of two.";
if(errno == ENOMEM) ss << " Not enough memory.";
throw std::runtime_error{ss.str()};
}

setFeatureCount( num_ext );
Expand Down
24 changes: 12 additions & 12 deletions src/popsift/gauss_filter.cu
Original file line number Diff line number Diff line change
Expand Up @@ -130,17 +130,17 @@ void init_filter( const Config& conf,
{
if( sigma0 > 2.0 )
{
cerr << __FILE__ << ":" << __LINE__ << ", ERROR: "
<< " Sigma > 2.0 is not supported. Re-size __constant__ array and recompile."
<< endl;
exit( -__LINE__ );
stringstream ss;
ss << __FILE__ << ":" << __LINE__ << ", ERROR: "
<< " Sigma > 2.0 is not supported. Re-size __constant__ array and recompile.";
throw std::runtime_error{ss.str()};
}
if( levels > GAUSS_LEVELS )
{
cerr << __FILE__ << ":" << __LINE__ << ", ERROR: "
<< " More than " << GAUSS_LEVELS << " levels not supported. Re-size __constant__ array and recompile."
<< endl;
exit( -__LINE__ );
stringstream ss;
ss << __FILE__ << ":" << __LINE__ << ", ERROR: "
<< " More than " << GAUSS_LEVELS << " levels not supported. Re-size __constant__ array and recompile.";
throw std::runtime_error{ss.str()};
}

if( conf.ifPrintGaussTables() ) {
Expand Down Expand Up @@ -291,10 +291,10 @@ int GaussInfo::getSpan( float sigma ) const
case Config::Fixed15 :
return 8;
default :
cerr << __FILE__ << ":" << __LINE__ << ", ERROR: "
<< " The mode for computing Gauss filter scan is invalid"
<< endl;
exit( -__LINE__ );
stringstream ss;
ss << __FILE__ << ":" << __LINE__ << ", ERROR: "
<< " The mode for computing Gauss filter scan is invalid";
throw std::runtime_error{ss.str()};
}
}

Expand Down
Loading

0 comments on commit c652202

Please sign in to comment.