cuda 学习(三) Page-Locked Host Memory
2016-09-08 20:14
363 查看
一、cudaHostAlloc的使用
二、cudaHostRegister与cudaHostGetDevicePointer使用
运行结果是:0 2 4 6 8 10 12 14 16 18 20 22
这两个函数有一个参数是flag,对应的含义为:
#include <iostream> #include <numeric> #include <stdlib.h> __global__ void add1(float* input){ int idx = threadIdx.x; input[idx] += idx; } int main(void) { float* temp; cudaHostAlloc(&temp, sizeof(float)*12, cudaHostAllocDefault); for(int i = 0; i < 12; ++i){ temp[i] = i; } add1<<<1,12>>>(temp); for(int i = 0; i < 12; ++i){ std::cout<< temp[i] << std::endl; } cudaFreeHost(temp); return 0; }
二、cudaHostRegister与cudaHostGetDevicePointer使用
#include <iostream> #include <numeric> #include <stdlib.h> __global__ void add1(float* input){ int idx = threadIdx.x; input[idx] += idx; } int main(void) { float* temp = (float*)malloc(sizeof(float)*12); cudaHostRegister(temp, sizeof(float)*12, cudaHostRegisterMapped); for(int i = 0; i < 12; ++i){ temp[i] = i; } float* device; cudaHostGetDevicePointer(&device, temp, 0); add1<<<1,12>>>(device); for(int i = 0; i < 12; ++i){ std::cout<< temp[i] << std::endl; } cudaHostUnregister(temp); return 0; }
运行结果是:0 2 4 6 8 10 12 14 16 18 20 22
这两个函数有一个参数是flag,对应的含义为:
Portable Memory: a block of page-locked memory can be used in conjunction with any device in the system Write-Combining Memory: By default page-locked host memory is allocated as cacheable, it can improve the transportation from cpu to gpu. Mapped Memory:a block of page-locked memory can be used in conjunction with any device in the system
相关文章推荐
- Poj 1061 青蛙的约会(扩展欧几里得)
- 【BZOJ-4326】运输计划 树链剖分 + 树上差分 + 二分
- 《TCP/IP详解 卷1:协议》 读书笔记 第6章 ICMP:Internet控制报文协议
- unity执行顺序问题(如何再次执行start方法)
- 第二周项目2 程序的多文件组织
- 视图和表
- 第四周《C语言及程序设计》实践项目22 用指针法访问数组元素
- 深入理解分布式事务
- 总结volley源码解析
- notifyDataSetChanged
- 【Hibernate】--关联关系映射:多对多映射
- Ubuntu 14.04LTS 触摸板无法使用
- codeforces基础题——#362(div2)D
- 手机访问 电脑虚拟机的服务器
- js+jquery+html实现在三种不通的情况下,点击图片放大的效果
- js+jquery+html实现在三种不通的情况下,点击图片放大的效果
- [pySpark][note]Linear Regression
- Milking Cows(USACO)
- 百度2017年暑假实习生编程题目(第三题)---3、进程调度算法 短作业优先(SJF, Shortest Job First)又称为“短进程优先
- Chrome浏览器快捷键大全