Line data Source code
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 <sal/log.hxx>
14 :
15 : #include "clcc/clew.h"
16 :
17 : #include <formula/token.hxx>
18 : #include <formula/vectortoken.hxx>
19 : #include <boost/shared_ptr.hpp>
20 : #include <boost/noncopyable.hpp>
21 : #include <set>
22 : #define ISNAN
23 :
24 : namespace sc { namespace opencl {
25 :
26 : class FormulaTreeNode;
27 :
28 : /// Exceptions
29 :
30 : /// Failed in parsing
31 0 : class UnhandledToken
32 : {
33 : public:
34 : UnhandledToken( formula::FormulaToken* t, const char* m, const std::string& fn = "", int ln = 0 );
35 :
36 : formula::FormulaToken* mToken;
37 : std::string mMessage;
38 : std::string mFile;
39 : int mLineNumber;
40 : };
41 :
42 : /// Failed in marshaling
43 0 : class OpenCLError
44 : {
45 : private:
46 : const char* strerror( cl_int i ) const;
47 :
48 : public:
49 : OpenCLError( cl_int err, const std::string& fn, int ln );
50 :
51 : cl_int mError;
52 : std::string mFile;
53 : int mLineNumber;
54 : };
55 :
56 : /// Inconsistent state
57 0 : class Unhandled
58 : {
59 : public:
60 : Unhandled( const std::string& fn = "", int ln = 0 );
61 :
62 : std::string mFile;
63 : int mLineNumber;
64 : };
65 :
66 : typedef boost::shared_ptr<FormulaTreeNode> FormulaTreeNodeRef;
67 :
68 0 : class FormulaTreeNode
69 : {
70 : public:
71 0 : FormulaTreeNode( const formula::FormulaToken* ft ) : mpCurrentFormula(ft)
72 : {
73 0 : Children.reserve(8);
74 0 : }
75 : std::vector<FormulaTreeNodeRef> Children;
76 0 : formula::FormulaToken* GetFormulaToken() const
77 : {
78 0 : return const_cast<formula::FormulaToken*>(mpCurrentFormula.get());
79 : }
80 :
81 : private:
82 : formula::FormulaConstTokenRef mpCurrentFormula;
83 : };
84 :
85 : /// (Partially) abstract base class for an operand
86 : class DynamicKernelArgument : boost::noncopyable
87 : {
88 : public:
89 : DynamicKernelArgument( const std::string& s, FormulaTreeNodeRef ft );
90 0 : virtual ~DynamicKernelArgument() {}
91 :
92 : /// Generate declaration
93 : virtual void GenDecl( std::stringstream& ss ) const = 0;
94 :
95 : /// When declared as input to a sliding window function
96 : virtual void GenSlidingWindowDecl( std::stringstream& ss ) const = 0;
97 :
98 : /// When referenced in a sliding window function
99 : virtual std::string GenSlidingWindowDeclRef( bool = false ) const = 0;
100 :
101 : /// Create buffer and pass the buffer to a given kernel
102 : virtual size_t Marshal( cl_kernel, int, int, cl_program ) = 0;
103 :
104 : virtual size_t GetWindowSize() const = 0;
105 :
106 : /// When Mix, it will be called
107 : virtual std::string GenDoubleSlidingWindowDeclRef( bool = false ) const;
108 :
109 : /// When Mix, it will be called
110 : virtual std::string GenStringSlidingWindowDeclRef( bool = false ) const;
111 :
112 : virtual bool IsMixedArgument() const;
113 :
114 : /// Generate use/references to the argument
115 : virtual void GenDeclRef( std::stringstream& ss ) const;
116 : virtual void GenNumDeclRef( std::stringstream& ss ) const;
117 :
118 : virtual void GenStringDeclRef( std::stringstream& ss ) const;
119 :
120 : virtual void GenSlidingWindowFunction( std::stringstream& );
121 : formula::FormulaToken* GetFormulaToken() const;
122 : virtual std::string DumpOpName() const;
123 : virtual void DumpInlineFun( std::set<std::string>&, std::set<std::string>& ) const;
124 : const std::string& GetName() const;
125 : virtual bool NeedParallelReduction() const;
126 :
127 : protected:
128 : std::string mSymName;
129 : FormulaTreeNodeRef mFormulaTree;
130 : };
131 :
132 : /// Holds an input (read-only) argument reference to a SingleVectorRef.
133 : /// or a DoubleVectorRef for non-sliding-window argument of complex functions
134 : /// like SumOfProduct
135 : /// In most of the cases the argument is introduced
136 : /// by a Push operation in the given RPN.
137 : class VectorRef : public DynamicKernelArgument
138 : {
139 : public:
140 : VectorRef( const std::string& s, FormulaTreeNodeRef ft, int index = 0 );
141 : virtual ~VectorRef();
142 :
143 : /// Generate declaration
144 : virtual void GenDecl( std::stringstream& ss ) const SAL_OVERRIDE;
145 : /// When declared as input to a sliding window function
146 : virtual void GenSlidingWindowDecl( std::stringstream& ss ) const SAL_OVERRIDE;
147 :
148 : /// When referenced in a sliding window function
149 : virtual std::string GenSlidingWindowDeclRef( bool = false ) const SAL_OVERRIDE;
150 :
151 : /// Create buffer and pass the buffer to a given kernel
152 : virtual size_t Marshal( cl_kernel, int, int, cl_program ) SAL_OVERRIDE;
153 :
154 : virtual void GenSlidingWindowFunction( std::stringstream& ) SAL_OVERRIDE;
155 : virtual size_t GetWindowSize() const SAL_OVERRIDE;
156 : virtual std::string DumpOpName() const SAL_OVERRIDE;
157 : virtual void DumpInlineFun( std::set<std::string>&, std::set<std::string>& ) const SAL_OVERRIDE;
158 : const std::string& GetName() const;
159 : virtual cl_mem GetCLBuffer() const;
160 : virtual bool NeedParallelReduction() const SAL_OVERRIDE;
161 :
162 : protected:
163 : // Used by marshaling
164 : cl_mem mpClmem;
165 : // index in multiple double vector refs that have multiple ranges
166 : const int mnIndex;
167 : };
168 :
169 : /// Abstract class for code generation
170 0 : class OpBase
171 : {
172 : public:
173 : typedef std::vector<std::string> ArgVector;
174 : typedef std::vector<std::string>::iterator ArgVectorIter;
175 0 : virtual std::string GetBottom() { return "";};
176 0 : virtual std::string Gen2( const std::string&/*lhs*/,
177 0 : const std::string&/*rhs*/ ) const { return "";}
178 0 : virtual std::string Gen( ArgVector& /*argVector*/ ) { return "";};
179 0 : virtual std::string BinFuncName() const { return "";};
180 0 : virtual void BinInlineFun( std::set<std::string>&,
181 0 : std::set<std::string>& ) { }
182 : virtual bool takeString() const = 0;
183 : virtual bool takeNumeric() const = 0;
184 : //Continue process 'Zero' or Not(like OpMul, not continue process when meet
185 : // 'Zero'
186 0 : virtual bool ZeroReturnZero() { return false;}
187 0 : virtual ~OpBase() { }
188 : };
189 :
190 0 : class SlidingFunctionBase : public OpBase
191 : {
192 : public:
193 : typedef boost::shared_ptr<DynamicKernelArgument> SubArgument;
194 : typedef std::vector<SubArgument> SubArguments;
195 : virtual void GenSlidingWindowFunction( std::stringstream&,
196 : const std::string&, SubArguments& ) = 0;
197 0 : virtual ~SlidingFunctionBase() { }
198 : };
199 :
200 0 : class Normal : public SlidingFunctionBase
201 : {
202 : public:
203 : virtual void GenSlidingWindowFunction( std::stringstream& ss,
204 : const std::string& sSymName, SubArguments& vSubArguments ) SAL_OVERRIDE;
205 0 : virtual bool takeString() const SAL_OVERRIDE { return false; }
206 0 : virtual bool takeNumeric() const SAL_OVERRIDE { return true; }
207 : };
208 :
209 0 : class CheckVariables : public Normal
210 : {
211 : public:
212 : void GenTmpVariables( std::stringstream& ss, SubArguments& vSubArguments );
213 : void CheckSubArgumentIsNan( std::stringstream& ss,
214 : SubArguments& vSubArguments, int argumentNum );
215 : void CheckAllSubArgumentIsNan( std::stringstream& ss,
216 : SubArguments& vSubArguments );
217 : // only check isNan
218 : void CheckSubArgumentIsNan2( std::stringstream& ss,
219 : SubArguments& vSubArguments, int argumentNum, std::string p );
220 : void UnrollDoubleVector( std::stringstream& ss,
221 : std::stringstream& unrollstr, const formula::DoubleVectorRefToken* pCurDVR,
222 : int nCurWindowSize );
223 : };
224 :
225 : }}
226 :
227 : #endif
228 :
229 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|