File tree Expand file tree Collapse file tree 2 files changed +29
-1
lines changed
Expand file tree Collapse file tree 2 files changed +29
-1
lines changed Original file line number Diff line number Diff line change @@ -18,6 +18,8 @@ lazy_static! {
1818 "pgml.huggingface_trust_remote_code_whitelist" ,
1919 GucSetting :: <Option <& ' static CStr >>:: new( None ) ,
2020 ) ;
21+ pub static ref PGML_OMP_NUM_THREADS : ( & ' static str , GucSetting <i32 >) =
22+ ( "pgml.omp_num_threads" , GucSetting :: <i32 >:: new( -1 ) ) ;
2123}
2224
2325pub fn initialize_server_params ( ) {
@@ -53,6 +55,16 @@ pub fn initialize_server_params() {
5355 GucContext :: Userset ,
5456 GucFlags :: default ( ) ,
5557 ) ;
58+ GucRegistry :: define_int_guc (
59+ PGML_OMP_NUM_THREADS . 0 ,
60+ "Specifies the number of threads used by default of underlying OpenMP library. Only positive integers are valid" ,
61+ "" ,
62+ & PGML_OMP_NUM_THREADS . 1 ,
63+ -1 ,
64+ i32:: max_value ( ) ,
65+ GucContext :: Backend ,
66+ GucFlags :: default ( ) ,
67+ ) ;
5668}
5769
5870#[ cfg( any( test, feature = "pg_test" ) ) ]
@@ -75,4 +87,10 @@ mod tests {
7587 set_config ( name, value) . unwrap ( ) ;
7688 assert_eq ! ( PGML_HF_WHITELIST . 1 . get( ) . unwrap( ) . to_string_lossy( ) , value) ;
7789 }
90+
91+ #[ pg_test]
92+ fn omp_num_threads_cannot_be_set_after_startup ( ) {
93+ let result = std:: panic:: catch_unwind ( || set_config ( "pgml.omp_num_threads" , "1" ) ) ;
94+ assert ! ( result. is_err( ) ) ;
95+ }
7896}
Original file line number Diff line number Diff line change @@ -21,10 +21,20 @@ pg_module_magic!();
2121
2222extension_sql_file ! ( "../sql/schema.sql" , name = "schema" ) ;
2323
24+ extern "C" {
25+ fn omp_set_num_threads ( num_threads : i32 ) ;
26+ }
27+
2428#[ cfg( not( feature = "use_as_lib" ) ) ]
2529#[ pg_guard]
2630pub extern "C" fn _PG_init ( ) {
2731 config:: initialize_server_params ( ) ;
32+ let omp_num_threads = config:: PGML_OMP_NUM_THREADS . 1 . get ( ) ;
33+ if omp_num_threads > 0 {
34+ unsafe {
35+ omp_set_num_threads ( omp_num_threads) ;
36+ }
37+ }
2838 bindings:: python:: activate ( ) . expect ( "Error setting python venv" ) ;
2939 orm:: project:: init ( ) ;
3040}
@@ -54,7 +64,7 @@ pub mod pg_test {
5464
5565 pub fn postgresql_conf_options ( ) -> Vec < & ' static str > {
5666 // return any postgresql.conf settings that are required for your tests
57- let mut options = vec ! [ "shared_preload_libraries = 'pgml'" ] ;
67+ let mut options = vec ! [ "shared_preload_libraries = 'pgml'" , "pgml.omp_num_threads = '1'" ] ;
5868 if let Some ( venv) = option_env ! ( "PGML_VENV" ) {
5969 let option = format ! ( "pgml.venv = '{venv}'" ) ;
6070 options. push ( Box :: leak ( option. into_boxed_str ( ) ) ) ;
You can’t perform that action at this time.
0 commit comments