[petsc-dev] Patch for ksp tutorial ex56

Patrick Sanan patrick.sanan at gmail.com
Tue Mar 31 12:08:24 CDT 2015


I’m using a branch off of master on a fork , but I’ll grab those changes from petsc/petsc/master - thanks!
Il giorno 31/mar/2015, alle ore 06:03 PM, Hong <hzhang at mcs.anl.gov> ha scritto:

> Patrick:
> We have replaced this segment of code (reading a data file) with initialized C arrays in petsc master branch. Are you using petsc-release version?
> 
> Hong
> 
> I tested this out and the strange behavior is very likely because the C string “sp" is not null-terminated, so there is undefined behavior when using snprintf . On different compilers this adds some extra characters (sometimes newlines as below, but arbitrary characters in general). Replacing sp with a null-terminated string fixed the problem for me:
> 
> diff --git a/src/ksp/ksp/examples/tutorials/ex56.c b/src/ksp/ksp/examples/tutorials/ex56.c
> index 16278bb..edccd03 100644
> --- a/src/ksp/ksp/examples/tutorials/ex56.c
> +++ b/src/ksp/ksp/examples/tutorials/ex56.c
> @@ -136,9 +136,10 @@ int main(int argc,char **args)
>      {
>        FILE *file;
>        char fname[256];
> -      char sp[1];
> +      char sp[2];
>        const char* petsc_dir  = getenv("PETSC_DIR");
>        sp[0] = PETSC_DIR_SEPARATOR;
> +      sp[1]='\0';
>        if (!petsc_dir) {
>          SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"'PETSC_DIR' not set.");
>        }
> 
> Il giorno 11/mar/2015, alle ore 02:12 AM, Hong <hzhang at mcs.anl.gov> ha scritto:
> 
>> Sascha:
>> I'll replace *.txt file with a C-array to simply ex56.c. 
>> Please ignore my previous request.
>> 
>> Hong
>> 
>> Hong,
>> I do not have an immediate explanation for this. In particular, I wonder why
>> there is no line break after 'sp=/' in this line:
>> > file = 0x0, sp=/,fname=/Users/Hong/soft/petsc/
>> but line breaks in the file path.
>> 
>> On my machine I tested with make runex56, too. I will look at this again tomorrow.
>> Can anyone else confirm this issue?
>> 
>> Sascha
>> 
>> 
>> 
>> On 10 Mar 2015, at 22:22, Hong <hzhang at mcs.anl.gov> wrote:
>> 
>> > Sascha:
>> > I prepared a patch regarding the reading of the data file in ksp tutorial ex56.
>> > It sets the path to the data file required for running the example relative to PETSC_DIR
>> > instead of relative to the tutorials folder to allow running the example from any folder.
>> >
>> > Also, I used PETSC_DIR_SEPARATOR instead of the currently hard coded ‘/‘
>> > so that the code would also run on a windows machine … if anyone cared.
>> > This fails on my MacPro by running 'make runex56':
>> >
>> > file = 0x0, sp=/,fname=/Users/Hong/soft/petsc/
>> > src/
>> > ksp/
>> > ksp/
>> > examples/
>> > tutorials/
>> > data/
>> > elem_3d_elast_v_25.txt
>> >         main failed to open input file '/Users/Hong/soft/petsc/
>> >
>> > I added
>> > file = fopen(fname, "r");
>> >       printf("file = %p, sp=%s,fname=%s\n",file,sp,fname);
>> >       if (file == 0) {
>> >
>> > as you see, 'sp=/' is followed by a 'new line'.
>> > I do not see this behavior on other machines, e.g., linux or an old iMac.
>> > Any suggestion?
>> >
>> > Hong
>> >
>> >
>> > Please discard the patch if you do not want to change the current behavior.
>> >
>> > Best,
>> > Sascha
>> >
>> >
>> 
>> 
> 
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-dev/attachments/20150331/e3be9dd3/attachment.html>


More information about the petsc-dev mailing list