1

I'm new to Fortran (more experienced in C), and am trying to allocate memory for a matrix of values using the allocate command. The code is:

module SMS
contains
    subroutine readSMS(elem)

    real, dimension(:,:), allocatable :: elem

    allocate( elem(3792, 3) ) ! this will be a variable later on, for now I've
                              ! hard-coded the number of elements in my test case
    end subroutine
end module

When running, the code always crashes on the allocate line, with the error: Program exception - access violation. This program, however, works fine if elem is a local variable as opposed to one passed in to the subroutine. In C, I would use something like:

double **elem;

readSMS(&elem); //pass in a pointer to elem so that memory can be allocated

Is there a similar method in Fortran that I can use to allocate memory in a subroutine?

Here is the call to the function:

program main

use SMS
implicit none

include 'mpif.h'
! Variables
integer ierr,num_procs,my_id
integer num_elem,num_regions
real, dimension(:,:), allocatable :: elem
real, dimension(:,:), allocatable :: regions

! Body of main
call MPI_INIT(ierr)

call MPI_COMM_RANK(MPI_COMM_WORLD,my_id,ierr)
call MPI_COMM_SIZE(MPI_COMM_WORLD,num_procs,ierr)

call readSMS(elem)

deallocate(elem)

call MPI_FINALIZE(ierr)

end program main

At the moment, this is the entirety of the program (as you can see, I'm just getting started).

wolfPack88
  • 4,163
  • 4
  • 32
  • 47
  • 2
    There is nothing wrong with the code in isolation (`intent` would make me happier). We need to see the calling code (and declarations there, too) to be able to say much more. – francescalus Aug 18 '14 at 19:06
  • @francescalus: Edited to add the calling code and declarations. – wolfPack88 Aug 18 '14 at 19:13
  • 2
    possible duplicate of [How to pass allocatable arrays to subroutines in Fortran](http://stackoverflow.com/questions/13058743/how-to-pass-allocatable-arrays-to-subroutines-in-fortran) – Jonathan Dursi Aug 18 '14 at 19:14
  • You need an explicit interface to use allocatable arrays as arguments; best is to have the subroutine in a module. – Jonathan Dursi Aug 18 '14 at 19:15
  • In particular with Jonathan Dursi's link, note the requirement for an _explicit interface_. This appears missing from the main program. – francescalus Aug 18 '14 at 19:16
  • @JonathanDursi: I changed the code so that the subroutine is now in a module and I use that module in `main` (see edits above) and still get the same issue. – wolfPack88 Aug 18 '14 at 19:22
  • With the code as above (with a module) it works for me with ifort and gfortran; openmpi and intelmpi. – Jonathan Dursi Aug 18 '14 at 19:29
  • @JonathanDursi: I'm using intelmpi and it's giving me the same error. I'm using Windows (I'll redirect any comments on that to my employer), but that really shouldn't make a difference. The code works fine for me on ideone, as well. Not really sure what to do with that... – wolfPack88 Aug 18 '14 at 19:32
  • Could you try to replace `include 'mpif.h'` by `use mpi`? – Stefan Aug 18 '14 at 20:37

1 Answers1

0

@francescalus I think the subroutine readSMS is in the module, so the explicit interface is not the problem. @wolfPack88 look at the link provided by @Jonathan and you will be able to figure out the solution.

update:

module SMS
contains
subroutine readSMS(elem)

real, dimension(:,:), allocatable :: elem

allocate( elem(10, 3) ) 
elem=10                 
end subroutine
end module

program main

use SMS
implicit none

real, dimension(:,:), allocatable :: elem

call readSMS(elem)
print *, elem

deallocate(elem)
end program main

This code works in ifort without any issue...

sukhbinder
  • 1,001
  • 10
  • 9