Segmentation fault after removing debug printing

2024/10/13 15:18:31

I have a (for me) very weird segmentation error. At first, I thought it was interference between my 4 cores due to openmp, but removing openmp from the equation is not what I want. It turns out that when I do, the segfault still occurs.

What's weird is that if I add a print or write anywhere within the inner-do, it works.

subroutine histogrambins(rMatrix, N, L,  dr, maxBins, bins)implicit none;double precision, dimension(N,3), intent(in):: rMatrix;integer, intent(in) :: maxBins, N;double precision, intent(in) :: L, dr;integer, dimension(maxBins, 1), intent(out)  :: bins;integer :: i, j, b;  double precision, dimension(N,3) :: cacheParticle, cacheOther;double precision :: r;do b= 1, maxBinsbins(b,1) = 0;end do !$omp parallel do &!$omp default(none) &!$omp firstprivate(N, L, dr, rMatrix, maxBins) &!$omp private(cacheParticle, cacheOther, r, b) &!$omp shared(bins) do i = 1,N do j = 1,N!Check the pair distance between this one (i) and its (j) closest image if (i /= j) then !should be faster, because it doesn't have to look for matrix indices cacheParticle(1, :) = rMatrix(i,:); cacheOther(1, :) = rMatrix(j, :);  call inbox(cacheParticle, L);call inbox(cacheOther, L);  call closestImage(cacheParticle, cacheOther, L);    r = sum( (cacheParticle - cacheOther) * (cacheParticle - cacheOther) ) ** .5; if (r /= r) then! r is NaN bins(maxBins,1) = bins(maxBins,1) + 1;else   b = floor(r/dr);if (b > maxBins) thenb = maxBins;end if     bins(b,1) = bins(b,1) + 1;end ifend ifend doend do!$omp end parallel do
end subroutine histogramBins 

I enabled -debug-capi in the f2py command:

f2py --fcompiler=gfortran --f90flags="-fopenmp -fcheck=all" -lgomp --debug-capi --debug -m -c modulename module.f90; 

Which gives me this:

debug-capi:Fortran subroutine histogrambins(rmatrix,&n,&l,&dr,&maxbins,bins)'
At line 320 of file mol-dy.f90
Fortran runtime error: Aborted

It also does a load of other checking, listing arguments given and other subroutines called and so on.

Anyway, the two subroutines called in are both non-parallel subroutines. I use them in several other subroutines and I thought it best not to call a parallel subroutine with the parallel code of another subroutine. So, at the time of processing this function, no other function should be active.

What's going on here? How can adding "print *, ;"" cause a segfault to go away?

Thank you for your time.

Answer

It's not unusual for print statements to impact - and either create or remove the segfault. The reason is that they change the way memory is laid out to make room for the string being printed, or you will be making room for temporary strings if you're doing some formatting. That change can be sufficient to cause a bug to either appear as a crash for the first time, or to disappear.

I see you're calling this from Python. If you're using Linux - you could try following a guide to using a debugger with Fortran called from Python and find the line and the data values that cause the crash. This method also works for OpenMP. You can also try using GDB as the debugger.

Without the source code to your problem, I don't think you're likely to get an "answer" to the question - but hopefully the above ideas will help you to solve this yourself.

Using a debugger is (in my experience) considerably less likely to have this now-you-see-it-now-you-don't behaviour than with print statements (almost certainly so if only using one thread).

https://en.xdnf.cn/q/118066.html

Related Q&A

numpy get 2d array where last dimension is indexed according to a 2d array

I did read on numpy indexing but I didnt find what I was looking for.I have a 288*384 image, where each pixel can have a labelling in [0,15]. It is stored in a 3d (288,384,16)-shaped numpy array im.Wit…

Error sending html email with mailgun python API

I can send text email with the Mailgun python API fine:def send_simple_message(mailtext, filename=""):requests.post("https://api.mailgun.net/v3/mydomain.in/messages",auth=("api…

How to take HTML user input and query it via Python SQL?

Is there a way to take user input from HTML, and use python to run the input through to a SQL database? Does the input need to be parsed? I want the the user to be able to type in a store name, and f…

Reading and taking specific file contents in a list in python

I have a file containing:name: Sam placing: 2 quote: Ill win.name: Jamie placing: 1 quote: Be the best.and I want to read the file through python and append specific contents into a list. I want my fir…

Scipy / ctypes build-config: How to load lib?

These docs have a nice example on how to compile some external C/C++ code and load this using ctypes. This works great with manual compilation (where im controlling the names of my library which later …

Webfaction Django 1.4.1: easy_thumbnails 3.0b – Couldnt get the thumbnail error

I use easy_thumbnails and it works fine on a development machine but in production I get errors like shown below, when I use {% thumbnail photo.image 300x170 %} templatetag. Though can directly browse …

acronym replacement with its value using python

i have dictionary like that i need to replace acronyms in text with its value in dictionary i use this code but it doesnt give me the appropriate result when i test the function using acronyms("we…

Grako - How to do error handling?

How do I do error handling with Grako?EBNF (MyGrammar.ebnf):pattern = { tag | function }* ; tag = tag:( "%" name:id "%" ); function = function:("$" name:id "…

How get the softlayer storage credendials?

Im trying to get the Username,password and host IQN of a authorized Softlayer Network Storage. I used this python script, but the shell returns []import SoftLayerAPI_USERNAME = xxxAPI_KEY = yyyystorage…

dopy.manager.DoError: Unable to authenticate you

Im trying to configure a Virtual Machine(with Vagrant and Ansible), that needs a file.py to the full correct configuration of this machine (according to the book that Im studying),Im was using the Digi…