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

Access violation error when trying to share allocatable arrays through dll

csH2
초급자
2,003 조회수

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 포인트
8 응답
csH2
초급자
2,001 조회수

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

0 포인트
jimdempseyatthecove
명예로운 기여자 III
1,976 조회수

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 포인트
csH2
초급자
1,970 조회수

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 포인트
Steve_Lionel
명예로운 기여자 III
1,946 조회수

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 포인트
csH2
초급자
1,935 조회수

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 포인트
Steve_Lionel
명예로운 기여자 III
1,928 조회수

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 포인트
csH2
초급자
1,904 조회수
0 포인트
csH2
초급자
1,954 조회수

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 포인트
응답