Setting up basic pararrayfun in GNU Octave

I have a file testPowr.m

function testPowr(x)
   printf(x^2)
   end

I am trying the following in another file main.m in the same folder as testPowr.m:

clear; clc;
pkg load parallel;
vector = 1:10;
fun = @(x) x^2;
pararrayfun(nproc, @(n) testPowr(n), vector)
%a2 = pararrayfun(nproc, fun, vector)

But testPowr seems not visible to pararrayfun as the commented line above works. I have tried multiple different syntax but I am unable to get it right.

I am getting the following error in one of the syntax:

execution error
error: __parcellfun_get_next_result__: could not receive result
error: called from
    parcellfun at line 201 column 16
    pararrayfun at line 85 column 28
    main at line 5 column 1

Thanks for your problem report. Can you tell us what system do you run and how did you install Octave?

See also:
Can someone help with implementing pararrayfun in my program? - help - GNU Octave

fun takes in a number and returns its power. But testPowr takes in a number and printfs it, but it does not appear to return any result.
Perhaps if you modified it to

function result = testPowr(x)
result = x^2
end
1 Like

I have a similar problem. Using the example code on the wiki page I get:

octave:1> pkg load parallel
octave:2> fun = @(x) x^2;
octave:3> vector_x = 1:10;
octave:4> vector_y = pararrayfun(nproc, fun, vector_x)
error: ‘parcellfun_set_nproc_used’ undefined near line 117, column 117
error: called from
parcellfun at line 117 column 9
pararrayfun at line 85 column 28

I am running Octave 6.4.0 on MacOS with all packages updates just now.

I am using Windows 10. Octave 6.2.0.

Your solution works but why can’t I implement the printf within the function. In my production case the function doesn’t return anything. I also want to be able to see what the values are within the parallel implementation so I was hoping to make print statement work.

… but why can’t I implement the printf within the function.

In theory, Octave will try to execute the given function on each element of the array/cell-array parallely. But the screen to which the printf prints is a single one; How can multiple parallely executing printf functions write to a single screen simultaneously? It doesn’t make sense in most applications IMHO.

In my production case the function doesn’t return anything.

You can try returning an empty matrix [] from the function function result = tryPowr(input) to satisfy the parallel arrayfun. I don’t know if it will work though.

I also want to be able to see what the values are within the parallel implementation so I was hoping to make print statement work.

Since they are executing parallely, how will you know which printed result belongs to which input element ?

If this is for debugging, debug the non-parallelized version of the code. As long as the function is pure (e.g. it doesn’t modify any global variables), de bugging the non-parallel version of the code should be good enough for most cases.

If you want to view the internal variable values, you can try making result a cell array and use the equivalent of the option UniformOutput=false and return multiple values from the function. See below for the non-parallel version. I don’t know if it will work for the parallel version.

function result = func(input)
  result = {input, ... % actual result
            "debugging string", ... some debugging string
            rand(1) ... % some de bugging values
           };
end

arrayfun (@func, [1,2,3,4,5], 'UniformOutput', false)