Questions tagged [rayon]

A Rust data-parallelism library that makes it easy to convert sequential computations into parallel Rayon is lightweight and convenient for introducing parallelism into existing code. It guarantees data-race free executions and takes advantage of parallelism when sensible, based on work-load at runtime.

See the official documentation for more information.

123 questions
4
votes
1 answer

Is there a way to directly consume a Rayon chain without collecting it first?

I am using Rayon to produce reasonably large return values. This uses a lot of memory when collecting all returned values into a Vec. Is there a way to avoid creating a Vec and directly consuming as an iterable? Here is an example which doesn't…
user964375
  • 2,201
  • 3
  • 26
  • 27
4
votes
1 answer

Can't find crate for `rayon`

I am trying to find the diameter of a BST using parallelization: extern crate rayon; use std::cmp::Ordering::*; use std::ops::Index; use rayon::prelude::*; #[derive(Debug)] struct Node { left: Option>>, right:…
3
votes
1 answer

Is it sound to use raw pointers to an allocated vector to allow multiple threads to write to nonoverlapping parts?

I have multiple threads doing a computation and want to collect the results into a pre-allocated vector. To turn off the borrow checker, I wrote the function: fn set_unsync(vec: &Vec, idx: usize, val: usize) { let first_elem =…
user1494080
  • 2,064
  • 2
  • 17
  • 36
3
votes
1 answer

How to avoid mutable and immutable borrow with IntoParallelIterator bound

I have a function that operates on a Vec which purpose is to extend the vector with new items generated using reference to existing items. I'm trying to run the generation of new data in parallel using rayon. This is a minimal example: use…
Nick
  • 10,309
  • 21
  • 97
  • 201
3
votes
0 answers

Parallelize groupby with Rayon in Rust

I am trying to do something where I have a little logic in groupby. Consider this working code for example: use itertools::Itertools; let counts = vec![Some(1), Some(1), None, None, Some(1), Some(1), Some(3), Some(3)]; let vals = vec![1, 3, 2, 2, 1,…
ste_kwr
  • 820
  • 1
  • 5
  • 21
3
votes
3 answers

Executing external commands in parallel and capturing the output in an array in Rust

I have the following while loop that runs generate_user_key for each of the file in the file_array, and outputs the result. I would like to parallelize this such that an array of the generated keys is returned, and the process is executed in…
Saqib Ali
  • 3,953
  • 10
  • 55
  • 100
3
votes
1 answer

Parallelizing nested loops in rust with rayon

I am trying to parallelize simple nested for loop in Rust with rayon but am unable to: fn repulsion_force(object: &mut Vec) { let v0 = 500.0; let dx = 0.1; for i in 0..object.len() { for j in i + 1..object.len() { …
3
votes
1 answer

Why does filter() have different type requirements on parallel iterators?

I'm trying to understand why Rayon's filter() function won't work without needing to specify the right type, whereas filter() will work correctly if I'm not using a parallel iterator. Here's my code: use rayon::prelude::*; fn is_even(n: i64) ->…
Daniel Giger
  • 2,023
  • 21
  • 20
3
votes
1 answer

How to iterate with enumeration using rayon?

I can iterate and process both index and the variable within such as: let x = vec![5, 6, 7, 8]; for (index, val) in x.iter().enumerate() { println!("{} {}", val, index); } Now with rayon, from what I know, parallel iteration through par_iter()…
user12513149
2
votes
1 answer

Rust threadpool with init code in each thread?

Following code is working, it can be tested in Playground use std::{thread, time::Duration}; use rand::Rng; fn main() { let mut hiv = Vec::new(); let (sender, receiver) = crossbeam_channel::unbounded(); // make workers for t in…
WebOrCode
  • 6,852
  • 9
  • 43
  • 70
2
votes
4 answers

How to have seedable RNG in parallel in rust

I am learning rust by implementing a raytracer. I have a working prototype that is single threaded and I am trying to make it multithreaded. In my code, I have a sampler which is basically a wrapper around StdRng::seed_from_u64(123) (this will…
Bob Parker
  • 99
  • 7
2
votes
1 answer

Parallel work stealing in arbitrary order in Rust

I'm trying to write a parallel data loader for deep learning in Rust. The task is to write an iterator that under the hood does the following Reads files from disk and applies some compute-heavy preprocessing to them, the result is generally a…
Jatentaki
  • 11,804
  • 4
  • 41
  • 37
2
votes
1 answer

Rayon Can't turn .chars() iterator into .par_iter()

i'm trying to parallelize the following function: pub fn encode(&self, s: &String) -> String { s.chars() .par_iter() // error here .map(|c| Character::try_from(c)) .enumerate() .map(|(n, c)|…
Brandon Piña
  • 614
  • 1
  • 6
  • 20
2
votes
0 answers

Parallelizing a self modifying loop in Rust

I have a loop in Rust, which basically looks like this: while let Some(next) = myqueue.pop_front() { let result = next.activate(); if result.0 { myqueue.extend(result.1.into_iter()); } } I want to paralelize this loop. Naturally,…
2
votes
1 answer

Parallel write to array with a unique indices array

This question is analogical to Parallel write to array with an indices array except that I guarantee the indices to be unique. let indices = [1, 4, 7, 8]; let mut arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; indices.iter_par().for_each(|x| { arr[x] =…
alagris
  • 1,838
  • 16
  • 31
1
2
3
8 9