Skip to content

Commit

Permalink
Merge pull request #141 from Azhng/exception
Browse files Browse the repository at this point in the history
Replace exit() call with thrown exceptions
  • Loading branch information
simogasp authored Jun 10, 2023
2 parents e1b030a + e8bb205 commit 4b4b247
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 149 deletions.
98 changes: 24 additions & 74 deletions src/popsift/common/debug_macros.cu
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,7 @@ void pop_sync_check_last_error( const char* file, size_t line )
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__ );
}
POP_CUDA_FATAL_TEST(err, "cudaGetLastError failed: ");
}

namespace popsift { namespace cuda {
Expand All @@ -34,11 +29,7 @@ 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__ );
}
POP_CUDA_FATAL_TEST(err, "cudaMalloc failed: ");
#ifdef DEBUG_INIT_DEVICE_ALLOCATIONS
popsift::cuda::memset_sync( *ptr, 0, sz, file, line );
#endif // NDEBUG
Expand All @@ -51,11 +42,7 @@ 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__ );
}
POP_CUDA_FATAL_TEST(err, "cudaMallocHost failed: ");
#ifdef DEBUG_INIT_DEVICE_ALLOCATIONS
memset( *ptr, 0, sz );
#endif // NDEBUG
Expand All @@ -74,16 +61,13 @@ 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 << "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;
POP_FATAL(ss.str());
}
POP_CUDA_FATAL_TEST( err, "Failed to copy host-to-device: " );
}

void memcpy_sync( void* dst, const void* src, size_t sz, cudaMemcpyKind type, const char* file, size_t line )
Expand All @@ -95,37 +79,27 @@ 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;
POP_FATAL(ss.str())
}
POP_CUDA_FATAL_TEST( err, "Failed to copy host-to-device: " );
}

void memset_async( void* ptr, int value, size_t bytes, cudaStream_t stream, const char* file, size_t line )
{
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__ );
}
POP_CUDA_FATAL_TEST(err, "cudaMemsetAsync failed: ");
}

void memset_sync( void* ptr, int value, size_t bytes, const char* file, size_t line )
{
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__ );
}
POP_CUDA_FATAL_TEST(err, "cudaMemset failed: ");
}
} }

Expand All @@ -135,68 +109,44 @@ cudaStream_t stream_create( const char* file, size_t line )
cudaStream_t stream;
cudaError_t err;
err = cudaStreamCreate( &stream );
if( err != cudaSuccess ) {
std::cerr << file << ":" << line << std::endl
<< " cudaStreamCreate failed: " << cudaGetErrorString(err) << std::endl;
exit( -__LINE__ );
}
POP_CUDA_FATAL_TEST(err, "cudaStreamCreate failed: ");
return stream;
}
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__ );
}
POP_CUDA_FATAL_TEST(err, "cudaStreamDestroy failed: ");
}
cudaEvent_t event_create( const char* file, size_t line )
{
cudaEvent_t ev;
cudaError_t err;
err = cudaEventCreate( &ev );
if( err != cudaSuccess ) {
std::cerr << file << ":" << line << std::endl
<< " cudaEventCreate failed: " << cudaGetErrorString(err) << std::endl;
exit( -__LINE__ );
}
POP_CUDA_FATAL_TEST(err, "cudaEventCreate failed: ");
return ev;
}
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__ );
}
POP_CUDA_FATAL_TEST(err, "cudaEventDestroy failed: ");
}
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__ );
}
POP_CUDA_FATAL_TEST(err, "cudaEventRecord failed: ");
}
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__ );
}
POP_CUDA_FATAL_TEST(err, "cudaStreamWaitEvent failed: ");
}

float event_diff( cudaEvent_t from, cudaEvent_t to )
{
{
float ms;
cudaEventElapsedTime( &ms, from, to );
return ms;
Expand Down
28 changes: 18 additions & 10 deletions src/popsift/common/debug_macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <stdexcept>
#include <string>
#include <sstream>

// synchronize device and check for an error
void pop_sync_check_last_error( const char* file, size_t line );
Expand Down Expand Up @@ -117,14 +119,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 +153,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
15 changes: 7 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,11 @@ 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 << "Failed to allocate " << sz << " bytes of unaligned host memory." << endl
<< "Cause: " << buf;
POP_FATAL(ss.str());
} else if(m == PageAligned) {
void* ptr = memalign(getPageSize(), sz);
if(ptr)
return ptr;
Expand All @@ -93,9 +94,7 @@ 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 );
POP_FATAL("Alignment not correctly specified in host plane allocation");
}
}

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 @@ -16,6 +16,8 @@
#include <sstream>
#include <stdexcept>

#include "debug_macros.h"

#define PLANE2D_CUDA_OP_DEBUG

#ifndef NDEBUG
Expand Down Expand Up @@ -407,14 +409,16 @@ __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 << "Error: source columns (" << this->_cols << ") and dest columns (" << devPlane._cols
<< ") must be identical";
POP_FATAL(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 << "Error: source rows (" << this->_rows << ") and dest rows (" << devPlane._rows
<< ") must be identical";
POP_FATAL(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 << "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.";
POP_FATAL(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 << "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.";
POP_FATAL(ss.str());
}

setFeatureCount( num_ext );
Expand Down
23 changes: 11 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 << "ERROR: "
<< " Sigma > 2.0 is not supported. Re-size __constant__ array and recompile.";
POP_FATAL(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 << "ERROR: "
<< " More than " << GAUSS_LEVELS << " levels not supported. Re-size __constant__ array and recompile.";
POP_FATAL(ss.str());
}

if( conf.ifPrintGaussTables() ) {
Expand Down Expand Up @@ -291,10 +291,9 @@ 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 << "ERROR: The mode for computing Gauss filter scan is invalid";
POP_FATAL(ss.str());
}
}

Expand Down
Loading

0 comments on commit 4b4b247

Please sign in to comment.