gfxgfx
 
Please login or register.

Login with username, password and session length
 
gfx gfx
gfx
1565 Posts in 517 Topics by 752 Members - Latest Member: zaikunzhang September 25, 2021, 05:30:33 PM
*
gfx* Home | Help | Search | Login | Register | gfx
gfx
Absoft User Forum  |  Support  |  Windows  |  ALLOCATE an array to a memory address - how? Solved :-)
gfx
gfxgfx
 

Author Topic: ALLOCATE an array to a memory address - how? Solved :-)  (Read 508 times)

Mike Linacre

  • Sr. Member
  • ****
  • Posts: 250
ALLOCATE an array to a memory address - how? Solved :-)
« on: June 28, 2021, 01:04:15 AM »
Folks:  The Fortran Reference Manual is cryptic. How do I do this?
The memory location is in an integer variable "MEMADDRESS"

There is the memory allocation statement:
  ALLOCATE (MYARRAY (NI, LOCALMAXSCORE, 0:NJMAX))

Now I want this array to start at location MEMADDRESS.

This doesn't work:
MEMADDRESS => MYARRAY
not this
MYARRAY => MEMADDRESS

What does work?

Thanks ....

« Last Edit: June 29, 2021, 10:31:23 PM by Mike Linacre »

forumadmin

  • Administrator
  • Sr. Member
  • *****
  • Posts: 320
Re: ALLOCATE an array to a memory address - how?
« Reply #1 on: June 28, 2021, 10:15:49 AM »
It's not completely clear to me from your post what you are trying to accomplish.  You have an integer variable  MEMADDRESS and allocatable array MYARRAY. Do you want the storage associated with allocatable array MYARRAY to start at the same memory address as MEMADDRESS, essentially an equivalence relationship? Or, does the MEMADDRESS variable contain a value that you want to use as the starting address for the storage associated with MEMARRAY?

Mike Linacre

  • Sr. Member
  • ****
  • Posts: 250
Re: ALLOCATE an array to a memory address - how?
« Reply #2 on: June 29, 2021, 12:13:48 AM »
Thanks, forumadmin:

Either option is good. I can arrange that MEMADDRESS contains the memory address
INTEGER  MEMADDRESS
MEMADDRESS = loc(where I want)
 or I can arrange that MEMADDRESS is at the memory address.
POINTER (PMEMADDRESS, MEMADDRESS)
PMEMADDRESS = loc(where I want)

Now for the fun :-) How to get ALLOCATE() to start at the memory address ...

forumadmin

  • Administrator
  • Sr. Member
  • *****
  • Posts: 320
Re: ALLOCATE an array to a memory address - how?
« Reply #3 on: June 29, 2021, 06:59:55 AM »
In the Absoft compiler, at least, there is no way to specify the storage location for an allocatable array by hand. The ALLOCATE statement calls into the operating system to obtain a block of memory to hold the array, and stores the result in an internal housekeeping structure that is not accessible to user code.

Mike Linacre

  • Sr. Member
  • ****
  • Posts: 250
Re: ALLOCATE an array to a memory address - how? Solved :-)
« Reply #4 on: June 29, 2021, 10:00:23 PM »
Thanks forumadmin. Very clear.

Here's my solution, in case anyone has this same problem:

(a) Allocate the array to somewhere in memory
REAL, DIMENSION(:,:,:), ALLOCATABLE :: MYARRAY
ALLOCATE (MYARRAY (NI, LOCALMAXSCORE, 0:NJMAX))

(b)  Move data from elsewhere in memory into the array
CHARACTER*(*)  A, B
POINTER (PA,A), (PB,B)
PA = LOC(MEMADDRESS)
PB = LOC(MYARRAY)
ARRAYLENGTH = NI * LOCALMAXSCORE*(1+NJMAX)*4  ! REAL*4
B(1:ARRAYLENGTH) = A(1:ARRAYLENGTH)

(c) ... do the processing of the array here ....

(d) move back, if needed:
A(1:ARRAYLENGTH) = B(1:ARRAYLENGTH)

(e) clean up
DEALLOCATE (MYARRAY)

Simple!
« Last Edit: June 29, 2021, 10:30:37 PM by Mike Linacre »

mecej4

  • Jr. Member
  • **
  • Posts: 70
Re: ALLOCATE an array to a memory address - how? Solved :-)
« Reply #5 on: July 06, 2021, 02:38:39 AM »
The posted "solution" relies on a nonstandard feature, namely, the Cray pointer. Cray pointers are quite different from Fortran 90+ pointers, as can be seen from, for example, https://docs.oracle.com/cd/E19957-01/805-4941/z40000a54ba7/index.html .


Furthermore, caution is needed to avoid unpleasant surprises when more than one pointee can be associated with the same pointer, as in the following example, taken from the same Sun/Oracle manual:
Code: [Select]
program crayptr
POINTER ( p, b ),  ( p, c )
REAL x, b, c
p = LOC( x )
b = 1.0
c = 2.0
PRINT *, b
end
What do you expect the output to be?
« Last Edit: July 07, 2021, 09:09:21 AM by mecej4 »

Mike Linacre

  • Sr. Member
  • ****
  • Posts: 250
Re: ALLOCATE an array to a memory address - how? Solved :-)
« Reply #6 on: July 15, 2021, 10:45:02 PM »
Thanks, mecej4.  Yes, my solution is klutzy.

Can you suggest a FORTRAN 90+ solution to this memory-addressing problem?

mecej4

  • Jr. Member
  • **
  • Posts: 70
Re: ALLOCATE an array to a memory address - how? Solved :-)
« Reply #7 on: July 20, 2021, 09:16:49 PM »
The problem with what you want to do begins with The memory location is in an integer variable "MEMADDRESS".


A Fortran "pointer" is quite different from C pointers. You have probably noted by now that Fortran does not have a dereferencing operator such as C's '*' (if p is a C pointer, *p is the pointee). You cannot assign arbitrary integer values to a Fortran "pointer".


If you exclude the facilities provided by the ISO-C interoperability features of Fortran 2003+, the simple answer to your problem goal is "it can't be done".


Think of a Fortran pointer as a Fortran variable with an additional attribute: it can be associated with another variable, which may or may not itself be a pointer. One could think of a Fortran pointer variable as an alias for another variable; in the special case where we allocate a Fortran pointer variable, the target has no name; i.e., the pointer variable is aliased to an anonymous variable.


As ForumAdmin said, it is not clear what you want to do. Why do you want to be able to tie a variable to an absolute memory address in a user mode program in a virtual memory OS?
« Last Edit: July 21, 2021, 01:49:48 AM by mecej4 »

Absoft User Forum  |  Support  |  Windows  |  ALLOCATE an array to a memory address - how? Solved :-)
 

gfxgfx
gfx gfx
Powered by MySQL Powered by PHP Valid XHTML 1.0! Valid CSS!