gpt4 book ai didi

multithreading - Julia - 读取大文件的并行性

转载 作者:行者123 更新时间:2023-12-03 12:45:28 25 4
gpt4 key购买 nike

在 Julia v1.1 中,假设我有一个非常大的文本文件 (30GB) 并且我希望并行(多线程)读取每一行,我该怎么做?

此代码是在检查 Julia's documentation on multi-threading 之后尝试执行此操作的, 但它根本不起作用

open("pathtofile", "r") do file
# Count number of lines in file
seekend(file)
fileSize = position(file)
seekstart(file)

# skip nseekchars first characters of file
seek(file, nseekchars)

# progress bar, because it's a HUGE file
p = Progress(fileSize, 1, "Reading file...", 40)
Threads.@threads for ln in eachline(file)
# do something on ln
u, v = map(x->parse(UInt32, x), split(ln))
.... # other interesting things
update!(p, position(file))
end
end

注意 1:您需要使用 ProgressMeter(我希望我的代码在并行读取文件时显示进度条)

注意 2:nseekchars 是一个 Int 和我想在文件开头跳过的字符数

注意 3:如果没有 Threads.@threads 循环旁边的宏,代码可以运行但不会执行并行处理

最佳答案

为了获得最大的 I/O 性能:

  1. 并行化硬件 - 即使用磁盘阵列而不是单个驱动器。尝试搜索 raid performance 以获得许多出色的解释(或提出单独的问题)

  2. 使用 Julia memory mapping机制

s = open("my_file.txt","r")
using Mmap
a = Mmap.mmap(s)
  1. 一旦有了内存映射,就可以并行处理。当心false sharing对于线程(取决于您的实际场景)。

关于multithreading - Julia - 读取大文件的并行性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54874366/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com