ViennaCL - The Vienna Computing Library  1.6.2
Free open-source GPU-accelerated linear algebra and solver library.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
multithreaded.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 
28 #ifndef VIENNACL_WITH_OPENCL
29  #define VIENNACL_WITH_OPENCL
30 #endif
31 
32 // include necessary system headers
33 #include <iostream>
34 
35 //include basic scalar and vector types of ViennaCL
36 #include "viennacl/scalar.hpp"
37 #include "viennacl/vector.hpp"
38 #include "viennacl/matrix.hpp"
39 
40 #include "viennacl/ocl/device.hpp"
42 #include "viennacl/ocl/backend.hpp"
43 
44 //include the generic inner product functions of ViennaCL
46 
47 // Some helper functions for this tutorial:
48 #include "Random.hpp"
49 
50 #include <boost/thread.hpp>
51 
52 
57 template<typename NumericT>
58 class worker
59 {
60 public:
61  worker(std::size_t tid) : thread_id_(tid) {}
62 
63  void operator()()
64  {
65  std::size_t N = 6;
66 
67  viennacl::context ctx(viennacl::ocl::get_context(static_cast<long>(thread_id_)));
68  viennacl::vector<NumericT> u = viennacl::scalar_vector<NumericT>(N, NumericT(1) * NumericT(thread_id_ + 1), ctx);
69  viennacl::vector<NumericT> v = viennacl::scalar_vector<NumericT>(N, NumericT(2) * NumericT(thread_id_ + 1), ctx);
72 
73  u += v;
74  NumericT result = viennacl::linalg::norm_2(u);
75 
76  std::stringstream ss;
77  ss << "Result of thread " << thread_id_ << " on device " << viennacl::ocl::get_context(static_cast<long>(thread_id_)).devices()[0].name() << ": " << result << std::endl;
78  ss << " A: " << A << std::endl;
79  ss << " x: " << x << std::endl;
80  message_ = ss.str();
81  }
82 
83  std::string message() const { return message_; }
84 
85 private:
86  std::string message_;
87  std::size_t thread_id_;
88 };
89 
93 int main()
94 {
95  // Change this type definition to double if your gpu supports that
96  typedef float ScalarType;
97 
99  {
100  std::cerr << "Error: No platform found!" << std::endl;
101  return EXIT_FAILURE;
102  }
103 
108  std::vector<viennacl::ocl::device> const & devices = pf.devices();
109 
110  // Set first device to first context:
111  viennacl::ocl::setup_context(0, devices[0]);
112 
113  // Set second device for second context (use the same device for the second context if only one device available):
114  if (devices.size() > 1)
115  viennacl::ocl::setup_context(1, devices[1]);
116  else
117  viennacl::ocl::setup_context(1, devices[0]);
118 
123  worker<ScalarType> work_functor0(0);
124  worker<ScalarType> work_functor1(1);
125  boost::thread worker_thread_0(boost::ref(work_functor0));
126  boost::thread worker_thread_1(boost::ref(work_functor1));
127 
128  worker_thread_0.join();
129  worker_thread_1.join();
130 
131  std::cout << work_functor0.message() << std::endl;
132  std::cout << work_functor1.message() << std::endl;
133 
137  std::cout << "!!!! TUTORIAL COMPLETED SUCCESSFULLY !!!!" << std::endl;
138 
139  return EXIT_SUCCESS;
140 }
141 
T norm_2(std::vector< T, A > const &v1)
Definition: norm_2.hpp:86
std::vector< platform > get_platforms()
Definition: platform.hpp:124
Generic interface for the l^2-norm. See viennacl/linalg/vector_operations.hpp for implementations...
Represents an OpenCL device within ViennaCL.
Implements a OpenCL platform within ViennaCL.
Wrapper class for an OpenCL platform.
Definition: platform.hpp:45
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
A dense matrix class.
Definition: forwards.h:374
Represents a generic 'context' similar to an OpenCL context, but is backend-agnostic and thus also su...
Definition: context.hpp:39
vcl_size_t size(VectorType const &vec)
Generic routine for obtaining the size of a vector (ViennaCL, uBLAS, etc.)
Definition: size.hpp:144
Implementations of the OpenCL backend, where all contexts are stored in.
viennacl::matrix_expression< const vector_base< NumericT >, const vector_base< NumericT >, op_prod > outer_prod(const vector_base< NumericT > &vec1, const vector_base< NumericT > &vec2)
Returns a proxy class for the operation mat += vec1 * vec2^T, i.e. a rank 1 update.
The vector type with operator-overloads and proxy classes is defined here. Linear algebra operations ...
Represents a vector consisting of scalars 's' only, i.e. v[i] = s for all i. To be used as an initial...
Definition: vector_def.hpp:87
float ScalarType
Definition: fft_1d.cpp:42
viennacl::ocl::context & get_context(long i)
Convenience function for returning the current context.
Definition: backend.hpp:225
std::vector< viennacl::ocl::device > const & devices() const
Returns a vector with all devices in this context.
Definition: context.hpp:105
Implementation of the ViennaCL scalar class.
void setup_context(long i, std::vector< cl_device_id > const &devices)
Convenience function for setting devices for a context.
Definition: backend.hpp:231