opr_impl.h 1.7 KB
Newer Older
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
/**
 * \file dnn/src/cuda/rng/opr_impl.h
 * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
 *
 * Copyright (c) 2014-2020 Megvii Inc. All rights reserved.
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 */
#pragma once

#include "megdnn/oprs.h"
#include "src/cuda/handle.h"
#include <curand.h>

namespace megdnn {
namespace cuda {

class CuRandHandle {
    curandGenerator_t m_gen;
    uint64_t m_seed;

    CuRandHandle(const CuRandHandle&) = delete;
    CuRandHandle& operator = (const CuRandHandle&) = delete;

    public:
        CuRandHandle(cudaStream_t stream, uint64_t seed = 0);
        ~CuRandHandle();

        void seed(uint64_t seed);

        curandGenerator_t gen() const {
            return m_gen;
        }

        void ensure_seed(uint64_t seed) {
            if (m_seed != seed) {
                this->seed(seed);
            }
        }
};

class UniformRNGImpl: public UniformRNG {
    CuRandHandle m_curand_handle;

    public:
        UniformRNGImpl(Handle *handle);
        void exec(_megdnn_tensor_inout dst, _megdnn_workspace) override;

        size_t get_workspace_in_bytes(const TensorLayout&) override {
            return 0;
        }
};

class GaussianRNGImpl: public GaussianRNG {
    CuRandHandle m_curand_handle;

    public:
        GaussianRNGImpl(Handle *handle);


        void exec(_megdnn_tensor_inout dst, _megdnn_workspace) override;

        size_t get_workspace_in_bytes(const TensorLayout &layout) override;
};


} // namespace cuda
} // namespace megdnn
// vim: syntax=cpp.doxygen