Copy device data back to Fortran pointer

[Old posts from the commercial version of ArrayFire] Discussion of ArrayFire using CUDA or OpenCL.

Moderator: pavanky

Copy device data back to Fortran pointer

Postby hazm13 » Sun Feb 16, 2014 1:19 pm

Hi,

Is there any way to copy device data directly back to a pointer in Fortran rather than to an allocatable array, and then setting the pointer equal to that allocatable array?

i.e.
Code: Select all
type(array) ARR1, ARR2, ARR3
real,pointer,dimension(:,:) :: pointer

pointer => some other array

ARR1 = some array
ARR2 = some array
ARR3 = matmul(ARR1, ARR2)

pointer = ARR3


Instead of this I currently have to use an allocatable array, copy the device data back into this, and then set the pointer equal to the allocatable array.

If I try the method in the psuedo code above, I get:

Error: Can't convert TYPE(array) to REAL(4)

Oddly, copying data to a device array from a pointer works fine, just not going back the other way.

Thanks, Harry
hazm13
 
Posts: 12
Joined: Wed Feb 12, 2014 3:22 pm
Location: Southampton

Re: Copy device data back to Fortran pointer

Postby pavanky » Mon Feb 17, 2014 7:20 pm

Hi,

Please follow these steps.

1) Make a backup of arrayfire/fortran/module/arrayfire.f95
2) Download the attachment and rename it as arrayfire.f95
3) Copy the renamed file into arrayfire/fortran/module/

To copy the data from GPU to the pointer you will need to call something like this:

Code: Select all
program basic
  use arrayfire
  implicit none
 
  real, target, dimension(3,3) :: a
  real, target, dimension(3,3) :: b
  real, pointer, dimension(:,:) :: ptrA
  real, pointer, dimension(:,:) :: ptrB
 
  type(array) M1
 
  a(1,:) = (/ 1, 0, 0 /)
  a(2,:) = (/ 0, 3, 0 /)
  a(3,:) = (/ 0, 0, 2 /)
 
  ptrA => a(:,:)
  ptrB => b(:,:)
   
  ! Copy data from host to device                                                                                                                                                             
  M1 = ptrA
  write(*,*) "Showing the matrix after mem copy from host (M1)"
  call print(M1)
 
  ! Get back to host                                                                                                                                                                           
  call getptr(ptrB, M1)
  write(*,*) "Showing min(M2) data back on host "
  write(*,*) a(:,:)
  write(*,*) b(:,:)

end program basic


This is a quick hack to get what you are asking to work. There are no checks on boundary conditions done. Please make sure the location being pointed to has enough memory allocated already.
Attachments
arrayfire.f95.txt
(99.06 KiB) Downloaded 494 times
Pavan Yalamanchili,
ArrayFire
--
~ If it is not broken, you have not tried hard enough ~
User avatar
pavanky
Site Admin
 
Posts: 1123
Joined: Mon Mar 15, 2010 7:39 pm
Location: Atlanta, GA

Re: Copy device data back to Fortran pointer

Postby hazm13 » Mon Feb 17, 2014 8:57 pm

Thank you, that works exactly how I need it to.
hazm13
 
Posts: 12
Joined: Wed Feb 12, 2014 3:22 pm
Location: Southampton


Return to [archive-commercial] Programming & Development with ArrayFire

cron