spla
cl_program_builder.hpp
Go to the documentation of this file.
1 /**********************************************************************************/
2 /* This file is part of spla project */
3 /* https://github.com/SparseLinearAlgebra/spla */
4 /**********************************************************************************/
5 /* MIT License */
6 /* */
7 /* Copyright (c) 2023 SparseLinearAlgebra */
8 /* */
9 /* Permission is hereby granted, free of charge, to any person obtaining a copy */
10 /* of this software and associated documentation files (the "Software"), to deal */
11 /* in the Software without restriction, including without limitation the rights */
12 /* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell */
13 /* copies of the Software, and to permit persons to whom the Software is */
14 /* furnished to do so, subject to the following conditions: */
15 /* */
16 /* The above copyright notice and this permission notice shall be included in all */
17 /* copies or substantial portions of the Software. */
18 /* */
19 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR */
20 /* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, */
21 /* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE */
22 /* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER */
23 /* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */
24 /* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */
25 /* SOFTWARE. */
26 /**********************************************************************************/
27 
28 #ifndef SPLA_CL_PROGRAM_BUILDER_HPP
29 #define SPLA_CL_PROGRAM_BUILDER_HPP
30 
31 #include <core/top.hpp>
32 #include <core/ttype.hpp>
34 #include <opencl/cl_program.hpp>
36 
37 #include <svector.hpp>
38 
39 #include <memory>
40 #include <sstream>
41 #include <string>
42 #include <vector>
43 
44 namespace spla {
45 
55  class CLProgramBuilder final {
56  public:
57  CLProgramBuilder& set_name(const char* name);
58  CLProgramBuilder& add_define(const char* define, int value);
59  CLProgramBuilder& add_type(const char* alias, const ref_ptr<Type>& type);
60  CLProgramBuilder& add_op(const char* name, const ref_ptr<OpUnary>& op);
61  CLProgramBuilder& add_op(const char* name, const ref_ptr<OpBinary>& op);
62  CLProgramBuilder& add_op(const char* name, const ref_ptr<OpSelect>& op);
63  CLProgramBuilder& set_source(const char* source);
64  void acquire();
65 
66  const std::shared_ptr<CLProgram>& get_program() { return m_program; };
67  cl::Kernel make_kernel(const char* name) { return m_program->make_kernel(name); }
68 
69  private:
70  ankerl::svector<std::pair<std::string, std::string>, 8> m_defines;
71  ankerl::svector<std::pair<std::string, ref_ptr<Op>>, 8> m_functions;
72  std::string m_name;
73  const char* m_source = nullptr;
74  std::shared_ptr<CLProgram> m_program;
75  std::string m_program_code;
76  };
77 
82 }// namespace spla
83 
84 #endif//SPLA_CL_PROGRAM_BUILDER_HPP
Runtime opencl program builder.
Definition: cl_program_builder.hpp:55
CLProgramBuilder & add_op(const char *name, const ref_ptr< OpUnary > &op)
Definition: cl_program_builder.cpp:49
const std::shared_ptr< CLProgram > & get_program()
Definition: cl_program_builder.hpp:66
CLProgramBuilder & add_define(const char *define, int value)
Definition: cl_program_builder.cpp:41
CLProgramBuilder & set_name(const char *name)
Definition: cl_program_builder.cpp:37
CLProgramBuilder & add_type(const char *alias, const ref_ptr< Type > &type)
Definition: cl_program_builder.cpp:45
cl::Kernel make_kernel(const char *name)
Definition: cl_program_builder.hpp:67
CLProgramBuilder & set_source(const char *source)
Definition: cl_program_builder.cpp:61
void acquire()
Definition: cl_program_builder.cpp:65
Automates reference counting and behaves as shared smart pointer.
Definition: ref.hpp:117
Definition: algorithm.hpp:37