- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
Compile with:
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
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Interestingly, test2.exe get the size of the array-tv with size() successfully but it fails to access the tv(1).
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.)
Jim Dempsey
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thank you. I have tested the common array and it works. But I have to deal with allocatable array. So is there any solution for sharing allocatable array?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
You can also do this using Windows API and/or Linux API (the MPI method will be transportable.)
Do you refer to the memory-mapped file?

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page