Writing Target-Specific Code Using a Pragma

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 intrinsic functions.

When the compiler generates code for the coprocessor, it defines the macro __MIC__. You can write target-specific code within an #ifdef __MIC__ section.

Note

Do not use the __MIC__ macro inside the statement following a #pragma offload statement. You can, however, use this macro in a subprogram called from the pragma.

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.

// The class F32vec will be used in offloaded code
// It needs the target(mic) attribute

#pragma offload_attribute(push,target(mic))

// Use customized versions for the CPU and the coprocessor

#ifdef __MIC__

        // The Intel® MIC Architecture version of the class is in micvec.h
        #include <micvec.h>

#else
        // The CPU version is written inline
        class F32vec16
        {
                public:
                ...
                friend F32vec16 sqrt(const F32vec16& a);
        };
#endif

mysqrt()
{
   #ifdef __MIC__
        F32vec16 w = ...;
        F32vec16 s;
        s = sqrt(w);
   #endif
               ...
}

#pragma offload_attribute(pop)

int main()
{
   #pragma offload target(mic)
     {
        mysqrt();
     }
     ...
}
For more complete information about compiler optimizations, see our Optimization Notice.