您的位置:首页 > 其它

常数存储器实例

2016-01-06 11:00 239 查看
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<stdio.h>
__constant__ char p_HelloCUDA[11];
__constant__ int t_HelloCUDA[11]={0,1,2,3,4,5,6,7,89,10};
__constant__ int num=11;

__global__ static void HelloCUDA(char* result){
int i=0;
for(i=0;i<num;i++)
result[i]=p_HelloCUDA[i]+t_HelloCUDA[i];
}

int main(int argc,char* argv[]){
char helloCUDA[]="Hdjik CUDA!";
char *device_result=0;
char host_result[12]={0};
cudaMalloc((void**)&device_result,sizeof(char)*11);
cudaMemcpyToSymbol(p_HelloCUDA,helloCUDA,sizeof(char)*11);
HelloCUDA<<<1,1,0>>>(device_result);
cudaMemcpy(&host_result,device_result,sizeof(char)*11,cudaMemcpyDeviceToHost);
printf("%s\n",host_result);
cudaFree(device_result);
return 0;
}


  

常数存储器的两种定义方法:

一、直接在定义时直接初始化常数存储器,然后再kkernel里面直接使用就可以了

_ _constant_ _int t_HelloCUDA[11]={0.1.2.3.4.5.6.7.8.9.10}

_ _constant_ _int num=11;

二、定义一个constant数组,然后使用函数进行赋值

_ _constant_ _char P_HelloCUDA[11];

cudaMemcpyToSymbol(p)HelloCUDA,helloCUDA,sizeof(char)*11);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: