This repository has been archived by the owner on May 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
yFFT: Fix SEGV caused by the alignment issue of lookup tables
Tested with: rustc 1.32.0-nightly (25a42b2ce 2018-11-07) jemalloc was removed from the standard library recently: <rust-lang/rust#55238> As a result, we started seeing cases where the alignment requirements enforced on allocated regions are weaker than before. Some code ceased working because it erroneously relied on jemalloc's larger alignment values. This commit fixes this issue by introducing `AlignedVec`, a wrapper of `Vec`, which provides access to a portion of `Vec` that meets an alignment requirement required by SIMD operations, and by modifying code in question to use `AlignedVec` in place of `Vec`.
- Loading branch information
Showing
5 changed files
with
84 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// | ||
// Copyright 2018 yvt, all rights reserved. | ||
// | ||
// This source code is a part of Nightingales. | ||
// | ||
use std::{ | ||
fmt, | ||
mem::size_of, | ||
ops::{Deref, DerefMut}, | ||
}; | ||
|
||
/// The alignment value guaranteed by `AlignedVec`. | ||
const ALIGN: usize = 32; | ||
|
||
fn ptr_lsbs(x: usize) -> usize { | ||
x & (ALIGN - 1) | ||
} | ||
|
||
/// Provides a subset of `Vec`'s interface while providing a minimum alignment | ||
/// guarantee that is convenient for SIMD operations. | ||
pub struct AlignedVec<T> { | ||
storage: Vec<T>, | ||
offset: usize, | ||
} | ||
|
||
impl<T: Copy + Default> AlignedVec<T> { | ||
pub fn with_capacity(i: usize) -> Self { | ||
debug_assert!(size_of::<T>() <= ALIGN); | ||
debug_assert!(ALIGN % size_of::<T>() == 0); | ||
|
||
let mut storage: Vec<T> = Vec::with_capacity(i + ALIGN / size_of::<T>() - 1); | ||
let mut offset = 0; | ||
|
||
// Increase the padding until the storage is aligned | ||
while ptr_lsbs(storage.as_ptr().wrapping_add(offset) as _) != 0 { | ||
storage.push(T::default()); | ||
offset += 1; | ||
|
||
debug_assert!(offset < ALIGN / size_of::<T>()); | ||
} | ||
|
||
Self { storage, offset } | ||
} | ||
|
||
pub fn push(&mut self, x: T) { | ||
if self.storage.len() >= self.storage.capacity() { | ||
panic!("collection is full"); | ||
} | ||
self.storage.push(x); | ||
} | ||
} | ||
|
||
impl<T: fmt::Debug> fmt::Debug for AlignedVec<T> { | ||
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { | ||
fmt.debug_struct("AlignedVec") | ||
.field("offset", &self.offset) | ||
.field("entries", &&self[..]) | ||
.finish() | ||
} | ||
} | ||
|
||
impl<T> Deref for AlignedVec<T> { | ||
type Target = [T]; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
&self.storage[self.offset..] | ||
} | ||
} | ||
|
||
impl<T> DerefMut for AlignedVec<T> { | ||
fn deref_mut(&mut self) -> &mut Self::Target { | ||
&mut self.storage[self.offset..] | ||
} | ||
} |
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 |
---|---|---|
|
@@ -42,6 +42,7 @@ use num_complex::Complex; | |
|
||
#[macro_use] | ||
mod simdutils; | ||
mod aligned; | ||
mod env; | ||
mod kernel; | ||
mod setup; | ||
|