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

How to create Fortran array referencing SafeArrayAccessData

MWind2
New Contributor III
1,161 Views

VBArray INOUT length known
integer(C_INTPTR_T) :: dptr
...
iRes=SafeArrayAccessData(VBArray,dptr)
dptr address is starting point of one dimensional c array of real{8}

I want to put it in a Fortran array so I can see it in debugger for one reason, but I want the Fortran array to reference the underlying data and not be a copy. Intention would be changes to data in Fortran array would be preserved in dptr array.

0 Kudos
1 Solution
Steve_Lionel
Black Belt Retired Employee
1,099 Views

Two errors. First, you omitted the size argument I specified to C_F_POINTER. Second, you treated the array as if it were zero-origin, but the array you get from C_F_POINTER is one-origin. (This could be finessed with pointer assignment redeclaring the bounds, but that seems unnecessary.)

I attach my modified df.f90. I didn't change anything else.

View solution in original post

23 Replies
Steve_Lionel
Black Belt Retired Employee
996 Views

Something like this:

use, intrinsic :: iso_c_binding
...
real(c_double), pointer :: array_data(:)
...
call c_f_pointer(transfer(dptr,c_null_ptr),array_data,[array_size])

where array_size is the number of elements in the array (can be a variable or a constant). After this, array_data is your one-dimensional array, no copy done.

MWind2
New Contributor III
982 Views

I seem to be getting an Exception thrown at 0x00007FF88E912D83 (df.dll) in EXCEL.EXE: 0xC0000005: Access violation writing location 0x0000000000000000 because of the "c_null_ptr" in the transfer.  Data is "adptr"(changed from dptr as dptr used as another pointer).

Steve_Lionel
Black Belt Retired Employee
978 Views

C_NULL_PTR as the second argument to TRANSFER is just the "mold" - it provides the type to interpret the source (first argument) as.  It is not accessed as data. If you got an access violation, then adptr was zero. I suggest adding some logging to your code (write to a file perhaps.)

MWind2
New Contributor III
967 Views

I attached a vs2022 folder with all inside. Excel is 64 bit and projects. 

Steve_Lionel
Black Belt Retired Employee
1,100 Views

Two errors. First, you omitted the size argument I specified to C_F_POINTER. Second, you treated the array as if it were zero-origin, but the array you get from C_F_POINTER is one-origin. (This could be finessed with pointer assignment redeclaring the bounds, but that seems unnecessary.)

I attach my modified df.f90. I didn't change anything else.

MWind2
New Contributor III
952 Views

Thank you! I spent all day trying for magic incantations. Intention here was to show that a Excel vba array of doubles used in calculations could be easily sent to a fortran dll and the results sent back in a position(s) in the array for Re: Reading Data from an Excel Spreadsheet? - Page 2 - Intel Communities. One change was made in formula 

array_data(4)=PI*array_data(1)*array_data(1)*array_data(2)*array_data(3)/4.0 for weight of cylinder agree with others I had Option 0 on in vba, but array in fortran was [1,4]. Without mucking it up, how to set array_data to be [0,3]?

Steve_Lionel
Black Belt Retired Employee
938 Views

Add a new array pointer declaration as follows:

 

real(8), pointer :: array_data(:), array_data1(:)

 

then replace the C_F_POINTER call with these two lines:

 

    call c_f_pointer(cptr,array_data1,[4])
    array_data(0:) => array_data1

 

I had thought I could do:

 

array_data(0:) => array_data

 

but that didn't work, for reasons still unclear to me (it caused array_data to become disassociated - this MIGHT be a bug, and I will investigate further.)

Edit: I can't reproduce the problem in a simple test. Shrug....

MWind2
New Contributor III
924 Views

Thank you! Sometimes I'm "cset" in my ways if permitted, and I would have still been looking for Rosetta stone.

While trying to find things that applied in books, I was thinking that most write to the overview, not that there is anything wrong with that, but a compendium of use questions and answers would be more useful at times. It seems forums nearly do the job, but there are problems with that like too much archiving or deleting of still relevant material, besides signal to noise.

