|
| 1 | +/** Copyright © 2019-2025 Université de Genève, LMU Munich - Faculty of Physics, IAP-CNRS/Sorbonne Université |
| 2 | + * |
| 3 | + * This library is free software; you can redistribute it and/or modify it under |
| 4 | + * the terms of the GNU Lesser General Public License as published by the Free |
| 5 | + * Software Foundation; either version 3.0 of the License, or (at your option) |
| 6 | + * any later version. |
| 7 | + * |
| 8 | + * This library is distributed in the hope that it will be useful, but WITHOUT |
| 9 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 10 | + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
| 11 | + * details. |
| 12 | + * |
| 13 | + * You should have received a copy of the GNU Lesser General Public License |
| 14 | + * along with this library; if not, write to the Free Software Foundation, Inc., |
| 15 | + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 16 | + */ |
| 17 | + |
| 18 | +/** |
| 19 | + * @file src/program/BenchVariablePsfStack.cpp |
| 20 | + * @date 06/27/25 |
| 21 | + * @author marc schefer |
| 22 | + */ |
| 23 | + |
| 24 | +#include <map> |
| 25 | +#include <string> |
| 26 | +#include <random> |
| 27 | + |
| 28 | +#include <boost/program_options.hpp> |
| 29 | +#include <boost/timer/timer.hpp> |
| 30 | +#include "ElementsKernel/ProgramHeaders.h" |
| 31 | +#include "ElementsKernel/Real.h" |
| 32 | +#include "SEFramework/Psf/VariablePsfStack.h" |
| 33 | + |
| 34 | +namespace po = boost::program_options; |
| 35 | +namespace timer = boost::timer; |
| 36 | +using namespace SourceXtractor; |
| 37 | + |
| 38 | +static Elements::Logging logger = Elements::Logging::getLogger("BenchVariablePsfStack"); |
| 39 | + |
| 40 | +class BenchVariablePsfStack : public Elements::Program { |
| 41 | +private: |
| 42 | + std::default_random_engine random_generator; |
| 43 | + std::uniform_real_distribution<double> random_dist{0.0, 1000.0}; |
| 44 | + |
| 45 | +public: |
| 46 | + |
| 47 | + po::options_description defineSpecificProgramOptions() override { |
| 48 | + po::options_description options{}; |
| 49 | + options.add_options() |
| 50 | + ("iterations", po::value<int>()->default_value(100000), "Number of getPsf calls to benchmark") |
| 51 | + ("measures", po::value<int>()->default_value(3), "Number of timing measurements to take") |
| 52 | + ("fits-file", po::value<std::string>()->default_value(""), "FITS file containing PSF stack"); |
| 53 | + return options; |
| 54 | + } |
| 55 | + |
| 56 | + Elements::ExitCode mainMethod(std::map<std::string, po::variable_value> &args) override { |
| 57 | + |
| 58 | + auto iterations = args["iterations"].as<int>(); |
| 59 | + auto measures = args["measures"].as<int>(); |
| 60 | + auto fits_file = args["fits-file"].as<std::string>(); |
| 61 | + |
| 62 | + logger.info() << "Benchmarking VariablePsfStack::getPsf() with " << iterations << " iterations"; |
| 63 | + logger.info() << "Taking " << measures << " timing measurements"; |
| 64 | + |
| 65 | + // Initialize VariablePsfStack with FITS file if provided, otherwise nullptr |
| 66 | + std::shared_ptr<CCfits::FITS> fitsPtr = nullptr; |
| 67 | + if (!fits_file.empty()) { |
| 68 | + try { |
| 69 | + fitsPtr = std::make_shared<CCfits::FITS>(fits_file); |
| 70 | + logger.info() << "Using FITS file: " << fits_file; |
| 71 | + } catch (const std::exception& e) { |
| 72 | + logger.error() << "Failed to load FITS file '" << fits_file << "': " << e.what(); |
| 73 | + return Elements::ExitCode::DATAERR; |
| 74 | + } |
| 75 | + } else { |
| 76 | + logger.error() << "No FITS file provided"; |
| 77 | + return Elements::ExitCode::USAGE; |
| 78 | + } |
| 79 | + |
| 80 | + try { |
| 81 | + VariablePsfStack psfStack(fitsPtr); |
| 82 | + |
| 83 | + logger.info() << "VariablePsfStack loaded successfully with " << psfStack.getNumberOfPsfs() << " PSFs"; |
| 84 | + logger.info() << "PSF size: " << psfStack.getWidth() << "x" << psfStack.getHeight(); |
| 85 | + logger.info() << "Pixel sampling: " << psfStack.getPixelSampling(); |
| 86 | + |
| 87 | + std::cout << "Iterations,Measurement,Time_nanoseconds" << std::endl; |
| 88 | + |
| 89 | + for (int m = 0; m < measures; ++m) { |
| 90 | + logger.info() << "Measurement " << (m + 1) << "/" << measures; |
| 91 | + |
| 92 | + timer::cpu_timer timer; |
| 93 | + timer.stop(); |
| 94 | + |
| 95 | + // Prepare test values for getPsf calls |
| 96 | + std::vector<std::vector<double>> testValues; |
| 97 | + testValues.reserve(iterations); |
| 98 | + |
| 99 | + for (int i = 0; i < iterations; ++i) { |
| 100 | + testValues.push_back({random_dist(random_generator), random_dist(random_generator)}); |
| 101 | + } |
| 102 | + |
| 103 | + // Start timing |
| 104 | + timer.start(); |
| 105 | + |
| 106 | + for (int i = 0; i < iterations; ++i) { |
| 107 | + try { |
| 108 | + auto psf = psfStack.getPsf(testValues[i]); |
| 109 | + // Prevent compiler optimization by using the result |
| 110 | + volatile auto width = psf->getWidth(); |
| 111 | + (void)width; // Suppress unused variable warning |
| 112 | + } catch (const std::exception& e) { |
| 113 | + // Expected to fail with nullptr, but we still measure the timing |
| 114 | + // until the exception is thrown |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + timer.stop(); |
| 119 | + |
| 120 | + auto elapsed_ns = timer.elapsed().wall; |
| 121 | + std::cout << iterations << "," << (m + 1) << "," << elapsed_ns << std::endl; |
| 122 | + |
| 123 | + logger.info() << "Time for " << iterations << " calls: " << (elapsed_ns / 1e9) << " seconds"; |
| 124 | + logger.info() << "Average time per call: " << (elapsed_ns / iterations) << " nanoseconds"; |
| 125 | + } |
| 126 | + |
| 127 | + } catch (const std::exception& e) { |
| 128 | + logger.error() << "Error initializing VariablePsfStack: " << e.what(); |
| 129 | + return Elements::ExitCode::DATAERR; |
| 130 | + } |
| 131 | + |
| 132 | + return Elements::ExitCode::OK; |
| 133 | + } |
| 134 | +}; |
| 135 | + |
| 136 | +MAIN_FOR(BenchVariablePsfStack) |
0 commit comments