PETSc Problem with fgets

Barry Smith bsmith at mcs.anl.gov
Fri May 12 13:58:06 CDT 2006



On Fri, 12 May 2006, Joshua L. Adelman wrote:

> Just to clarify, it appears from the documentation that this causes several 
> processors to get the same line from a file.

    Yes

> If I just want to have a single 
> processor get a line and then parse that to some variables within a struct, 
> it would seem like I don't need all of my processors reading the line.
>
   Yes, you don't need

> Would I just use PETSC_WORLD_SELF in the PetscFOpen() command to have a 
> single processor open the file, and then use _SELF again in the synchronized 
> fgets to have a single processor take care of retrieving the line?

   Yes, you can just use SELF, but you need to put all of this stuff INSIDE
the test of if (!rank) {  In fact, if you are only having process 0 open the
file, read the data and parse it then you don't need to use PetscFOpen(), etc
AT ALL, you can just use plan old fopen(), fgets() inside the if (!rank).

   On the other hand if you want all the information from the file on all
the processes you can just use the PetscFOpen(), PetscFGets(), PetscFClose()
with NO if (!rank) { code needed.

    Barry

>
> Thanks again for your help.
>
> Josh
>
> On May 12, 2006, at 11:23 AM, Barry Smith wrote:
>
>>
>>  Stupid me, I don't know my own code.
>>
>>  You can use PetscSynchronizedFGets() exactly for this purpose.
>>
>>   Barry
>>
>>  I have updated the manual page for PetscFOpen() to seealso:
>> PetscSynchronizedFGets()
>> 
>> 
>> On Fri, 12 May 2006, Barry Smith wrote:
>> 
>>> 
>>> Joshua,
>>>
>>>   PetscFOpen() only produces a valid fd on process 0. Thus fgets()
>>> can only be used on process 0. It is crashing on process 1 because
>>> fd is garbage there.
>>>
>>>   You need to MPI_Scatter the data to the other processes. There is
>>> no PetscFGets(), though come to think of it there should be! I will write
>>> one and send it to you.
>>>
>>>  Barry
>>> 
>>> 
>>> 
>>> On Thu, 11 May 2006, Joshua Adelman wrote:
>>> 
>>>> I am trying to read some parameters into my PETSc simulation using fgets, 
>>>> and am getting a strange error. It appears that fgets and sscanf are 
>>>> working since the proper values are read into the simulation, but it 
>>>> throws an error anyways. This problem only arises when I am using the 
>>>> parallel version of the code (i.e. setting -n in mpiexec to a value 
>>>> greater than 1). Basically if I comment everything out and then start 
>>>> uncommenting one line at a time, the error appears once I've uncommented 
>>>> the line with fgets. One strange thing to note is that the error happens 
>>>> after the program has already finished executing the function where the 
>>>> problem is. Here's a snippet of the code that seems to be causing the 
>>>> problem:
>>>> PetscErrorCode DataReadParams(SimData *sdata) {
>>>> 	int 			rank, size;
>>>> 	PetscErrorCode	ierr;
>>>> 	int				i;
>>>> 	FILE			*fd;
>>>> 	char        	str[256];
>>>> 	char			line[100];
>>>> 	int				max = 100;
>>>> 		PetscFunctionBegin;
>>>>
>>>> 	ierr = MPI_Comm_size(PETSC_COMM_WORLD,&size); CHKERRQ(ierr);
>>>> 	ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank); CHKERRQ(ierr);
>>>>
>>>> 		/* Read data from files	*/
>>>> 	sprintf(str,"%s%s",sdata->simname,".param");
>>>> 	ierr = PetscFOpen(PETSC_COMM_WORLD,str,"r",&fd); CHKERRQ(ierr);
>>>> 	if (!rank && !fd) {
>>>> 	    SETERRQ1(PETSC_ERR_FILE_OPEN,"Cannot open %s\n",str);
>>>> 	}
>>>> 	// PetscPrintf(PETSC_COMM_WORLD,"Opening file %s\n",str);
>>>> 	fgets(line,max,fd);
>>>> 	sscanf(line,"%d",&sdata->NP);
>>>> The error that is kicked out is:
>>>> [1]PETSC ERROR: Caught signal number 10 BUS: Bus Error, possibly illegal 
>>>> memory access
>>>> [1]PETSC ERROR: Try option -start_in_debugger or 
>>>> -on_error_attach_debugger
>>>> [1]PETSC ERROR: or try http://valgrind.org on linux to find memory 
>>>> corruption errors
>>>> [1]PETSC ERROR: likely location of problem given in stack below
>>>> [1]PETSC ERROR: --------------- Stack Frames ---------------
>>>> [1]PETSC ERROR: Note: The EXACT line numbers in the stack are not 
>>>> available,
>>>> [1]PETSC ERROR:       INSTEAD the line number of the start of the 
>>>> function
>>>> [1]PETSC ERROR:       is given.
>>>> [1]PETSC ERROR: [1] DataReadParams line 23 datareadparams.c
>>>> [1]PETSC ERROR: [1] SimInit line 19 siminit.c
>>>> [1]PETSC ERROR: --------------------------------------------
>>>> [1]PETSC ERROR: User provided function() line 0 in unknown directory 
>>>> unknown file
>>>> [1]PETSC ERROR: Signal received!
>>>> [1]PETSC ERROR:  !
>>>> Any insight would be most appreciated.
>>>> Josh
>
> ------------------------------------------------------------------------------------------------------
> Joshua L. Adelman
> Biophysics Graduate Group              Lab: 510.643.2159
> 218 Wellman Hall                                Fax: 510.642.7428
> University of California, Berkeley     http://www.ocf.berkeley.edu/~jadelman
> Berkeley, CA 94720 USA                   jadelman at ocf.berkeley.edu
> ------------------------------------------------------------------------------------------------------
>
>
>




More information about the petsc-users mailing list