.. _program_listing_file_include_util_function_stft_OpenclBackend.hpp: Program Listing for File OpenclBackend.hpp ========================================== |exhale_lsh| :ref:`Return to documentation for file ` (``include\util\function\stft\OpenclBackend.hpp``) .. |exhale_lsh| unicode:: U+021B0 .. UPWARDS ARROW WITH TIP LEFTWARDS .. code-block:: cpp #pragma once #include "STFT_Parallel.hpp" #include #include #include #include #include #include #include #include #include #include CMRC_DECLARE(pdje_okl); namespace PDJE_PARALLEL { using namespace cl; using REAL_VEC = std::vector; using IMAG_VEC = std::vector; class OPENCL_STFT final : public IStftBackend { private: static constexpr uint32_t kMelBins = 80; static constexpr int kDefaultSampleRate = 48000; uint32_t prev_origin_size = 0; uint32_t prev_overlap_fullsize = 0; uint32_t prev_overlap_subbuffer_fullsize = 0; uint32_t prev_bin_fullsize = 0; uint32_t prev_mel_fullsize = 0; int prev_fft_size = 0; Program opencl_kernel_code; struct { std::optional EXP6STFT; std::optional EXP7STFT; std::optional EXP8STFT; std::optional EXP9STFT; std::optional EXP10STFT; std::optional EXP11STFT; std::optional EXPCommon; std::optional Overlap; std::optional DCRemove; std::optional Hanning; std::optional Hamming; std::optional Blackman; std::optional Nuttall; std::optional Blackman_Nuttall; std::optional Blackman_Harris; std::optional FlatTop; std::optional Gaussian; std::optional toBinOnly; std::optional BinPowerChain; std::optional toPower; std::optional MelScale; std::optional MelDBChain; std::optional toDB; } built_kernels; struct { std::optional origin; std::optional real; std::optional imag; std::optional subreal; std::optional subimag; std::optional bin_real; std::optional bin_imag; std::optional power; std::optional mel; std::optional mel_filter_bank; } memories; std::optional gpu; std::optional CQ; std::optional gpu_ctxt; std::optional gpu_codes; std::vector mel_filter_bank_host; bool SetMemory(const uint32_t origin_cpu_memory_sz, const StftArgs &args, const POST_PROCESS &post_process, const bool needSubBuffer); void EnsureMelFilterBank(int windowSize); bool GetResult() { if (CQ->flush() != CL_SUCCESS) { return false; } if (CQ->finish() != CL_SUCCESS) { return false; } return true; } public: StftResult Execute(REAL_VEC &origin_cpu_memory, WINDOW_LIST window, POST_PROCESS post_process, unsigned int win_expsz, const StftArgs &args) override; OPENCL_STFT() { std::vector platforms; int device_power_score = 0; cl::Platform::get(&platforms); for (auto &i : platforms) { std::vector calc_devs; i.getDevices(CL_DEVICE_TYPE_ALL, &calc_devs); for (auto target_dev : calc_devs) { int local_power_score = 0; target_dev.getInfo(CL_DEVICE_MAX_COMPUTE_UNITS, &local_power_score); if (local_power_score > device_power_score) { gpu = std::move(target_dev); device_power_score = local_power_score; } } } if (device_power_score == 0 || !gpu.has_value()) { throw std::runtime_error("failed to load opencl device."); } else { gpu_ctxt = cl::Context(gpu.value()); } auto fs = cmrc::pdje_okl::get_filesystem(); auto file = fs.open("STFT_MAIN.cl"); std::string cl_codes(file.begin(), file.end()); gpu_codes.emplace(gpu_ctxt.value(), cl_codes); if (gpu_codes->build(gpu.value()) != CL_SUCCESS) { throw std::runtime_error("failed to build cl kernel codes."); } CQ.emplace(gpu_ctxt.value(), gpu.value()); } ~OPENCL_STFT() override; }; } // namespace PDJE_PARALLEL