import opencl_course source for a start
This commit is contained in:
69
opencl/vector_add_simplified/main.cpp
Normal file
69
opencl/vector_add_simplified/main.cpp
Normal file
@@ -0,0 +1,69 @@
|
||||
///original author: Erwin Coumans
|
||||
#include "btOpenCLUtils.h"
|
||||
#include "../parallel_primitives/host/btOpenCLArray.h"
|
||||
#include "../parallel_primitives/host/btLauncherCL.h"
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
#define MSTRINGIFY(A) #A
|
||||
const char* kernelString= MSTRINGIFY(
|
||||
__kernel void VectorAdd(__global const float* a, __global const float* b, __global float* c, int numElements)
|
||||
{
|
||||
int iGID = get_global_id(0);
|
||||
if (iGID>=numElements)
|
||||
return;
|
||||
float aGID = a[iGID];
|
||||
float bGID = b[iGID];
|
||||
float result = aGID + bGID;
|
||||
c[iGID] = result;
|
||||
}
|
||||
);
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
int ciErrNum = 0;
|
||||
int preferred_device = -1;
|
||||
int preferred_platform = -1;
|
||||
cl_platform_id platformId;
|
||||
cl_context ctx;
|
||||
cl_command_queue queue;
|
||||
cl_device_id device;
|
||||
cl_kernel addKernel;
|
||||
ctx = btOpenCLUtils::createContextFromType(CL_DEVICE_TYPE_GPU, &ciErrNum,0,0,preferred_device,preferred_platform,&platformId);
|
||||
btOpenCLUtils::printPlatformInfo(platformId);
|
||||
oclCHECKERROR(ciErrNum, CL_SUCCESS);
|
||||
if (!ctx) {
|
||||
printf("No OpenCL capable GPU found!");
|
||||
return 0;
|
||||
}
|
||||
|
||||
device = btOpenCLUtils::getDevice(ctx,0);
|
||||
queue = clCreateCommandQueue(ctx, device, 0, &ciErrNum);
|
||||
addKernel = btOpenCLUtils::compileCLKernelFromString(ctx,device,kernelString,"VectorAdd",&ciErrNum);
|
||||
oclCHECKERROR(ciErrNum, CL_SUCCESS);
|
||||
int numElements = 32;
|
||||
btOpenCLArray<float> a(ctx,queue);
|
||||
btOpenCLArray<float> b(ctx,queue);
|
||||
btOpenCLArray<float> c(ctx,queue);
|
||||
for (int i=0;i<numElements;i++)
|
||||
{
|
||||
a.push_back(float(i));
|
||||
b.push_back(float(i));
|
||||
}
|
||||
|
||||
c.resize(numElements);
|
||||
btLauncherCL launcher( queue, addKernel);
|
||||
launcher.setBuffer( a.getBufferCL());
|
||||
launcher.setBuffer( b.getBufferCL());
|
||||
launcher.setBuffer( c.getBufferCL());
|
||||
launcher.setConst( numElements );
|
||||
launcher.launch1D( numElements);
|
||||
for (int i=0;i<numElements;i++)
|
||||
{
|
||||
float v = c.at(i);
|
||||
printf("c[%d]=%f\n",i,v);
|
||||
}
|
||||
clReleaseCommandQueue(queue);
|
||||
clReleaseContext(ctx);
|
||||
return 0;
|
||||
}
|
||||
37
opencl/vector_add_simplified/premake4.lua
Normal file
37
opencl/vector_add_simplified/premake4.lua
Normal file
@@ -0,0 +1,37 @@
|
||||
|
||||
function createProject (vendor)
|
||||
|
||||
local hasCL = findOpenCL(vendor)
|
||||
|
||||
if (hasCL) then
|
||||
|
||||
project ( "OpenCL_vector_add_simplified_" .. vendor)
|
||||
|
||||
initOpenCL(vendor)
|
||||
|
||||
language "C++"
|
||||
|
||||
kind "ConsoleApp"
|
||||
targetdir "../../bin"
|
||||
|
||||
links {
|
||||
"OpenCL_lib_parallel_primitives_host_" .. vendor
|
||||
}
|
||||
|
||||
includedirs {
|
||||
"../basic_initialize"
|
||||
}
|
||||
|
||||
files {
|
||||
"main.cpp",
|
||||
"../basic_initialize/btOpenCLUtils.cpp",
|
||||
"../basic_initialize/btOpenCLUtils.h"
|
||||
}
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
createProject("AMD")
|
||||
createProject("NVIDIA")
|
||||
createProject("Intel")
|
||||
createProject("Apple")
|
||||
Reference in New Issue
Block a user