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

This improves drastically overthreading issue (>48cores) #11

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions gemm-common/src/gemm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,17 +345,27 @@ pub unsafe fn gemm_basic_generic<

let n_threads = match parallelism {
Parallelism::None => 1,
Parallelism::Rayon(n_threads) => {
Parallelism::Rayon(max_threads) => {
let threading_threshold = get_threading_threshold();
if m * n_chunk * k_chunk <= threading_threshold {
1

let max_threads = if max_threads == 0 {
rayon::current_num_threads()
} else {
if n_threads == 0 {
rayon::current_num_threads()
} else {
n_threads
}
}
max_threads
};
let total_work = m * n_chunk * k_chunk;
let n_threads = if total_work > threading_threshold {
std::cmp::max(
1,
std::cmp::min(
max_threads,
(total_work - threading_threshold + 1) / threading_threshold,
),
)
} else {
1
};
n_threads
}
};

Expand Down
1 change: 1 addition & 0 deletions gemm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ rand = "0.8.5"
nalgebra = "0.32.2"
assert_approx_eq = "1.1.0"
rayon = "1.7"
num_cpus = "1.16.0"

[[bench]]
name = "bench"
Expand Down
114 changes: 113 additions & 1 deletion gemm/benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,110 @@ pub fn criterion_benchmark(c: &mut Criterion) {
}
}

pub fn criterion_benchmark_parallelism(c: &mut Criterion) {
let mnks = vec![(6, 768 * 3, 768)];
// let mut push = |m, n, k| {
// mnks.push((m, n, k));
// };
// push(64, 64, 64);
// push(8192, 8192, 8192);
// push(4096, 4096, 4096);
// push(1024, 1024, 1024);
// push(896, 128, 128);
// push(512, 256, 256);
// push(448, 448, 128);
// push(256, 256, 256);
// push(256, 32, 256);
// push(52, 52, 256);
// push(48, 48, 256);
// push(63, 1, 10);
// push(63, 2, 10);
// push(63, 3, 10);
// push(63, 4, 10);

// push(1024, 1, 1024);
// push(1024, 2, 1024);
// push(1024, 3, 1024);
// push(1024, 4, 1024);
//
let n_cpus = num_cpus::get();

for (m, n, k) in mnks.iter().copied() {
let a_vec = vec![0.0_f32; m * k];
let b_vec = vec![0.0_f32; k * n];
let mut c_vec = vec![0.0_f32; m * n];

for (dst_label, dst_cs, dst_rs) in [("n", m, 1), ("t", 1, n)] {
for (lhs_label, lhs_cs, lhs_rs) in [("n", m, 1), ("t", 1, k)] {
for (rhs_label, rhs_cs, rhs_rs) in [("n", k, 1), ("t", 1, n)] {
c.bench_function(
&format!(
"parallelism-{}-f32-{}{}{}-gemm-{}×{}×{}",
n_cpus, dst_label, lhs_label, rhs_label, m, n, k
),
|b| {
b.iter(|| unsafe {
gemm(
m,
n,
k,
c_vec.as_mut_ptr(),
dst_cs as isize,
dst_rs as isize,
true,
a_vec.as_ptr(),
lhs_cs as isize,
lhs_rs as isize,
b_vec.as_ptr(),
rhs_cs as isize,
rhs_rs as isize,
0.0_f32,
0.0_f32,
false,
false,
false,
gemm::Parallelism::Rayon(n_cpus),
)
})
},
);
c.bench_function(
&format!(
"parallelism-none-f32-{}{}{}-gemm-{}×{}×{}",
dst_label, lhs_label, rhs_label, m, n, k
),
|b| {
b.iter(|| unsafe {
gemm(
m,
n,
k,
c_vec.as_mut_ptr(),
dst_cs as isize,
dst_rs as isize,
true,
a_vec.as_ptr(),
lhs_cs as isize,
lhs_rs as isize,
b_vec.as_ptr(),
rhs_cs as isize,
rhs_rs as isize,
0.0_f32,
0.0_f32,
false,
false,
false,
gemm::Parallelism::None,
)
})
},
);
}
}
}
}
}

criterion_group!(
name = benches;
config = Criterion::default()
Expand All @@ -264,4 +368,12 @@ criterion_group!(
.sample_size(10);
targets = criterion_benchmark
);
criterion_main!(benches);
criterion_group!(
name = benches_parallelism;
config = Criterion::default()
.warm_up_time(Duration::from_secs(1))
.measurement_time(Duration::from_secs(2))
.sample_size(10);
targets = criterion_benchmark_parallelism
);
criterion_main!(benches, benches_parallelism);