Skip to content

Generic over number of simd lanes with rand #1359

Open
@maxbla

Description

@maxbla

I'm writing a function that is supposed to be generic over the number of simd lanes. Minimal example:

more code
#![feature(portable_simd)]

use std::simd::{Simd, SupportedLaneCount, LaneCount};
use rand::SeedableRng;
pub fn example<const LANES: usize> (max: i32) -> i32 
    where LaneCount<LANES>: SupportedLaneCount
{
    let mut rng = rand::rngs::StdRng::from_entropy();
    let zero = Simd::from_array([0; LANES]);
    let max = Simd::from_array([max; LANES]);
    let dist = rand::distributions::Uniform::new(zero, max).unwrap();
    let rands = dist.sample(&mut rng);
}

Unfortunately I can't do this. The compiler gives error[E0277]: the trait bound Simd<u32, LANES>: distributions::utils::WideningMultiply is not satisfied on line 7 of this example. I tried adding that trait bound

pub fn example<const LANES: usize> (max: i32) -> i32 
where LaneCount<LANES>: SupportedLaneCount,
    Simd<u32, LANES> : WideningMultiply<Output = (Simd<u32, LANES>, Simd<u32, LANES>)>

but WideningMultiply is pub(crate) so I can't use it.

Is there any way I can make this work? I realize SMID is experimental in rand and in rust. I have a workaround for now, which is to just declare a constant LANES outside the function, but then benchmarking with different number of lanes involves copy pasting or a macro.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions