Skip to content
Snippets Groups Projects
Commit fe2606d5 authored by Sarod Yatawatta's avatar Sarod Yatawatta Committed by Sarod Yatawatta
Browse files

add h5parm singleton

parent 791b90f3
No related branches found
No related tags found
No related merge requests found
......@@ -315,6 +315,20 @@ SagecalPredict::BeamDataSingle::~BeamDataSingle() {
}
#endif /* HAVE_LIBDIRAC || HAVE_LIBDIRAC_CUDA */
schaapcommon::h5parm::H5Parm& SagecalPredict::H5ParmSingle::open_file(
const std::string& h5_name) {
std::lock_guard<std::mutex> lock(mutex_);
if (is_opened_) {
return h5_parm_;
}
h5_parm_ = H5Parm(h5_name, false);
is_opened_ = true;
return h5_parm_;
}
void SagecalPredict::SetOperation(const std::string& operation) {
if (operation == "replace") {
operation_ = Operation::kReplace;
......@@ -434,7 +448,9 @@ void SagecalPredict::init(
}
std::vector<std::string> h5directions;
if (parm_on_disk_) {
h5_parm_ = H5Parm(h5_name_, false);
// Create a singleton to only open and read the H5 file once
h5_parm_reference_ = SagecalPredict::H5ParmSingle::get_instance();
h5_parm_ = h5_parm_reference_->open_file(h5_name_);
// Check to see soltab is initialized at the constructor
if (soltab_name_.empty()) {
soltab_name_ = parset.getString(prefix + "applycal.correction");
......
......@@ -129,6 +129,29 @@ class SagecalPredict : public ModelDataStep {
};
#endif /* HAVE_LIBDIRAC || HAVE_LIBDIRAC_CUDA */
// Singleton to open and read H5 file only by one thread
// But H5Parm itself is assumed thread-safe
class H5ParmSingle {
private:
explicit H5ParmSingle() : is_opened_(false) {}
std::mutex mutex_;
bool is_opened_;
public:
H5ParmSingle(H5ParmSingle const&) = delete;
H5ParmSingle& operator=(H5ParmSingle const&) = delete;
~H5ParmSingle(){};
static H5ParmSingle* get_instance() {
static H5ParmSingle instance;
return &instance;
}
schaapcommon::h5parm::H5Parm& open_file(const std::string& h5_name);
schaapcommon::h5parm::H5Parm h5_parm_;
};
public:
SagecalPredict(const common::ParameterSet&, const std::string& prefix,
MsType input_type = MsType::kRegular);
......@@ -203,6 +226,7 @@ class SagecalPredict : public ModelDataStep {
// H5 solutions handling
schaapcommon::h5parm::H5Parm h5_parm_;
H5ParmSingle* h5_parm_reference_{nullptr};
std::string solset_name_;
std::string soltab_name_;
bool invert_{false};
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment