Created
August 31, 2014 02:28
-
-
Save metalix00/369106c7ea22febf7382 to your computer and use it in GitHub Desktop.
Example code for http://vfxcode.blogspot.co.nz/ cuda intro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "cudaTest.h" | |
CudaTest::CudaTest() | |
{ | |
} | |
void* CudaTest::creator() | |
{ | |
return new CudaTest; | |
} | |
void incrementArrayOnHost(float *a, int N) | |
{ | |
int i; | |
for (i=0; i<N; i++) | |
{ | |
a[i] = a[i]+1.f; | |
} | |
} | |
extern "C" | |
float * test(float *a, int N); | |
MStatus CudaTest::doIt( const MArgList& argList ) | |
{ | |
MGlobal::displayInfo( "Testing" ); | |
float *a_h, *b_h; // host memory pointers | |
int N = 100; // number of arrays | |
int i; | |
// allocate arrays on host | |
a_h = (float *)malloc(sizeof(float)*N); | |
b_h = (float *)malloc(sizeof(float)*N); | |
// initialize host data | |
for (i=0; i<N; i++){ | |
a_h[i] = (float)i; | |
} | |
// Calculate on Host | |
incrementArrayOnHost(a_h, N); | |
// Calculate on Device | |
b_h = test(a_h, N); | |
// Check Result | |
for (i=0; i<N; i++) | |
assert(a_h[i] == b_h[i]); | |
//cleanup | |
free(a_h); | |
free(b_h); | |
MGlobal::displayInfo( "Done" ); | |
return MS::kSuccess; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Includes, system | |
#include <stdio.h> | |
#include <assert.h> | |
// CUDA runtime | |
#include <cuda_runtime.h> | |
__global__ void incrementArrayOnDevice(float *a, int N) | |
{ | |
int idx = blockIdx.x*blockDim.x + threadIdx.x; | |
if (idx<N) | |
{ | |
a[idx] = a[idx]+1.f; | |
} | |
} | |
extern "C" | |
float * test(float *a_h, int N) | |
{ | |
float *b_h; // host memory pointers | |
float *a_d; // device memory pointers | |
// allocate arrays on device | |
cudaMalloc((void **) &a_d, sizeof(float)*N); | |
b_h = (float *)malloc(sizeof(float)*N); | |
// send data from host to device: a_h to a_d | |
// cudaMemCpy(target, source, memToAllocate, function) | |
cudaMemcpy(a_d, a_h, sizeof(float)*N, cudaMemcpyHostToDevice); | |
int blockSize = 4; | |
int nBlocks = N/blockSize + (N%blockSize == 0?0:1); | |
incrementArrayOnDevice <<< nBlocks, blockSize >>> (a_d, N); | |
// copy data from device to host | |
cudaMemcpy(b_h, a_d, sizeof(float)*N, cudaMemcpyDeviceToHost); | |
cudaFree(a_d); | |
return b_h; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef CUDATEST_H | |
#define CUDATEST_H | |
// APP | |
#include <stdio.h> | |
#include <string.h> | |
#include <assert.h> | |
#include <maya/MPxCommand.h> | |
#include <maya/MGlobal.h> | |
#include <maya/MObject.h> | |
class CudaTest : public MPxCommand | |
{ | |
public: | |
CudaTest(); | |
virtual MStatus doIt( const MArgList& argList ); | |
static void* creator(); | |
}; | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "cudaTest.h" | |
#include <maya/MFnPlugin.h> | |
MStatus initializePlugin( MObject obj ) | |
{ | |
MStatus status; | |
MFnPlugin fnPlugin( obj, "Alex Telford", "1.0", "Any" ); | |
status = fnPlugin.registerCommand( "cudaTest", CudaTest::creator ); | |
CHECK_MSTATUS_AND_RETURN_IT( status ); | |
return MS::kSuccess; | |
} | |
MStatus uninitializePlugin( MObject obj ) | |
{ | |
MStatus status; | |
MFnPlugin fnPlugin( obj ); | |
status = fnPlugin.deregisterCommand( "cudaTest" ); | |
CHECK_MSTATUS_AND_RETURN_IT( status ); | |
return MS::kSuccess; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment