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

Access violation error when trying to share allocatable arrays through dll

csH2
Beginner
951 Views

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

0 Kudos
8 Replies
csH2
Beginner
949 Views

Interestingly, test2.exe get the size of the array-tv with size() successfully but it fails to access the tv(1).

0 Kudos
jimdempseyatthecove
Honored Contributor III
924 Views

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

0 Kudos
csH2
Beginner
918 Views

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.

 

0 Kudos
Steve_Lionel
Honored Contributor III
894 Views

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.

0 Kudos
csH2
Beginner
883 Views

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?

0 Kudos
Steve_Lionel
Honored Contributor III
876 Views

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.

0 Kudos
csH2
Beginner
852 Views
0 Kudos
csH2
Beginner
902 Views

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?

0 Kudos
Reply