博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mexcuda中矩阵数据的传输
阅读量:4041 次
发布时间:2019-05-24

本文共 2915 字,大约阅读时间需要 9 分钟。

数据的传输以0开始,然后以列序传输

/* * Example of how to use the mxGPUArray API in a MEX file.  This example shows * how to write a MEX function that takes a gpuArray input and returns a * gpuArray output, e.g. B=mexFunction(A). * * Copyright 2012 The MathWorks, Inc. */#include "mex.h"#include "gpu/mxGPUArray.h"/* * Device code */void __global__ TimesTwo(double const * const A,                         double * const B,                         int const N){    /* Calculate the global linear index, assuming a 1-d grid. */    int const i = blockDim.x * blockIdx.x + threadIdx.x;    if (i < N ) {        if( i == 4 )             B[i] = A[i];        else            B[i] = 2 * A[i];    }}/* * Host code */void mexFunction(int nlhs, mxArray *plhs[],                 int nrhs, mxArray const *prhs[]){    /* Declare all variables.*/    mxGPUArray const *A;    mxGPUArray *B;    double const *d_A;    double *d_B;    int N;    char const * const errId = "parallel:gpu:mexGPUExample:InvalidInput";    char const * const errMsg = "Invalid input to MEX file.";    /* Choose a reasonably sized number of threads for the block. */    int const threadsPerBlock = 256;    int blocksPerGrid;    /* Initialize the MathWorks GPU API. */    mxInitGPU();    /* Throw an error if the input is not a GPU array. */    if ((nrhs!=1) || !(mxIsGPUArray(prhs[0]))) {        mexErrMsgIdAndTxt(errId, errMsg);    }    A = mxGPUCreateFromMxArray(prhs[0]);    /*     * Verify that A really is a double array before extracting the pointer.     */    if (mxGPUGetClassID(A) != mxDOUBLE_CLASS) {        mexErrMsgIdAndTxt(errId, errMsg);    }    /*     * Now that we have verified the data type, extract a pointer to the input     * data on the device.     */    d_A = (double const *)(mxGPUGetDataReadOnly(A));    /* Create a GPUArray to hold the result and get its underlying pointer. */    B = mxGPUCreateGPUArray(mxGPUGetNumberOfDimensions(A),                            mxGPUGetDimensions(A),                            mxGPUGetClassID(A),                            mxGPUGetComplexity(A),                            MX_GPU_DO_NOT_INITIALIZE);    d_B = (double *)(mxGPUGetData(B));    /*     * Call the kernel using the CUDA runtime API. We are using a 1-d grid here,     * and it would be possible for the number of elements to be too large for     * the grid. For this example we are not guarding against this possibility.     */    N = (int)(mxGPUGetNumberOfElements(A));    blocksPerGrid = (N + threadsPerBlock - 1) / threadsPerBlock;    TimesTwo<<
>>(d_A, d_B, N); /* Wrap the result up as a MATLAB gpuArray for return. */ plhs[0] = mxGPUCreateMxArrayOnGPU(B); /* * The mxGPUArray pointers are host-side structures that refer to device * data. These must be destroyed before leaving the MEX function. */ mxGPUDestroyGPUArray(A); mxGPUDestroyGPUArray(B);}

>> y = mexGPUExample(x)

y =
     2     1     2     2
     2     2     2     2
     2     2     2     2
     2     2     2     2

转载地址:http://bsxdi.baihongyu.com/

你可能感兴趣的文章
网络编程Demo, 下载文件。
查看>>
条件变量之虚假唤醒
查看>>
从零实现简易播放器-合集
查看>>
从零实现简易播放器-0.音视频基本概念
查看>>
从零实现简易播放器-1.模拟视频播放
查看>>
从零实现简易播放器-2.opengl渲染yuv图像
查看>>
Windows下编译可调试的ffmpeg, 包含ffplay
查看>>
意外消息:ppsjy:[MyHookProc]__read web cfg: success ------ :
查看>>
从零实现简易播放器:4.ffmpeg 解码视频为yuv数据-使用avcodec_send_packet与avcodec_receive_frame
查看>>
56个民族的C++ map定义
查看>>
Yotta企业云盘如何实现保护文件安全
查看>>
区块链技术让Yotta企业云盘为行政事业服务助力
查看>>
yotta企业云盘助力制造行业创高峰
查看>>
Yotta企业云盘更好的为媒体广告业服务
查看>>
Yotta企业云盘助力旅游行业新发展
查看>>
Yotta企业云盘助力科技行业创高峰
查看>>
Yotta企业云盘更好地为教育行业服务
查看>>
Yotta企业云盘怎么帮助到能源化工行业
查看>>
企业云盘如何助力商业新发展
查看>>
医疗行业运用企业云盘可以带来什么样的提升
查看>>