使用线程特定的预分配数据并行运行任务

发布于 2025-01-14 11:57:08 字数 831 浏览 2 评论 0原文

我想并行运行一些任务。每个任务都使用堆分配的数据。为了加快速度,我希望每个线程重新使用相同的数据,而不是取消分配它并在之后重新分配它。可行吗?

这是我想要做的一个基本示例:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白况 2025-01-21 11:57:08

您可以使用 map_with< /a> 创建一个 HashMap ,它将为每个线程克隆一次,然后传递给您的闭包:

use rayon::prelude::*;
use std::collections::HashMap;

fn main() {
    // Data for the tasks to run in parallel.
    let tasks: Vec<_> = (0..10).collect();

    let task_results: Vec<_> = tasks
        .par_iter()
        .map_with(
            HashMap::<usize, usize>::with_capacity(1024),
            |data, task| {
                // Clear the data for this run
                data.clear();
                // Do something the heap-allocated data and the task.
                task * 2
            },
        )
        .collect();

    println!("{:?}", task_results);
}

游乐场

You can use map_with to create a HashMap that will be cloned once for each thread and then passed to your closure:

use rayon::prelude::*;
use std::collections::HashMap;

fn main() {
    // Data for the tasks to run in parallel.
    let tasks: Vec<_> = (0..10).collect();

    let task_results: Vec<_> = tasks
        .par_iter()
        .map_with(
            HashMap::<usize, usize>::with_capacity(1024),
            |data, task| {
                // Clear the data for this run
                data.clear();
                // Do something the heap-allocated data and the task.
                task * 2
            },
        )
        .collect();

    println!("{:?}", task_results);
}

Playground

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文