1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
10 #ifndef INCLUDED_SC_SOURCE_CORE_OPENCL_OPBASE_HXX
11 #define INCLUDED_SC_SOURCE_CORE_OPENCL_OPBASE_HXX
13 #include <sal/log.hxx>
15 #include <clew/clew.h>
17 #include <formula/token.hxx>
18 #include <formula/vectortoken.hxx>
19 #include <boost/shared_ptr.hpp>
20 #include <boost/noncopyable.hpp>
23 #include "calcconfig.hxx"
25 namespace sc
{ namespace opencl
{
27 class FormulaTreeNode
;
35 UnhandledToken( formula::FormulaToken
* t
, const char* m
, const std::string
& fn
= "", int ln
= 0 );
37 formula::FormulaToken
* mToken
;
43 /// Failed in marshaling
47 OpenCLError( const std::string
& function
, cl_int error
, const std::string
& file
, int line
);
49 std::string mFunction
;
55 /// Inconsistent state
59 Unhandled( const std::string
& fn
= "", int ln
= 0 );
65 typedef boost::shared_ptr
<FormulaTreeNode
> FormulaTreeNodeRef
;
70 FormulaTreeNode( const formula::FormulaToken
* ft
) : mpCurrentFormula(ft
)
74 std::vector
<FormulaTreeNodeRef
> Children
;
75 formula::FormulaToken
* GetFormulaToken() const
77 return const_cast<formula::FormulaToken
*>(mpCurrentFormula
.get());
81 formula::FormulaConstTokenRef mpCurrentFormula
;
84 /// (Partially) abstract base class for an operand
85 class DynamicKernelArgument
: boost::noncopyable
88 DynamicKernelArgument( const ScCalcConfig
& config
, const std::string
& s
, FormulaTreeNodeRef ft
);
89 virtual ~DynamicKernelArgument() {}
91 /// Generate declaration
92 virtual void GenDecl( std::stringstream
& ss
) const = 0;
94 /// When declared as input to a sliding window function
95 virtual void GenSlidingWindowDecl( std::stringstream
& ss
) const = 0;
97 /// When referenced in a sliding window function
98 virtual std::string
GenSlidingWindowDeclRef( bool = false ) const = 0;
100 /// Create buffer and pass the buffer to a given kernel
101 virtual size_t Marshal( cl_kernel
, int, int, cl_program
) = 0;
103 virtual size_t GetWindowSize() const = 0;
105 /// When Mix, it will be called
106 virtual std::string
GenDoubleSlidingWindowDeclRef( bool = false ) const;
108 /// When Mix, it will be called
109 virtual std::string
GenStringSlidingWindowDeclRef( bool = false ) const;
111 virtual bool IsMixedArgument() const;
113 /// Generate use/references to the argument
114 virtual void GenDeclRef( std::stringstream
& ss
) const;
115 virtual void GenNumDeclRef( std::stringstream
& ss
) const;
117 virtual void GenStringDeclRef( std::stringstream
& ss
) const;
119 virtual void GenSlidingWindowFunction( std::stringstream
& );
120 formula::FormulaToken
* GetFormulaToken() const;
121 virtual std::string
DumpOpName() const;
122 virtual void DumpInlineFun( std::set
<std::string
>&, std::set
<std::string
>& ) const;
123 const std::string
& GetName() const;
124 virtual bool NeedParallelReduction() const;
127 const ScCalcConfig
& mCalcConfig
;
128 std::string mSymName
;
129 FormulaTreeNodeRef mFormulaTree
;
132 typedef boost::shared_ptr
<DynamicKernelArgument
> DynamicKernelArgumentRef
;
134 /// Holds an input (read-only) argument reference to a SingleVectorRef.
135 /// or a DoubleVectorRef for non-sliding-window argument of complex functions
136 /// like SumOfProduct
137 /// In most of the cases the argument is introduced
138 /// by a Push operation in the given RPN.
139 class VectorRef
: public DynamicKernelArgument
142 VectorRef( const ScCalcConfig
& config
, const std::string
& s
, FormulaTreeNodeRef ft
, int index
= 0 );
143 virtual ~VectorRef();
145 /// Generate declaration
146 virtual void GenDecl( std::stringstream
& ss
) const SAL_OVERRIDE
;
147 /// When declared as input to a sliding window function
148 virtual void GenSlidingWindowDecl( std::stringstream
& ss
) const SAL_OVERRIDE
;
150 /// When referenced in a sliding window function
151 virtual std::string
GenSlidingWindowDeclRef( bool = false ) const SAL_OVERRIDE
;
153 /// Create buffer and pass the buffer to a given kernel
154 virtual size_t Marshal( cl_kernel
, int, int, cl_program
) SAL_OVERRIDE
;
156 virtual void GenSlidingWindowFunction( std::stringstream
& ) SAL_OVERRIDE
;
157 virtual size_t GetWindowSize() const SAL_OVERRIDE
;
158 virtual std::string
DumpOpName() const SAL_OVERRIDE
;
159 virtual void DumpInlineFun( std::set
<std::string
>&, std::set
<std::string
>& ) const SAL_OVERRIDE
;
160 const std::string
& GetName() const;
161 cl_mem
GetCLBuffer() const;
162 virtual bool NeedParallelReduction() const SAL_OVERRIDE
;
165 // Used by marshaling
167 // index in multiple double vector refs that have multiple ranges
171 /// Abstract class for code generation
175 typedef std::vector
<std::string
> ArgVector
;
176 typedef std::vector
<std::string
>::iterator ArgVectorIter
;
177 virtual std::string
GetBottom() { return "";};
178 virtual std::string
Gen2( const std::string
&/*lhs*/,
179 const std::string
&/*rhs*/ ) const { return "";}
180 static std::string
Gen( ArgVector
& /*argVector*/ ) { return "";};
181 virtual std::string
BinFuncName() const { return "";};
182 virtual void BinInlineFun( std::set
<std::string
>&,
183 std::set
<std::string
>& ) { }
184 virtual bool takeString() const = 0;
185 virtual bool takeNumeric() const = 0;
186 //Continue process 'Zero' or Not(like OpMul, not continue process when meet
188 virtual bool ZeroReturnZero() { return false;}
189 virtual ~OpBase() { }
192 class SlidingFunctionBase
: public OpBase
195 typedef std::vector
<DynamicKernelArgumentRef
> SubArguments
;
196 virtual void GenSlidingWindowFunction( std::stringstream
&,
197 const std::string
&, SubArguments
& ) = 0;
198 virtual ~SlidingFunctionBase() { }
201 class Normal
: public SlidingFunctionBase
204 virtual void GenSlidingWindowFunction( std::stringstream
& ss
,
205 const std::string
& sSymName
, SubArguments
& vSubArguments
) SAL_OVERRIDE
;
206 virtual bool takeString() const SAL_OVERRIDE
{ return false; }
207 virtual bool takeNumeric() const SAL_OVERRIDE
{ return true; }
210 class CheckVariables
: public Normal
213 static void GenTmpVariables( std::stringstream
& ss
, SubArguments
& vSubArguments
);
214 static void CheckSubArgumentIsNan( std::stringstream
& ss
,
215 SubArguments
& vSubArguments
, int argumentNum
);
216 static void CheckAllSubArgumentIsNan( std::stringstream
& ss
,
217 SubArguments
& vSubArguments
);
219 static void CheckSubArgumentIsNan2( std::stringstream
& ss
,
220 SubArguments
& vSubArguments
, int argumentNum
, std::string p
);
221 static void UnrollDoubleVector( std::stringstream
& ss
,
222 std::stringstream
& unrollstr
, const formula::DoubleVectorRefToken
* pCurDVR
,
223 int nCurWindowSize
);
230 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */