I tried to share an allocatable array between two programs using dll. Here is my code:
data.f90-defines the shared array
module shared_allocate INTEGER, ALLOCATABLE, DIMENSION(:) :: TV !DEC$ ATTRIBUTES DLLEXPORT :: TV contains subroutine initialize (n) !DEC$ ATTRIBUTES DLLEXPORT :: initialize integer n allocate (tv(n)) return end subroutine initialize end module shared_allocate
test1.f90-initialize the array
program main1 use shared_allocate IMPLICIT NONE call initialize(3) tv = 2 DO WHILE (.true.) print *, "size=", size(tv) call sleep(1) END DO end program main1
test2.f90-read the array
program main use shared_allocate IMPLICIT NONE print *, "size=", size(tv) print *, "tv(1)=", tv(1) end program main
ifort /dll /libs:dll data.f90 /link /section:.data,RWS ifort /libs:dll test1.f90 data.lib ifort /libs:dll test2.f90 data.lib
When I run test1.exe and test2.exe at the same time, it reports the error:
forrtl: severe (157): Program Exception - access violation
Any suggestion? Thanks
The problem you are observing is you have two processes, each with their own virtual address spaces. While the DLL is shared, and where the array descriptor may or may not align at the same virtual address, the allocation, when made, will come from the virtual address of the process performing the call to the allocation routine.
What you may want to consider doing is use the OpenMPI feature that permits multiple processes to allocate a shared memory buffer. You can also do this using Windows API and/or Linux API (the MPI method will be transportable.)
Thank you for your prompt reply.
You mean that it's impossible to share allocatable array through dll?
In my tasks, I want to couple two different programs. I'm not sure if OpenMPI can apply to this.
You can share data between programs using a DLL, but not using ALLOCATABLE or POINTER. There is a worked example DLL\DLL_Shared_Data in the Parallel Studio XE for Windows Samples Bundle. It uses a module variable.
Not with an allocatable array, but if you combine the Windows memory map functions with a pointer array (and the C interoperability features C_PTR and C_F_POINTER) you can do something similar. You will be doing the "allocation" yourself through the Windows API. See https://docs.microsoft.com/en-us/windows/win32/memory/file-mapping to start - you would be using a pagefile-backed section.