Commit f206996d authored by Alejandro Homs Puron's avatar Alejandro Homs Puron Committed by Generic Bliss account for Control Software
Browse files

[PROC][FAI] Move OpenCL helpers from peak_finder.hpp to opencl.hpp

parent a020ccc3
// Copyright (C) 2020 Samuel Debionne, ESRF.
// Use, modification and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#pragma once
#include <filesystem>
#include <boost/compute.hpp>
#include <lima/exceptions.hpp>
namespace lima
{
namespace processing::fai
{
namespace bcl = boost::compute;
inline auto build_with_source_file(bcl::context& context, const std::filesystem::path& cl_source_path,
const std::string& file, const std::string& options = std::string())
{
std::string final_options = options;
auto file_path = file;
if (!cl_source_path.string().empty()) {
std::string sep = final_options.empty() ? "" : " ";
final_options += sep + "-I" + cl_source_path.string();
file_path = cl_source_path / file;
}
bcl::program res = bcl::program::create_with_source_file(file_path, context);
try {
// attempt to compile to program
res.build(final_options);
} catch (boost::compute::opencl_error& e) {
// program failed to compile, print out the build log
std::cerr << res.build_log();
LIMA_THROW_EXCEPTION(std::runtime_error(res.build_log()));
}
return res;
}
} // namespace processing::fai
} // namespace lima
......@@ -10,13 +10,10 @@
#include <functional>
#include <memory>
#include <filesystem>
#include <boost/compute.hpp>
#include <lima/exceptions.hpp>
#include <lima/logging.hpp>
#include <lima/processing/fai/opencl.hpp>
#include <lima/processing/fai/enums.hpp>
#include <lima/processing/fai/typedefs.hpp>
......@@ -24,30 +21,7 @@ namespace lima
{
namespace processing::fai
{
namespace bcl = boost::compute;
inline auto build_with_source_file(bcl::context& context, const std::filesystem::path& cl_source_path,
const std::string& file, const std::string& options = std::string())
{
std::string final_options = options;
auto file_path = file;
if (!cl_source_path.string().empty()) {
std::string sep = final_options.empty() ? "" : " ";
final_options += sep + "-I" + cl_source_path.string();
file_path = cl_source_path / file;
}
bcl::program res = bcl::program::create_with_source_file(file_path, context);
try {
// attempt to compile to program
res.build(final_options);
} catch (boost::compute::opencl_error& e) {
// program failed to compile, print out the build log
std::cerr << res.build_log();
LIMA_THROW_EXCEPTION(std::runtime_error(res.build_log()));
}
return res;
}
using pf_ret_t = std::tuple<std::size_t, std::size_t>;
inline auto make_peak_finder(bcl::context& context, //
std::size_t width, std::size_t height, //
......@@ -81,24 +55,23 @@ namespace processing::fai
// Preconditions
auto build_program = [&cl_source_path](auto&& context, auto&& file,
const std::string& options = std::string()) {
auto build_program = [&context, &cl_source_path](auto&& file, const std::string& options = std::string()) {
return build_with_source_file(context, cl_source_path, file, options);
};
// Stage 0 - Initialization
auto stage0_program = build_program(context, "init.cl");
auto stage0_program = build_program("init.cl");
bcl::kernel stage0(stage0_program, "init");
// Stage 1 - Pedestal gain correction
auto stage1_program = build_program(context, "pedestal_gain_correction.cl");
auto stage1_program = build_program("pedestal_gain_correction.cl");
bcl::vector<float> gain_d(gain, queue); //!< Gain device buffer
bcl::vector<float> pedestal_d(pedestal, queue); //!< Pedestal device buffer
bcl::vector<float> corrected_d(nb_pixels, context); //!< Gain / pedestal corrected data
bcl::kernel stage1(stage1_program, "pedestal_gain_correction");
// Stage 2 - Preprocessing
auto stage2_program = build_program(context, "preprocess.cl");
auto stage2_program = build_program("preprocess.cl");
bcl::vector<float> variance_d(variance, queue);
bcl::vector<float> dark_d(dark, queue);
bcl::vector<float> dark_variance_d(dark_variance, queue);
......@@ -122,7 +95,7 @@ namespace processing::fai
bcl::kernel stage2(stage2_program, "corrections4");
// Stage 3 - Sigma clipping
auto stage3_program = build_program(context, "csr_sigma_clip.cl", "-D WORKGROUP_SIZE=32");
auto stage3_program = build_program("csr_sigma_clip.cl", "-D WORKGROUP_SIZE=32");
bcl::vector<float> csr_data_d(csr_data, queue);
bcl::vector<int> csr_indices_d(csr_indices, queue);
bcl::vector<int> csr_indptr_d(csr_indptr, queue);
......@@ -134,7 +107,7 @@ namespace processing::fai
// Stage 4 - Peak finding
constexpr int peak_finder_wg_size = 1024;
std::string wg_option = "-D WORKGROUP_SIZE=" + std::to_string(peak_finder_wg_size);
auto stage4_program = build_program(context, "peak_finder.cl", wg_option.c_str());
auto stage4_program = build_program("peak_finder.cl", wg_option.c_str());
bcl::vector<float> radius2d_d(radius2d, queue);
bcl::vector<float> radius1d_d(radius1d, queue);
......@@ -265,17 +238,16 @@ namespace processing::fai
};
//using peak_finder = decltype(make_peak_finder(std::declval<bcl::context>(), std::declval<bcl::command_queue>()));
using ret_t = std::tuple<std::size_t, std::size_t>;
using peak_finder = std::function<ret_t(bcl::command_queue&, //
vector<std::uint16_t> const&, // Input
vector<float>& corrected_dbg, // Corrected
vector<bcl::float4_>& preprocessed_dbg, // Preprocessed
vector<bcl::float8_>& cliped_dbg, // Summed (after sigma clip)
vector<bcl::float4_>& found_dbg, // Finded (after peak find)
vector<int>&, vector<float>&, // Outputs Peaks
vector<float>&, vector<float>&, // Background
vector<float>&, vector<float>&, bool // Accumulation
)>;
using peak_finder = std::function<pf_ret_t(bcl::command_queue&, //
vector<std::uint16_t> const&, // Input
vector<float>& corrected_dbg, // Corrected
vector<bcl::float4_>& preprocessed_dbg, // Preprocessed
vector<bcl::float8_>& cliped_dbg, // Summed (after sigma clip)
vector<bcl::float4_>& found_dbg, // Finded (after peak find)
vector<int>&, vector<float>&, // Outputs Peaks
vector<float>&, vector<float>&, // Background
vector<float>&, vector<float>&, bool // Accumulation
)>;
} // namespace processing::fai
} // namespace lima
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment