PGI User Forum
 SearchSearch   MemberlistMemberlist     RegisterRegister   ProfileProfile    Log inLog in 

CUDA-x86.

passing device data to C function from Fortran

 
Post new topic   Reply to topic    PGI User Forum Forum Index -> Accelerator Programming
View previous topic :: View next topic  
Author Message
Tuan



Joined: 11 Jun 2009
Posts: 233

PostPosted: Wed Jun 09, 2010 3:04 pm    Post subject: passing device data to C function from Fortran Reply with quote

Suppose that I have a C function which receives void* arguments pointing to a location in device memory. This C host function will do something with these device data, e.g. call a C kernel to perform adding two arrays arr1, arr2 into arr3.

My question is that
Code:
void func1(const void* arr1,const void*arr2, void* arr3)

How could I call this function from Fortran.
I tried this

Code:
interface
  subroutine func1(arr1, arr2, arr3)
     use iso_c_binding
      type(c_ptr), intent(IN) :: arr1, arr2
      type(c_ptr) :: arr3
  end subroutine
end interface

double precision, allocatable, dimension(:), device, target: arr1,arr2, arr3
type(c_ptr) :: carr1, carr2, carr3
// generate data for arr1, arr2, arr3 on device
   ...

// map to C ptr
carr1 = c_loc(arr1)
carr2 = c_loc(arr2)
carr3 = c_loc(arr3)
//
call func1(carr1, carr2, carr3)


However, it seems that I cannot use TARGET with DEVICE attribute together. Could you please give me a solution for this problem.

Thanks,
Tuan
Back to top
View user's profile
mkcolg



Joined: 30 Jun 2004
Posts: 5952
Location: The Portland Group Inc.

PostPosted: Thu Jun 10, 2010 10:44 am    Post subject: Reply with quote

Hi Tuan,

For device pointers, you shouldn't need to use the c_ptr type. Just declare them as 'device' allocatable.

Something like:
Code:
interface
  subroutine func1(arr1, arr2, arr3)
     use iso_c_binding
      real(c_double), allocatable, dimension(:), device :: arr1,arr2, arr3
  end subroutine
end interface

double precision, allocatable, dimension(:), device :: arr1,arr2, arr3

Hope this helps,
Mat
Back to top
View user's profile
Tuan



Joined: 11 Jun 2009
Posts: 233

PostPosted: Thu Jun 10, 2010 12:50 pm    Post subject: Reply with quote

Thanks Mat for your prompt response. I'll check it.

Tuan
Back to top
View user's profile
brentl



Joined: 20 Jul 2004
Posts: 108

PostPosted: Thu Jun 10, 2010 6:02 pm    Post subject: Reply with quote

The assumed shape might give you troubles. Try this:

interface
subroutine func1(arr1, arr2, arr3) bind(c)
real(c_double), device, dimension(*) :: arr1, arr2, arr3
end subroutine
end interface
Back to top
View user's profile
Display posts from previous:   
Post new topic   Reply to topic    PGI User Forum Forum Index -> Accelerator Programming All times are GMT - 7 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © phpBB Group