Skip to content

Commit 57ccbee

Browse files
authored
Merge pull request opencv#26245 from cudawarped:cuda_update_to_npp_stream_ctx
cuda - update npp calls to use the new NppStreamContext API if available
2 parents 4398e0b + e375d57 commit 57ccbee

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

modules/core/include/opencv2/core/private.cuda.hpp

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,36 @@ namespace cv { namespace cuda
134134
template<> struct NPPTypeTraits<CV_32F> { typedef Npp32f npp_type; };
135135
template<> struct NPPTypeTraits<CV_64F> { typedef Npp64f npp_type; };
136136

137+
#define nppSafeCall(expr) cv::cuda::checkNppError(expr, __FILE__, __LINE__, CV_Func)
138+
// 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
139+
// - have an NppStreamContext argument (e.g. nppiHistogramEvenGetBufferSize_8u_C1R_Ctx in CUDA 12.3) and/or
140+
// - 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)
141+
// 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.
142+
#define USE_NPP_STREAM_CTX NPP_VERSION >= 12205
143+
#if USE_NPP_STREAM_CTX
144+
class NppStreamHandler
145+
{
146+
public:
147+
inline explicit NppStreamHandler(cudaStream_t newStream)
148+
{
149+
nppStreamContext = {};
150+
nppSafeCall(nppGetStreamContext(&nppStreamContext));
151+
nppStreamContext.hStream = newStream;
152+
cudaSafeCall(cudaStreamGetFlags(nppStreamContext.hStream, &nppStreamContext.nStreamFlags));
153+
}
154+
155+
inline explicit NppStreamHandler(Stream& newStream) : NppStreamHandler(StreamAccessor::getStream(newStream)) {}
156+
157+
inline operator NppStreamContext() const {
158+
return nppStreamContext;
159+
}
160+
161+
inline NppStreamContext get() { return nppStreamContext; }
162+
163+
private:
164+
NppStreamContext nppStreamContext;
165+
};
166+
#else
137167
class NppStreamHandler
138168
{
139169
public:
@@ -157,9 +187,9 @@ namespace cv { namespace cuda
157187
private:
158188
cudaStream_t oldStream;
159189
};
190+
#endif
160191
}}
161192

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

165195
#endif // HAVE_CUDA

0 commit comments

Comments
 (0)