pararrayfun“无法接收结果”错误,尽管 FUN 返回一个值

问题描述 投票:0回答:1

我刚刚在我的机器上安装了

parallel
(Linux Mint 20,Octave 版本:5.2.0)。

我正在尝试运行从here获取的示例代码,但我收到此错误:

pararrayfun

error: 'myfun' undefined near line 1 column 6
execution error
error: __parcellfun_get_next_result__: could not receive result

我在here读到

pararrayfun
需要返回值才能工作,示例中就是这种情况(请参阅下面的代码)。

# In this script 4 ways of calculating the values of a one-dimensional function
# are compared, the time taken by each way is measured and it is verified that
# there are no discrepancies in the results.
pkg load parallel

nmax = 10000; # Number of points where the function is calculated

function [a,b] = myfun(n); # Function used in this test
  a = pi*(n-2)/n;
  f = @(x) (cos(x).^n + sin(x).^(n-1));
  b = quadgk(f,0,a);
endfunction

# Fist method, using a for loop, defining the function to calculate
# within the loop.
tic
for n = 1:nmax;
  a1(n) = pi*(n-2)/n;
  b1(n) = quadgk(@(x) (cos(x).^n + sin(x).^(n-1)),0,a1(n));
endfor
t1 = toc

# Second method, using a for loop and calling "myfun"
tic
for n = 1:nmax;
  [a2(n),b2(n)] = myfun(n);
endfor
t2= toc

# Third method, using arrayfun to call "myfun"
tic
ni = 1:nmax;
[a3,b3] = arrayfun("myfun",ni);
t3 = toc

# Forth method, using parrayfun to call "myfun"
tic
ni = 1:nmax;
[a4,b4] = pararrayfun(4,@(n) myfun(n),ni);
t4 = toc

# Are discrepancies in the results?
discrepancies_1 = max(a2-a1) + max(b2-b1) + max(a3-a1)
discrepancies_2 = max(b3-b1) + max(a4-a1) + max(b4-b1)

可能是什么问题?

multithreading parallel-processing functional-programming octave
1个回答
0
投票

感谢 GNU Octave 讨论组中的这篇文章,我终于解决了这个问题。

“并行”包在 Octave 中的工作方式是,它生成 Octave 的各种“独立”实例,并在所有这些实例中运行给定的代码。 为了使其正常工作,所有必要的功能必须在所有这些实例中都可以访问。如果我正确理解您的代码,您将内联定义 myFun 。这样它只会在“main”实例中定义。 将该函数移至专用文件并将其存储在所有实例的加载路径中的位置。 (也许使用 .octaverc 将其添加到您的默认加载路径。)

所以基本上我必须将函数定义移动到单独的文件“myfun.m”中(正如用户 Cris Luengo 在我的问题的评论中所建议的那样),并将此文件移动到 Octave 加载路径中的文件夹中。

为此,我创建了一个新的专用文件夹(“~/octave/custom_funcs”),并使用

addpath("~/octave/custom_funcs")
将其添加到加载路径中。

希望这对任何人都有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.