Software Archive
Read-only legacy content
Announcements
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.
17060 Discussions

Fortran Calling C Offloaded Function

Silva__Rafael
Beginner
1,452 Views

Hi Guys,

  I would like to know if it is possible to do a code like this:

!dir$ offload begin target (mic:0)

call C_SUBROUTINE(x, y)

!dir$ end offload

Where the subroutine code is:

__attribute__ ((target(mic:0)))
void c_subroutine_(float * __restrict x, float * __restrict y)
{  

(...)

}

Where x and y are arrays of floats. I've tried but I'm getting invalid pointer error when the subroutine is called, even including "!dir$ attributes offload:mic :: C_SUBROUTINE" inside the calling Fortran subroutine.

0 Kudos
5 Replies
Vidura_Dhananjaya
New Contributor II
1,452 Views
Just try them from ints again
0 Kudos
Silva__Rafael
Beginner
1,452 Views

Diko, I'm not sure I understood you suggestion. Do you mean to use integers? But I need floats, do it with integers does not solve my problem.

0 Kudos
Kevin_D_Intel
Employee
1,452 Views

Here is a short example.

example.f90:

program example_F_call_C
   USE ISO_C_BINDING
   USE MIC_LIB
   integer (C_INT) :: length = 5
   real (C_FLOAT)  :: answer = 0.0

   real (C_FLOAT), dimension(:), allocatable, target :: A, B

   interface
     real (C_FLOAT) function dotprod(length,A,B) bind(C,name="dotprod")
        USE ISO_C_BINDING
        integer (C_INT), VALUE :: length
        TYPE (C_PTR), VALUE :: A, B
     end function
   end interface

   ALLOCATE(A(length),B(length))

   do i = 1, length
      A(i) = REAL(i)
      B(i) = REAL((length+1) - i)
   end do

   ! calculate dot product of A and B
   !DIR$ ATTRIBUTES OFFLOAD : MIC :: dotprod
   !DIR$ OFFLOAD BEGIN target(mic) mandatory
       print "(A,i1)","Calc on card: ",OFFLOAD_GET_DEVICE_NUMBER()
       answer = dotprod(length, C_LOC(A), C_LOC(B))
   !DIR$ END OFFLOAD

   print "(F4.1)", answer

   do i = 1, length
      print "(F3.1,1x,F3.1)", A(i),B(i)
   end do

   DEALLOCATE(A,B)
end

dotprod.c:

__attribute__ ((target (mic)))
float dotprod(  int length, float *A, float *B )
{
    int i;
    float answer;
    answer = 0.0;

    for (i=0; i < length; i++) {
      answer += *(A+i) * *(B+i);
    }

    return answer;
}

Build and execute as follows:

$ ifort -V
Intel(R) Fortran Intel(R) 64 Compiler XE for applications running on Intel(R) 64, Version 15.0.1.133 Build 20141023

$ icc -V
Intel(R) C Intel(R) 64 Compiler XE for applications running on Intel(R) 64, Version 15.0.1.133 Build 20141023

$ icc -c dotprod.c
$ ifort example.f90 dotprod.o -o example.exe

$ ./example.exe
Calc on card: 0
35.0
1.0 5.0
2.0 4.0
3.0 3.0
4.0 2.0
5.0 1.0

 

0 Kudos
Silva__Rafael
Beginner
1,452 Views

Thank you very much guys. I found the problem. In my software the Intel libraries where deployed together with our executable. So, for some reason, something conflict when tried to load the Intel libraries when launching the offloaded code. When I stopped deploying then, the problem was solved. As I'm still in a evaluation phase, I don't care about not deploying them, but in the future, this is something I would have to take care.

0 Kudos
Kevin_D_Intel
Employee
1,452 Views

Glad you have your code working. Let us know if you still face problems deploying w/Intel libraries in the future and we will help you resolve that.

0 Kudos
Reply