On Tue, Apr 28, 2009 at 7:12 AM, Matt Reilly <span dir="ltr"><<a href="mailto:matthewreilly@mac.com">matthewreilly@mac.com</a>></span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br>
Message: 2<br>
Date: Sun, 26 Apr 2009 23:37:10 +0200<br>
From: Jed Brown <jed@59A2.org><br>
Subject: Re: How to write a program, which can be run on 1 and<br>
multiple processors?<br>
To: PETSc users list <<a href="mailto:petsc-users@mcs.anl.gov" target="_blank">petsc-users@mcs.anl.gov</a>><br>
Message-ID: <49F4D406.3000908@59A2.org><br>
Content-Type: text/plain; charset=ISO-8859-1<br>
<br>
Yixun Liu wrote:<br>
<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hi,<br>
I want to make my code run on 1 or multiple processors. The code, which<br>
can run on multiple processors is like the following,<br>
<br>
MatCreate(PETSC_COMM_WORLD, &A);<br>
MatSetSizes(A, 3*numOfVerticesOfOneProcessor,<br>
3*numOfVerticesOfOneProcessor, systemSize, systemSize);<br>
<br>
</blockquote>
<br>
You don't have to provide both local and global size unless you want<br>
PETSc to check that these numbers are compatible.<br>
<br>
<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
MatSetFromOptions(A);<br>
MatMPIAIJSetPreallocation(A, 50, PETSC_NULL, 50, PETSC_NULL);<br>
<br>
However if I want to run on 1 processor I have to change the last code to:<br>
MatSeqAIJSetPreallocation(A,1000,PETSC_NULL);<br>
<br>
</blockquote>
^^^^<br>
you probably mean 100<br>
<br>
<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
How to avoid changing code?<br>
<br>
</blockquote>
<br>
Call both always. You can call {Seq,MPI}BAIJ preallocation while you're<br>
at it. The preallocation functions don't do anything unless they match<br>
the matrix type that you have.<br>
<br>
Jed<br>
<br>
<br>
</blockquote>
Jed's suggestion works, and is certainly reasonable.<br>
<br>
Personally, I'd advocate wrapping the sequence of matcreate/setfromoptions/preallocate in a higher<br>
level function, if possible. One version of the function exists for serial codes, the other for<br>
parallel. This keeps the code cleaner, doesn't rely on functions that don't do anything if the<br>
conditions aren't right, and will probably look cleaner (be easier to maintain). </blockquote><div><br>These exist already. They are not easier to maintain, and have disadvantages. That is why we switched.<br> </div>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><br>
The disadvantage of this approach is that you have two routines that perform the same function,<br>
but in slightly different ways. If you find a bug in one, it is important that you remember to fix<br>
the bug in both routines. In my own code, when I'm forced into this situation, I put a large reminder<br>
comment in both routines to remind me that there bug fixes in one routine should be propagated to<br>
the other.</blockquote><div><br>I am not sure what you mean. These routines do not perform the same function at all. There is no<br>code reuse.<br><br> Matt<br> </div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br>
Finally, some would suggest using #ifdef #endif conditional compilation. I'm not one of them.<br>
The problem with conditional compilation is that it is a slippery slope. You start off with just one<br>
or two instances, and then they breed. Soon you've got a giant program with dozens of places<br>
where a human reader will have quite a difficult time figuring out just what code gets compiled<br>
and what code doesn't.<br><font color="#888888">
<br>
matt<br></font></blockquote></div>-- <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>