Quantcast
Channel: Intel® Fortran Compiler
Viewing all articles
Browse latest Browse all 3270

Writing Target-Specific Code Using a Directive

$
0
0

This topic only applies to Intel® Many Integrated Core Architecture (Intel® MIC Architecture).

You can write functions that you intend to run on a target, and sections of offloaded code, to take advantage of target-specific functions.

When the compiler generates code for the coprocessor, it defines the macro __MIC__. You can write target-specific code within a !DIR$ if defined __MIC__ section.

Note

Do not use the __MIC__ macro inside the statement following a !DIR$ OMP OFFLOAD statement. You can, however, use this macro in a subprogram called from the directive.

Example

The implementation of the class F32vec is specialized for the CPU and the coprocessor. The offloaded code can use the specialized version if it runs on the coprocessor. If the offload fails and the construct runs on the CPU, it uses the CPU version.

!DIR$ ATTRIBUTES OFFLOAD : mic :: foo
integer function foo()
        implicit none
!DIR$ if defined  (__MIC__)
        foo = 1 ! Code is running on coprocessor
!DIR$ else
        foo = 0 ! Code is running on host
!DIR$ endif
end function foo

program main
        implicit none

        ! Local variables
        integer :: result
        integer :: foo

        !DIR$ ATTRIBUTES OFFLOAD : mic :: foo

        ! Call foo on host
        result = foo()

        ! Print results
        if ( result ) then
                write (*,*) "Code ran on coprocessor"
        else
                write (*,*) "Code ran on host"
        end if

        ! Call foo on MIC
        !DIR$ OFFLOAD TARGET(mic)
        result = foo()

        ! Print results
        if ( result ) then
                write (*,*) "Code ran on coprocessor"
        else
                write (*,*) "Code ran on host"
        end if
end program main
English

Viewing all articles
Browse latest Browse all 3270

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>