<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Sat, Nov 30, 2013 at 2:56 PM, Gideon Simpson <span dir="ltr"><<a href="mailto:gideon.simpson@gmail.com" target="_blank">gideon.simpson@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Thanks Jed, see a few follow up comments below,<br>
On Nov 30, 2013, at 3:34 PM, Jed Brown <<a href="mailto:jedbrown@mcs.anl.gov">jedbrown@mcs.anl.gov</a>> wrote:<br>
<br>
> Gideon Simpson <<a href="mailto:gideon.simpson@gmail.com">gideon.simpson@gmail.com</a>> writes:<br>
><br>
>> I'm migrating some monte carlo code over to petsc, where the realizations will be done serially, but distributed across the cluster.  I'm inclined to involve petsc in this for its data management, and had a couple of questions.<br>

>><br>
>> If I plant to store the (scalar) results of the monte carlo simulations in a Petsc vector v, what is the recommended method for putting the results into this vector?  Currently, I have implemented:<br>
>><br>
>> VecGetOwnershipRange(sim_data, &local_start, &local_end);<br>
>><br>
>> VecGetArray(sim_data,& sim_data_local);<br>
>> k=0;<br>
>><br>
>> for(i=local_start;i<local_end;i++){<br>
>><br>
>>    sim_result = mc_sim_cod(mc_args);<br>
>><br>
>>    sim_data_local[k] = sim_result;<br>
>>    k++;<br>
>><br>
>>  }<br>
>> VecRestoreArray(sim_data, & sim_data_local);<br>
>><br>
>> Is this a *good* solution, or should I be using SetValue routines?<br>
><br>
> The above is fine.<br>
><br>
>> Also, I have MC simulations which, instead of returning a single<br>
>> scalar, return some n-dimensional array of real values.  In that case,<br>
>> I was thinking to make sim_data a petsc matrix.  If each result was<br>
>> n-dimensional, how would I initialize the matrix to hold M<br>
>> realizations of this n dimensional matrix?  ensuring that each set of<br>
>> n was contiguous on the local machine?<br>
><br>
> Mat is really for linear operators, not a container for data that you<br>
> want to interpret as 2-dimensional.  DMDA can do the latter, especially<br>
> if you want to distribute.<br>
><br>
>> Alternatively, this could be an n*M vector, but again, I'd need to<br>
>> ensure each chunk of n was located on the same machine?<br>
><br>
> Just set the local sizes of the Vec such that the entire subset is<br>
> local.  See the middle argument to VecSetSizes().<br>
<br>
If each realization generated a size n output, is there something slicker than this:<br>
<br>
VecGetOwnershipRange(sim_data, &local_start, &local_end);<br>
<br>
VecGetArray(sim_data,& sim_data_local);<br>
k=0;<br>
<br>
m_local = (local_end - local_start-1)/n;<br>
<br>
for(i=0;i< m_local;i++){<br>
<br>
   mc_sim_cod(mc_args, &sim_result);<br>
<br>
   for(k = 0;k<n;k++){<br>
<br>
      sim_data_local[i * n + k] = sim_result[k];<br>
   }<br>
<br>
 }<br>
VecRestoreArray(sim_data, & sim_data_local);<br>
<br>
I see  expressions like sim_data_local[i * n + k] to handle the indexing as  bit awkward.<br></blockquote><div><br></div><div>I guess it depends on what you want to do. You could have n independent serial vectors.</div><div>
If you want to maintain the global vector, you can always define the pointer at the top of</div><div>the loop.</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">

><br>
>> I presume here, I would use SetValue routines to insert each chunk of<br>
>> data.<br>
><br>
> You could, but writing directly into the array is also fine.<br>
><br>
>> Also, this may have to do with it being the newest matlab, 2013b, but when I try to load petsc vectors i receive the following error:<br>
>><br>
>> Error using load<br>
>> Number of columns on line 2 of ASCII file sim_data.out<br>
>> must be the same as previous lines.<br>
>><br>
>> where I constructed the sim_data.out file using the lines:<br>
>><br>
>> PetscViewerASCIIOpen(PETSC_COMM_WORLD, "sim_data.out", &output_viewer);<br>
>> PetscViewerSetFormat(output_viewer, PETSC_VIEWER_ASCII_MATLAB);<br>
>> VecView(sim_data,output_viewer);<br>
>> PetscViewerDestroy(&output_viewer);<br>
>><br>
>> Looking at the ascii file, it looks fine.<br>
><br>
> Did MATLAB change their ASCII format?<br>
<br>
Here's a quick test on 2013b:<br>
v = linspace(0,1,5);<br>
save('test.out', 'v', '-ascii');<br>
<br>
test.out looks like:<br>
   0.0000000e+00   2.5000000e-01   5.0000000e-01   7.5000000e-01   1.0000000e+00<br>
<br>
<br>
><br>
> Anyway, it is much better to write binary output and read with<br>
> PetscBinaryRead (bin/matlab/PetscBinaryRead.m).<br>
<br>
</blockquote></div><br><br clear="all"><div><br></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
</div></div>