[ create a new paste ] login | about

Link: http://codepad.org/9wpuiNTB    [ raw code | output | fork ]

C, pasted on Mar 1:
#include "cuda.h" 
#include "mex.h" 

__host__ void cleanup()
{
	cudaDeviceReset();
}

/* Kernel */
__global__ void elements(float* in, int N)
{
    int idx = threadIdx.x;
    if (idx < N)
        in[idx] = in[idx] * 2;
}

/* Gateway function */
void mexFunction(int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[])
{
    int j, m, n;
    double *data1, *data2;

    float *data1f_gpu; // Host Pointer
    float *data1f;	// device pointer to host memory

    mxClassID category;
    if (nrhs != nlhs)
        mexErrMsgTxt("The number of input and output arguments must be the same.");
     
        m = mxGetM(prhs[0]);
        n = mxGetN(prhs[0]);
        
        plhs[0] = mxCreateDoubleMatrix(m, n, mxREAL);

        cudaHostAlloc((void **) &data1f_gpu, sizeof (float) *m * n,cudaHostAllocMapped||cudaHostAllocWriteCombined);
      
        data1 = mxGetPr(prhs[0]);
    
        category = mxGetClassID(prhs[0]);

        if (category == mxSINGLE_CLASS) 
	{      
	   for(j = 0;j < m*n;j++)
	   {
		data1f_gpu[j] = data1[j];		
	   }			
        }

        if (category == mxDOUBLE_CLASS) 
	{         
            for(j = 0;j < m*n;j++)
	    {
		data1f_gpu[j] = (float) data1[j];		
	    }	
        }

	
	cudaHostGetDevicePointer(&data1f,data1f_gpu,0); //Get device pointer

        dim3 dimGrid(1);
        dim3 dimBlock(m * n);
        
        elements <<<dimGrid, dimBlock>>>(data1f, m * n);
	cudaThreadSynchronize();
        
        data2 = mxGetPr(plhs[0]);
        
        for (j = 0; j < m * n; j++) {
            data2[j] = (double) data1f[j];
        }
        
       
        cudaFreeHost(data1f_gpu);      
    
	cleanup();
} 


Output:
1
2
3
4
5
6
Line 18: error: cuda.h: No such file or directory
Line 17: error: mex.h: No such file or directory
Line 5: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'void'
Line 11: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'void'
Line 19: error: expected declaration specifiers or '...' before 'mxArray'
Line 19: error: expected ';', ',' or ')' before '*' token


Create a new paste based on this one


Comments: