https://code.google.com/p/deep-learning-faces/source/browse/trunk/cuda_ut/include/bsxfun.h?r=7&spec=svn7

/*
Copyright (C) 2013 Yichuan Tang.
contact: tang at cs.toronto.edu
http://www.cs.toronto.edu/~tang This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details. You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ #ifndef _BSXFUN_H_
#define _BSXFUN_H_ #include "cu_util.h"
#include "cu_clmatrix.h" /***********************************************************************************************************
* @brief: this function performs a matrix + col. vector operation *
* @param[in]: pA and pOut: nI by nJ matrix
* pB is a column vector nI by 1
* nInJ is the total dimensionality of the matrix pA
*
* @param[out]:
* @topology: assumes a 1D block layout in x direction and covers the entire matrix pA
* @note: assume column-major
* @change:
* @tested:
* @to_do:
***********************************************************************************************************
*/
template<class O, typename T>
__global__ void bsxfun_colvec_1dkernel( const T* pA, const T* pVec, T* pOut,
int nI, int nJ, int nInJ, O op)
{
const unsigned int ind = blockIdx.x*blockDim.x + threadIdx.x;
const unsigned int totalThreads = blockDim.x*gridDim.x; for (int i = ind; i < nInJ; i += totalThreads)
pOut[i] = op(pA[i], pVec[i % nI]);
} /***********************************************************************************************************
* @brief: this function performs a matrix + row. vector operation
* @param[in]: pA and pOut: nI by nJ matrix
* pVec is a row vector 1 by nJ
* nInJ is the total dimensionality of the matrix pA
*
* @param[out]:
* @topology: assumes a 1D block layout in x direction and covers the entire matrix pA
* @note: assume column-major
* @change:
* @tested:
* @to_do:
***********************************************************************************************************
*/
template<class O, typename T>
__global__ void bsxfun_rowvec_1dkernel( const T* pA, const T* pVec, T* pOut,
int nI, int nJ, int nInJ, O op)
{
const unsigned int ind = blockIdx.x*blockDim.x + threadIdx.x;
const unsigned int totalThreads = blockDim.x*gridDim.x; for (int i = ind; i < nInJ; i += totalThreads)
pOut[i] = op(pA[i], pVec[i / nI]);
} //alpha beta version
template<class O, typename T>
__global__ void bsxfun_colvec_1dkernel( T alpha, const T* pA, T beta, const T* pVec, T* pOut,
int nI, int nJ, int nInJ, O op)
{
const unsigned int ind = blockIdx.x*blockDim.x + threadIdx.x;
const unsigned int totalThreads = blockDim.x*gridDim.x; for (int i = ind; i < nInJ; i += totalThreads)
pOut[i] = op(pA[i], alpha, pVec[i % nI], beta);
} template<class O, typename T>
__global__ void bsxfun_rowvec_1dkernel( T alpha, const T * pA, T beta, const T* pVec, T* pOut,
int nI, int nJ, int nInJ, O op)
{
const unsigned int ind = blockIdx.x*blockDim.x + threadIdx.x;
const unsigned int totalThreads = blockDim.x*gridDim.x; for (int i = ind; i < nInJ; i += totalThreads)
pOut[i] = op(pA[i], alpha, pVec[i / nI], beta);
} /***********************************************************************************************************
* @brief: function similar to bsxfun of matlab
* A op B ---> Out
* @param[in]: op - type of operation
* A - first matrix
* B - col/row vector, one dimension must be 1
* @param[out]:
if Out is set to A, the operation is inplace, overwrites A
*
* @topology:
* @note:
* @change:
* @tested:
* @to_do: switch to shared memory operators to see if we can achieve speedup?!
***********************************************************************************************************
*/
template<class O, typename T>
int Bsxfun( const clMatrix<T>& A, O op, const clMatrix<T>& B, clMatrix<T>& Out){ if (! (B.nI == || B.nJ == ) )
return -;
if ( ( B.nI == && B.nJ != A.nJ) || ( B.nJ == && B.nI != A.nI) ){ if (!(B.nI == && B.nJ == )) //special case
return -;
}
if ( A.nI != Out.nI || A.nJ != Out.nJ)
return -; const unsigned int datadim = A.nJ*A.nI;
dim3 dim_block( MEDIUM_NUM_THREADS );
dim3 dim_grid( MIN( MAX_GRIDS, (datadim + dim_block.x-)/dim_block.x) ); if (B.nJ == && B.nI != ){
bsxfun_colvec_1dkernel<<<dim_grid, dim_block>>>( A.pData, B.pData, Out.pData,
A.nI, A.nJ, datadim, op);
}else if (B.nJ != && B.nI == ){
bsxfun_rowvec_1dkernel<<<dim_grid, dim_block>>>( A.pData, B.pData, Out.pData,
A.nI, A.nJ, datadim, op );
}else{ // when B is 1x1
if (A.nI == ){
bsxfun_colvec_1dkernel<<<dim_grid, dim_block>>>( A.pData, B.pData, Out.pData,
A.nI, A.nJ, datadim, op);
}else if (A.nJ == ){
bsxfun_rowvec_1dkernel<<<dim_grid, dim_block>>>( A.pData, B.pData, Out.pData,
A.nI, A.nJ, datadim, op );
}else{
return -; //invalid case
} }
return ;
} //alpha beta version
template<class O, typename T>
int Bsxfun(T alpha, const clMatrix<T>& A, O op, T beta, const clMatrix<T>& B, clMatrix<T>& Out){ if (! (B.nI == || B.nJ == ) )
return -;
if ( ( B.nI == && B.nJ != A.nJ) || ( B.nJ == && B.nI != A.nI) ){ if (!(B.nI == && B.nJ == )) //special case
return -;
}
if ( A.nI != Out.nI || A.nJ != Out.nJ)
return -; const uint64_t datadim = A.nJ*A.nI;
dim3 dim_block( MEDIUM_NUM_THREADS );
dim3 dim_grid( MIN( MAX_GRIDS, (datadim + dim_block.x-)/dim_block.x) ); if (B.nJ == && B.nI != ){
bsxfun_colvec_1dkernel<<<dim_grid, dim_block>>>( alpha, A.pData, beta, B.pData, Out.pData,
A.nI, A.nJ, datadim, op);
}else if (B.nJ != && B.nI == ){
bsxfun_rowvec_1dkernel<<<dim_grid, dim_block>>>( alpha, A.pData, beta, B.pData, Out.pData,
A.nI, A.nJ, datadim, op );
}else{
if (A.nI == ){
bsxfun_colvec_1dkernel<<<dim_grid, dim_block>>>(alpha, A.pData, beta, B.pData, Out.pData,
A.nI, A.nJ, datadim, op);
}else if (A.nJ == ){
bsxfun_rowvec_1dkernel<<<dim_grid, dim_block>>>(alpha, A.pData, beta, B.pData, Out.pData,
A.nI, A.nJ, datadim, op );
}else{
return -; //invalid case
} } return ;
} #endif

