Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cargo: Create workspace for all crates; fmt all crates #18

Merged
merged 3 commits into from
Jun 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 15 additions & 19 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,15 @@ jobs:
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
- run: wget -O ispc.tar.gz https://github.com/ispc/ispc/releases/download/v${ISPC_VERSION}/ispc-v${ISPC_VERSION}-linux.tar.gz
- run: tar -xf ispc.tar.gz
- run: echo "PATH=$PATH:`pwd`/ispc-v${ISPC_VERSION}-linux/bin/" >> $GITHUB_ENV
- run: cargo build
- run: cargo test
- run: cargo doc
- run: scripts/build-examples-linux.sh
- run: curl -L https://github.com/ispc/ispc/releases/download/v${{ env.ISPC_VERSION }}/ispc-v${{ env.ISPC_VERSION }}-linux.tar.gz | tar xzv ispc-v${{ env.ISPC_VERSION }}-linux/bin/ispc
- run: realpath "ispc-v${{ env.ISPC_VERSION }}-linux/bin/" >> $GITHUB_PATH
- run: cargo build --all --all-targets --features ispc
- run: cargo test --all
- run: cargo doc --all --no-deps --document-private-items --all-features
env:
RUSTDOCFLAGS: -Dwarnings
- name: Format Core
run: cargo fmt -- --check
- name: Format Examples
run: scripts/check-examples-formatting.sh
build_mac:
runs-on: macos-latest
steps:
Expand All @@ -30,20 +28,18 @@ jobs:
with:
toolchain: stable
- run: brew install ispc
- run: cargo build
- run: cargo test
- run: scripts/build-examples-linux.sh
- run: cargo build --all --all-targets --features ispc
- run: cargo test --all
build_windows:
runs-on: windows-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
- run: choco install wget
- run: wget -O LLVM-11.0.0.7z https://www.dl.dropboxusercontent.com/s/vontvxjexyk417e/LLVM-11.0.0.7z
- run: 7z x LLVM-11.0.0.7z -y;
- run: wget -O ispc.zip https://github.com/ispc/ispc/releases/download/v${env:ISPC_VERSION}/ispc-v${env:ISPC_VERSION}-windows.zip
- run: 7z x ispc.zip -y -o"ispc"
- run: scripts/build-test-windows.ps1

- run: |
curl -LO https://github.com/ispc/ispc/releases/download/v${{ env.ISPC_VERSION }}/ispc-v${{ env.ISPC_VERSION }}-windows.zip
unzip ispc-v${{ env.ISPC_VERSION }}-windows.zip bin/ispc.exe
Twinklebear marked this conversation as resolved.
Show resolved Hide resolved
Resolve-Path "./bin" | Add-Content -Path $env:GITHUB_PATH
- run: cargo build --all --all-targets --features ispc
- run: cargo test --all
15 changes: 11 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ description = """
A build-time dependency for Cargo build scripts to help with compiling
and linking to ISPC code and generating Rust bindings for the resulting library.
This crate is a meta-crate for the ispc_compile and ispc_rt crates, which provide
the actual functionality for building ISPC code and generating bindgs, and
the actual functionality for building ISPC code and generating bindings, and
importing those bindings into Rust.
"""
keywords = ["build-dependencies", "ispc", "simd"]
Expand All @@ -20,12 +20,19 @@ exclude = [
".travis.yml",
"*.png",
".gitignore",
".github",
"scripts/*",
"examples/*"
".github",
"scripts/*",
"examples/*",
]

[dependencies]
ispc_compile = { path = "./compile/", version = "1.0.14" }
ispc_rt = { path = "./runtime/", version = "1.0.7" }

[workspace]
resolver = "2"
members = [
"compile",
"runtime",
"examples/*",
]
4 changes: 2 additions & 2 deletions compile/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ exclude = [
".travis.yml",
"*.png",
".gitignore",
".github",
"scripts/*",
".github",
"scripts/*",
"examples/*"
]

