首页 > 其他 > 详细

常数存储器实例

时间:2016-01-06 11:37:48      阅读:194      评论:0      收藏:0      [点我收藏+]

 

 

 

 

#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);

常数存储器实例

原文:http://www.cnblogs.com/zhangchengbing/p/5104810.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!