ViennaCL - The Vienna Computing Library  1.6.0
Free open-source GPU-accelerated linear algebra and solver library.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
custom-context.cpp
Go to the documentation of this file.
1 /* =========================================================================
2  Copyright (c) 2010-2014, Institute for Microelectronics,
3  Institute for Analysis and Scientific Computing,
4  TU Wien.
5  Portions of this software are copyright by UChicago Argonne, LLC.
6 
7  -----------------
8  ViennaCL - The Vienna Computing Library
9  -----------------
10 
11  Project Head: Karl Rupp rupp@iue.tuwien.ac.at
12 
13  (A list of authors and contributors can be found in the PDF manual)
14 
15  License: MIT (X11), see file LICENSE in the base directory
16 ============================================================================= */
17 
25 // System headers
26 #include <iostream>
27 #include <string>
28 
29 #ifndef VIENNACL_WITH_OPENCL
30  #define VIENNACL_WITH_OPENCL
31 #endif
32 
33 
34 // ViennaCL headers
35 #include "viennacl/ocl/backend.hpp"
36 #include "viennacl/vector.hpp"
37 #include "viennacl/matrix.hpp"
40 #include "viennacl/linalg/prod.hpp"
41 
42 
43 // Some helper functions for this tutorial:
44 #include "Random.hpp"
45 
46 
47 
48 
60 static const char * my_compute_program =
61 "__kernel void elementwise_prod(\n"
62 " __global const float * vec1,\n"
63 " __global const float * vec2, \n"
64 " __global float * result,\n"
65 " unsigned int size) \n"
66 "{ \n"
67 " for (unsigned int i = get_global_id(0); i < size; i += get_global_size(0))\n"
68 " result[i] = vec1[i] * vec2[i];\n"
69 "};\n";
70 
74 int main()
75 {
76  typedef float ScalarType;
77 
78 
87  //manually set up a custom OpenCL context:
88  std::vector<cl_device_id> device_id_array;
89 
90  //get all available devices
92  std::cout << "Platform info: " << pf.info() << std::endl;
93  std::vector<viennacl::ocl::device> devices = pf.devices(CL_DEVICE_TYPE_DEFAULT);
94  std::cout << devices[0].name() << std::endl;
95  std::cout << "Number of devices for custom context: " << devices.size() << std::endl;
96 
97  //set up context using all found devices:
98  for (std::size_t i=0; i<devices.size(); ++i)
99  {
100  device_id_array.push_back(devices[i].id());
101  }
102 
103  std::cout << "Creating context..." << std::endl;
104  cl_int err;
105  cl_context my_context = clCreateContext(0, cl_uint(device_id_array.size()), &(device_id_array[0]), NULL, NULL, &err);
106  VIENNACL_ERR_CHECK(err);
107 
108 
109  //create two Vectors:
110  unsigned int vector_size = 10;
111  std::vector<ScalarType> vec1(vector_size);
112  std::vector<ScalarType> vec2(vector_size);
113  std::vector<ScalarType> result(vector_size);
114 
115  //
116  // fill the operands vec1 and vec2:
117  //
118  for (unsigned int i=0; i<vector_size; ++i)
119  {
120  vec1[i] = static_cast<ScalarType>(i);
121  vec2[i] = static_cast<ScalarType>(vector_size-i);
122  }
123 
124  //
125  // create memory in OpenCL context:
126  //
127  cl_mem mem_vec1 = clCreateBuffer(my_context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, vector_size * sizeof(ScalarType), &(vec1[0]), &err);
128  VIENNACL_ERR_CHECK(err);
129  cl_mem mem_vec2 = clCreateBuffer(my_context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, vector_size * sizeof(ScalarType), &(vec2[0]), &err);
130  VIENNACL_ERR_CHECK(err);
131  cl_mem mem_result = clCreateBuffer(my_context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, vector_size * sizeof(ScalarType), &(result[0]), &err);
132  VIENNACL_ERR_CHECK(err);
133 
134  //
135  // create a command queue for each device:
136  //
137 
138  std::vector<cl_command_queue> queues(devices.size());
139  for (std::size_t i=0; i<devices.size(); ++i)
140  {
141  queues[i] = clCreateCommandQueue(my_context, devices[i].id(), 0, &err);
142  VIENNACL_ERR_CHECK(err);
143  }
144 
145  //
146  // create and build a program in the context:
147  //
148  std::size_t source_len = std::string(my_compute_program).length();
149  cl_program my_prog = clCreateProgramWithSource(my_context, 1, &my_compute_program, &source_len, &err);
150  err = clBuildProgram(my_prog, 0, NULL, NULL, NULL, NULL);
151 
152 /* char buffer[1024];
153  cl_build_status status;
154  clGetProgramBuildInfo(my_prog, devices[1].id(), CL_PROGRAM_BUILD_STATUS, sizeof(cl_build_status), &status, NULL);
155  clGetProgramBuildInfo(my_prog, devices[1].id(), CL_PROGRAM_BUILD_LOG, sizeof(char)*1024, &buffer, NULL);
156  std::cout << "Build Scalar: Err = " << err << " Status = " << status << std::endl;
157  std::cout << "Log: " << buffer << std::endl;*/
158 
159  VIENNACL_ERR_CHECK(err);
160 
161  //
162  // create a kernel from the program:
163  //
164  const char * kernel_name = "elementwise_prod";
165  cl_kernel my_kernel = clCreateKernel(my_prog, kernel_name, &err);
166  VIENNACL_ERR_CHECK(err);
167 
168 
169  //
170  // Execute elementwise_prod kernel on first queue: result = vec1 .* vec2;
171  //
172  err = clSetKernelArg(my_kernel, 0, sizeof(cl_mem), (void*)&mem_vec1);
173  VIENNACL_ERR_CHECK(err);
174  err = clSetKernelArg(my_kernel, 1, sizeof(cl_mem), (void*)&mem_vec2);
175  VIENNACL_ERR_CHECK(err);
176  err = clSetKernelArg(my_kernel, 2, sizeof(cl_mem), (void*)&mem_result);
177  VIENNACL_ERR_CHECK(err);
178  err = clSetKernelArg(my_kernel, 3, sizeof(unsigned int), (void*)&vector_size);
179  VIENNACL_ERR_CHECK(err);
180  std::size_t global_size = vector_size;
181  std::size_t local_size = vector_size;
182  err = clEnqueueNDRangeKernel(queues[0], my_kernel, 1, NULL, &global_size, &local_size, 0, NULL, NULL);
183  VIENNACL_ERR_CHECK(err);
184 
185 
186  //
187  // Read and output result:
188  //
189  err = clEnqueueReadBuffer(queues[0], mem_vec1, CL_TRUE, 0, sizeof(ScalarType)*vector_size, &(vec1[0]), 0, NULL, NULL);
190  VIENNACL_ERR_CHECK(err);
191  err = clEnqueueReadBuffer(queues[0], mem_result, CL_TRUE, 0, sizeof(ScalarType)*vector_size, &(result[0]), 0, NULL, NULL);
192  VIENNACL_ERR_CHECK(err);
193 
194  std::cout << "vec1 : ";
195  for (std::size_t i=0; i<vec1.size(); ++i)
196  std::cout << vec1[i] << " ";
197  std::cout << std::endl;
198 
199  std::cout << "vec2 : ";
200  for (std::size_t i=0; i<vec2.size(); ++i)
201  std::cout << vec2[i] << " ";
202  std::cout << std::endl;
203 
204  std::cout << "result: ";
205  for (std::size_t i=0; i<result.size(); ++i)
206  std::cout << result[i] << " ";
207  std::cout << std::endl;
208 
215  viennacl::ocl::setup_context(0, my_context, device_id_array, queues);
216  viennacl::ocl::switch_context(0); //activate the new context (only mandatory with context-id not equal to zero)
217 
221  std::cout << "Existing context: " << my_context << std::endl;
222  std::cout << "ViennaCL uses context: " << viennacl::ocl::current_context().handle().get() << std::endl;
223 
227  viennacl::vector<ScalarType> vcl_vec1(mem_vec1, vector_size);
228  viennacl::vector<ScalarType> vcl_vec2(mem_vec2, vector_size);
229  viennacl::vector<ScalarType> vcl_result(mem_result, vector_size);
230  viennacl::scalar<ScalarType> vcl_s = 2.0;
231 
232  std::cout << "Standard vector operations within ViennaCL:" << std::endl;
233  vcl_result = vcl_s * vcl_vec1 + vcl_vec2;
234 
235  std::cout << "vec1 : ";
236  std::cout << vcl_vec1 << std::endl;
237 
238  std::cout << "vec2 : ";
239  std::cout << vcl_vec2 << std::endl;
240 
241  std::cout << "result: ";
242  std::cout << vcl_result << std::endl;
243 
249  std::cout << "Using existing kernel within the OpenCL backend of ViennaCL:" << std::endl;
250  viennacl::ocl::program & my_vcl_prog = viennacl::ocl::current_context().add_program(my_prog, "my_compute_program");
251  viennacl::ocl::kernel & my_vcl_kernel = my_vcl_prog.add_kernel(my_kernel, "elementwise_prod");
252  viennacl::ocl::enqueue(my_vcl_kernel(vcl_vec1, vcl_vec2, vcl_result, static_cast<cl_uint>(vcl_vec1.size()))); //Note that std::size_t might differ between host and device. Thus, a cast to cl_uint is necessary here.
253 
254  std::cout << "vec1 : ";
255  std::cout << vcl_vec1 << std::endl;
256 
257  std::cout << "vec2 : ";
258  std::cout << vcl_vec2 << std::endl;
259 
260  std::cout << "result: ";
261  std::cout << vcl_result << std::endl;
262 
263 
269  viennacl::matrix<ScalarType> vcl_matrix(mem_vec1, 3, 3);
270 
271  vcl_vec2.resize(3); //note that the resize operation leads to new memory, thus vcl_vec2 is now at a different memory location (values are copied)
272  vcl_result.resize(3); //note that the resize operation leads to new memory, thus vcl_vec2 is now at a different memory location (values are copied)
273  vcl_result = viennacl::linalg::prod(vcl_matrix, vcl_vec2);
274 
275  std::cout << "result of matrix-vector product: ";
276  std::cout << vcl_result << std::endl;
277 
282  std::cout << "!!!! TUTORIAL COMPLETED SUCCESSFULLY !!!!" << std::endl;
283 
284  return EXIT_SUCCESS;
285 }
286 
viennacl::ocl::kernel & add_kernel(cl_kernel kernel_handle, std::string const &kernel_name)
Adds a kernel to the program.
Definition: context.hpp:767
This class represents a single scalar value on the GPU and behaves mostly like a built-in scalar type...
Definition: forwards.h:226
Generic interface for the l^2-norm. See viennacl/linalg/vector_operations.hpp for implementations...
Implementations of dense matrix related operations including matrix-vector products.
Wrapper class for an OpenCL platform.
Definition: platform.hpp:45
Generic interface for matrix-vector and matrix-matrix products. See viennacl/linalg/vector_operations...
Represents an OpenCL kernel within ViennaCL.
Definition: kernel.hpp:58
Implementation of the dense matrix class.
int main()
Definition: bisect.cpp:160
std::vector< device > devices(cl_device_type dtype=CL_DEVICE_TYPE_DEFAULT)
Returns the available devices of the supplied device type.
Definition: platform.hpp:91
viennacl::ocl::context & current_context()
Convenience function for returning the current context.
Definition: backend.hpp:213
A dense matrix class.
Definition: forwards.h:374
const viennacl::ocl::handle< cl_context > & handle() const
Returns the context handle.
Definition: context.hpp:613
#define VIENNACL_ERR_CHECK(err)
Definition: error.hpp:656
VectorT prod(std::vector< std::vector< T, A1 >, A2 > const &matrix, VectorT const &vector)
Definition: prod.hpp:91
viennacl::ocl::program & add_program(cl_program p, std::string const &prog_name)
Adds a program to the context.
Definition: context.hpp:370
const OCL_TYPE & get() const
Definition: handle.hpp:189
Wrapper class for an OpenCL program.
Definition: program.hpp:42
Implementations of the OpenCL backend, where all contexts are stored in.
void switch_context(long i)
Convenience function for switching the current context.
Definition: backend.hpp:219
void enqueue(KernelType &k, viennacl::ocl::command_queue const &queue)
Enqueues a kernel in the provided queue.
Definition: enqueue.hpp:50
The vector type with operator-overloads and proxy classes is defined here. Linear algebra operations ...
float ScalarType
Definition: fft_1d.cpp:42
std::string info() const
Returns an information string.
Definition: platform.hpp:71
void setup_context(long i, std::vector< cl_device_id > const &devices)
Convenience function for setting devices for a context.
Definition: backend.hpp:231