cudaThreadSynchronize()
// 调用CUDA kernel 是非阻塞的,调用kernel语句后面的语句不等待kernel执行完,立即执行。所以在 call_kernel(see kernel.cu) 中执行 m5op.dump 是错误的!!!
// REF: https://www.cs.virginia.edu/~csadmin/wiki/index.php/CUDA_Support/Measuring_kernel_runtime
// cudaThreadSynchronize() 暂停调用者的执行,直到前面的 stream operation 执行完毕。
// REF: https://stackoverflow.com/questions/13485018/cudastreamsynchronize-vs-cudadevicesynchronize-vs-cudathreadsynchronize
// C++ thread join 问题,在 kernel.cpp 中也有 join,那么是在 kernel.cpp 中 dump 还是在main.cpp中join后面dump?
// REF: http://en.cppreference.com/w/cpp/algorithm/for_each
// 若 GPU 先执行完毕,在 main.cpp 中join后 dump 似乎合理; 若 CPU 先执行完毕,岂不是要阻塞在 cudaThreadSynchronize 处?
// 暂且在 kernel.cp p中 dump!
kernel.cpp
// CPU threads--------------------------------------------------------------------------------------
void run_cpu_threads(T *matrix_out, T *matrix, std::atomic_int *flags, int n, int m, int pad, int n_threads, int ldim, int n_tasks, float alpha
#ifdef CUDA_8_0
, std::atomic_int *worklist
#endif
) {
std::cout<<"run_cpu_threads start."<<std::endl; const int REGS_CPU = REGS * ldim;
std::vector<std::thread> cpu_threads;
for(int i = ; i < n_threads; i++) { cpu_threads.push_back(std::thread([=]() { #ifdef CUDA_8_0
Partitioner p = partitioner_create(n_tasks, alpha, i, n_threads, worklist);
#else
Partitioner p = partitioner_create(n_tasks, alpha, i, n_threads);
#endif const int matrix_size = m * (n + pad);
const int matrix_size_align = (matrix_size + ldim * REGS - ) / (ldim * REGS) * (ldim * REGS); for(int my_s = cpu_first(&p); cpu_more(&p); my_s = cpu_next(&p)) { // Declare on-chip memory
T reg[REGS_CPU];
int pos = matrix_size_align - - (my_s * REGS_CPU);
int my_s_row = pos / (n + pad);
int my_x = pos % (n + pad);
int pos2 = my_s_row * n + my_x;
// Load in on-chip memory
#pragma unroll
for(int j = ; j < REGS_CPU; j++) {
if(pos2 >= && my_x < n && pos2 < matrix_size)
reg[j] = matrix[pos2];
else
reg[j] = ;
pos--;
my_s_row = pos / (n + pad);
my_x = pos % (n + pad);
pos2 = my_s_row * n + my_x;
} // Set global synch
while((&flags[my_s])->load() == ) {
}
(&flags[my_s + ])->fetch_add(); // Store to global memory
pos = matrix_size_align - - (my_s * REGS_CPU);
#pragma unroll
for(int j = ; j < REGS_CPU; j++) {
if(pos >= && pos < matrix_size)
matrix_out[pos] = reg[j];
pos--;
}
}
}));
}
std::for_each(cpu_threads.begin(), cpu_threads.end(), [](std::thread &t) { t.join(); });
std::cout<<"dump.. after run_cpu_threads end."<<std::endl;
m5_dump_stats(,);
}
kernel.cu
cudaError_t call_Padding_kernel(int blocks, int threads, int n, int m, int pad, int n_tasks, float alpha,
T *matrix_out, T *matrix, int *flags
#ifdef CUDA_8_0
, int l_mem_size, int *worklist
#endif
){
std::cout<<"call_pad start."<<std::endl;
dim3 dimGrid(blocks);
dim3 dimBlock(threads);
Padding_kernel<<<dimGrid, dimBlock
#ifdef CUDA_8_0
, l_mem_size
#endif
>>>(n, m, pad, n_tasks, alpha,
matrix_out, matrix, flags
#ifdef CUDA_8_0
, worklist
#endif
);
cudaError_t err = cudaGetLastError();
std::cout<<"dump.. after call_pad end."<<std::endl;
m5_dump_stats(,);
return err;
}
main.cpp
for(int rep = ; rep < p.n_warmup + p.n_reps; rep++) {
// Reset
#ifdef CUDA_8_0
for(int i = ; i < p.n_bins; i++) {
h_histo[i].store();
}
#else
memset(h_histo, , p.n_bins * sizeof(unsigned int));
cudaStatus = cudaMemcpy(d_histo, h_histo, p.n_bins * sizeof(unsigned int), cudaMemcpyHostToDevice);
cudaThreadSynchronize();
CUDA_ERR();
#endif
std::cout<<"m5 work begin."<<std::endl;
// Launch GPU threads
// Kernel launch
if(p.n_gpu_blocks > ) {
std::cout<<"launch gpu."<<std::endl;
cudaStatus = call_Histogram_kernel(p.n_gpu_blocks, p.n_gpu_threads, p.in_size, p.n_bins, n_cpu_bins,
d_in, (unsigned int*)d_histo, p.n_bins * sizeof(unsigned int));
CUDA_ERR();
}
// Launch CPU threads
std::cout<<"launch cpu."<<std::endl;
std::thread main_thread(run_cpu_threads, (unsigned int *)h_histo, h_in, p.in_size, p.n_bins, p.n_threads,
p.n_gpu_threads, n_cpu_bins);
std::cout<<"cuda sync."<<std::endl;
cudaThreadSynchronize();
std::cout<<"cpu join after cuda sync."<<std::endl;
main_thread.join();
//m5_work_end(0, 0);
std::cout<<"m5 work end."<<std::endl;
}
cudaThreadSynchronize()的更多相关文章
- cuda多线程间通信
#include "cuda_runtime.h" #include "device_launch_parameters.h" #include <std ...
- cuda并行计算的几种模式
#include "cuda_runtime.h" #include "device_launch_parameters.h" #include <std ...
- 【OpenCV & CUDA】OpenCV和Cuda结合编程
一.利用OpenCV中提供的GPU模块 目前,OpenCV中已提供了许多GPU函数,直接使用OpenCV提供的GPU模块,可以完成大部分图像处理的加速操作. 基本使用方法,请参考:http://www ...
- CUDA入门1
1GPUs can handle thousands of concurrent threads. 2The pieces of code running on the gpu are calle ...
- win7(X64)系统下cuda7.5和VS2013的配置
&1 安装 cuda7.5文件:链接:http://pan.baidu.com/s/1bU2zIQ 密码:nvyw &2 环境变量 注意:CUDA_PATH是安装好cuda7.5之后会 ...
- 使用 CUBLAS 库给矩阵运算提速
前言 编写 CUDA 程序真心不是个简单的事儿,调试也不方便,很费时.那么有没有一些现成的 CUDA 库来调用呢? 答案是有的,如 CUBLAS 就是 CUDA 专门用来解决线性代数运算的库. 本文将 ...
- CUDA编程
目录: 1.什么是CUDA 2.为什么要用到CUDA 3.CUDA环境搭建 4.第一个CUDA程序 5. CUDA编程 5.1. 基本概念 5.2. 线程层次结构 5.3. 存储器层次结构 5.4. ...
- CUDA从入门到精通
http://blog.csdn.net/augusdi/article/details/12833235 CUDA从入门到精通(零):写在前面 在老板的要求下.本博主从2012年上高性能计算课程開始 ...
- CUDA编程-(2)其实写个矩阵相乘并不是那么难
程序代码及图解析: #include <iostream> #include "book.h" __global__ void add( int a, int b, i ...
随机推荐
- Acwing779 最长公共字符串后缀
题目大意:给定n个字符串,让你找到他们的最长公共字符串后缀是什么,可能为空. 分析:题目数据范围比较小,可以O(n*n)暴力匹配,即可解决这道问题.之所以写这道题的题解还是因为写字符串的题还不够多啊, ...
- 标准模板库中的队列(queue)
//C++数据结构与算法(第4版) Adam Drozdek 著 徐丹 吴伟敏<<清华大学出版社>> 队列容器默认由deque实现,用户也可以选择list容器来实现.如果用 ...
- 伪奢侈品iPhone大降价,肉搏国产手机胜算几何?
据国外媒体报道,苹果在中国降低iPhone价格的策略已收到明显的效果,自从1月11日正式调整价格以来,iPhone在苏宁电器平台上的销量飙升83%,而天猫上的销量也增长了76%,其中最受欢迎的机型是i ...
- JavaScript图片
<ul> <li> <a href="images/666.jpg" title="frist img">frist< ...
- win10下python3安装深度学习一般要用的库
matplotlib :绘图库 seaborn:基于matplotlib的图形可视化包 numpy:函数.矩阵运算库 pandas :基于numpy的结构化数据分析库 首先看一下cmd能不能使用pip ...
- 一种新的python局部调试手法
我们都知道,python里面可以用pdb来调试代码.但是pdb往往不大好用.有时候调试代码往往在多重条件里面,直接用pdb需要下条件断点,设定复杂的条件. 一个简单的办法就是这么干. __import ...
- Jquery元素筛选、html()和text()和val三者区别
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 攻防世界web新手区(3)
xff_referer:http://111.198.29.45:43071 打开网址,显示出这个页面: X-Forwarded-For:简称XFF头,它代表客户端,也就是HTTP的请求端真实的IP, ...
- OO第三次博客作业(第三单元总结)
(1)梳理JML语言的理论基础.应用工具链情况 Java 建模语言(JML)将注释添加到 Java 代码中,这样我们就可以确定方法所执行的内容,而不必说明它们如何做到这一点.有了 JML,我们就可以描 ...
- 设置zabbix (3.4.2)添加监控项,触发器,让CPU使用超过85%就报警:
zabbix (3.4.2)添加监控项,触发器,让CPU使用超过85%就报警: zabbix自带模板有一个 Template OS Linux模板.这个模板有监控CPU的监控项,如果没有添加一个监控项 ...