Hi,<div><br></div><div>I modify the KSP ex1 to a matrix free version, everything works. Basically, I define a usermult function and pass it to <span class="Apple-style-span" style="font-family: monospace; font-size: medium; white-space: pre; "><a href="http://www.mcs.anl.gov/petsc/petsc-as/snapshots/petsc-3.0.0/docs/manualpages/Mat/MatShellSetOperation.html#MatShellSetOperation">MatShellSetOperation</a>, as shown in </span><a href="http://www.mcs.anl.gov/petsc/petsc-as/snapshots/petsc-3.0.0/docs/manualpages/Mat/MatShellSetOperation.html">http://www.mcs.anl.gov/petsc/petsc-as/snapshots/petsc-3.0.0/docs/manualpages/Mat/MatShellSetOperation.html</a></div>

<meta http-equiv="content-type" content="text/html; charset=utf-8"><meta http-equiv="content-type" content="text/html; charset=utf-8"><div><br></div><div><meta http-equiv="content-type" content="text/html; charset=utf-8"><span class="Apple-style-span" style="font-family: &#39;Times New Roman&#39;; font-size: medium; "><pre id="aeaoofnhgocdbnbeljkmbjdmhbcokfdb-mousedown">

<a href="http://www.mcs.anl.gov/petsc/petsc-as/snapshots/petsc-3.0.0/docs/manualpages/Sys/PetscErrorCode.html#PetscErrorCode">PetscErrorCode</a> usermult(<a href="http://www.mcs.anl.gov/petsc/petsc-as/snapshots/petsc-3.0.0/docs/manualpages/Mat/Mat.html#Mat">Mat</a>,<a href="http://www.mcs.anl.gov/petsc/petsc-as/snapshots/petsc-3.0.0/docs/manualpages/Vec/Vec.html#Vec">Vec</a>,<a href="http://www.mcs.anl.gov/petsc/petsc-as/snapshots/petsc-3.0.0/docs/manualpages/Vec/Vec.html#Vec">Vec</a>);
</pre></span><span class="Apple-style-span" style="font-family: &#39;Times New Roman&#39;; font-size: medium; "><pre>ierr = <a href="http://www.mcs.anl.gov/petsc/petsc-as/snapshots/petsc-3.0.0/docs/manualpages/Mat/MatCreateShell.html#MatCreateShell">MatCreateShell</a>(<a href="http://www.mcs.anl.gov/petsc/petsc-as/snapshots/petsc-3.0.0/docs/manualpages/Sys/comm.html#comm">comm</a>,m,n,M,N,ctx,&amp;A);
</pre></span><span class="Apple-style-span" style="font-family: &#39;Times New Roman&#39;; font-size: medium; "><pre>ierr = <a href="http://www.mcs.anl.gov/petsc/petsc-as/snapshots/petsc-3.0.0/docs/manualpages/Mat/MatShellSetOperation.html#MatShellSetOperation">MatShellSetOperation</a>(A,MATOP_MULT,(void(*)(void))usermult);</pre>

<pre><br></pre><pre>Now, I have a problem when I try to change the above to a c++ version(I want to use a class).</pre><pre>Let&#39;s see I define the following class and it has a function usermult</pre><pre>Class classA{</pre>

<pre><meta http-equiv="content-type" content="text/html; charset=utf-8"><span class="Apple-style-span" style="font-family: &#39;Times New Roman&#39;; white-space: normal; "><pre id="aeaoofnhgocdbnbeljkmbjdmhbcokfdb-mousedown">

<a href="http://www.mcs.anl.gov/petsc/petsc-as/snapshots/petsc-3.0.0/docs/manualpages/Sys/PetscErrorCode.html#PetscErrorCode">PetscErrorCode</a> usermult(<a href="http://www.mcs.anl.gov/petsc/petsc-as/snapshots/petsc-3.0.0/docs/manualpages/Mat/Mat.html#Mat">Mat</a>,<a href="http://www.mcs.anl.gov/petsc/petsc-as/snapshots/petsc-3.0.0/docs/manualpages/Vec/Vec.html#Vec">Vec</a>,<a href="http://www.mcs.anl.gov/petsc/petsc-as/snapshots/petsc-3.0.0/docs/manualpages/Vec/Vec.html#Vec">Vec</a>);</pre>

<pre id="aeaoofnhgocdbnbeljkmbjdmhbcokfdb-mousedown">....</pre></span></pre><pre>};</pre><pre><meta http-equiv="content-type" content="text/html; charset=utf-8"><span class="Apple-style-span" style="font-family: &#39;Times New Roman&#39;; white-space: normal; "><pre id="aeaoofnhgocdbnbeljkmbjdmhbcokfdb-mousedown">

