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 \n",h);
    #else
        printf("Index on CPU: %d \n",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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值