- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有返回 C++ 主机端数组的 cuda/C++ 代码。我想在 MATLAB 中操作这些数组,所以我用 mex 格式重写了我的代码并用 mex 编译。
我通过将预分配的数组从 MATLAB 传递到 mex 脚本来让它工作,但这会疯狂地减慢速度。 (54 秒 vs 14 秒没有 mex)
这是我的代码的简化、无输入 1 输出版本的缓慢解决方案:
#include "mex.h"
#include "gpu/mxGPUArray.h"
#include "matrix.h"
#include <stdio.h>
#include <stdlib.h>
#include "cuda.h"
#include "curand.h"
#include <cuda_runtime.h>
#include "math.h"
#include <curand_kernel.h>
#include <time.h>
#include <algorithm>
#include <iostream>
#define iterations 159744
#define transMatrixSize 2592 // Just for clarity. Do not change. No need to adjust this value for this simulation.
#define reps 1024 // Is equal to blocksize. Do not change without proper source code adjustments.
#define integralStep 13125 // Number of time steps to be averaged at the tail of the Force-Time curves to get Steady State Force
__global__ void kern(float *masterForces, ...)
{
int globalIdx = ((blockIdx.x + (blockIdx.y * gridDim.x)) * (blockDim.x * blockDim.y)) + (threadIdx.x + (threadIdx.y * blockDim.x));
...
...
{
...
{
masterForces[i] = buffer[0]/24576.0;
}
}
}
...
}
}
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, mxArray const *prhs[])
{
...
plhs[0] = mxCreateNumericMatrix(iterations,1,mxSINGLE_CLASS,mxREAL);
float *h_F0 = (float*) mxGetData(plhs[0]);
//Device input vectors
float *d_F0;
..
// Allocate memory for each vector on GPU
cudaMalloc((void**)&d_F0, iterations * sizeof(float));
...
//////////////////////////////////////////////LAUNCH ////////////////////////////////////////////////////////////////////////////////////
kern<<<1, 1024>>>( d_F0);
//////////////////////////////////////////////RETRIEVE DATA ////////////////////////////////////////////////////////////////////////////////////
cudaMemcpyAsync( h_F0 , d_F0 , iterations * sizeof(float), cudaMemcpyDeviceToHost);
///////////////////Free Memory///////////////////
cudaDeviceReset();
////////////////////////////////////////////////////
}
为什么这么慢?
编辑:Mex 正在使用旧架构 (SM_13) 而非 SM_35 进行编译。现在是时候了。 (mex 16 秒,仅 c++/cuda 14 秒)
最佳答案
如果您的 CUDA 代码的输出是纯旧数据 (POD) 主机端(相对于设备端)数组,则无需使用 mxGPUArray
,例如您的 Forces1
用 new
创建的 float
数组。您引用的 MathWorks 示例可能演示了 MATLAB 的 gpuArray
和内置 CUDA 功能的使用,而不是如何在 MEX 函数中将数据传入和传出常规 CUDA 函数。
如果您可以在 CUDA 函数之外和之前(例如在 mexFunction
),那么解决方案就是将 new
更改为 mxCreate*
函数之一(即 mxCreateNumericArray
,mxCreateDoubleMatrix
、mxCreateNumericMatrix
等),然后将数据指针传递给您的 CUDA 函数:
plhs[0] = mxCreateNumericMatrix(iterations,1,mxSINGLE_CLASS,mxREAL);
float *h_F0 = (float*) mxGetData(plhs[0]);
// myCudaWrapper(...,h_F0 ,...) /* i.e. cudaMemcpyAsync(h_F0,d_F0,...)
您的代码唯一的变化是:
替换:
float *h_F0 = new float[(iterations)];
与
plhs[0] = mxCreateNumericMatrix(iterations,1,mxSINGLE_CLASS,mxREAL);
float *h_F0 = (float*) mxGetData(plhs[0]);
移除:
delete h_F0;
注意:如果您的 CUDA 代码拥有输出主机端数组,则您必须复制数据到 mxArray
中。这是因为除非您使用 mx
API 分配 mexFunction
输出,否则您分配的任何数据缓冲区(例如使用 mxSetData
)将不会被处理MATLAB 内存管理器,您将遇到段错误或充其量是内存泄漏。
关于c++ - Mex Cuda 动态分配/慢速 mex 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24876229/
我试图通过用 C++ 编写一些函数并使用 mex 接口(interface)来集成它们来加速我的 Matlab 程序。我在 C++ 的向量中得到了我的结果。我想将它传输到 MATLAB 中的数组。我知
我用 C++ 编写了带有服务器和客户端的 TCPIP 套接字连接,它在 VisualStudio 中工作得很好。现在我想通过 MEX 文件在 MATLAB/Simulink 中使用 C++ - 客户端
我有返回 C++ 主机端数组的 cuda/C++ 代码。我想在 MATLAB 中操作这些数组,所以我用 mex 格式重写了我的代码并用 mex 编译。 我通过将预分配的数组从 MATLAB 传递到 m
我正在尝试将一段代码从 Matlab 转换为 python。我正在运行 Ubuntu 16.04LTS,需要依靠 Octave 来运行引用代码(Matlab 安装时出现深奥错误)。无论如何,以前从未使
我使用 VS2010 C-编译器在 Windows8 上开发了一个 Matlab mex 文件。很长一段时间,一切都很顺利…… 但是现在,mex 文件会阻止代码中的所有更改。无论我更改哪一行代码,重建
我的 C 代码应该将 Matlab 稀疏格式转换为 TAUCS format 也是列的主要格式。 当然,我是在 Matlab 本身生成 Matlab 稀疏格式,然后将其传输到 mex 文件。 代码编译
这是我编写的用于在制表符分隔文件中读取的 mex 代码。 mex 文件已创建,但它导致我的 MATLAB 突然结束并给出以下错误。谁能帮助我哪里出错了?如果需要任何进一步的信息,请告诉我 异常终止:分
我有一个 Matlab mex 函数,它重复调用名为 calculate(). 的 C 函数我制作了两个版本的函数: 版本A:每次mex()来电 calculate() ,它只传递输入参数,以及cal
我正在尝试在 MATLAB 中构建一个 mex 函数。该函数依赖于 C++ 库。但是,无论我做什么,我都会在 MATLAB 中得到 Unresolved external 问题。我创建了三个简单的文件
我正在尝试从 C++ 源代码编译一些 MATLAB MEX 文件。我正在尝试编译的文件 can be found here ;我在 32 位系统 MATLAB 2012a 上使用 Windows XP
我使用 mxCreateSparse 在 MEX 中创建了一个稀疏矩阵. mxArray *W; W=mxCreateSparse(n*n,n*n,xsize,mxREAL); double *wpo
我正在尝试在 C 语言的 MEX 文件中实现一些基本的线性代数例程以进行练习,但我被点积困住了。这是我到目前为止所拥有的: #define char16_t UINT16_T //shenanigan
我是编写 MEX 函数的新手,我有内存问题。 MEXf 逍遥法外的套路如下: void mexFunction (int nlhs, mxArray *plhs[], int nrhs,const m
我这里有一个可以正确执行的 C mex 文件,但是在执行完成后 MATLAB 因段错误而崩溃。由于它在程序完成执行后崩溃,这让我认为 MATLAB 自动释放分配的内存导致了这个问题。但是,我释放了我自
我有一个简单的 mex 函数,它从库中调用另一个 C++ 函数。我用编译源代码 mex -cxx mymexfunction.cpp -I/some/include -L/some/lib -lmyl
我正在寻找一种算法来找到 mex但除了这个 wiki 链接,找不到任何有用的东西。 看完后我拉出这段代码: nList = [int(x) for x in input().split()] nLis
我希望仅在通过 Matlab 中的 mex 命令编译我的代码时包含某个头文件。如果它是直接用 Visual Studio 编译的,我不希望包含它。 是否有一个宏可以帮助解决这个问题? 我想做这样的事情
如何在用 C 编写的 MEX 文件中创建二维稀疏矩阵。创建矩阵后如何像在 C 中一样单独访问元素,比如 mat[i][j]? 我厌倦了使用 mxCreateNumericArray函数,但我无法访问元
我正在尝试编译 a matlab wrapper for libdc1394这是一个用于火线相机的库。我收到一个奇怪的错误,涉及头文件中的一些内联函数。我正在使用 gcc-4.6 开发 ubuntu
我需要尽快将大量数据写入磁盘。在 MATLAB 中,我可以使用 fwrite 来做到这一点: function writeBinaryFileMatlab(data) fid = fopen(
我是一名优秀的程序员,十分优秀!