使用线程特定的预分配数据并行运行任务
我想并行运行一些任务。每个任务都使用堆分配的数据。为了加快速度,我希望每个线程重新使用相同的数据,而不是取消分配它并在之后重新分配它。可行吗?
这是我想要做的一个基本示例:
use rayon::prelude::*;
use std::collections::HashMap;
fn main() {
// Data for the tasks to run in parallel.
let tasks: Vec<_> = (0..1000).collect();
let task_results: Vec<_> = tasks
.par_iter()
.map(|task| {
// Allocate heap-allocated data.
let data: HashMap<usize, usize> = HashMap::with_capacity(1024);
// Do something the heap-allocated data and the task.
// Drop the heap-allocated data and return the result from the task.
task * 2
})
.collect();
}
每个任务都使用 HashMap
进行计算。任务完成后,HashMap
将被删除。我怎样才能做到这一点,以便每个线程使用一个在运行新任务之前被清除的HashMap
?
I want to run some tasks in parallel. Each task uses heap-allocated data. To speed things up, I would like that each thread re-use the same data instead of un-allocating it and re-allocating it just after. Is it feasible?
Here is a basic example of what I want to do:
use rayon::prelude::*;
use std::collections::HashMap;
fn main() {
// Data for the tasks to run in parallel.
let tasks: Vec<_> = (0..1000).collect();
let task_results: Vec<_> = tasks
.par_iter()
.map(|task| {
// Allocate heap-allocated data.
let data: HashMap<usize, usize> = HashMap::with_capacity(1024);
// Do something the heap-allocated data and the task.
// Drop the heap-allocated data and return the result from the task.
task * 2
})
.collect();
}
Each task uses a HashMap
for its computation. The HashMap
is dropped when the task is done. How can I do it such that each thread uses a single HashMap
that is cleared before running a new task?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
map_with
< /a> 创建一个HashMap
,它将为每个线程克隆一次,然后传递给您的闭包:游乐场
You can use
map_with
to create aHashMap
that will be cloned once for each thread and then passed to your closure:Playground