MWind2
New Contributor III
904 Views

Is it possible to do this for the two dimensional array? So far I have had no success?

 

The bounds part works.

Steve_Lionel
Black Belt Retired Employee
896 Views

Look again at the VB.NET-Safearrays sample, from which you borrowed some code. It illustrates how to access arrays of any dimension.

MWind2
New Contributor III
887 Views

The looping into each one array goes fine, but combining or assigning those to a Fortran 2d array is where I am not getting.

Steve_Lionel
Black Belt Retired Employee
867 Views

Please zip and attach what you have now, and I'll take a look. 

MWind2
New Contributor III
858 Views

I've been using x1.xlsm, dfad and dcpp1, no progress with fortran.

Steve_Lionel
Black Belt Retired Employee
850 Views

You have a lot of code commented out, but I see this:

cptr=transfer(adptr,c_null_ptr)
  call c_f_pointer(cptr,array_data,[12])
  array_data(0:) => array_data1

That should be array_data1 in the second line. 

Where is the code where you're "assigning to a 2D Fortran array"?

MWind2
New Contributor III
844 Views

I was getting nowhere but locking exceptions, so this was what I was doing . Trying to do in fortran what was done in dcpp1 and so far getting nowhere.

Steve_Lionel
Black Belt Retired Employee
837 Views

I have not tried to edit your sources, but 1) You still have that error on the call to c_f_pointer, and 2) unlike the C++ code, you unlock the SafeArray before accessing it.

MWind2
New Contributor III
826 Views

When I use 

 

 

readloop:  do
  ires=SafeArrayGetElemsize(VBArray)
  ires = SafeArrayGetElement (VBArray, indexes(1), loc(adptr))
  !
  cptr=transfer(adptr,c_null_ptr)
  call c_f_pointer(cptr,array_data,[ilen])  
  call c_f_pointer(cptr,array_data1,[ilen])
  array_data(0:) => array_data1
  !
  !  array_data1(3)=PI*array_data1(0)*array_data1(0)*array_data1(1)*array_data1(2)
  do i = nbounds, 1, -1
     indexes(i) = indexes(i) + 1
     if (indexes(i) <= bounds(i)%ub) exit
       indexes(i) = bounds(i)%lb
     if (i == 1) exit readloop
  end do
  end do readloop
 iRes=SafeArrayUnlock(VBArray)

 

 

I get an element size of 8, which does not seem to be an array size. Maybe I'm reading it wrong but GetElement may be just a double.

MWind2
New Contributor III
822 Views

Got a fortran array with such but maybe order wrong:

 iRes=SafeArrayLock(VBArray)
  indexes = bounds%lb  ! Initialize to all lower bounds
  ilen=bounds(2)%ub-bounds(2)%lb
  ires=SafeArrayGetElemsize(VBArray)
  !ires = SafeArrayPtrOfIndex(VBArray, indx, cptr0)
  ires=SafeArrayAccessData(VBArray,adptr)
  !ires = SafeArrayGetElement (VBArray, indexes(1), loc(adptr))
  !
  cptr=transfer(adptr,c_null_ptr)
  call c_f_pointer(cptr,array_data,[12])  
  !call c_f_pointer(cptr,array_data1,[12])
  !array_data(0:) => array_data1
  !
  !  array_data1(3)=PI*array_data1(0)*array_data1(0)*array_data1(1)*array_data1(2)
 
  iRes=SafeArrayUnlock(VBArray)
MWind2
New Contributor III
817 Views

Not pretty but curious.

MWind2
New Contributor III
774 Views

I'm working on a general dimensional case now, but I found what I needed to do provided I know the ub,lb of each dimension with the following working for the 3X4:

                                 

 

 

call c_f_pointer(cptr,array_data,[ilen])  
  call c_f_pointer(cptr,array_data1,[ilen])
  array_data(0:) => array_data1
  a2d(0:2,0:3)=>array_data
  do i=0,2
      a2d(i,3)=PI*a2d(i,0)*a2d(i,0)*a2d(i,1)*a2d(i,2)/4
  end do

 

 

Reply