Skip to content

Commit

Permalink
Merge pull request opencv#26245 from cudawarped:cuda_update_to_npp_st…
Browse files Browse the repository at this point in the history
…ream_ctx

cuda - update npp calls to use the new NppStreamContext API if available
  • Loading branch information
asmorkalov authored Oct 22, 2024
2 parents 4398e0b + e375d57 commit 57ccbee
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion modules/core/include/opencv2/core/private.cuda.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,36 @@ namespace cv { namespace cuda
template<> struct NPPTypeTraits<CV_32F> { typedef Npp32f npp_type; };
template<> struct NPPTypeTraits<CV_64F> { typedef Npp64f npp_type; };

#define nppSafeCall(expr) cv::cuda::checkNppError(expr, __FILE__, __LINE__, CV_Func)
// NppStreamContext is introduced in NPP version 10100 included in CUDA toolkit 10.1 (CUDA_VERSION == 10010) however not all of the NPP functions called internally by OpenCV
// - have an NppStreamContext argument (e.g. nppiHistogramEvenGetBufferSize_8u_C1R_Ctx in CUDA 12.3) and/or
// - have a corresponding function in the supplied library (e.g. nppiEvenLevelsHost_32s_Ctx is not present in nppist.lib or libnppist.so as of CUDA 12.6)
// Because support for these functions has gradually been introduced without being mentioned in the release notes this flag is set to a version of NPP (version 12205 included in CUDA toolkit 12.4) which is known to work.
#define USE_NPP_STREAM_CTX NPP_VERSION >= 12205
#if USE_NPP_STREAM_CTX
class NppStreamHandler
{
public:
inline explicit NppStreamHandler(cudaStream_t newStream)
{
nppStreamContext = {};
nppSafeCall(nppGetStreamContext(&nppStreamContext));
nppStreamContext.hStream = newStream;
cudaSafeCall(cudaStreamGetFlags(nppStreamContext.hStream, &nppStreamContext.nStreamFlags));
}

inline explicit NppStreamHandler(Stream& newStream) : NppStreamHandler(StreamAccessor::getStream(newStream)) {}

inline operator NppStreamContext() const {
return nppStreamContext;
}

inline NppStreamContext get() { return nppStreamContext; }

private:
NppStreamContext nppStreamContext;
};
#else
class NppStreamHandler
{
public:
Expand All @@ -157,9 +187,9 @@ namespace cv { namespace cuda
private:
cudaStream_t oldStream;
};
#endif
}}

#define nppSafeCall(expr) cv::cuda::checkNppError(expr, __FILE__, __LINE__, CV_Func)
#define cuSafeCall(expr) cv::cuda::checkCudaDriverApiError(expr, __FILE__, __LINE__, CV_Func)

#endif // HAVE_CUDA
Expand Down

0 comments on commit 57ccbee

Please sign in to comment.