Expand Down
4 changes: 2 additions & 2 deletions runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ exclude = [
".travis.yml",
"*.png",
".gitignore",
".github",
"scripts/*",
".github",
"scripts/*",
"examples/*"
]

Expand Down
90 changes: 68 additions & 22 deletions runtime/src/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
use libc;
use num_cpus;

use std::time::Duration;
use std::cell::RefCell;
use std::sync::{Arc, RwLock, Mutex};
use std::sync::atomic::{self, AtomicUsize};
use std::sync::{Arc, Mutex, RwLock};
use std::thread::{self, JoinHandle};
use std::time::Duration;

use task::{ISPCTaskFn, Context};
use task::{Context, ISPCTaskFn};

/// Trait to be implemented to provide ISPC task execution functionality.
///
Expand All @@ -26,7 +26,12 @@ pub trait TaskSystem {
/// The `handle_ptr` should be set to some context tracking facility so that you can later
/// track task groups launched in the context and perform finer grained synchronization in
/// `sync`.
unsafe fn alloc(&self, handle_ptr: *mut *mut libc::c_void, size: i64, align: i32) -> *mut libc::c_void;
unsafe fn alloc(
&self,
handle_ptr: *mut *mut libc::c_void,
size: i64,
align: i32,
) -> *mut libc::c_void;
/// Launch is called when a new group of tasks is being launched and should schedule them to
/// be executed in some way.
///
Expand All @@ -50,8 +55,15 @@ pub trait TaskSystem {
/// }
/// }
/// ```
unsafe fn launch(&self, handle_ptr: *mut *mut libc::c_void, f: ISPCTaskFn, data: *mut libc::c_void,
count0: i32, count1: i32, count2: i32);
unsafe fn launch(
&self,
handle_ptr: *mut *mut libc::c_void,
f: ISPCTaskFn,
data: *mut libc::c_void,
count0: i32,
count1: i32,
count2: i32,
);
/// Synchronize an execution context with the tasks it's launched. Use `handle` to determine
/// the task context that's being synchronized.
///
Expand All @@ -77,24 +89,29 @@ pub struct Parallel {
impl Parallel {
/// Create a parallel task execution environment that will use `num_cpus` threads
/// to run tasks.
pub fn new() -> Arc<Parallel> { Parallel::oversubscribed(1.0) }
pub fn new() -> Arc<Parallel> {
Parallel::oversubscribed(1.0)
}
/// Create an oversubscribued parallel task execution environment that will use
/// `oversubscribe * num_cpus` threads to run tasks.
pub fn oversubscribed(oversubscribe: f32) -> Arc<Parallel> {
assert!(oversubscribe >= 1.0);
let par = Arc::new(Parallel { context_list: RwLock::new(Vec::new()),
next_context_id: AtomicUsize::new(0),
threads: Mutex::new(Vec::new()),
chunk_size: 8 });
let par = Arc::new(Parallel {
context_list: RwLock::new(Vec::new()),
next_context_id: AtomicUsize::new(0),
threads: Mutex::new(Vec::new()),
chunk_size: 8,
});
{
let mut threads = par.threads.lock().unwrap();
let num_threads = (oversubscribe * num_cpus::get() as f32) as usize;
let chunk_size = par.chunk_size;
for i in 0..num_threads {
let task_sys = Arc::clone(&par);
// Note that the spawned thread ids start at 1 since the main thread is 0
threads.push(thread::spawn(move || Parallel::worker_thread(task_sys, i + 1, num_threads + 1,
chunk_size)));
threads.push(thread::spawn(move || {
Parallel::worker_thread(task_sys, i + 1, num_threads + 1, chunk_size)
}));
}
}
par
Expand All @@ -105,9 +122,19 @@ impl Parallel {
/// Note that due to threading issues you shouldn't assume the context returned actually has
/// outstanding tasks by the time it's returned to the caller and a chunk is requested.
fn get_context(&self) -> Option<Arc<Context>> {
self.context_list.read().unwrap().iter().find(|c| !c.current_tasks_done()).cloned()
self.context_list
.read()
.unwrap()
.iter()
.find(|c| !c.current_tasks_done())
.cloned()
}
fn worker_thread(task_sys: Arc<Parallel>, thread: usize, total_threads: usize, chunk_size: usize) {
fn worker_thread(
task_sys: Arc<Parallel>,
thread: usize,
total_threads: usize,
chunk_size: usize,
) {
THREAD_ID.with(|f| *f.borrow_mut() = thread);
loop {
// Get a task group to run
Expand All @@ -129,7 +156,12 @@ impl Parallel {
}

impl TaskSystem for Parallel {
unsafe fn alloc(&self, handle_ptr: *mut *mut libc::c_void, size: i64, align: i32) -> *mut libc::c_void {
unsafe fn alloc(
&self,
handle_ptr: *mut *mut libc::c_void,
size: i64,
align: i32,
) -> *mut libc::c_void {
// If the handle is null this is the first time this function has spawned tasks
// and we should create a new Context structure in the TASK_LIST for it, otherwise
// it's the pointer to where we should append the new Group
Expand All @@ -139,7 +171,9 @@ impl TaskSystem for Parallel {
// unbox it into a raw ptr to get a ptr we can pass back to ISPC through
// the handle_ptr and then re-box it into our TASK_LIST so it will
// be free'd properly when we erase it from the vector in ISPCSync
let c = Arc::new(Context::new(self.next_context_id.fetch_add(1, atomic::Ordering::SeqCst)));
let c = Arc::new(Context::new(
self.next_context_id.fetch_add(1, atomic::Ordering::SeqCst),
));
{
let h = &*c;
*handle_ptr = h as *const Context as *mut libc::c_void;
Expand All @@ -150,12 +184,22 @@ impl TaskSystem for Parallel {
} else {
let context_list = self.context_list.read().unwrap();
let handle_ctx = *handle_ptr as *mut Context;
let ctx = context_list.iter().find(|c| (*handle_ctx).id == c.id).unwrap();
let ctx = context_list
.iter()
.find(|c| (*handle_ctx).id == c.id)
.unwrap();
ctx.alloc(size as usize, align as usize)
}
}
unsafe fn launch(&self, handle_ptr: *mut *mut libc::c_void, f: ISPCTaskFn, data: *mut libc::c_void,
count0: i32, count1: i32, count2: i32) {
unsafe fn launch(
&self,
handle_ptr: *mut *mut libc::c_void,
f: ISPCTaskFn,
data: *mut libc::c_void,
count0: i32,
count1: i32,
count2: i32,
) {
// Push the tasks being launched on to the list of task groups for this function
let context: &mut Context = &mut *(*handle_ptr as *mut Context);
context.launch((count0, count1, count2), data, f);
Expand Down Expand Up @@ -209,8 +253,10 @@ impl TaskSystem for Parallel {
}
// Now erase this context from our vector
let mut context_list = self.context_list.write().unwrap();
let pos = context_list.iter().position(|c| context.id == c.id).unwrap();
let pos = context_list
.iter()
.position(|c| context.id == c.id)
.unwrap();
context_list.remove(pos);
}
}

11 changes: 8 additions & 3 deletions runtime/src/instrument.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,14 @@ pub struct SimpleInstrument;

impl Instrument for SimpleInstrument {
fn instrument(&self, file: &CStr, note: &CStr, line: i32, mask: u64, active_count: u32) {
println!("SimpleInstrument:\n\tFile: {}\n\tNote: {}\
println!(
"SimpleInstrument:\n\tFile: {}\n\tNote: {}\
\n\tLine: {}\n\tActive: {}\nt\tMask: 0x{:x}",
file.to_str().unwrap(), note.to_str().unwrap(), line, active_count, mask);
file.to_str().unwrap(),
note.to_str().unwrap(),
line,
active_count,
mask
);
}
}