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. python安装包API文档

    在python开发过程中,经常会使用第三方包,或者内置的包. 那么这些包,具体有哪些选项,有哪些方法,你知道吗?下面介绍一种万能方法. 使用命令:<注意,命令里python显示的API版本是根据 ...

  2. Python压缩指定文件及文件夹为zip

    Python压缩指定的文件及文件夹为.zip 代码: def zipDir(dirpath,outFullName): """ 压缩指定文件夹 :param dirpat ...

  3. 50.JQ---jQuery 常用小技巧

    1. 禁止右键点击 $(document).ready(function(){ $(document).bind("contextmenu",function(e){ return ...

  4. Python str byte 互相转换

  5. PHP 批量操作删除,支持单个删除

    PHP  执行部分: <?php include('checkadmin.php'); header('Content-Type: text/html; charset=utf-8'); if( ...

  6. 解决IDEA无法安装插件的问题

    进入2018年以来,在IDEA插件中心中,安装插件经常安装失败,报连接超时的错误.如下: 我们发现连接IDEA的插件中心使用的是https的链接,我们在浏览器中使用https访问插件中心并不能访问. ...

  7. elasticsearch 安装 windows linux macOS

    导读 在上一章节我们介绍Elasticsearch基本概念,今天我们继续进行本章内容,Elasticsearch在各种环境下安装,下面将逐一讲解在各种操作系统或不同安装在不同环境中注意事项. 安装 E ...

  8. Oarcle 入门之from关键字

    作用:检索“表” 注意:检索的表后可以添加别名(别名不需要被双引号引起) *每一句都不可缺少

  9. 使用 mysqladmin debug 查看死锁信息

    使用 mysqladmin debug 查看死锁信息 mysqladmin -S /mysql/data/mysql.sock debug 然后在error日志中,会看到: 11 lock struc ...

  10. Shell cace条件语句

    cace条件语句,取相对应的多个值,进行输出. 语句:case语句:case $n in 回车\  值)回车\ 命令 :: 值)命令 esac case $1 in start) echo “启动” ...