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 ...
随机推荐
- 页面自动执行js的3种方法
1.最简单的调用方式,直接写到html的body标签里面: <html> <body onload="load();"> </body> & ...
- 一键GHOST使用图文教程
一.准备工作 系统安装好以后,对系统进行及时备份,以防不测,以后恢复时三五分钟即可完成,还你一个全新的完整系统. 一键GHOST V8.3 Build 060903 硬盘版免费下载 从以上地址下载软件 ...
- Java程序生成exe可执行文件
Java程序打包成exe可执行文件,分为两大步骤. 第一步:将Java程序通过Eclipse或者Myeclipse导成Jar包 第二步:通过exe4j讲Jar包程序生成exe可执行文件 第一步详解: ...
- redis之常见操作
目录 redis的常见操作 1. redis客户端登录方式 2. 设置密码 3. 获取redis的配置 4. redis键(key) 语法 实例 Redis keys (黄色为重点) redis的常见 ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 表格:表示成功的操作
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- session存取时 getOutputStream()和getWriter()问题
情况1: 在使用httpResponse的getWriter()会写json是出现 getWriter() has already been called for this response,经我查看 ...
- JavaScript引用类型与对象
1.引用类型 引用类型的值(对象)是引用类型的一个实例.引用类型有时候也被称为对象定义,因为它们描述的是一类对象所具有的属性和方法. 对象是某个特定引用类型的实例.新对象是使用new操作符后跟一个构造 ...
- JS控制输入框和文本框字数
文本框限制字数: HTML结构: JS: $('.advert-title').each(function(){ var TXTlength = $(this).text().length; // 当 ...
- R语言作图 绘制中国地图
参考:https://zhuanlan.zhihu.com/p/27360411 第一步.下载shapefile文件 一直都没有找到下载地址,死在了第一步 第二步.导入shp文件 第三步.画图
- 如何确定Redis集群中各个节点的主从关系
1.首先通过命令(以192.168.203.141为例,-c代表集群的意思) ./redis-cli -h 192.168.203.141 -p 8001 -c 2.然后在输入 cluster no ...