Debian Codegraph @

Tests/Cuda/SharedRuntimePlusToolkit/nppif.cpp

raw
1 // Comes from
2 // https://devtalk.nvidia.com/default/topic/1037482/gpu-accelerated-libraries/help-me-help-you-with-modern-cmake-and-cuda-mwe-for-npp/post/5271066/#5271066
3
4 #ifdef _WIN32
5 # define EXPORT __declspec(dllexport)
6 #else
7 # define EXPORT
8 #endif
9
10 #include <cstdio>
11
12 #include <assert.h>
13 #include <cuda_runtime_api.h>
14 #include <nppi_filtering_functions.h>
15
16 EXPORT int nppif_main()
17 {
18 /**
19 * 8-bit unsigned single-channel 1D row convolution.
20 */
21 int const simgrows = 32;
22 int const simgcols = 32;
23 Npp8u *d_pSrc, *d_pDst;
24 int const nMaskSize = 3;
25 NppiSize oROI;
26 oROI.width = simgcols - nMaskSize;
27 oROI.height = simgrows;
28 int const simgsize = simgrows * simgcols * sizeof(d_pSrc[0]);
29 int const dimgsize = oROI.width * oROI.height * sizeof(d_pSrc[0]);
30 int const simgpix = simgrows * simgcols;
31 int const dimgpix = oROI.width * oROI.height;
32 int const nSrcStep = simgcols * sizeof(d_pSrc[0]);
33 int const nDstStep = oROI.width * sizeof(d_pDst[0]);
34 int const pixval = 1;
35 int const nDivisor = 1;
36 Npp32s const h_pKernel[nMaskSize] = { pixval, pixval, pixval };
37 Npp32s* d_pKernel;
38 Npp32s const nAnchor = 2;
39 cudaError_t err = cudaMalloc((void**)&d_pSrc, simgsize);
40 if (err != cudaSuccess) {
41 fprintf(stderr, "Cuda error %d\n", __LINE__);
42 return 1;
43 }
44 err = cudaMalloc((void**)&d_pDst, dimgsize);
45 if (err != cudaSuccess) {
46 fprintf(stderr, "Cuda error %d\n", __LINE__);
47 return 1;
48 }
49 err = cudaMalloc((void**)&d_pKernel, nMaskSize * sizeof(d_pKernel[0]));
50 if (err != cudaSuccess) {
51 fprintf(stderr, "Cuda error %d\n", __LINE__);
52 return 1;
53 }
54 // set image to pixval initially
55 err = cudaMemset(d_pSrc, pixval, simgsize);
56 if (err != cudaSuccess) {
57 fprintf(stderr, "Cuda error %d\n", __LINE__);
58 return 1;
59 }
60 err = cudaMemset(d_pDst, 0, dimgsize);
61 if (err != cudaSuccess) {
62 fprintf(stderr, "Cuda error %d\n", __LINE__);
63 return 1;
64 }
65 err = cudaMemcpy(d_pKernel, h_pKernel, nMaskSize * sizeof(d_pKernel[0]),
66 cudaMemcpyHostToDevice);
67 if (err != cudaSuccess) {
68 fprintf(stderr, "Cuda error %d\n", __LINE__);
69 return 1;
70 }
71 // copy src to dst
72 NppStatus ret;
73 #if CUDART_VERSION >= 13000
74 NppStreamContext context;
75 context.hStream = 0; // execute on the default stream
76 ret =
77 nppiFilterRow_8u_C1R_Ctx(d_pSrc, nSrcStep, d_pDst, nDstStep, oROI,
78 d_pKernel, nMaskSize, nAnchor, nDivisor, context);
79
80 #else
81 ret = nppiFilterRow_8u_C1R(d_pSrc, nSrcStep, d_pDst, nDstStep, oROI,
82 d_pKernel, nMaskSize, nAnchor, nDivisor);
83 #endif
84 assert(ret == NPP_NO_ERROR);
85 Npp8u* h_imgres = new Npp8u[dimgpix];
86 err = cudaMemcpy(h_imgres, d_pDst, dimgsize, cudaMemcpyDeviceToHost);
87 if (err != cudaSuccess) {
88 fprintf(stderr, "Cuda error %d\n", __LINE__);
89 return 1;
90 }
91 // test for filtering
92 for (int i = 0; i < dimgpix; i++) {
93 if (h_imgres[i] != (pixval * pixval * nMaskSize)) {
94 fprintf(stderr, "h_imgres at index %d failed to match\n", i);
95 return 1;
96 }
97 }
98
99 return 0;
100 }