ViennaCL - The Vienna Computing Library  1.6.1
Free open-source GPU-accelerated linear algebra and solver library.
custom-kernels.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 
26 // System headers
27 #include <iostream>
28 #include <string>
29 
30 #ifndef VIENNACL_WITH_OPENCL
31  #define VIENNACL_WITH_OPENCL
32 #endif
33 
34 // ViennaCL headers
35 #include "viennacl/ocl/backend.hpp"
36 #include "viennacl/vector.hpp"
38 
39 
40 // Some helper functions for this tutorial:
41 #include "Random.hpp"
42 
43 
58 static const char * my_compute_program =
59 "__kernel void elementwise_prod(\n"
60 " __global const float * vec1,\n"
61 " __global const float * vec2, \n"
62 " __global float * result,\n"
63 " unsigned int size) \n"
64 "{ \n"
65 " for (unsigned int i = get_global_id(0); i < size; i += get_global_size(0))\n"
66 " result[i] = vec1[i] * vec2[i];\n"
67 "};\n\n"
68 "__kernel void elementwise_div(\n"
69 " __global const float * vec1,\n"
70 " __global const float * vec2, \n"
71 " __global float * result,\n"
72 " unsigned int size) \n"
73 "{ \n"
74 " for (unsigned int i = get_global_id(0); i < size; i += get_global_size(0))\n"
75 " result[i] = vec1[i] / vec2[i];\n"
76 "};\n";
77 
81 int main()
82 {
83  typedef float ScalarType;
84 
88  unsigned int vector_size = 10;
89  viennacl::vector<ScalarType> vec1(vector_size);
90  viennacl::vector<ScalarType> vec2(vector_size);
91  viennacl::vector<ScalarType> result_mul(vector_size);
92  viennacl::vector<ScalarType> result_div(vector_size);
93 
97  for (unsigned int i=0; i<vector_size; ++i)
98  {
99  vec1[i] = static_cast<ScalarType>(i);
100  vec2[i] = static_cast<ScalarType>(vector_size-i);
101  }
102 
107  viennacl::ocl::program & my_prog = viennacl::ocl::current_context().add_program(my_compute_program, "my_compute_program");
108  // Note: Releases older than ViennaCL 1.5.0 required calls to add_kernel(). This is no longer needed, the respective interface has been removed.
109 
115  viennacl::ocl::kernel & my_kernel_mul = my_prog.get_kernel("elementwise_prod");
116  viennacl::ocl::kernel & my_kernel_div = my_prog.get_kernel("elementwise_div");
117 
122  viennacl::ocl::enqueue(my_kernel_mul(vec1, vec2, result_mul, static_cast<cl_uint>(vec1.size())));
123  viennacl::ocl::enqueue(my_kernel_div(vec1, vec2, result_div, static_cast<cl_uint>(vec1.size())));
124 
128  std::cout << " vec1: " << vec1 << std::endl;
129  std::cout << " vec2: " << vec2 << std::endl;
130  std::cout << "vec1 .* vec2: " << result_mul << std::endl;
131  std::cout << "vec1 /* vec2: " << result_div << std::endl;
132  std::cout << "norm_2(vec1 .* vec2): " << viennacl::linalg::norm_2(result_mul) << std::endl;
133  std::cout << "norm_2(vec1 /* vec2): " << viennacl::linalg::norm_2(result_div) << std::endl;
134 
138  std::cout << "!!!! TUTORIAL COMPLETED SUCCESSFULLY !!!!" << std::endl;
139 
140  return EXIT_SUCCESS;
141 }
142 
T norm_2(std::vector< T, A > const &v1)
Definition: norm_2.hpp:86
Generic interface for the l^2-norm. See viennacl/linalg/vector_operations.hpp for implementations...
Represents an OpenCL kernel within ViennaCL.
Definition: kernel.hpp:58
int main()
Definition: bisect.cpp:160
viennacl::ocl::context & current_context()
Convenience function for returning the current context.
Definition: backend.hpp:213
viennacl::ocl::program & add_program(cl_program p, std::string const &prog_name)
Adds a program to the context.
Definition: context.hpp:370
Wrapper class for an OpenCL program.
Definition: program.hpp:42
Implementations of the OpenCL backend, where all contexts are stored in.
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
viennacl::ocl::kernel & get_kernel(std::string const &name)
Returns the kernel with the provided name.
Definition: context.hpp:775