Fortran recursion segmentation errors

I needed to design and implement a Fortran subroutine to determine the size of the clusters on a square lattice and it was very convenient to code the subroutine recursively. However, whenever my lattice size grows above a certain value (about 200 / bok), the subroutine consistently crashes. Here's my procedure for discovering clusters:

RECURSIVE SUBROUTINE growCluster(lattice, adj, idx, area)
    INTEGER, INTENT(INOUT)  :: lattice(:), area 
    INTEGER, INTENT(IN)     :: adj(:,:), idx

    lattice(idx) = -1
    area = area + 1

    IF (lattice(adj(1,idx)).GT.0) &
        CALL growCluster(lattice,adj,adj(1,idx),area)

    IF (lattice(adj(2,idx)).GT.0) &
        CALL growCluster(lattice,adj,adj(2,idx),area)

    IF (lattice(adj(3,idx)).GT.0) &
        CALL growCluster(lattice,adj,adj(3,idx),area)

    IF (lattice(adj(4,idx)).GT.0) &
        CALL growCluster(lattice,adj,adj(4,idx),area)
END SUBROUTINE growCluster

      

where adj (1, n) represents the north neighbor of site n, adj (2, n) represents west, and so on. What can cause erratic segfault behavior? Is the cluster "too huge" for the large lattice size?

+2


source to share


2 answers


I think you are facing a stack overflow. If your lattice is more than 200 units per side, that's 40,000 units, which means you are recursing 40,000 times. Depending on the size of the stack and the size of the stack frame, you can easily discharge from stack space.



You will have to convert your algorithm to one that uses less stack space to handle large lattices. Wikipedia provides several implementations (in pseudocode) on how to make a fill fill without blowing your stack.

+3


source


1) Have you tried to compile and then run with index range checking? Just to make sure the large size does not show an error where the code goes past the array bordering on illegal memory. This is a simple test.



2) Your program may be out of memory: try increasing the stack size limit for each process. How to do this depends on the operating system - search here or Google, or tell us your operating system.

+1


source







All Articles