Rank: 42859 based on 0 downloads (last 30 days) and 0 files submitted
photo

Sara

E-mail

Personal Profile:
Professional Interests:

 

Watch this Author's files

 

Comments and Ratings by Sara View all
Updated File Comments Rating
23 Oct 2009 CUDA MEX Compile CUDA from within MATLAB! Just write cuda_mex filename.cu and it is ready. Author: Haim Avron

Ok, hopefully I'm not overstaying my welcome on this forum, but I've found out more answers to my cutCreateTimer issue and I wanted to put them out there incase someone else falls into this issue.
It appears that somehow the cuda_mex compilation couldn't find my cutil libraries. This is not necessarily it's fault, I don't know much about make files, linking, etc. so it's fairly probable that I screwed something up somewhere. There is likely a short, easy, and correct way to fix this, but since I don't know it, here's my work around. If you find your way into the CUDA SDK folder you will find a Visual Studio solution for compiling everything cutil. If you change the output of these libraries to the cuda standard library ( likely something along the lines of CUDA/bin) and then rebuild the solution the cutil32 libraries will then be found by the compiler/linker. Will keep posting if I find out a more elegant solution.

22 Oct 2009 CUDA MEX Compile CUDA from within MATLAB! Just write cuda_mex filename.cu and it is ready. Author: Haim Avron

Still having trouble with the cutCreateTimer. Here is an example. I have modified the example.cu that comes with this package to create this timer. In addition, the cutil32.lib and cutil32D.lib are linked in the setup_cuda_mex.m file. If anyone has a solution, I would be very excited to hear it.

#include <stdio.h>
#include <cuda.h>
#include <cutil.h>

#include "mex.h"

__global__ void square_array(float *a, int N)
{
  int idx = blockIdx.x * blockDim.x + threadIdx.x;
  if (idx<N) a[idx] = a[idx] * a[idx];
}

void mexFunction(int nargout, mxArray *argout[], int nargin, const mxArray *argin[])
{
  float *input, *cuda_mem;
  int N;
  size_t size;
unsigned int hTimer;

cutCreateTimer(&hTimer);

  argout[0] = mxDuplicateArray(argin[0]);
  input = (float *)mxGetPr(argout[0]);
  N = mxGetN(argout[0]) * mxGetM(argout[0]);
  size = N * sizeof(float);
  cudaMalloc((void **) &cuda_mem, size);
  cudaMemcpy(cuda_mem, input, size, cudaMemcpyHostToDevice);
  int block_size = 4;
  int n_blocks = N/block_size + (N%block_size == 0 ? 0:1);
  square_array <<< n_blocks, block_size >>> (cuda_mem, N);
  cudaMemcpy(input, cuda_mem, size, cudaMemcpyDeviceToHost);
  cudaFree(cuda_mem);
}

22 Oct 2009 CUDA MEX Compile CUDA from within MATLAB! Just write cuda_mex filename.cu and it is ready. Author: Haim Avron

I'm working on Windows XP...has anyone had problems with cutCreateTimer not working? When it is called by either the CUT_SAFE_CALL or directly it crashes the program.

20 Oct 2009 CUDA MEX Compile CUDA from within MATLAB! Just write cuda_mex filename.cu and it is ready. Author: Haim Avron

If you add cutil32.lib to

        if (strcmp(name, 'LINKFLAGS'))
            value = [value ' cudart.lib cutil32.lib'];
        end

in setup_cuda_mex.m, the linker will properly access cutil based functions. I spent a lot of time trying to figure out why I would get errors in the linking involving various cuda based functions even though the compiler knew the address to the header files...

15 Sep 2009 CUDA MEX Compile CUDA from within MATLAB! Just write cuda_mex filename.cu and it is ready. Author: Haim Avron

Works!!! Thank you so much! I've spent the last week arguing with Matlab 2008a, Visual Studio 2008, and CUDA trying to get them to talk to each other.

Contact us at files@mathworks.com