Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

use of pointer or allocatable array

lxh37
Beginner
619 Views

 

I developed an web applet in Ruby On Rails in MAc OSX, and have the computation part written in Fortran.
I wrapped Fortran by C and be able to call it from Ruby. I tested FORTRAN codes with a simple
console application and I can call it from Ruby as long as I don't call it using a thread.
When I called it using a thread, it crashes in function "prob" at line
            if( ran_genmn(rand, tmp1, tmp2, tmp3, err ) == RETURN_FAIL )

I commented out all codes inside function ran_genmn, it still crashes.

 codes:

    real(our_dble) function prob( n, delta, conf, rand, err ) result( answer )
        use program_constants
        use random_generator
        implicit none
        
        .....  
  
        real(our_dble), target :: one1( niter, 4 ), big_R( niter, 4), Sigma( 4, 4 )
        real(our_dble), pointer :: tmp1(:,:), tmp2(:,:), tmp3(:,:)

        ...
        nullify( tmp1, tmp2, tmp3 )
        ...
         do i=1,100
          
            ....
            tmp1 => big_R ! set pointers before passing them to genmn
            tmp2 => one1
            tmp3 => Sigma
            if( ran_genmn(rand, tmp1, tmp2, tmp3, err ) == RETURN_FAIL ) then
                answer = -1.d0
                return
            end if
            nullify( tmp1, tmp2, tmp3 ) ! clear the pointers
            ....
         end do
     ...
     return
     end function prob

   integer(our_int) function ran_genmn(gendata, x, mean, cov, &
      err ) result(answer)
      ...
      real(kind=our_dble), pointer :: mean(:,:), cov(:,:)
      real(kind=our_dble), pointer :: x(:,:), chol(:,:)
      ...

      ! local variables
      character(len=*), parameter :: subname = "ran_genmn"
      integer(kind=our_int) :: i, j, k, n, p, status
      real(kind=our_dble), allocatable :: rtmp(:)

      ....
  end function ran_genmn

After I changed one1 to allocatable array:
        real(kind=our_dble), allocatable :: one1(:,:)
and change function ran_genmn as:

   integer(our_int) function ran_genmn(gendata, n, p, x, mean, cov, &
      err ) result(answer)
      ....
      integer(our_int), intent(in) :: n, p
      real(kind=our_dble), intent(in) :: mean(n,p)
      real(kind=our_dble), pointer :: cov(:,:)
      real(kind=our_dble), pointer :: x(:,:), chol(:,:)
      ....
   end function ran_genmn

Then I have no problem to call it using a thread, I wonder why I can't use pointer for variable
one1 similar as the other two variables x and cov, the difference for one1 and the other two variables in function ran_genmn
is that one1 is only needed as input, the other two are computed inside ran_genmn.

0 Kudos
0 Replies
Reply