5

I wrote a recursive function (basically a flood fill), it works fine on smaller datasets, but for slightly larger input it throws StackOverflowError.

How to increase the stack size for Julia under Windows 10? Ideally the solution should be applicable to JupyterLab.

It's a singe use program, no point in optimizing/rewriting it, I just need to peak at the result and forget about the code.

Update: As a test case, I provide the following MWE. This is just a simple algorithm that recursively visits each cell of n by n array:

n = 120

visited = fill(false, (n,n))

function visit_single_neighbour(i,j,Δi,Δj)
  if 1 ≤ i + Δi ≤ n && 1 ≤ j + Δj ≤ n
    if !visited[i+Δi, j+Δj]
      visited[i+Δi, j+Δj] = true
      visit_four_neighbours(i+Δi, j+Δj)
    end
  end
end

function visit_four_neighbours(i,j)
  visit_single_neighbour(i,j,1,0)
  visit_single_neighbour(i,j,0,1)
  visit_single_neighbour(i,j,-1,0)
  visit_single_neighbour(i,j,0,-1)
end

@time visit_four_neighbours(1,1)

For n = 120 the output is 0.003341 seconds, but for n = 121 it throws StackOverflowError.

On a Linux machine with ulimit -s unlimited the code runs no problem for n = 2000 and takes about 2.4 seconds.

I've mirrored the question to Julia Discource: https://discourse.julialang.org/t/ow-to-increase-stack-size-for-julia-in-windows/79932

4
  • Can you add a link to the problem that you are trying to solve if there is one? I assume this is a CS academic question or for Leetcode. Commented Apr 23, 2022 at 21:21
  • @PCDSandwichMan no, I just need to segment data on a lidar range image. And the question is spot on the increasing the stack size in Julia; I won't except any solution that is not applicable to /every/ situation of recursion stackoverflow in problems that can be executed in reasonable time. Commented Apr 24, 2022 at 7:26
  • Does your program involve 2 (or more) mutually recursive functions as in the example ? Commented Apr 24, 2022 at 13:41
  • @Yrogirg That is not how a StackOverflow error works. It is not some configurable hyperparameter. Commented Apr 24, 2022 at 16:53

1 Answer 1

5
+300

As you are no doubt aware Julia is not very optimized for recursion and the recommendation will probably always be to rewrite the code in some way.

With that said there are of course ways to increase the stack limit. One undocumented way to achieve it from inside julia is to reserve stack space when creating a Task:

Core.Task(f, reserved_stack::Int=0)

Let's create a function wrapping such a task:

with_stack(f, n) = fetch(schedule(Task(f,n)))

for n = 2000 the following works on both windows and linux (as long as enough memory is available):

julia> with_stack(2_000_000_000) do
  visit_four_neighbours(1,1)
end
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.