Created
June 11, 2015 05:46
-
-
Save aokomoriuta/a7c88d4302582a877628 to your computer and use it in GitHub Desktop.
SVM
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
#define _SCL_SECURE_NO_WARNINGS | |
#define __CL_ENABLE_EXCEPTIONS | |
#ifdef _MSC_VER | |
#pragma warning(push, 1) | |
#pragma warning(disable: 4996) | |
#endif | |
#include <iostream> | |
#include <CL/cl.hpp> | |
#ifdef _MSC_VER | |
#undef max | |
#pragma warning(pop) | |
#endif | |
#define OCL_EXTERNAL_INCLUDE(x) #x | |
static const char srcStr[] = | |
"kernel void Simple(" | |
" global double dst[])" | |
"{" | |
" const int i = get_global_id(0);" | |
" dst[i] = ((double)i)/(i+1);" | |
" printf(\"%d: %g\\n\", i, dst[i]);" | |
"}"; | |
int main() | |
{ | |
std::cout << "= Simple =" << std::endl; | |
const std::size_t n = 20;// 10 * 1000 * 1000; | |
// プラットフォーム取得(複数ある場合は一番最後) | |
std::vector<cl::Platform> platforms; | |
cl::Platform::get(&platforms); | |
const auto& platform = *(platforms.rbegin()); | |
// デバイスを取得(複数ある場合は一番最後) | |
std::vector<cl::Device> devices; | |
platform.getDevices(CL_DEVICE_TYPE_GPU, &devices); | |
const auto& device = *(devices.rbegin()); | |
// コンテキスト作成 | |
cl::Context context(device); | |
// プログラムの作成&ビルド | |
cl::Program program(context, srcStr); | |
try | |
{ | |
program.build(devices); | |
} | |
// OpenCL例外があった場合 | |
catch (cl::Error error) | |
{ | |
// ビルドログを表示 | |
std::cout << "Build error #" << error.err() << " @ " << error.what() << std::endl; | |
std::cout << program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(device) << std::endl; | |
#ifdef _MSC_VER | |
system("pause"); | |
#endif | |
return -1; | |
} | |
// カーネルを作成 | |
const std::string KERNEL_FUNCTION_NAME = "Simple"; | |
cl::Kernel kernel(program, KERNEL_FUNCTION_NAME.c_str()); | |
// キュー作成 | |
cl::CommandQueue queue(context, device); | |
#define USE_SVM | |
#ifdef USE_SVM | |
std::cout << "SVM" << std::endl; | |
double* const dst = static_cast<double*>(clSVMAlloc(context(), CL_MEM_READ_WRITE, sizeof(double)*n, 0)); | |
clSetKernelArgSVMPointer(kernel(), 0, dst); | |
#else | |
std::cout << "Not SVM" << std::endl; | |
cl::Buffer dst(context, CL_MEM_WRITE_ONLY, sizeof(double)*n); | |
kernel.setArg(0, dst); | |
#endif | |
queue.enqueueNDRangeKernel(kernel, cl::NullRange, cl::NDRange(n)); | |
queue.finish(); | |
#ifdef USE_SVM | |
clSVMFree(context(), dst); | |
#endif | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment