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 #pragma once
11 
12 #include <clew/clew.h>
13 #include <formula/token.hxx>
14 #include <formula/types.hxx>
15 #include <memory>
16 #include <set>
17 #include <vector>
18 
19 namespace formula { class DoubleVectorRefToken; }
20 namespace formula { class FormulaToken; }
21 struct ScCalcConfig;
22 
23 namespace sc::opencl {
24 
25 class FormulaTreeNode;
26 
28 
31 {
32 public:
33  UnhandledToken( const char* m, const std::string& fn, int ln );
34 
35  std::string mMessage;
36  std::string mFile;
38 };
39 
42 {
43 public:
44  OpenCLError( const std::string& function, cl_int error, const std::string& file, int line );
45 
46  std::string mFunction;
47  cl_int mError;
48  std::string mFile;
50 };
51 
53 class Unhandled
54 {
55 public:
56  Unhandled( const std::string& fn, int ln );
57 
58  std::string mFile;
60 };
61 
63 {
64 public:
65  InvalidParameterCount( int parameterCount, const std::string& file, int ln );
66 
68  std::string mFile;
69  int const mLineNumber;
70 };
71 
72 // Helper macro to be used in code emitting OpenCL code for Calc functions.
73 // Requires the vSubArguments parameter.
74 #define CHECK_PARAMETER_COUNT(min, max) \
75  do { \
76  const int count = vSubArguments.size(); \
77  if( count < ( min ) || count > ( max )) \
78  throw InvalidParameterCount( count, __FILE__, __LINE__ ); \
79  } while( false )
80 
81 typedef std::shared_ptr<FormulaTreeNode> FormulaTreeNodeRef;
82 
84 {
85 public:
87  {
88  Children.reserve(8);
89  }
90  std::vector<FormulaTreeNodeRef> Children;
92  {
93  return const_cast<formula::FormulaToken*>(mpCurrentFormula.get());
94  }
95 
96 private:
98 };
99 
102 {
103 public:
105  DynamicKernelArgument( const DynamicKernelArgument& ) = delete;
106 
108  const DynamicKernelArgument& operator=( const DynamicKernelArgument& ) = delete;
109 
110  DynamicKernelArgument( const ScCalcConfig& config, const std::string& s, const FormulaTreeNodeRef& ft );
112 
114  virtual void GenDecl( std::stringstream& ss ) const = 0;
115 
117  virtual void GenSlidingWindowDecl( std::stringstream& ss ) const = 0;
118 
120  virtual std::string GenSlidingWindowDeclRef( bool = false ) const = 0;
121 
123  virtual size_t Marshal( cl_kernel, int, int, cl_program ) = 0;
124 
125  virtual size_t GetWindowSize() const = 0;
126 
128  virtual std::string GenDoubleSlidingWindowDeclRef( bool = false ) const;
129 
131  virtual std::string GenStringSlidingWindowDeclRef( bool = false ) const;
132 
134  virtual void GenDeclRef( std::stringstream& ss ) const;
135 
136  virtual void GenSlidingWindowFunction( std::stringstream& );
138  virtual std::string DumpOpName() const;
139  virtual void DumpInlineFun( std::set<std::string>&, std::set<std::string>& ) const;
140  const std::string& GetName() const;
141  virtual bool NeedParallelReduction() const;
143  virtual bool IsEmpty() const { return false; }
144 
145 protected:
147  std::string mSymName;
148  FormulaTreeNodeRef mFormulaTree;
149 };
150 
151 typedef std::shared_ptr<DynamicKernelArgument> DynamicKernelArgumentRef;
152 
159 {
160 public:
161  VectorRef( const ScCalcConfig& config, const std::string& s, const FormulaTreeNodeRef& ft, int index = 0 );
162  virtual ~VectorRef() override;
163 
165  virtual void GenDecl( std::stringstream& ss ) const override;
167  virtual void GenSlidingWindowDecl( std::stringstream& ss ) const override;
168 
170  virtual std::string GenSlidingWindowDeclRef( bool = false ) const override;
171 
173  virtual size_t Marshal( cl_kernel, int, int, cl_program ) override;
174 
175  virtual void GenSlidingWindowFunction( std::stringstream& ) override;
176  virtual size_t GetWindowSize() const override;
177  virtual std::string DumpOpName() const override;
178  virtual void DumpInlineFun( std::set<std::string>&, std::set<std::string>& ) const override;
179  const std::string& GetName() const;
180  cl_mem GetCLBuffer() const;
181  virtual bool NeedParallelReduction() const override;
182 
183 protected:
184  // Used by marshaling
185  cl_mem mpClmem;
186  // index in multiple double vector refs that have multiple ranges
187  const int mnIndex;
188 };
189 
191 class OpBase
192 {
193 public:
194  virtual std::string GetBottom() { return "";};
195  virtual std::string Gen2( const std::string&/*lhs*/,
196  const std::string&/*rhs*/ ) const { return "";}
197  static std::string Gen( std::vector<std::string>& /*argVector*/ ) { return "";};
198  virtual std::string BinFuncName() const { return "";};
199  virtual void BinInlineFun( std::set<std::string>&,
200  std::set<std::string>& ) { }
201  virtual bool takeString() const = 0;
202  virtual bool takeNumeric() const = 0;
203  // Whether DoubleRef containing more than one column is handled properly.
204  virtual bool canHandleMultiVector() const { return false; }
205  //Continue process 'Zero' or Not(like OpMul, not continue process when meet
206  // 'Zero'
207  virtual bool ZeroReturnZero() { return false;}
208  virtual ~OpBase() { }
209 };
210 
212 {
213 public:
214  typedef std::vector<DynamicKernelArgumentRef> SubArguments;
215  virtual void GenSlidingWindowFunction( std::stringstream&,
216  const std::string&, SubArguments& ) = 0;
217 };
218 
220 {
221 public:
222  virtual void GenSlidingWindowFunction( std::stringstream& ss,
223  const std::string& sSymName, SubArguments& vSubArguments ) override;
224  virtual bool takeString() const override { return false; }
225  virtual bool takeNumeric() const override { return true; }
226 };
227 
228 class CheckVariables : public Normal
229 {
230 public:
231  static void GenTmpVariables( std::stringstream& ss, const SubArguments& vSubArguments );
232  static void CheckSubArgumentIsNan( std::stringstream& ss,
233  SubArguments& vSubArguments, int argumentNum );
234  static void CheckAllSubArgumentIsNan( std::stringstream& ss,
235  SubArguments& vSubArguments );
236  // only check isnan
237  static void CheckSubArgumentIsNan2( std::stringstream& ss,
238  SubArguments& vSubArguments, int argumentNum, const std::string& p );
239  static void UnrollDoubleVector( std::stringstream& ss,
240  const std::stringstream& unrollstr, const formula::DoubleVectorRefToken* pCurDVR,
241  int nCurWindowSize );
242 };
243 
244 }
245 
246 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
tuple line
Holds an input (read-only) argument reference to a SingleVectorRef.
Definition: opbase.hxx:158
::boost::intrusive_ptr< const FormulaToken > FormulaConstTokenRef
const DynamicKernelArgument & operator=(const DynamicKernelArgument &)=delete
delete copy-assignment operator
std::shared_ptr< DynamicKernelArgument > DynamicKernelArgumentRef
Definition: opbase.hxx:151
static void CheckSubArgumentIsNan2(std::stringstream &ss, SubArguments &vSubArguments, int argumentNum, const std::string &p)
Definition: opbase.cxx:273
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:41
FormulaTreeNode(const formula::FormulaToken *ft)
Definition: opbase.hxx:86
static std::string Gen(std::vector< std::string > &)
Definition: opbase.hxx:197
Inconsistent state.
Definition: opbase.hxx:53
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:300
std::shared_ptr< FormulaTreeNode > FormulaTreeNodeRef
Definition: opbase.hxx:81
(Partially) abstract base class for an operand
Definition: opbase.hxx:101
formula::FormulaConstTokenRef mpCurrentFormula
Definition: opbase.hxx:97
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:225
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:191
FormulaTreeNodeRef mFormulaTree
Definition: opbase.hxx:148
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:48
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:310
virtual void BinInlineFun(std::set< std::string > &, std::set< std::string > &)
Definition: opbase.hxx:199
virtual std::string GetBottom()
Definition: opbase.hxx:194
DynamicKernelArgument(const DynamicKernelArgument &)=delete
delete copy constructor
virtual std::string BinFuncName() const
Definition: opbase.hxx:198
formula::FormulaToken * GetFormulaToken() const
Definition: opbase.hxx:91
std::vector< DynamicKernelArgumentRef > SubArguments
Definition: opbase.hxx:214
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:208
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:46
std::vector< FormulaTreeNodeRef > Children
Definition: opbase.hxx:90
virtual void GenDeclRef(std::stringstream &ss) const
Generate use/references to the argument.
Definition: opbase.cxx:56
virtual bool ZeroReturnZero()
Definition: opbase.hxx:207
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:195
const ScCalcConfig & mCalcConfig
Definition: opbase.hxx:146
virtual bool takeString() const override
Definition: opbase.hxx:224
virtual bool canHandleMultiVector() const
Definition: opbase.hxx:204
virtual bool IsEmpty() const
If there's actually no argument, i.e. it expands to no code.
Definition: opbase.hxx:143
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:58
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