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
19namespace formula { class DoubleVectorRefToken; }
20namespace formula { class FormulaToken; }
21struct ScCalcConfig;
22
23namespace sc::opencl {
24
25class FormulaTreeNode;
26
28
31{
32public:
33 UnhandledToken( const char* m, const std::string& fn, int ln );
34
35 std::string mMessage;
36 std::string mFile;
38};
39
42{
43public:
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
54{
55public:
56 Unhandled( const std::string& fn, int ln );
57
58 std::string mFile;
60};
61
63{
64public:
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
81typedef std::shared_ptr<FormulaTreeNode> FormulaTreeNodeRef;
82
84{
85public:
87 {
88 Children.reserve(8);
89 }
90 std::vector<FormulaTreeNodeRef> Children;
92 {
93 return const_cast<formula::FormulaToken*>(mpCurrentFormula.get());
94 }
95
96private:
98};
99
102{
103public:
106
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
145protected:
147 std::string mSymName;
149};
150
151typedef std::shared_ptr<DynamicKernelArgument> DynamicKernelArgumentRef;
152
159{
160public:
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
183protected:
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
192{
193public:
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{
213public:
214 typedef std::vector<DynamicKernelArgumentRef> SubArguments;
215 virtual void GenSlidingWindowFunction( std::stringstream&,
216 const std::string&, SubArguments& ) = 0;
217};
218
220{
221public:
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
228class CheckVariables : public Normal
229{
230public:
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: */
static void CheckAllSubArgumentIsNan(std::stringstream &ss, SubArguments &vSubArguments)
Definition: opbase.cxx:300
static void GenTmpVariables(std::stringstream &ss, const SubArguments &vSubArguments)
Definition: opbase.cxx:199
static void CheckSubArgumentIsNan(std::stringstream &ss, SubArguments &vSubArguments, int argumentNum)
Definition: opbase.cxx:210
static void CheckSubArgumentIsNan2(std::stringstream &ss, SubArguments &vSubArguments, int argumentNum, const std::string &p)
Definition: opbase.cxx:273
static void UnrollDoubleVector(std::stringstream &ss, const std::stringstream &unrollstr, const formula::DoubleVectorRefToken *pCurDVR, int nCurWindowSize)
Definition: opbase.cxx:310
(Partially) abstract base class for an operand
Definition: opbase.hxx:102
virtual bool IsEmpty() const
If there's actually no argument, i.e. it expands to no code.
Definition: opbase.hxx:143
virtual size_t Marshal(cl_kernel, int, int, cl_program)=0
Create buffer and pass the buffer to a given kernel.
virtual std::string GenStringSlidingWindowDeclRef(bool=false) const
When Mix, it will be called.
Definition: opbase.cxx:50
virtual void GenDeclRef(std::stringstream &ss) const
Generate use/references to the argument.
Definition: opbase.cxx:56
virtual void DumpInlineFun(std::set< std::string > &, std::set< std::string > &) const
Definition: opbase.cxx:73
virtual void GenDecl(std::stringstream &ss) const =0
Generate declaration.
virtual std::string DumpOpName() const
Definition: opbase.cxx:68
formula::FormulaToken * GetFormulaToken() const
Definition: opbase.cxx:63
virtual bool NeedParallelReduction() const
Definition: opbase.cxx:80
const DynamicKernelArgument & operator=(const DynamicKernelArgument &)=delete
delete copy-assignment operator
const std::string & GetName() const
Definition: opbase.cxx:75
virtual void GenSlidingWindowFunction(std::stringstream &)
Definition: opbase.cxx:61
virtual std::string GenSlidingWindowDeclRef(bool=false) const =0
When referenced in a sliding window function.
DynamicKernelArgument(const DynamicKernelArgument &)=delete
delete copy constructor
FormulaTreeNodeRef mFormulaTree
Definition: opbase.hxx:148
virtual size_t GetWindowSize() const =0
virtual void GenSlidingWindowDecl(std::stringstream &ss) const =0
When declared as input to a sliding window function.
const ScCalcConfig & mCalcConfig
Definition: opbase.hxx:146
virtual std::string GenDoubleSlidingWindowDeclRef(bool=false) const
When Mix, it will be called.
Definition: opbase.cxx:44
std::vector< FormulaTreeNodeRef > Children
Definition: opbase.hxx:90
formula::FormulaToken * GetFormulaToken() const
Definition: opbase.hxx:91
formula::FormulaConstTokenRef mpCurrentFormula
Definition: opbase.hxx:97
FormulaTreeNode(const formula::FormulaToken *ft)
Definition: opbase.hxx:86
InvalidParameterCount(int parameterCount, const std::string &file, int ln)
Definition: opbase.cxx:37
virtual bool takeString() const override
Definition: opbase.hxx:224
virtual void GenSlidingWindowFunction(std::stringstream &ss, const std::string &sSymName, SubArguments &vSubArguments) override
Definition: opbase.cxx:176
virtual bool takeNumeric() const override
Definition: opbase.hxx:225
Abstract class for code generation.
Definition: opbase.hxx:192
virtual std::string GetBottom()
Definition: opbase.hxx:194
virtual bool ZeroReturnZero()
Definition: opbase.hxx:207
virtual bool canHandleMultiVector() const
Definition: opbase.hxx:204
virtual std::string Gen2(const std::string &, const std::string &) const
Definition: opbase.hxx:195
static std::string Gen(std::vector< std::string > &)
Definition: opbase.hxx:197
virtual bool takeString() const =0
virtual void BinInlineFun(std::set< std::string > &, std::set< std::string > &)
Definition: opbase.hxx:199
virtual bool takeNumeric() const =0
virtual std::string BinFuncName() const
Definition: opbase.hxx:198
virtual ~OpBase()
Definition: opbase.hxx:208
Failed in marshaling.
Definition: opbase.hxx:42
std::string mFunction
Definition: opbase.hxx:46
std::string mFile
Definition: opbase.hxx:48
OpenCLError(const std::string &function, cl_int error, const std::string &file, int line)
Definition: opbase.cxx:24
virtual void GenSlidingWindowFunction(std::stringstream &, const std::string &, SubArguments &)=0
std::vector< DynamicKernelArgumentRef > SubArguments
Definition: opbase.hxx:214
UnhandledToken(const char *m, const std::string &fn, int ln)
Definition: opbase.cxx:20
Inconsistent state.
Definition: opbase.hxx:54
Unhandled(const std::string &fn, int ln)
Definition: opbase.cxx:34
std::string mFile
Definition: opbase.hxx:58
Holds an input (read-only) argument reference to a SingleVectorRef.
Definition: opbase.hxx:159
VectorRef(const ScCalcConfig &config, const std::string &s, const FormulaTreeNodeRef &ft, int index=0)
Definition: opbase.cxx:85
virtual bool NeedParallelReduction() const override
Definition: opbase.cxx:171
virtual std::string GenSlidingWindowDeclRef(bool=false) const override
When referenced in a sliding window function.
Definition: opbase.cxx:119
const std::string & GetName() const
Definition: opbase.cxx:161
virtual void GenDecl(std::stringstream &ss) const override
Generate declaration.
Definition: opbase.cxx:107
virtual void DumpInlineFun(std::set< std::string > &, std::set< std::string > &) const override
Definition: opbase.cxx:159
cl_mem GetCLBuffer() const
Definition: opbase.cxx:166
virtual std::string DumpOpName() const override
Definition: opbase.cxx:154
virtual size_t GetWindowSize() const override
Definition: opbase.cxx:134
virtual ~VectorRef() override
Definition: opbase.cxx:96
virtual void GenSlidingWindowDecl(std::stringstream &ss) const override
When declared as input to a sliding window function.
Definition: opbase.cxx:113
virtual size_t Marshal(cl_kernel, int, int, cl_program) override
Create buffer and pass the buffer to a given kernel.
virtual void GenSlidingWindowFunction(std::stringstream &) override
Definition: opbase.cxx:132
void * p
::boost::intrusive_ptr< const FormulaToken > FormulaConstTokenRef
config
line
index
m
Arguments that are actually compile-time constant string Currently, only the hash is passed.
std::shared_ptr< FormulaTreeNode > FormulaTreeNodeRef
Definition: opbase.hxx:81
std::shared_ptr< DynamicKernelArgument > DynamicKernelArgumentRef
Definition: opbase.hxx:151
Configuration options for formula interpreter.
Definition: calcconfig.hxx:44