LibreOffice Module sc (master)  1
opbase.hxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  */
9 
10 #ifndef INCLUDED_SC_SOURCE_CORE_OPENCL_OPBASE_HXX
11 #define INCLUDED_SC_SOURCE_CORE_OPENCL_OPBASE_HXX
12 
13 #include <clew/clew.h>
14 #include <formula/token.hxx>
15 #include <formula/types.hxx>
16 #include <memory>
17 #include <set>
18 #include <vector>
19 
20 namespace formula { class DoubleVectorRefToken; }
21 namespace formula { class FormulaToken; }
22 struct ScCalcConfig;
23 
24 namespace sc::opencl {
25 
26 class FormulaTreeNode;
27 
29 
32 {
33 public:
34  UnhandledToken( const char* m, const std::string& fn, int ln );
35 
36  std::string mMessage;
37  std::string mFile;
39 };
40 
43 {
44 public:
45  OpenCLError( const std::string& function, cl_int error, const std::string& file, int line );
46 
47  std::string mFunction;
48  cl_int mError;
49  std::string mFile;
51 };
52 
54 class Unhandled
55 {
56 public:
57  Unhandled( const std::string& fn, int ln );
58 
59  std::string mFile;
61 };
62 
64 {
65 public:
66  InvalidParameterCount( int parameterCount, const std::string& file, int ln );
67 
69  std::string mFile;
70  int const mLineNumber;
71 };
72 
73 // Helper macro to be used in code emitting OpenCL code for Calc functions.
74 // Requires the vSubArguments parameter.
75 #define CHECK_PARAMETER_COUNT(min, max) \
76  do { \
77  const int count = vSubArguments.size(); \
78  if( count < ( min ) || count > ( max )) \
79  throw InvalidParameterCount( count, __FILE__, __LINE__ ); \
80  } while( false )
81 
82 typedef std::shared_ptr<FormulaTreeNode> FormulaTreeNodeRef;
83 
85 {
86 public:
88  {
89  Children.reserve(8);
90  }
91  std::vector<FormulaTreeNodeRef> Children;
93  {
94  return const_cast<formula::FormulaToken*>(mpCurrentFormula.get());
95  }
96 
97 private:
99 };
100 
103 {
104 public:
106  DynamicKernelArgument( const DynamicKernelArgument& ) = delete;
107 
109  const DynamicKernelArgument& operator=( const DynamicKernelArgument& ) = delete;
110 
111  DynamicKernelArgument( const ScCalcConfig& config, const std::string& s, const FormulaTreeNodeRef& ft );
113 
115  virtual void GenDecl( std::stringstream& ss ) const = 0;
116 
118  virtual void GenSlidingWindowDecl( std::stringstream& ss ) const = 0;
119 
121  virtual std::string GenSlidingWindowDeclRef( bool = false ) const = 0;
122 
124  virtual size_t Marshal( cl_kernel, int, int, cl_program ) = 0;
125 
126  virtual size_t GetWindowSize() const = 0;
127 
129  virtual std::string GenDoubleSlidingWindowDeclRef( bool = false ) const;
130 
132  virtual std::string GenStringSlidingWindowDeclRef( bool = false ) const;
133 
135  virtual void GenDeclRef( std::stringstream& ss ) const;
136 
137  virtual void GenSlidingWindowFunction( std::stringstream& );
139  virtual std::string DumpOpName() const;
140  virtual void DumpInlineFun( std::set<std::string>&, std::set<std::string>& ) const;
141  const std::string& GetName() const;
142  virtual bool NeedParallelReduction() const;
144  virtual bool IsEmpty() const { return false; }
145 
146 protected:
148  std::string mSymName;
149  FormulaTreeNodeRef mFormulaTree;
150 };
151 
152 typedef std::shared_ptr<DynamicKernelArgument> DynamicKernelArgumentRef;
153 
160 {
161 public:
162  VectorRef( const ScCalcConfig& config, const std::string& s, const FormulaTreeNodeRef& ft, int index = 0 );
163  virtual ~VectorRef() override;
164 
166  virtual void GenDecl( std::stringstream& ss ) const override;
168  virtual void GenSlidingWindowDecl( std::stringstream& ss ) const override;
169 
171  virtual std::string GenSlidingWindowDeclRef( bool = false ) const override;
172 
174  virtual size_t Marshal( cl_kernel, int, int, cl_program ) override;
175 
176  virtual void GenSlidingWindowFunction( std::stringstream& ) override;
177  virtual size_t GetWindowSize() const override;
178  virtual std::string DumpOpName() const override;
179  virtual void DumpInlineFun( std::set<std::string>&, std::set<std::string>& ) const override;
180  const std::string& GetName() const;
181  cl_mem GetCLBuffer() const;
182  virtual bool NeedParallelReduction() const override;
183 
184 protected:
185  // Used by marshaling
186  cl_mem mpClmem;
187  // index in multiple double vector refs that have multiple ranges
188  const int mnIndex;
189 };
190 
192 class OpBase
193 {
194 public:
195  virtual std::string GetBottom() { return "";};
196  virtual std::string Gen2( const std::string&/*lhs*/,
197  const std::string&/*rhs*/ ) const { return "";}
198  static std::string Gen( std::vector<std::string>& /*argVector*/ ) { return "";};
199  virtual std::string BinFuncName() const { return "";};
200  virtual void BinInlineFun( std::set<std::string>&,
201  std::set<std::string>& ) { }
202  virtual bool takeString() const = 0;
203  virtual bool takeNumeric() const = 0;
204  // Whether DoubleRef containing more than one column is handled properly.
205  virtual bool canHandleMultiVector() const { return false; }
206  //Continue process 'Zero' or Not(like OpMul, not continue process when meet
207  // 'Zero'
208  virtual bool ZeroReturnZero() { return false;}
209  virtual ~OpBase() { }
210 };
211 
213 {
214 public:
215  typedef std::vector<DynamicKernelArgumentRef> SubArguments;
216  virtual void GenSlidingWindowFunction( std::stringstream&,
217  const std::string&, SubArguments& ) = 0;
218 };
219 
221 {
222 public:
223  virtual void GenSlidingWindowFunction( std::stringstream& ss,
224  const std::string& sSymName, SubArguments& vSubArguments ) override;
225  virtual bool takeString() const override { return false; }
226  virtual bool takeNumeric() const override { return true; }
227 };
228 
229 class CheckVariables : public Normal
230 {
231 public:
232  static void GenTmpVariables( std::stringstream& ss, const SubArguments& vSubArguments );
233  static void CheckSubArgumentIsNan( std::stringstream& ss,
234  SubArguments& vSubArguments, int argumentNum );
235  static void CheckAllSubArgumentIsNan( std::stringstream& ss,
236  SubArguments& vSubArguments );
237  // only check isnan
238  static void CheckSubArgumentIsNan2( std::stringstream& ss,
239  SubArguments& vSubArguments, int argumentNum, const std::string& p );
240  static void UnrollDoubleVector( std::stringstream& ss,
241  const std::stringstream& unrollstr, const formula::DoubleVectorRefToken* pCurDVR,
242  int nCurWindowSize );
243 };
244 
245 }
246 
247 #endif
248 
249 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
tuple line
Holds an input (read-only) argument reference to a SingleVectorRef.
Definition: opbase.hxx:159
::boost::intrusive_ptr< const FormulaToken > FormulaConstTokenRef
const DynamicKernelArgument & operator=(const DynamicKernelArgument &)=delete
delete copy-assignment operator
std::shared_ptr< DynamicKernelArgument > DynamicKernelArgumentRef
Definition: opbase.hxx:152
static void CheckSubArgumentIsNan2(std::stringstream &ss, SubArguments &vSubArguments, int argumentNum, const std::string &p)
Definition: opbase.cxx:274
const std::string & GetName() const
Definition: opbase.cxx:75
virtual void GenSlidingWindowFunction(std::stringstream &ss, const std::string &sSymName, SubArguments &vSubArguments) override
Definition: opbase.cxx:176
Failed in marshaling.
Definition: opbase.hxx:42
FormulaTreeNode(const formula::FormulaToken *ft)
Definition: opbase.hxx:87
static std::string Gen(std::vector< std::string > &)
Definition: opbase.hxx:198
Inconsistent state.
Definition: opbase.hxx:54
virtual void GenSlidingWindowFunction(std::stringstream &)
Definition: opbase.cxx:61
const std::string & GetName() const
Definition: opbase.cxx:161
virtual void GenSlidingWindowDecl(std::stringstream &ss) const override
When declared as input to a sliding window function.
Definition: opbase.cxx:113
virtual void GenSlidingWindowFunction(std::stringstream &, const std::string &, SubArguments &)=0
static void CheckAllSubArgumentIsNan(std::stringstream &ss, SubArguments &vSubArguments)
Definition: opbase.cxx:301
std::shared_ptr< FormulaTreeNode > FormulaTreeNodeRef
Definition: opbase.hxx:82
(Partially) abstract base class for an operand
Definition: opbase.hxx:102
formula::FormulaConstTokenRef mpCurrentFormula
Definition: opbase.hxx:98
virtual bool takeNumeric() const =0
virtual void GenDecl(std::stringstream &ss) const override
Generate declaration.
Definition: opbase.cxx:107
virtual std::string DumpOpName() const override
Definition: opbase.cxx:154
virtual bool takeNumeric() const override
Definition: opbase.hxx:226
virtual size_t GetWindowSize() const =0
virtual std::string GenSlidingWindowDeclRef(bool=false) const =0
When referenced in a sliding window function.
virtual std::string GenDoubleSlidingWindowDeclRef(bool=false) const
When Mix, it will be called.
Definition: opbase.cxx:44
static void GenTmpVariables(std::stringstream &ss, const SubArguments &vSubArguments)
Definition: opbase.cxx:199
virtual ~VectorRef() override
Definition: opbase.cxx:96
virtual std::string GenSlidingWindowDeclRef(bool=false) const override
When referenced in a sliding window function.
Definition: opbase.cxx:119
Abstract class for code generation.
Definition: opbase.hxx:192
FormulaTreeNodeRef mFormulaTree
Definition: opbase.hxx:149
virtual void GenSlidingWindowDecl(std::stringstream &ss) const =0
When declared as input to a sliding window function.
VectorRef(const ScCalcConfig &config, const std::string &s, const FormulaTreeNodeRef &ft, int index=0)
Definition: opbase.cxx:85
virtual void DumpInlineFun(std::set< std::string > &, std::set< std::string > &) const override
Definition: opbase.cxx:159
std::string mFile
Definition: opbase.hxx:49
static void CheckSubArgumentIsNan(std::stringstream &ss, SubArguments &vSubArguments, int argumentNum)
Definition: opbase.cxx:210
virtual std::string DumpOpName() const
Definition: opbase.cxx:68
static void UnrollDoubleVector(std::stringstream &ss, const std::stringstream &unrollstr, const formula::DoubleVectorRefToken *pCurDVR, int nCurWindowSize)
Definition: opbase.cxx:311
virtual void BinInlineFun(std::set< std::string > &, std::set< std::string > &)
Definition: opbase.hxx:200
virtual std::string GetBottom()
Definition: opbase.hxx:195
DynamicKernelArgument(const DynamicKernelArgument &)=delete
delete copy constructor
virtual std::string BinFuncName() const
Definition: opbase.hxx:199
formula::FormulaToken * GetFormulaToken() const
Definition: opbase.hxx:92
std::vector< DynamicKernelArgumentRef > SubArguments
Definition: opbase.hxx:215
virtual void GenDecl(std::stringstream &ss) const =0
Generate declaration.
virtual size_t Marshal(cl_kernel, int, int, cl_program)=0
Create buffer and pass the buffer to a given kernel.
Arguments that are actually compile-time constant string Currently, only the hash is passed...
virtual ~OpBase()
Definition: opbase.hxx:209
formula::FormulaToken * GetFormulaToken() const
Definition: opbase.cxx:63
tuple index
Unhandled(const std::string &fn, int ln)
Definition: opbase.cxx:34
virtual bool NeedParallelReduction() const override
Definition: opbase.cxx:171
virtual void DumpInlineFun(std::set< std::string > &, std::set< std::string > &) const
Definition: opbase.cxx:73
Configuration options for formula interpreter.
Definition: calcconfig.hxx:43
cl_mem GetCLBuffer() const
Definition: opbase.cxx:166
std::string mFunction
Definition: opbase.hxx:47
std::vector< FormulaTreeNodeRef > Children
Definition: opbase.hxx:91
virtual void GenDeclRef(std::stringstream &ss) const
Generate use/references to the argument.
Definition: opbase.cxx:56
virtual bool ZeroReturnZero()
Definition: opbase.hxx:208
virtual bool takeString() const =0
virtual void GenSlidingWindowFunction(std::stringstream &) override
Definition: opbase.cxx:132
tuple config
InvalidParameterCount(int parameterCount, const std::string &file, int ln)
Definition: opbase.cxx:37
void * p
virtual std::string Gen2(const std::string &, const std::string &) const
Definition: opbase.hxx:196
const ScCalcConfig & mCalcConfig
Definition: opbase.hxx:147
virtual bool takeString() const override
Definition: opbase.hxx:225
virtual bool canHandleMultiVector() const
Definition: opbase.hxx:205
virtual bool IsEmpty() const
If there's actually no argument, i.e. it expands to no code.
Definition: opbase.hxx:144
tuple m
virtual size_t Marshal(cl_kernel, int, int, cl_program) override
Create buffer and pass the buffer to a given kernel.
UnhandledToken(const char *m, const std::string &fn, int ln)
Definition: opbase.cxx:20
OpenCLError(const std::string &function, cl_int error, const std::string &file, int line)
Definition: opbase.cxx:24
virtual bool NeedParallelReduction() const
Definition: opbase.cxx:80
std::string mFile
Definition: opbase.hxx:59
virtual size_t GetWindowSize() const override
Definition: opbase.cxx:134
virtual std::string GenStringSlidingWindowDeclRef(bool=false) const
When Mix, it will be called.
Definition: opbase.cxx:50