[ create a new paste ] login | about

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

C, pasted on Dec 27:
#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 i, j, m, n;
    double *data1, *data2;

    float *data1f;

    float *data1f_gpu;
    mxClassID category;
    if (nrhs != nlhs)
        mexErrMsgTxt("The number of input and output arguments must be the same.");

    for (i = 0; i < nrhs; i++) {
        
        m = mxGetM(prhs[i]);
        n = mxGetN(prhs[i]);
        
        plhs[i] = mxCreateDoubleMatrix(m, n, mxREAL);

        cudaMallocHost((void **) &data1f_gpu, sizeof (float) *m * n);
      
        data1 = mxGetPr(prhs[i]);
    
        category = mxGetClassID(prhs[i]);

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

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

        dim3 dimGrid(1);
        dim3 dimBlock(m * n);
        
        elements <<<dimGrid, dimBlock>>>(data1f_gpu, m * n);
	cudaThreadSynchronize();
        
        data2 = mxGetPr(plhs[i]);
        
        for (j = 0; j < m * n; j++) {
            data2[j] = (double) data1f_gpu[j];
        }
        
        mxFree(data1f);       
        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: