Mali OpenCL SDK v1.1.0
 All Classes Files Functions Variables Macros Pages
template.cpp
Go to the documentation of this file.
1 /*
2  * This confidential and proprietary software may be used only as
3  * authorised by a licensing agreement from ARM Limited
4  * (C) COPYRIGHT 2013 ARM Limited
5  * ALL RIGHTS RESERVED
6  * The entire notice above must be reproduced on all authorised
7  * copies and copies may only be made to the extent permitted
8  * by a licensing agreement from ARM Limited.
9  */
10 
11 #include "common.h"
12 #include "image.h"
13 
14 #include <CL/cl.h>
15 #include <iostream>
16 
17 using namespace std;
18 
24 int main(void)
25 {
26  cl_context context = 0;
27  cl_command_queue commandQueue = 0;
28  cl_program program = 0;
29  cl_device_id device = 0;
30  cl_kernel kernel = 0;
31  const int numMemoryObjects = 1;
32  cl_mem memoryObjects[numMemoryObjects] = {0};
33  cl_int errorNumber;
34 
35 
36  /* Set up OpenCL environment: create context, command queue, program and kernel. */
37 
38  /* [Create Context] */
39  if (!createContext(&context))
40  {
41  cleanUpOpenCL(context, commandQueue, program, kernel, memoryObjects, numMemoryObjects);
42  cerr << "Failed to create an OpenCL context. " << __FILE__ << ":"<< __LINE__ << endl;
43  return 1;
44  }
45  /* [Create Context] */
46 
47  /* [Create Command Queue] */
48  if (!createCommandQueue(context, &commandQueue, &device))
49  {
50  cleanUpOpenCL(context, commandQueue, program, kernel, memoryObjects, numMemoryObjects);
51  cerr << "Failed to create the OpenCL command queue. " << __FILE__ << ":"<< __LINE__ << endl;
52  return 1;
53  }
54  /* [Create Command Queue] */
55 
56  /* [Create Program] */
57  if (!createProgram(context, device, "assets/template.cl", &program))
58  {
59  cleanUpOpenCL(context, commandQueue, program, kernel, memoryObjects, numMemoryObjects);
60  cerr << "Failed to create OpenCL program." << __FILE__ << ":"<< __LINE__ << endl;
61  return 1;
62  }
63  /* [Create Program] */
64 
65  /* [Create kernel] */
66  kernel = clCreateKernel(program, "template", &errorNumber);
67  if (!checkSuccess(errorNumber))
68  {
69  cleanUpOpenCL(context, commandQueue, program, kernel, memoryObjects, numMemoryObjects);
70  cerr << "Failed to create OpenCL kernel. " << __FILE__ << ":"<< __LINE__ << endl;
71  return 1;
72  }
73  /* [Create kernel] */
74 
75 
76  /*
77  * Add code here to set up memory/data, for example:
78  * - Create memory buffers.
79  * - Initialise the input data.
80  * - Set up kernel arguments.
81  */
82 
83 
84  /* Execute the kernel instances. */
85 
86  /* [Set the kernel work size] */
87  const int workDimensions = 1;
88  size_t globalWorkSize[workDimensions] = {1};
89  /* [Set the kernel work size] */
90 
91  /* [Enqueue the kernel] */
92  /* An event to associate with the kernel. Allows us to retrieve profiling information later. */
93  cl_event event = 0;
94 
95  if (!checkSuccess(clEnqueueNDRangeKernel(commandQueue, kernel, workDimensions, NULL, globalWorkSize, NULL, 0, NULL, &event)))
96  {
97  cleanUpOpenCL(context, commandQueue, program, kernel, memoryObjects, numMemoryObjects);
98  cerr << "Failed enqueuing the kernel. " << __FILE__ << ":"<< __LINE__ << endl;
99  return 1;
100  }
101  /* [Enqueue the kernel] */
102 
103  /* [Wait for kernel execution completion] */
104  if (!checkSuccess(clFinish(commandQueue)))
105  {
106  cleanUpOpenCL(context, commandQueue, program, kernel, memoryObjects, numMemoryObjects);
107  cerr << "Failed waiting for kernel execution to finish. " << __FILE__ << ":"<< __LINE__ << endl;
108  return 1;
109  }
110  /* [Wait for kernel execution completion] */
111 
112 
113  /* After execution. */
114 
115  /* [Print the profiling information for the event] */
116  printProfilingInfo(event);
117  /* Release the event object. */
118  if (!checkSuccess(clReleaseEvent(event)))
119  {
120  cleanUpOpenCL(context, commandQueue, program, kernel, memoryObjects, numMemoryObjects);
121  cerr << "Failed releasing the event object. " << __FILE__ << ":"<< __LINE__ << endl;
122  return 1;
123  }
124  /* [Print the profiling information for the event] */
125 
126 
127  /* Add code here to retrieve results of the kernel execution. */
128 
129 
130  /* [Release OpenCL objects] */
131  cleanUpOpenCL(context, commandQueue, program, kernel, memoryObjects, numMemoryObjects);
132  /* [Release OpenCL objects] */
133 
134  return 0;
135 }