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. iot-web增加apis-namespace组件

    1  文件夹复制 apis 2 增加 3 增加module

  2. [Android] macOS的Android Studio快捷键

    - 快速输入serialVersionUID - - 设置,左上角,Android Studio -> Preferences -> 搜索“Serializable class witho ...

  3. 关于easy ui 的combobox遍历选中

    $.post("fleetAction!queryAllCompanyByCompanyID.do",{"truckCompany.id":companyId} ...

  4. Java UTC时间与本地时间互相转换

    协调世界时,又称世界统一时间.世界标准时间.国际协调时间.由于英文(CUT)和法文(TUC)的缩写不同,作为妥协,简称UTC. 这套时间系统被应用于许多互联网和万维网的标准中,例如,网络时间协议就是协 ...

  5. 正则表达式 re.findall 用法

    正则 re.findall 的简单用法(返回string中所有与pattern相匹配的全部字串,返回形式为数组)语法: findall(pattern, string, flags=0) import ...

  6. 利用“Java同包同名类执行顺序”取消Java 网站应用程序Licence验证

    如果是在tomcat里运行,lib目录下一大堆的JAR包,不同的JAR包里可能会有相同的包名类名,JRE按照JAR名字的字母顺序加载JAR文件,同名类如果已加载,则后面的同名类会忽略. 公司购买的一款 ...

  7. linux 安装nginx+php+mysql

    http://www.cnblogs.com/kyuang/p/6801942.htmlnginx安装 本文是介绍使用源码编译安装,包括具体的编译参数信息. 正式开始前,编译环境gcc g++ 开发库 ...

  8. 【Alpha】Scrum Meeting 5

    目录 前言 任务分配 燃尽图 会议照片 签入记录 遇到的困难 前言 时间: 4.9日中午11.30 地点:F-220 本次会议旨在统计各位同学的进度以及催促任务的进展. 任务分配 姓名 当前阶段任务 ...

  9. NPOI解决由于excel删除数据导致空行读取问题

    1.解决问题思路一:申明判断是否空行变量用于判断是否空行,声明变量数组用于临时非空行数据,最后存于datatable中. /// <summary>读取excel, /// 默认第一行为表 ...

  10. SPSS for Mac 安装教程

    Step1 下载安装文件 链接:https://pan.baidu.com/s/1M5Eh7ph3ys6mHRbAn_h_Wg 提取码:o0m7 Step2 解压安装 将下载好的压缩包解压,点击SPS ...