[petsc-users] petsc vector management for monte carlo

Gideon Simpson gideon.simpson at gmail.com
Sat Nov 30 14:19:42 CST 2013


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.

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:

VecGetOwnershipRange(sim_data, &local_start, &local_end);

VecGetArray(sim_data,& sim_data_local);
k=0;

for(i=local_start;i<local_end;i++){

    sim_result = mc_sim_cod(mc_args);

    sim_data_local[k] = sim_result;
    k++;

  }
VecRestoreArray(sim_data, & sim_data_local);

Is this a *good* solution, or should I be using SetValue routines?

Also, I have MC simulations which, instead of returning a single scalar, return some n-dimensional array of real values.  In that case, I was thinking to make sim_data a petsc matrix.  If each result was n-dimensional, how would I initialize the matrix to hold M realizations of this n dimensional matrix?  ensuring that each set of n was contiguous on the local machine?  

Alternatively, this could be an n*M vector, but again, I'd need to ensure each chunk of n was located on the same machine?  I presume here, I would use SetValue routines to insert each chunk of data.

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:

Error using load
Number of columns on line 2 of ASCII file sim_data.out
must be the same as previous lines.

where I constructed the sim_data.out file using the lines:

PetscViewerASCIIOpen(PETSC_COMM_WORLD, "sim_data.out", &output_viewer);
PetscViewerSetFormat(output_viewer, PETSC_VIEWER_ASCII_MATLAB);
VecView(sim_data,output_viewer);
PetscViewerDestroy(&output_viewer);

Looking at the ascii file, it looks fine.


-gideon



More information about the petsc-users mailing list