<a href="http://www.mcs.anl.gov/petsc/petsc-as/snapshots/petsc-3.0.0/docs/manualpages/Sys/PetscErrorCode.html#PetscErrorCode">PetscErrorCode</a> classA::usermult(<a href="http://www.mcs.anl.gov/petsc/petsc-as/snapshots/petsc-3.0.0/docs/manualpages/Mat/Mat.html#Mat">Mat</a>,<a href="http://www.mcs.anl.gov/petsc/petsc-as/snapshots/petsc-3.0.0/docs/manualpages/Vec/Vec.html#Vec">Vec</a>,<a href="http://www.mcs.anl.gov/petsc/petsc-as/snapshots/petsc-3.0.0/docs/manualpages/Vec/Vec.html#Vec">Vec</a>)</pre>

<pre id="aeaoofnhgocdbnbeljkmbjdmhbcokfdb-mousedown">{</pre><pre id="aeaoofnhgocdbnbeljkmbjdmhbcokfdb-mousedown">....</pre><pre id="aeaoofnhgocdbnbeljkmbjdmhbcokfdb-mousedown">}</pre></span></pre><pre>If I want to use the matrix free method, in the main function, I tried several different ways to pass the function usermult to MatShellSetOperation, but they all fails. Does anyone know how to do this?</pre>

<pre>int main(int argc, char ** argv)</pre><pre>{</pre><pre>classA ob;</pre><pre>...</pre><pre><meta http-equiv="content-type" content="text/html; charset=utf-8"><span class="Apple-style-span" style="font-family: arial; white-space: normal; font-size: small; "><span class="Apple-style-span" style="font-family: &#39;Times New Roman&#39;; font-size: medium; "><pre>

ierr = <a href="http://www.mcs.anl.gov/petsc/petsc-as/snapshots/petsc-3.0.0/docs/manualpages/Mat/MatCreateShell.html#MatCreateShell">MatCreateShell</a>(<a href="http://www.mcs.anl.gov/petsc/petsc-as/snapshots/petsc-3.0.0/docs/manualpages/Sys/comm.html#comm">comm</a>,m,n,M,N,ctx,&amp;A);
</pre></span><span class="Apple-style-span" style="font-family: &#39;Times New Roman&#39;; font-size: medium; "><pre>ierr = <a href="http://www.mcs.anl.gov/petsc/petsc-as/snapshots/petsc-3.0.0/docs/manualpages/Mat/MatShellSetOperation.html#MatShellSetOperation">MatShellSetOperation</a>(A,MATOP_MULT,(void(*)(void))ob.usermult); ?</pre>

</span></span></pre><pre><br></pre><pre>}</pre></span><br><div class="gmail_quote">On Wed, Jun 8, 2011 at 5:38 AM, Matthew Knepley <span dir="ltr">&lt;<a href="mailto:knepley@gmail.com">knepley@gmail.com</a>&gt;</span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><div><div></div><div class="h5">On Tue, Jun 7, 2011 at 10:20 PM, Xiang Hao <span dir="ltr">&lt;<a href="mailto:haoxiang@yahoo.cn" target="_blank">haoxiang@yahoo.cn</a>&gt;</span> wrote:<br>

</div></div><div class="gmail_quote"><div><div></div><div class="h5"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<span style="border-collapse:collapse;font-family:arial, sans-serif;font-size:13px"><div>

Hi all,</div><div><br></div><div>I need to solve a PDE ( basically a Poisson&#39;s equation with Neumann boundary condition ), and I solved it using a steepest descent method, which is very slow. Now I want to solve the PDE using GMRES. In addition, I do have the matrix A, but I have a function which computes Ax, so I also need to use the matrix free method.</div>




<div><br></div><div>I am very new to PETSc, so I am looking for examples on solving linear systems using GMRES and/or matrix-free method.</div></span></blockquote><div><br></div></div></div><div>You can look at SNES ex5 for the Poisson equation (after setting lambda = 0.0). SNES ex19 also shows how to use geometric Multigrid.</div>


<div>Its not hard to do this for Poisson.</div><div><br></div><div>  Thanks,</div><div><br></div><div>     Matt</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">


<span style="border-collapse:collapse;font-family:arial, sans-serif;font-size:13px"><div>Thanks,</div><div>Xiang</div></span><font color="#888888"><br><br>
</font></blockquote></div><br><br clear="all"><br>-- <br>What most experimenters take for granted before they begin their experiments is infinitely more interesting than any results to which their experiments lead.<br>-- Norbert Wiener<br>



</blockquote></div><br></div>