RockyML  0.0.1
A High-Performance Scientific Computing Framework
system.h
1 /*
2  Copyright (C) 2022 Amirabbas Asadi , All Rights Reserved
3  distributed under Apache-2.0 license
4 */
5 #ifndef ROCKY_ZAGROS_SYSTEM_GUARD
6 #define ROCKY_ZAGROS_SYSTEM_GUARD
7 #include<iostream>
8 #include<string>
9 #include<memory>
10 
11 #include<tbb/tbb.h>
12 
13 #include "spdlog/spdlog.h"
14 
15 
16 namespace rocky{
17 namespace zagros{
18 
19 template<typename T_e>
20 class system{
21 public:
22  virtual T_e objective(T_e* params) = 0;
28  virtual T_e lower_bound(){ return -1.0;}
35  virtual T_e lower_bound(int p_index){ return lower_bound();}
41  virtual T_e upper_bound(){ return 1.0;}
48  virtual T_e upper_bound(int p_index){ return upper_bound();}
49  virtual std::string to_string(){
50  return "optimization problem";
51  }
52 };
53 
58 template<typename T_e>
59 class blocked_system: public system<T_e>{
60 public:
61  int block_dim_;
62  int original_dim_;
63  // thread-specific solution states provided by the runtime
64  tbb::enumerable_thread_specific<std::vector<T_e>>* solution_state_;
65  // main system
66  system<T_e>* main_system_;
67  // block mask
68  int* bcd_mask_;
69 
70  int original_dim() const{
71  return original_dim_;
72  }
73 
74  int block_dim() const{
75  return block_dim_;
76  }
77 
78  blocked_system(system<T_e>* main_system, int original_dim, int block_dim, int* mask){
79  this->main_system_ = main_system;
80  this->original_dim_ = original_dim;
81  this->block_dim_ = block_dim;
82  this->bcd_mask_ = mask;
83  }
84  // change the solution state
85  void set_solution_state(tbb::enumerable_thread_specific<std::vector<T_e>>* solution_state){
86  this->solution_state_ = solution_state;
87  }
88  virtual T_e objective(T_e* partial){
89  // get a thread specific solution
90  T_e* full_solution = this->solution_state_->local().data();
91  // copy the partial solution to the full solution
92  for(int i=0; i<block_dim_; i++)
93  full_solution[bcd_mask_[i]] = partial[i];
94  // evaluate the full solution
95  return main_system_->objective(full_solution);
96  }
102  virtual T_e lower_bound(){ return this->main_system_->lower_bound();}
109  virtual T_e lower_bound(int p_index){ return this->main_system_->lower_bound(bcd_mask_[p_index]);}
115  virtual T_e upper_bound(){ return this->main_system_->upper_bound();}
122  virtual T_e upper_bound(int p_index){ return this->main_system_->upper_bound(bcd_mask_[p_index]);}
123 };
124 
125 }; // end of zagros namespace
126 }; // end of rocky namespace
127 #endif
rocky::zagros::blocked_system::lower_bound
virtual T_e lower_bound()
lower bound specification should be used when lower bound is same for all parameters
Definition: system.h:102
rocky::zagros::blocked_system
a virtual system to implement blocked coordinate descent
Definition: system.h:59
rocky::zagros::system::upper_bound
virtual T_e upper_bound(int p_index)
upper bound specification for each parameter should be used if parameters have different upper bounds
Definition: system.h:48
rocky::zagros::blocked_system::lower_bound
virtual T_e lower_bound(int p_index)
lower bound specification for each parameter should be used if parameters have different lower bounds
Definition: system.h:109
rocky::zagros::system
Definition: system.h:20
rocky::zagros::blocked_system::upper_bound
virtual T_e upper_bound(int p_index)
upper bound specification for each parameter should be used if parameters have different upper bounds
Definition: system.h:122
rocky::zagros::system::upper_bound
virtual T_e upper_bound()
upper bound specification should be used when upper bound is same for all parameters
Definition: system.h:41
rocky::zagros::system::lower_bound
virtual T_e lower_bound()
lower bound specification should be used when lower bound is same for all parameters
Definition: system.h:28
rocky::zagros::blocked_system::upper_bound
virtual T_e upper_bound()
upper bound specification should be used when upper bound is same for all parameters
Definition: system.h:115
rocky::zagros::system::lower_bound
virtual T_e lower_bound(int p_index)
lower bound specification for each parameter should be used if parameters have different lower bounds
Definition: system.h:35