1

i am simulating a problem(3d Ising Model) in c but when the problem size gets larger the program stop and the below error appears :

Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)

in the program I have a recursive function that does all the works, i suspect that the error is because of stack overflow(in the recursive function) but i do not know how to be sure.

and if it is because of stack overflow, is there any way to solve this problem without changing program design?

i am using Clion IDE.

/*
 * recursive function to form Wolff Cluster(= WC)
 */
void grow_Wolff_cluster(lattic* l, Wolff* wolff, site *seed){

    /*a neighbor of site seed*/
    site* neighbor;

    /*go through all neighbors of seed*/
    for (int i = 0 ; i < neighbors ; ++i) {


        neighbor = seed->neighbors[i];

        /*add to WC according to the Wolff Algorithm*/
        if(neighbor->spin == seed->spin && neighbor->WC == -1 && ((double)rand() / RAND_MAX) < add_probability)
        {
            wolff->Wolff_cluster[wolff->WC_pos] = neighbor;
            wolff->WC_pos++;                  // the number of sites that is added to WC
            neighbor->WC = 1;          // for avoiding of multiple addition of site
            neighbor->X = 0;


            ///controller_site_added_to_WC();


            /*continue growing Wolff cluster(recursion)*/
            grow_Wolff_cluster(l, wolff, neighbor);
        }
    }
}
mehrdad
  • 161
  • 2
  • 11
  • 5
    Catch the crash in a debugger. Look at the function call stack. If it's full of `grow_Wolff_cluster` calls only, then you have a stack overflow. – Some programmer dude Jan 09 '18 at 17:14
  • 2
    recursive algorithms often produce elegant solutions, but practically they can be dangerous for these reasons, and generally should be avoided unless you're _sure_ it will only recurse a safe number of times. I don't know what you're doing here, but you're probably best-served re-implementing it in a non-recursive manner. – yano Jan 09 '18 at 17:16
  • 2
    https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ https://stackoverflow.com/questions/2069367/how-to-debug-using-gdb should be helpful reads. Maybe also https://ericlippert.com/2014/03/21/find-a-simpler-problem/ to get back to firm ground of something working reliably, then start incrementing again. – Yunnosch Jan 09 '18 at 17:18
  • 1
    MOst basic method: put some printfs at strategic points of your program and see what is displayed. – Jabberwocky Jan 09 '18 at 17:19
  • yes it is full of the recursive function, it is too slow i cannot navigate to see how many of them is there. but after all i have a recursive function and i expect to see lots of the recursive function – mehrdad Jan 09 '18 at 17:19
  • no need for printf, the Clion debugger says it is inside of the recursive function – mehrdad Jan 09 '18 at 17:22
  • Successful recursive functions typically have two paths: (1) stop recursing or (2) make progress and then recurse. Your function seems to recurse based (at least in part) on a random number, which seems suspiciously like you have no guarantee that you're making progress toward termination and thus you might recurse until you blow the stack. – Adrian McCarthy Jan 09 '18 at 19:44
  • @Adrian McCarthy, NO! the recursive function is fine and it is working good for small problem but when problem size gets larger it blow up. and its not based on a random number!! in the if condition there is some other conditions that terminates the function(for more information you can see Ising Model Wolff single cluster allgorithm) – mehrdad Jan 09 '18 at 21:50
  • Does every site have the same number of neighbors? What is that number, and what is the total number of sites? The first reference I found for the algorithm says that the cluster size can grow as large as the total number of sites in the volume. If there's a high connectivity, then, in the worst case, the recursive depth could match the total number of sites. – Adrian McCarthy Jan 09 '18 at 23:37
  • @Adrian McCarthy, you are right the recursive depth could match the total number of sites, total number of sites is something from 30*30*30 up to 150*150*150 for my problem, and up to 80*80*80(=512000) program works fine but for larger than this stack overflows. so , for recursive depth of 512000 stack overflow is normal??! – mehrdad Jan 10 '18 at 00:15
  • @Adrian McCarthy, yes every site have exactly 6 neighbors. – mehrdad Jan 10 '18 at 00:16

1 Answers1

3

Use the GDB Debugger and look at the Call Stack.

gdb main
r
bt
QuesterDesura
  • 385
  • 2
  • 18