bsxfun.h multiple threads backup的更多相关文章

  1. caffe网络在多线程中无法使用GPU的解决方案 | cpp caffe net run in multiple threads

    本文首发于个人博客https://kezunlin.me/post/8d877e63/,欢迎阅读! cpp caffe net run in multiple threads Guide set_mo ...

  2. Multiple Threads reading from the same file(转载)

    问 I have a xml file that needs to be read from many many times. I am trying to use the Parallel.ForE ...

  3. Android 性能优化(16)线程优化:Creating a Manager for Multiple Threads 如何创建一个线程池管理类

    Creating a Manager for Multiple Threads 1.You should also read Processes and Threads The previous le ...

  4. 临界区代码 critical section Locks and critical sections in multiple threads

    临界区 在同步的程序设计中,临界区段(Critical section)指的是一个访问共享资源(例如:共享设备或是共享存储器)的程序片段,而这些共享资源有无法同时被多个线程访问的特性. 当有线程进入临 ...

  5. SQLite multiple threads

    const int loops = 1000; public void DatabaseThreadSafetyTest() { var backgroundThread = new Thread(n ...

  6. Hashtable insert failed. Load factor too high. The most common cause is multiple threads writing to the Hashtable simultaneously

    暂时也没准确定位到问题 https://support.microsoft.com/zh-cn/help/2803754/hotfix-rollup-2803754-is-available-for- ...

  7. PatentTips - Controlling TSC offsets for multiple cores and threads

    BACKGROUND Many processors include a time stamp count (TSC) counter which is typically implemented a ...

  8. Libevent源码学习笔记一:event2/event.h

    一.libevent标准使用方法: 每个程序使用Libevent必须include <event2/event.h> 头文件,并 传给 -levent  链接器.如果只是想使用主要的eve ...

  9. OpenMPI源码剖析4:rte.h 头文件的说明信息

    上一篇文章中说道,我们在 rte.h 中发现了有价值的说明: 我们一块一块来分析,首先看到第一块,关于 Process name Object: * (a) Process name objects ...

随机推荐

  1. 使用jfreechart生成柱状图、折线图、和饼状图

    JFreeChart是JAVA平台上的一个开放的图表绘制类库.它完全使用JAVA语言编写,是为applications, applets, servlets 以及JSP等使用所设计.下面我就详细介绍如 ...

  2. MySQL Backup mysqldump 常用选项与主要用法

    The mysqldump client utility performs logical backups, producing a set of SQL statements that can be ...

  3. 《linux就该这么学》第三节课 第二节命令笔记

    命令笔记 (随笔原创,借鉴请修改) linux系统中一切都是文件 2.4  系统状态的命令:  ifconfig   :    查看系统网卡信息,包括网卡名称,ip地址,掩码,mac地址,收到数据包大 ...

  4. 函数作用域之闭包与this!

    函数基础友情链接:http://speakingjs.com/es5/ch01.html#_functions 作用域链图解   var x = 1; function foo(){     var ...

  5. acm 2015北京网络赛 F Couple Trees 主席树+树链剖分

    提交 题意:给了两棵树,他们的跟都是1,然后询问,u,v 表 示在第一棵树上在u点往根节点走 , 第二棵树在v点往根节点走,然后求他们能到达的最早的那个共同的点 解: 我们将第一棵树进行书链剖,然后第 ...

  6. [openjudge-动态规划]鸣人的影分身

    题目描述 描述 在火影忍者的世界里,令敌人捉摸不透是非常关键的.我们的主角漩涡鸣人所拥有的一个招数--多重影分身之术--就是一个很好的例子. 影分身是由鸣人身体的查克拉能量制造的,使用的查克拉越多,制 ...

  7. ansible的高级应用-roles

    在之前我们知道了playbook,类似于shell的脚本,playbook适用于一些不太麻烦的部署任务,比如说使用playbook安装mysql,那么我们直接写一个playbook文件即可.可是如果我 ...

  8. Html Title 标签

    Html Title 标签 Title 是 HTML Head 内部标签 <html> <head> <!-- Title标签:HTML的标头标题 --> < ...

  9. WebApi返回的Json去掉XML

    在global.asax.cs文件中的 application_start()方法中加入下面一句话 GlobalConfiguration.Configuration.Formatters.XmlFo ...

  10. Docker入门 配置篇

    docker配置 http://www.runoob.com/docker/docker-tutorial.html