[petsc-users] Reshaping a vector into a matrix

S V N Vishwanathan vishy at stat.purdue.edu
Wed Jan 5 21:06:30 CST 2011


Hi

In one of my programs I need to reshape a PETSc dense vector of
dimensions n*k into a dense matrix of dimension n x k (similar to what
you can do in, say, Matlab).  The vector is either  sequential or
parallel. What is the most painless way to achieve this?

My first crude attempt was as follows:

info=MPI_Comm_size(PETSC_COMM_WORLD,&size);CHKERRQ(info);

if(size==1){
    PetscScalar *vec_array;
    info=VecGetArray(vec,&vec_array);CHKERRQ(info);
info=MatCreateSeqDense(PETSC_COMM_WORLD,n,k,vec_array,&matrix);CHKERRQ(info);
    MatView(matrix,PETSC_VIEWER_STDOUT_WORLD);
    info=VecRestoreArray(vec,&vec_array);CHKERRQ(info);
    // MatDestroy here?
  }else{
	// Don't know how to handle this
}

This gave me a access violation :(

If you are curious as to the application where this arises, I am using
TAO to solve a machine learning problem. TAO expects the optimization
parameters to be a vector, but when I evaluate the objective function I
need to reshape them into a matrix. 

vishy



More information about the petsc-users mailing list