Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Beginner
144 Views

Access violation error when trying to share allocatable arrays through dll

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
Highlighted
Beginner
142 Views

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

0 Kudos
Highlighted
117 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
Highlighted
Beginner
111 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
Highlighted
Beginner
95 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
Highlighted
Black Belt Retired Employee
87 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.

--
Steve (aka "Doctor Fortran") - https://stevelionel.com/drfortran
0 Kudos
Highlighted
Beginner
76 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
Highlighted
Black Belt Retired Employee
69 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.

--
Steve (aka "Doctor Fortran") - https://stevelionel.com/drfortran
0 Kudos
Highlighted
Beginner
45 Views

Thank you for your kind help! 

0 Kudos