Problems with VecGetArray in Fortran

Barai, Pallab baraip at ornl.gov
Tue Dec 15 13:59:38 CST 2009


Hello,

I am initializing a Petsc Vector with 1.0 using the VecSet function. When I try to extract the values using VecGetArray, I get junk output in Fortran. I implemented the same thing in C and it works fine in C. 

The output and test code for both the Fortran and C are given below.

Is this a problem with the VecGetArray or am I missing something in my Fortran version of the code. Any help will be highly appreciated.

I ran it on 1 processor in a Cray XT-4 machine using PGI compilers.

Thanking you.
Pallab



The output from Fortran code looks like: [The correct value is 1.0]

********************************************
 arr(            1 )=   4.5088266034982808E-313
 arr(            2 )=   4.5088266034982808E-313
 arr(            3 )=   4.5088266034982808E-313
 arr(            4 )=   4.5088266034982808E-313
 arr(            5 )=   4.5088266034982808E-313
 arr(            6 )=   4.5088266034982808E-313
 arr(            7 )=   4.5088266034982808E-313
 arr(            8 )=   4.5088266034982808E-313
 arr(            9 )=   4.5088266034982808E-313
 arr(           10 )=   4.5088266034982808E-313
***********************************************

The output from the C code is: [this is the correct output]

************************************************
arr[0] = 1.000000
arr[1] = 1.000000
arr[2] = 1.000000
arr[3] = 1.000000
arr[4] = 1.000000
arr[5] = 1.000000
arr[6] = 1.000000
arr[7] = 1.000000
arr[8] = 1.000000
arr[9] = 1.000000
**************************************************


The test codes are as follows:

Fortran:
**************************************************
program F_test_Vec

#include "finclude/petsc.h"
#include "finclude/petscvec.h"

      Vec tmp
      PetscScalar arr(1)
      integer i
      PetscOffset i_x

      call PetscInitialize(PETSC_NULL_CHARACTER,ierr)

      call VecCreate(PETSC_COMM_WORLD, tmp, ierr)
      call VecSetSizes(tmp, 10, PETSC_DECIDE, ierr)
      call VecSetType(tmp,VECSEQ,ierr)
      call VecSet(tmp, 1.0, ierr)

      call VecGetArray(tmp, arr, i_x, ierr)

      do i=1,10
        write(*,*) 'arr(',i,')=',arr(i_x+i)
      enddo

      call VecRestoreArray(tmp, arr, i_x, ierr)

      call VecDestroy(tmp, ierr)

      call PetscFinalize(ierr)
      end
**********************************************************



C:
***********************************************************
#include "petsc.h"
#include "petscvec.h"
#include "stdio.h"

int main(int argc, char** argv) {
  Vec tmp;
  PetscScalar* arr;
  int i;

  PetscInitialize(&argc, &argv, NULL, NULL);

  VecCreate(PETSC_COMM_WORLD, &tmp);
  VecSetSizes(tmp, 10, PETSC_DECIDE);
  VecSetType(tmp,VECSEQ);
  VecSet(tmp, 1.0);

  VecGetArray(tmp, &arr);

  for(i = 0; i < 10; i++) {
    printf("arr[%d] = %lf\n", i, arr[i]);
  }

  VecRestoreArray(tmp, &arr);

  VecDestroy(tmp);

  PetscFinalize();
}
****************************************************


More information about the petsc-users mailing list