I am currently writing a Fortran subroutine that processes a (possibly large) data array. I am using multiple subroutines from multiple modules and would like to have the input data array, as well as the output data array as global variables so that I can read / write them in every subroutine. However I would like to refrain from copying the data arrays unnecessarily, because I fear it would slow down the whole program (as said before, the data arrays are potentially very big, ~10.000x5 entries or so).
At the moment, I use a variable module which contains global variables for all subroutines. I read the input and output arrays into my subroutine and then copy the input values onto the global array, perform the calculations and then copy the global output array onto the output array I have within my subroutine. The code for the subroutine looks as follows:
subroutine flexible_clustering(data_array_in, limits_in, results_array_out)
use globalVariables_mod
use clusterCreation_mod
use clusterEvaluation_mod
implicit none
real*8, dimension(:,:) :: data_array_in
real*8, dimension(:) :: limits_in
real*8, dimension(:,:) :: results_array_out
! determine dimensions
data_entries_number = size(data_array_in(:,1))
data_input_dimension = size(data_array_in(1,:))
data_output_dimension = size(results_array_out(1,:))
! allocate and fill arrays
call allocate_global_variable_arrays()
data_array = data_array_in
limits = limits_in
! clustering
call cluster_creation()
call cluster_evaluation()
results_array_out = results_array
call reset_global_variables()
end subroutine flexible_clustering
The global variables used here are defined as follows in globalVariables_mod (with appropriate allocate / deallocate subroutines):
integer :: data_entries_number = 0, data_input_dimension = 0, data_output_dimension = 0
integer :: total_cluster_number = 0
real*8, allocatable, dimension(:) :: limits
real*8, allocatable, dimension(:,:) :: data_array
real*8, allocatable, dimension(:,:) :: results_array
Summed up, I take data_array_in, limits_in and results_array_out and copy them to data_array, limits and results_array to make them global variables in all subroutines.
Is there a way to omit this copying? Maybe using pointers? Can I optimize this another way?
Thanks in advance!