Parallel processing Julia-模与并行性

Parallel processing Julia-模与并行性,parallel-processing,julia,Parallel Processing,Julia,我很难理解如何使用辅助进程和模块。我会尽量简单地解释我的困难 我的主进程(进程1)上有一个模块,模块a 关于辅助进程,我有模块B,它应该处理许多复杂的计算(与并行辅助进程的情况一样) 问题在于,似乎需要在workers上定义模块A 问题在于模块A比模块B大得多,它包含数千行代码,但模块B只使用大约15个短函数 是否有任何变通办法,使我的主要工作人员可以访问模块A,但工作人员可以访问模块B,但可以从模块A中的方法调用工作人员,并使他们运行模块B中定义的函数?例如,模块B中可能有一个名为calcul

我很难理解如何使用辅助进程和模块。我会尽量简单地解释我的困难

我的主进程(进程1)上有一个模块,
模块a

关于辅助进程,我有
模块B
,它应该处理许多复杂的计算(与并行辅助进程的情况一样)

问题在于,似乎需要在workers上定义
模块A

问题在于
模块A
模块B
大得多,它包含数千行代码,但
模块B
只使用大约15个短函数

是否有任何变通办法,使我的主要工作人员可以访问
模块A
,但工作人员可以访问
模块B
,但可以从
模块A
中的方法调用工作人员,并使他们运行
模块B
中定义的函数?例如,
模块B
中可能有一个名为
calculate\u stuff()
的方法

我希望实现的结构是:

module A # main worker process using this module 
    function call_worker_and_calculate()
        remotecall_fetch(calculate_stuff, 2)
    end 

    export call_worker_and_calculate
end

module B # worker process 2 is using this module  
    function calculate_stuff()
        # some stuff 
    end 

    export calculate_stuff 
end 
此特定示例将返回错误消息:

julia> A.call_worker_and_calculate()
ERROR: On worker 2:
UndefVarError: A not defined
错误消息很奇怪(我不能用你的代码重现它,你使用的是什么版本?)。所以我不确定这是否回答了你的问题

如果要在另一个模块
a
中使用模块
B
中的名称,则必须
导入
使用
a
中的
模块
B

要使其正常工作,
B
必须位于Julia变量
LOAD\u path
中包含的路径中,或在定义
a
之前在所有具有
@where
的工作人员上定义,例如

@everywhere module B  # defined on all workers
    function calculate_stuff()
        # do stuff 
    end 

    export calculate_stuff 
end

module A # only defined on the main worker process
    using B  # introduces calculate_stuff into A's scope because it is exported in B
    function call_worker_and_calculate()
        remotecall_fetch(calculate_stuff, 2)    
    end 

    export call_worker_and_calculate
end
然后
A.调用\u worker\u和\u calculate()
工作,并且
A
也没有在其他worker上定义:

julia> remotecall_fetch(whos,2)
    From worker 2:                               B   4537 bytes  Module
    From worker 2:                            Base  34048 KB     Module
    From worker 2:                            Core  12482 KB     Module
    From worker 2:                            Main  40807 KB     Module

您可以将
A
中的函数更改为接受要作为参数执行的函数,例如,
call\u-worker\u和\u-calculate(f)=remotecall\u-fetch(f,2)
和调用类似
A.call\u-worker\u和\u-calculate(B.calculate\u-stuff)
。在任何情况下,
B
必须位于路径中,或者使用
@everywhere
在每个工作者上定义。非常感谢!我昨天放弃了,扔掉了我的测试代码,所以我不能百分之百确定哪里出了问题,但我想我没有把模块
B
正确地纳入
A
的范围,也就是说,我没有使用
A
中的B
部分来做
。你刚刚帮我节省了在7个内核上加载3000行模块的时间,同时在主模块中添加了15个无用的方法