[petsc-users] A very strange bug about using multiple KSPs

Matthew Knepley knepley at gmail.com
Tue Mar 3 10:26:49 CST 2015


On Tue, Mar 3, 2015 at 10:15 AM, Sun, Hui <hus003 at ucsd.edu> wrote:

>  Dave, thank you for your suggestions. I don't have a debugger, and I
> think without debugger we can still figure out if a function is called or
> not.
>
>  Putting the print commend before or after PetscFunctionBeginUser doesn't
> really matter. I just tried to put it after PetscFunctionBeginUser, but
> something doesn't print just doesn't print.
>

The ComputeMatrix function is stored in the DM, or more precisely in the
DMKSP object. Since both KSPs have the
same DM, they have the same function.

I believe you can DMClone(da, &da2), and then this will work. Please tell
me if it does not.

  Thanks,

    Matt


> There are other ways to see that computeMatrix1 is not called. For
> example, I just change b1 to a vector of 1's, and b2 to a vector of 0.5's,
> and ComputeMatrix1 to give diagonal matrix with entries 1, and  ComputeMatrix2
> to give diagonal matrix with entries 2.
>
>  Then I output the matrices and the vectors into a file, and visualize
> them in MATLAB. It does show me that the ksp1 and ksp2 both use ComputeMatrix2;
> while the ksp1 uses the rhs b1, and the ksp2 uses the rhs b2. This is not
> expected because I've set ksp1 to use ComputeMatrix1 before:
>
>       ierr = KSPSetComputeOperators(ksp1,ComputeMatrix1,NULL
> );CHKERRQ(ierr);
>
>     ierr = KSPSetComputeOperators(ksp2,ComputeMatrix2,NULL);CHKERRQ(ierr);
>
>
>  Now if I comment out the lines
>
>       ierr = KSPSetComputeOperators(ksp2,ComputeMatrix2,NULL
> );CHKERRQ(ierr);
>
>  and
>
>       ierr = KSPSolve(ksp2,b2,x2);CHKERRQ(ierr);
>
>
>  Then ksp1 start to use ComputeMatrix1, as can be visualized from both
> the output file and the terminal print. So it shows me that there are some
> crosstalk between ksp1 and ksp2. If I set
>
>      ierr = KSPSetComputeOperators(ksp1,ComputeMatrix1,NULL
> );CHKERRQ(ierr);
>
>  ksp1 uses ComputeMatrix1. But if I set
>
>       ierr = KSPSetComputeOperators(ksp1,ComputeMatrix1,NULL
> );CHKERRQ(ierr);
>
>     ierr = KSPSetComputeOperators(ksp2,ComputeMatrix2,NULL);CHKERRQ(ierr);
>
>  ksp1 uses ComputeMatrix2.
>
>
>
>  ------------------------------
> *From:* Dave May [dave.mayhem23 at gmail.com]
> *Sent:* Tuesday, March 03, 2015 1:11 AM
> *To:* Sun, Hui
> *Cc:* petsc-users at mcs.anl.gov
> *Subject:* Re: [petsc-users] A very strange bug about using multiple KSPs
>
>  Try moving your print statement in ComputeMatrix2 to be after
> PetscFunctionBeginUser;
>
> You should really use a debugger to see if these functions are being
> called or not - don't use print statements.
>
> On Tuesday, 3 March 2015, Sun, Hui <hus003 at ucsd.edu> wrote:
>
>>  I set up 2 KSPs through KSPSetComputeOperators, with separate compute
>> matrix functions.
>>
>>
>>
>>     ierr = KSPSetComputeOperators(ksp1,ComputeMatrix1,NULL
>> );CHKERRQ(ierr);
>>
>>     ierr = KSPSetComputeOperators(ksp2,ComputeMatrix2,NULL
>> );CHKERRQ(ierr);
>>
>>  Within each computeMatrix function, I define the matrix, and I also
>> print something to the screen. So
>>
>>  PetscErrorCode ComputeMatrix1(KSP ksp, Mat J,Mat jac, void *ctx)
>>
>> {
>>
>>     ......
>>
>>     ierr    = PetscPrintf(PETSC_COMM_WORLD,"1\n");CHKERRQ(ierr);
>>
>>     ......
>>
>> }
>>
>>
>>  PetscErrorCode ComputeMatrix2(KSP ksp, Mat J,Mat jac, void *ctx)
>>
>> {
>>
>>     ......
>>
>>     ierr    = PetscPrintf(PETSC_COMM_WORLD,"2\n");CHKERRQ(ierr);
>>
>>     ......
>>
>> }
>>
>>
>>  Now if I compile and execute the code, it only print 2, completely
>> ignoring 1, which is not expected, since I have setup ksp1 to use
>> ComputeMatrix1.
>>
>>  Does anyone know what might be wrong? Thank you. I have attached the
>> short code here in this email as well.
>>
>>  Hui
>>
>


-- 
What most experimenters take for granted before they begin their
experiments is infinitely more interesting than any results to which their
experiments lead.
-- Norbert Wiener
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20150303/deddc724/attachment-0001.html>


More information about the petsc-users mailing list