Hi *,
I would like to increase the values of a vector. I wrote two scripts:
incrementa.m
clc;
clear all;
global v
v = ones(100,1);
nvmex('incrementa.cu');
R = incrementa(v);
incrementa.cu:
int *x,*Y;
_global_ void incrementa(int *x) {
for(int i=1; i<100; i++)
x[i] = x[i]++;
}
void fz_cuda() {
printf("sono in fz_cuda()\n");
int *device_x = NULL;
// Creating objects for the measurement of the times
cudaEvent_t start,stop;
float time;
cudaEventCreate(&start);
cudaEventCreate(&stop);
// Allocation on GPU
// INPUT:
if(cudaMalloc((void**) &device_x, 100*sizeof(int)) != cudaSuccess)
mexErrMsgTxt("Memory allocating of x failure on the GPU.");
// Copy on GPU
cudaMemcpy(device_x, x, 100*sizeof(int), cudaMemcpyHostToDevice);
// ==================== // Invoke CUDA kernel // ====================
cudaEventRecord(start,0);
incrementa<<<1,1>>>(device_x);
// Copy result on CPU
cudaMemcpy(x,device_x,100*sizeof(int), cudaMemcpyDeviceToHost);
// Memory free
cudaFree(device_x);
cudaEventElapsedTime(&time, start,stop);
printf ("Tempo per il kernel: %f ms\n", time);
}
void mexFunction(int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[]) { // prhs[0] = v if (nrhs != 1)
mexErrMsgIdAndTxt("Invalid Input:nrhs","Require 1 inputs");
x = (int*)mxGetData(prhs[0]);
fz_cuda();
const mwSize outDims[2] = { 100,1 };
plhs[0] = mxCreateNumericArray(2, outDims, mxDOUBLE_CLASS, mxREAL);
Y = (int*)mxGetData( plhs[0] );
}
but this doesn't work. Where I wrong?
Thanks, Davide
Best Answer