- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
This topic has appeared in several posts, but I am having trouble making a slightly different implementation work and could use some help.
I have a FORTRAN simulation that must dynamically allocate a very large array of aero data. The array is too large to statically dimension. This array is defined in a module and sized using the allocate statement. I would like move this module definition into a DLL that can then be shared across multiple simulations running in parallel. The data is read only and the simulations do not interact with each other.
From an example I found in a post, the DLL code is:
module sharedmemory
!dec$ attributes dllexport :: darray, dummy
real, dimension(:), allocatable :: darray
real :: dummy = 111.1
end module sharedmemory
subroutine allocatearray
!dec$ attributes dllexport :: allocatearray
use sharedmemory
allocate( darray(3) )
return
end subroutine
The first process that makes the call to allocate the memory is:
program service
!dec$ attributes dllimport :: allocatearray
use sharedmemory
call allocatearray
! manipulate the numbers over a period of time
stop
end program
The second process that reads the data is:
program reader
use sharedmemory
!print the numbers over a period of time
stop
end program
I have changed the properties of both process to use the same run-time library as the DLL, and linker has "/section:.data, RWS" added.
Both processes see the dummy variable; it is statically allocated. Service allocates the array and manipulates it with no problem. Reader will either show garbage for the darray or throws a seg fault. If I don't allocate the memory in Service, Reader will throw an error about the pointer not being initialized. My guess is that, while the pointer variable darray is shared, the data it points to is actually local to the Service process.
How do I get the allocated data shared through the DLL?
Thanks,
Jim
Link Copied
- 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
I think you need to tidy up when you are done....
"Mapped views of a file mapping object maintain internal references to the object, and a file mapping object does not close until all references to it are released. Therefore, to fully close a file mapping object, an application must unmap all mapped views of the file mapping object by calling UnmapViewOfFile and close the file mapping object handle by calling CloseHandle. These functions can be called in any order."
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
That's probably good practice in case the structure of the program changes, but that clean-up will happen anyway when the process terminates.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks for the replies. I am heading down to the lab to give it a try. The code base for this simulation is very large, and I am trying to implement a solution that integrates with this base and doesn't require a lot of changes. The aero data array is already defined in a module and dynamically allocated. The path I went down seemed to offer a solution that had no code impact below the definition and allocation; except I couldn't allocate the memory across processes!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks for the code ... I have it running in our lab. I have tripped over something else though. I am trying to put the code to set up shared memory in a subroutine, and have created an include file containing the definitions for rk and data. The subroutine is able to create the shared memory and it can manipulate the data array. Once control returns to the program, data is still seen as an undimensioned array and an error gets thrown. The program does have the include file.
I am not understanding something.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Scratch that last question ... as you might have guessed FORTRAN is not my principle programming language.

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