-
Notifications
You must be signed in to change notification settings - Fork 0
/
openclkernelloader.h
97 lines (88 loc) · 2.72 KB
/
openclkernelloader.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#ifndef _OPENCLKERNELLOADER_H_
#define _OPENCLKERNELLOADER_H_
#include <string>
#include <iostream>
#include <fstream>
static bool LoadOpenCLKernelFromFile(const std::string & name, std::string & programcode)
{
std::ifstream fin((name + ".cl").c_str());
if (!fin)
{
std::cerr << "Failed to open \"" << name << ".cl\"\n";
return false;
}
programcode += std::string((std::istreambuf_iterator<char>(fin)), std::istreambuf_iterator<char>());
programcode += "\n";
fin.close();
return true;
}
#include <algorithm>
#define NOMINMAX
#include <Windows.h>
#include "resource.h"
static bool LoadOpenCLKernelFromRes(const std::string & name, std::string & programcode)
{
std::string resname = "IDR_OPENCLKERNEL" + name;
std::transform(resname.begin(), resname.end(), resname.begin(), toupper);
HRSRC res = FindResourceA(NULL, resname.c_str(), "OPENCLKERNEL");
if (!res)
goto err;
HGLOBAL resMem = LoadResource(NULL, res);
if (!resMem)
goto err;
char * source = (char *)LockResource(resMem);
if (!source)
goto err;
DWORD resSize = SizeofResource(NULL, res);
programcode += std::string(source, resSize);
programcode += "\n";
return true;
err:
std::cerr << "Can't find kernel \"" << name << "\" in program resources.";
if (res == NULL)
{
std::cerr << " FindResourceA failed with error: ";
char * errorMsg;
FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER, NULL, GetLastError(), 0, (LPSTR)&errorMsg, 0, NULL);
std::cerr << errorMsg;
LocalFree(errorMsg);
}
std::cerr << "Trying to load kernel from file" << std::endl;
return LoadOpenCLKernelFromFile(name, programcode);
}
#define LoadOpenCLKernel LoadOpenCLKernelFromRes
#else
#define DECL_OCL_KERNEL(name) \
extern const char opencl_kernel_ ## name []; \
extern int opencl_kernel_ ## name ## _size;
#define OCL_KERNEL_PTR(name) opencl_kernel_ ## name
#define OCL_KERNEL_SIZE(name) opencl_kernel_ ## name ## _size
#define OCL_KERNEL_CHECK(n) \
if (name.compare(#n) == 0) \
{ \
ptr = OCL_KERNEL_PTR(n); \
size = OCL_KERNEL_SIZE(n); \
}
DECL_OCL_KERNEL(mfcc)
DECL_OCL_KERNEL(delta)
DECL_OCL_KERNEL(norm)
static bool LoadOpenCLKernelFromELF(const std::string & name, std::string & programcode)
{
const char * ptr = NULL;
int size = 0;
OCL_KERNEL_CHECK(mfcc);
OCL_KERNEL_CHECK(delta);
OCL_KERNEL_CHECK(norm);
if (ptr && size)
{
programcode += std::string(ptr, (size_t)size) + "\n";
return true;
}
else
{
std::cerr << "Can't find kernel \"" << name << "\" in program executable file, trying to load file\n";
return LoadOpenCLKernelFromFile(name, programcode);
}
}
#define LoadOpenCLKernel LoadOpenCLKernelFromELF
#endif