🌐 AI搜索 & 代理 主页
Skip to content

Commit 58e2aec

Browse files
higuoxingXuebin Suxuebinsu
committed
Allow user to limit the number of threads that OpenMP spawns.
This patch introduces a new GUC parameter pgml.omp_num_threads to control control the number of threads that OpenMP can spawn. Co-authored-by: Xuebin Su <sxuebin@vmware.com> Co-authored-by: Xuebin Su (苏学斌) <12034000+xuebinsu@users.noreply.github.com>
1 parent 4612b4f commit 58e2aec

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

pgml-extension/src/config.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff 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

2325
pub 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
}

pgml-extension/src/lib.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,20 @@ pg_module_magic!();
2121

2222
extension_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]
2630
pub 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()));

0 commit comments

Comments
 (0)