Intel® Fortran Compiler 17.0 Developer Guide and Reference

Writing Target-Specific Code Using a Directive

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

This topic only applies to the Intel® Xeon Phi™ coprocessor x100 product family (formerly code name Knights Corner).

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 a macrodirective specific to the coprocessor architecture, for example __MIC__ for the Intel® Xeon Phi™ coprocessor x100 product family (formerly code name Knights Corner) . You can then write target-specific code within a !DIR$ if defined __MIC__ section.

Note

Do not use coprocessor-specific directives inside the statement following a !DIR$ OMP OFFLOAD statement. You can, however, use them 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

See Also