C++ Library Extensions 2022.12.09
To help learn modern C++ programming
tpf_opencl_utils.hpp
Go to the documentation of this file.
1#ifndef _TPF_OPENCL_UTILS_HPP
2#define _TPF_OPENCL_UTILS_HPP
3
4#ifndef NOMINMAX
5#define NOMINMAX
6#endif
7
8/*****************************************************************************
9 * Copyright (c) 2013-2016 Intel Corporation
10 * All rights reserved.
11 *
12 * WARRANTY DISCLAIMER
13 *
14 * THESE MATERIALS ARE PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR ITS
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY OR TORT (INCLUDING
23 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THESE
24 * MATERIALS, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *
26 * Intel Corporation is the author of the Materials, and requests that all
27 * problem reports or change requests be submitted to it directly
28 *****************************************************************************/
29
30#ifndef CL_TARGET_OPENCL_VERSION
31#define CL_TARGET_OPENCL_VERSION 220
32#endif
33
34#include <stdio.h>
35#include <stdlib.h>
36#include <tchar.h>
37#include <memory.h>
38#include <windows.h>
39#include <CL\cl.h>
40#include <CL\cl_ext.h>
41#include <assert.h>
42#include <d3d9.h>
43
44//we want to use POSIX functions
45#pragma warning( push )
46#pragma warning( disable : 4996 )
47
48template<typename = int>
49void LogInfo(const char* str, ...)
50{
51 if (str)
52 {
53 va_list args;
54 va_start(args, str);
55
56 vfprintf(stdout, str, args);
57
58 va_end(args);
59 }
60}
61
62template<typename = int>
63void LogError(const char* str, ...)
64{
65 if (str)
66 {
67 va_list args;
68 va_start(args, str);
69
70 vfprintf(stderr, str, args);
71
72 va_end(args);
73 }
74}
75
76// Upload the OpenCL C source code to output argument source
77// The memory resource is implicitly allocated in the function
78// and should be deallocated by the caller
79template<typename = int>
80int ReadSourceFromFile(const char* fileName, char** source, size_t* sourceSize)
81{
82 int errorCode = CL_SUCCESS;
83
84 FILE* fp = NULL;
85 fopen_s(&fp, fileName, "rb");
86 if (fp == NULL)
87 {
88 LogError("Error: Couldn't find program source file '%s'.\n", fileName);
89 errorCode = CL_INVALID_VALUE;
90 }
91 else {
92 fseek(fp, 0, SEEK_END);
93 *sourceSize = ftell(fp);
94 fseek(fp, 0, SEEK_SET);
95
96 *source = new char[*sourceSize];
97 if (*source == NULL)
98 {
99 LogError("Error: Couldn't allocate %d bytes for program source from file '%s'.\n", *sourceSize, fileName);
100 errorCode = CL_OUT_OF_HOST_MEMORY;
101 }
102 else {
103 fread(*source, 1, *sourceSize, fp);
104 }
105 }
106 return errorCode;
107}
108#pragma warning( pop )
109
110#endif // _TPF_OPENCL_UTILS_HPP
int ReadSourceFromFile(const char *fileName, char **source, size_t *sourceSize)
void LogError(const char *str,...)
void LogInfo(const char *str,...)