gpt4 book ai didi

module - 为什么多处理 Julia 会破坏我的模块导入?

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

我的团队正在尝试使用多处理运行一个库(带有 JuMP 的 Cbc)并使用 julia -p #争论。我们的代码在一个 julia 包中,所以我们可以使用 julia --project 运行我们的代码。 ,它只运行一个进程。试图同时指定两者 julia --project -p 8破坏了我们自运行 using PackageName 以来运行项目的能力在导致错误之后。我们还打算使用 PackageCompiler 来编译它。库,所以让它与项目一起工作是必要的。

我们的项目在一个文件夹中,其中有一个 src 目录、一个 Project.toml 和一个 Manifest.toml
src 包含:main.jl 和 Solver.jl

Project.toml 包含:

name = "Solver"
uuid = "5a323fe4-ce2a-47f6-9022-780aeeac18fe"
authors = ["..."]
version = "0.1.0"

通常,我们的项目以这种方式开始工作正常(单线程):
julia --project
julia> using Solver
julia> include("src/main.jl")

如果我们添加 -p 8启动 Julia 时的参数,输入 using Solver 时出现错误:
ERROR: On worker 2:
ArgumentError: Package Solver [5a323fe4-ce2a-47f6-9022-780aeeac18fe] is required but does not seem to be installed:
- Run `Pkg.instantiate()` to install all recorded dependencies.

我们已经尝试运行 using Pkg; Pkg.instantiate(); using Solver但这无济于事,因为稍后会发生另一个错误(在 include("src/main.jl") 步骤):
ERROR: LoadError: On worker 2:
ArgumentError: Package Solver not found in current path:
- Run `import Pkg; Pkg.add("Solver")` to install the Solver package.

然后遵循该建议会产生另一个错误:
ERROR: The following package names could not be resolved:
* Solver (not found in project, manifest or registry)
Please specify by known `name=uuid`.

为什么这个模块导入工作 单进程模式下没问题 , 但是 不与 -p 8 ?

提前感谢您的考虑

最佳答案

首先,重要的是要注意您不是在使用多线程并行,而是在使用分布式并行。当您以 -p 2 开始时您正在启动两个不同的进程,它们不共享相同的内存。此外,项目仅在主进程中加载​​,这就是为什么其他进程无法看到项目中的任何内容。您可以了解更多关于 Julia 提供的不同类型的并行性 in the official documentation .

要在所有工作人员中加载环境,您可以将其添加到文件的开头。

using Distributed
addprocs(2; exeflags="--project")
@everywhere using Solver
@everywhere include("src/main.jl")

并删除 -p 2用于启动 Julia 的生产线的一部分。这会将项目加载到所有进程中。 @everywhere宏用于指示执行给定任务的所有进程。 This part of the docs explains it.

但是请注意,并行性不会自动工作,因此如果您的软件没有考虑到分布式并行性,它可能不会从新推出的工作人员中获得任何好处。

关于module - 为什么多处理 Julia 会破坏我的模块导入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60934852/

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