ViennaCL - The Vienna Computing Library  1.7.0
Free open-source GPU-accelerated linear algebra and solver library.
nmf.hpp
Go to the documentation of this file.
1 #ifndef VIENNACL_LINALG_OPENCL_KERNELS_NMF_HPP
2 #define VIENNACL_LINALG_OPENCL_KERNELS_NMF_HPP
3 
4 /* =========================================================================
5  Copyright (c) 2010-2015, Institute for Microelectronics,
6  Institute for Analysis and Scientific Computing,
7  TU Wien.
8  Portions of this software are copyright by UChicago Argonne, LLC.
9 
10  -----------------
11  ViennaCL - The Vienna Computing Library
12  -----------------
13 
14  Project Head: Karl Rupp rupp@iue.tuwien.ac.at
15 
16  (A list of authors and contributors can be found in the manual)
17 
18  License: MIT (X11), see file LICENSE in the base directory
19 ============================================================================= */
20 
21 #include "viennacl/tools/tools.hpp"
22 #include "viennacl/ocl/kernel.hpp"
24 #include "viennacl/ocl/utils.hpp"
25 
28 namespace viennacl
29 {
30 namespace linalg
31 {
32 namespace opencl
33 {
34 namespace kernels
35 {
36 
37 template<typename StringT>
38 void generate_nmf_el_wise_mul_div(StringT & source, std::string const & numeric_string)
39 {
40  source.append("__kernel void el_wise_mul_div( \n");
41  source.append(" __global "); source.append(numeric_string); source.append(" * matrix1, \n");
42  source.append(" __global const "); source.append(numeric_string); source.append(" * matrix2, \n");
43  source.append(" __global const "); source.append(numeric_string); source.append(" * matrix3, \n");
44  source.append(" unsigned int size) \n");
45  source.append("{ \n");
46  source.append(" for (unsigned int i = get_global_id(0); i < size; i += get_global_size(0)) \n");
47  source.append(" { \n");
48  source.append(" "); source.append(numeric_string); source.append(" val = matrix1[i] * matrix2[i]; \n");
49  source.append(" "); source.append(numeric_string); source.append(" divisor = matrix3[i]; \n");
50  source.append(" matrix1[i] = (divisor > ("); source.append(numeric_string); source.append(")0.00001) ? (val / divisor) : ("); source.append(numeric_string); source.append(")0; \n");
51  source.append(" } \n");
52  source.append("} \n");
53 }
54 
55 // main kernel class
57 template<typename NumericT>
58 struct nmf
59 {
60  static std::string program_name()
61  {
63  }
64 
65  static void init(viennacl::ocl::context & ctx)
66  {
67  static std::map<cl_context, bool> init_done;
68  if (!init_done[ctx.handle().get()])
69  {
71  std::string numeric_string = viennacl::ocl::type_to_string<NumericT>::apply();
72 
73  std::string source;
74  source.reserve(8192);
75 
76  viennacl::ocl::append_double_precision_pragma<NumericT>(ctx, source);
77 
78  // only generate for floating points (forces error for integers)
79  if (numeric_string == "float" || numeric_string == "double")
80  {
81  generate_nmf_el_wise_mul_div(source, numeric_string);
82  }
83 
84  std::string prog_name = program_name();
85  #ifdef VIENNACL_BUILD_INFO
86  std::cout << "Creating program " << prog_name << std::endl;
87  #endif
88  ctx.add_program(source, prog_name);
89  init_done[ctx.handle().get()] = true;
90  } //if
91  } //init
92 };
93 
94 } // namespace kernels
95 } // namespace opencl
96 } // namespace linalg
97 } // namespace viennacl
98 #endif
99 
Implements a OpenCL platform within ViennaCL.
void generate_nmf_el_wise_mul_div(StringT &source, std::string const &numeric_string)
Definition: nmf.hpp:38
Various little tools used here and there in ViennaCL.
Manages an OpenCL context and provides the respective convenience functions for creating buffers...
Definition: context.hpp:55
Provides OpenCL-related utilities.
const viennacl::ocl::handle< cl_context > & handle() const
Returns the context handle.
Definition: context.hpp:611
Main namespace in ViennaCL. Holds all the basic types such as vector, matrix, etc. and defines operations upon them.
Definition: cpu_ram.hpp:34
static std::string program_name()
Definition: nmf.hpp:60
static void apply(viennacl::ocl::context const &)
Definition: utils.hpp:40
const OCL_TYPE & get() const
Definition: handle.hpp:189
Main kernel class for generating OpenCL kernels for nonnegative matrix factorization of a dense matri...
Definition: nmf.hpp:58
Representation of an OpenCL kernel in ViennaCL.
static void init(viennacl::ocl::context &ctx)
Definition: nmf.hpp:65
Helper class for converting a type to its string representation.
Definition: utils.hpp:57