// 调用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()的更多相关文章

  1. cuda多线程间通信

    #include "cuda_runtime.h" #include "device_launch_parameters.h" #include <std ...

  2. cuda并行计算的几种模式

    #include "cuda_runtime.h" #include "device_launch_parameters.h" #include <std ...

  3. 【OpenCV & CUDA】OpenCV和Cuda结合编程

    一.利用OpenCV中提供的GPU模块 目前,OpenCV中已提供了许多GPU函数,直接使用OpenCV提供的GPU模块,可以完成大部分图像处理的加速操作. 基本使用方法,请参考:http://www ...

  4. CUDA入门1

      1GPUs can handle thousands of concurrent threads. 2The pieces of code running on the gpu are calle ...

  5. win7(X64)系统下cuda7.5和VS2013的配置

    &1 安装 cuda7.5文件:链接:http://pan.baidu.com/s/1bU2zIQ 密码:nvyw &2 环境变量 注意:CUDA_PATH是安装好cuda7.5之后会 ...

  6. 使用 CUBLAS 库给矩阵运算提速

    前言 编写 CUDA 程序真心不是个简单的事儿,调试也不方便,很费时.那么有没有一些现成的 CUDA 库来调用呢? 答案是有的,如 CUBLAS 就是 CUDA 专门用来解决线性代数运算的库. 本文将 ...

  7. CUDA编程

    目录: 1.什么是CUDA 2.为什么要用到CUDA 3.CUDA环境搭建 4.第一个CUDA程序 5. CUDA编程 5.1. 基本概念 5.2. 线程层次结构 5.3. 存储器层次结构 5.4. ...

  8. CUDA从入门到精通

    http://blog.csdn.net/augusdi/article/details/12833235 CUDA从入门到精通(零):写在前面 在老板的要求下.本博主从2012年上高性能计算课程開始 ...

  9. CUDA编程-(2)其实写个矩阵相乘并不是那么难

    程序代码及图解析: #include <iostream> #include "book.h" __global__ void add( int a, int b, i ...

随机推荐

  1. JSON传输图片帮助类

    JSON传输图片帮助类 2014-05-27 16:11:22|  分类: Java |  标签:解决方案  java  json  |举报|字号 订阅     原理:将图片转换为字节流,再将字节流用 ...

  2. Java程序基本优化

    1.尽量指定类的final修饰符,因为带有final修饰符的类是不可派生的. 2.尽量重用对象. 3.尽量使用局部变量. 4.不要重复初始化变量. 5.在Java+Oracle的应用系统开发中,Jav ...

  3. JPG加入RAR文件原理详解

    在水木看到有人上传了一张图片,说如果将其后缀改为rar,解压后会有别的文件,试了一下,果然如此.用十六进制的编辑器看了看,发现的确有理. 先是,文件头部是以JPG格式起始的,如下: ......JFI ...

  4. java并发(一):初探线程的创建

    线程的创建两种方式 创建线程有四种方式,今天主要演示的是两种:继承Thread,实现Runable接口 继承Thread创建线程 import lombok.extern.slf4j.Slf4j; @ ...

  5. day14-Python运维开发基础(内置函数、pickle序列化模块、math数学模块)

    1. 内置函数 # ### 内置函数 # abs 绝对值函数 res = abs(-10) print(res) # round 四舍五入 (n.5 n为偶数则舍去 n.5 n为奇数,则进一!) 奇进 ...

  6. java虚拟机之内存分配

    Java 的自动内存管理主要是针对对象内存的回收和对象内存的分配.同时,Java 自动内存管理最核心的功能是 堆 内存中对象的分配与回收. JDK1.8之前的堆内存示意图: 从上图可以看出堆内存分为新 ...

  7. ActivePerl 安装

    下载 https://www.activestate.com/products/activeperl/downloads/ 链接:https://pan.baidu.com/s/1IXPdYFd5bD ...

  8. 剑指 offer 树的子结构

    题目描述: 输入两棵二叉树A,B,判断B是不是A的子结构.(ps:我们约定空树不是任意一个树的子结构). 第一遍没写出来错误点:认为首先应该找到pRoot1等于pRoot2的节点,但是递归就是自己在不 ...

  9. 104、Java中String类之使用indexOf()等功能查找

    01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...

  10. SSH框架系列:Spring读取配置文件以及获取Spring注入的Bean

    分类: [java]2013-12-09 16:29 1020人阅读 评论(0) 收藏 举报 1.简介 在SSH框架下,假设我们将配置文件放在项目的src/datasource.properties路 ...