Writing Code that Should Not Be Built for CPU-Only Execution

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

You can write code that should not be built when the target is a CPU-only executable.

By default, the compiler defines the macro __INTEL_OFFLOAD. You can write code within an #ifdef __INTEL_OFFLOAD section when the source code is customized for running on the coprocessor, either heterogeneously or natively.

For example, you can use this macro to protect code on the host that should only be executed for an offload build, such as calls to the omp_set_num_threads_target family of APIs in offload.h.

Example

The section for the host compiler works only when you compile with the [Q]offload compiler option with the keyword optional, or the Qmic (Windows*) or mmic (Linux*) compiler option.

#include <stdio.h>
__declspec(target(mic)) void print()
{
#ifdef __INTEL_OFFLOAD
#ifdef __MIC__
    printf("Using offload compiler :  Hello from the coprocessor\n");
    fflush(0);
#else  /* !__MIC__ */
    printf("Using offload compiler :  Hello from the CPU\n");
#endif  /* __MIC__ */
#else /* !__INTEL_OFFLOAD */ 
    printf("Using host compiler :  Hello from the CPU\n");
#endif /* __INTEL_OFFLOAD */

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