MIC中函数和变量的声明

c++/c使用

__declspec(target(mic))函数或变量声明

__attribute__((target(mic)))函数或变量声明

举例如下:

__attribute__((target(mic))) int a;
__attribute__((target(mic))) void func();

如果变量或函数较多,MIC问为我们提供了批量声明的办法,让我们一次可以申请多个函数或变量,并且函数或变量,并且函数和变量可以混合声明。

#pragma offload_attribute([push,]target(target-name))
   // 变量或函数声明
#pragma offload_atrribute(pop|{target(none)})

C/C++有两种方法:

#pragma offload_attribute(push,target(mic))
//函数或变量声明
#pragma offload_attribute (pop)

#pragma offload_attribute(target(mic))
//函数或变量声明
#pragma offload_arrtibute (target(none))

举例如下:

#pragma offload_attribute(push,target(mic))
    inta;
    float func();
#pragma offload_attribute(pop)

这里注意attribute前后均是两个下划线,示例代码如下:

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#define LEN 5
__attribute__((target(mic))) void funcheck(int h){
    #ifdef    __MIC__
        printf("Index on MIC: %d 
",h);
    #else
        printf("Index on CPU: %d 
",h);
    #endif    
}

int main(int argc,int** argv){
    int i;
    #pragma offload target(mic)
    for(i=0;i<LEN;i++){
        funcheck(i);
    }
    return 0;
}

MIC前后也均是两个下划线,这段代码中__MIC__是MIC提供的一个宏定义,这个宏定义用来检查程序是否运行在设备端,也就是MIC端,需要注意的是,这个定义不能在offload代码段内检查!

编译 icc -o demo demo.c 

执行 ./demo 

结果如下:

Index on MIC: 0
Index on MIC: 1
Index on MIC: 2
Index on MIC: 3
Index on MIC: 4

原文地址:https://www.cnblogs.com/sdxk/p/4209413.html