-
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Threadpool: remove stdlib dependencies (#293)
* Address part of #291, detect physical cores, note: Linux limited to 64 cores at the moment. * change getNumPhysicalCores to return int instead of int32 * renaming cpuinfo -> cpudetect * system/ansi_c dependency cleanup * forgot to delete renamed cpuinfo_x86 * dump WIP AMD CPUID detection [skip ci] * Export available threads at the OS to Nim/C/Rust + Rust constantine-core crate * cpudetect: CPUID function update * windows barrier exporting WinBool twice * fix threadpool.new type changes * cpudetect: workaround Nim flaky asm interpolation - similar to nim-lang/Nim#23114 * topology: BSD/MacOS fused + no libc dependency + sysctl auto ID / dynamic ID handling * topology BSD: reorg decls * cpudetect x86: cleanup * topology BSD: try to fix import macro define * topo BSD: defines from C macros needs to be imported as global
- Loading branch information
Showing
37 changed files
with
857 additions
and
165 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
benchmarks-threadpool/bouncing_producer_consumer/threadpool_bpc.nim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[package] | ||
name = "constantine-core" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
authors = ["Mamy André-Ratsimbazafy"] | ||
license = "MIT/Apache-2.0" | ||
repository = "https://github.com/mratsim/constantine" | ||
|
||
[dependencies] | ||
constantine-sys = { path = "../constantine-sys" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
//! Constantine | ||
//! Copyright (c) 2018-2019 Status Research & Development GmbH | ||
//! Copyright (c) 2020-Present Mamy André-Ratsimbazafy | ||
//! Licensed and distributed under either of | ||
//! * MIT license (license terms in the root directory or at http://opensource.org/licenses/MIT). | ||
//! * Apache v2 license (license terms in the root directory or at http://www.apache.org/licenses/LICENSE-2.0). | ||
//! at your option. This file may not be copied, modified, or distributed except according to those terms. | ||
|
||
use constantine_sys::*; | ||
|
||
// Cryptographically secure RNGs | ||
// ------------------------------------------------------------ | ||
|
||
pub mod csprngs { | ||
use constantine_sys::ctt_csprng_sysrand; | ||
use core::ffi::c_void; | ||
#[inline(always)] | ||
pub fn sysrand(buffer: &mut [u8]) { | ||
unsafe { | ||
ctt_csprng_sysrand(buffer.as_mut_ptr() as *mut c_void, buffer.len()); | ||
} | ||
} | ||
} | ||
|
||
// Hardware detection | ||
// ------------------------------------------------------------ | ||
|
||
pub mod hardware { | ||
use constantine_sys::ctt_cpu_get_num_threads_os; | ||
#[inline(always)] | ||
#[doc = " Query the number of threads available at the OS-level\n to run computations.\n\n This takes into account cores disabled at the OS-level, for example in a VM.\n However this doesn't detect restrictions based on time quotas often used for Docker\n or taskset / cpuset restrictions from cgroups.\n\n For Simultaneous-Multithreading (SMT often call HyperThreading),\n this returns the number of available logical cores."] | ||
pub fn get_num_threads_os() -> usize { | ||
unsafe { ctt_cpu_get_num_threads_os() }.try_into().unwrap() | ||
} | ||
} | ||
|
||
// Threadpool | ||
// ------------------------------------------------------------ | ||
|
||
#[derive(Debug)] | ||
pub struct Threadpool { | ||
ctx: *mut ctt_threadpool, | ||
} | ||
|
||
impl Threadpool { | ||
/// Instantiate a new Threadpool with `num_threads` threads. | ||
/// A single threadpool can be active on a given thread. | ||
/// A new threadpool may be instantiated if the previous one has been shutdown. | ||
#[inline(always)] | ||
pub fn new(num_threads: usize) -> Self { | ||
let ctx = unsafe { ctt_threadpool_new(num_threads.try_into().unwrap()) }; | ||
Self { ctx } | ||
} | ||
|
||
/// Access the private context of Threadpool | ||
/// For use, only in Constantine's crates. | ||
/// No guarantee of continuous support. | ||
#[inline(always)] | ||
pub fn get_private_context(&self) -> *mut ctt_threadpool { | ||
self.ctx | ||
} | ||
} | ||
|
||
impl Drop for Threadpool { | ||
#[inline(always)] | ||
fn drop(&mut self) { | ||
unsafe { ctt_threadpool_shutdown(self.ctx) } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.