map_resources_scope.h 810 Bytes
Newer Older
Nianchen Deng's avatar
sync    
Nianchen Deng committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <cuda_runtime.h>
#include <vector>
#include "error.h"

namespace utils::cuda {
    class MapResourcesScope {
    public:
        MapResourcesScope(const std::vector<cudaGraphicsResource_t> &resources,
                          cudaStream_t stream = nullptr)
            : _resources(resources), _stream(stream) {
            if (!_resources.empty())
                THROW_IF_FAILED(
                    cudaGraphicsMapResources((int)_resources.size(), _resources.data(), _stream));
        }

        ~MapResourcesScope() {
            if (!_resources.empty())
                cudaGraphicsUnmapResources((int)_resources.size(), _resources.data(), _stream);
        }

    private:
        std::vector<cudaGraphicsResource_t> _resources;
        cudaStream_t _stream;
    };
} // namespace utils::cuda