1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
/*
* This module exists to validate the OpenCL implementation,
* where necessary during startup; and before we load or
* calculate using OpenCL.
*/
#include "app.hxx"
#include <config_version.h>
#include <config_features.h>
#include <config_folders.h>
#include <rtl/bootstrap.hxx>
#include <officecfg/Office/Calc.hxx>
#include <officecfg/Office/Common.hxx>
#include <com/sun/star/table/XCell2.hpp>
#include <com/sun/star/sheet/XCalculatable.hpp>
#include <com/sun/star/sheet/XSpreadsheet.hpp>
#include <com/sun/star/sheet/XSpreadsheets.hpp>
#include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
#if HAVE_FEATURE_OPENCL
#include <opencl/openclwrapper.hxx>
#endif
#include <opencl/OpenCLZone.hxx>
#include <osl/file.hxx>
using namespace ::osl;
using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::frame;
namespace desktop {
#if HAVE_FEATURE_OPENCL
bool testOpenCLCompute(const Reference< XDesktop2 > &xDesktop, const OUString &rURL)
{
bool bSuccess = false;
css::uno::Reference< css::lang::XComponent > xComponent;
sal_uInt64 nKernelFailures = opencl::kernelFailures;
SAL_INFO("opencl", "Starting CL test spreadsheet");
try {
css::uno::Reference< css::frame::XComponentLoader > xLoader(xDesktop, css::uno::UNO_QUERY_THROW);
css::uno::Sequence< css::beans::PropertyValue > aArgs(1);
aArgs[0].Name = "Hidden";
aArgs[0].Value = makeAny(true);
xComponent.set(xLoader->loadComponentFromURL(rURL, "_blank", 0, aArgs));
// What an unpleasant API to use.
css::uno::Reference< css::sheet::XCalculatable > xCalculatable( xComponent, css::uno::UNO_QUERY_THROW);
css::uno::Reference< css::sheet::XSpreadsheetDocument > xSpreadDoc( xComponent, css::uno::UNO_QUERY_THROW );
css::uno::Reference< css::sheet::XSpreadsheets > xSheets( xSpreadDoc->getSheets(), css::uno::UNO_QUERY_THROW );
css::uno::Reference< css::container::XIndexAccess > xIndex( xSheets, css::uno::UNO_QUERY_THROW );
css::uno::Reference< css::sheet::XSpreadsheet > xSheet( xIndex->getByIndex(0), css::uno::UNO_QUERY_THROW);
// So we insert our MAX call at the end on a named range.
css::uno::Reference< css::table::XCell2 > xThresh( xSheet->getCellByPosition(1,1), css::uno::UNO_QUERY_THROW ); // B2
double fThreshold = xThresh->getValue();
// We need pure OCL formulae all the way through the
// dependency chain, or we fall-back.
xCalculatable->calculateAll();
// So we insert our MAX call at the end on a named range.
css::uno::Reference< css::table::XCell2 > xCell( xSheet->getCellByPosition(1,0), css::uno::UNO_QUERY_THROW );
xCell->setFormula("=MAX(results)");
double fResult = xCell->getValue();
// Ensure the maximum variance is below our tolerance.
if (fResult > fThreshold)
{
SAL_WARN("opencl", "OpenCL results unstable - disabling; result: "
<< fResult << " vs. " << fThreshold);
}
else
{
SAL_INFO("opencl", "calculating smoothly; result: " << fResult);
bSuccess = true;
}
}
catch (const css::uno::Exception &e)
{
(void)e;
SAL_WARN("opencl", "OpenCL testing failed - disabling: " << e.Message);
}
if (nKernelFailures != opencl::kernelFailures)
{
// tdf#100883 - defeat SEH exception handling fallbacks.
SAL_WARN("opencl", "OpenCL kernels failed to compile, "
"or took SEH exceptions "
<< nKernelFailures << " != " << opencl::kernelFailures);
bSuccess = false;
}
if (!bSuccess)
OpenCLZone::hardDisable();
if (xComponent.is())
xComponent->dispose();
return bSuccess;
}
void Desktop::CheckOpenCLCompute(const Reference< XDesktop2 > &xDesktop)
{
if (!opencl::canUseOpenCL() || Application::IsSafeModeEnabled())
return;
SAL_INFO("opencl", "Initiating test of OpenCL device");
OpenCLZone aZone;
OpenCLZone::enterInitialTest();
OUString aDevice = officecfg::Office::Calc::Formula::Calculation::OpenCLDevice::get();
OUString aSelectedCLDeviceVersionID;
if (!opencl::switchOpenCLDevice(
&aDevice,
officecfg::Office::Calc::Formula::Calculation::OpenCLAutoSelect::get(),
false /* bForceEvaluation */,
aSelectedCLDeviceVersionID))
{
SAL_WARN("opencl", "Failed to initialize OpenCL for test");
OpenCLZone::hardDisable();
return;
}
// Append our app version as well.
aSelectedCLDeviceVersionID += "--";
aSelectedCLDeviceVersionID += LIBO_VERSION_DOTTED;
// Append timestamp of the file.
OUString aURL("$BRAND_BASE_DIR/" LIBO_ETC_FOLDER "/opencl/cl-test.ods");
rtl::Bootstrap::expandMacros(aURL);
DirectoryItem aItem;
DirectoryItem::get( aURL, aItem );
FileStatus aFileStatus( osl_FileStatus_Mask_ModifyTime );
aItem.getFileStatus( aFileStatus );
TimeValue aTimeVal = aFileStatus.getModifyTime();
aSelectedCLDeviceVersionID += "--";
aSelectedCLDeviceVersionID += OUString::number(aTimeVal.Seconds);
if (aSelectedCLDeviceVersionID != officecfg::Office::Common::Misc::SelectedOpenCLDeviceIdentifier::get())
{
// OpenCL device changed - sanity check it and disable if bad.
boost::optional<sal_Int32> nOrigMinimumSize = officecfg::Office::Calc::Formula::Calculation::OpenCLMinimumDataSize::get();
{ // set the minimum group size to something small for quick testing.
std::shared_ptr<comphelper::ConfigurationChanges> xBatch(comphelper::ConfigurationChanges::create());
officecfg::Office::Calc::Formula::Calculation::OpenCLMinimumDataSize::set(3 /* small */, xBatch);
xBatch->commit();
}
bool bSucceeded = testOpenCLCompute(xDesktop, aURL);
{ // restore the minimum group size
std::shared_ptr<comphelper::ConfigurationChanges> xBatch(comphelper::ConfigurationChanges::create());
officecfg::Office::Calc::Formula::Calculation::OpenCLMinimumDataSize::set(nOrigMinimumSize, xBatch);
officecfg::Office::Common::Misc::SelectedOpenCLDeviceIdentifier::set(aSelectedCLDeviceVersionID, xBatch);
xBatch->commit();
}
if (!bSucceeded)
OpenCLZone::hardDisable();
}
}
#endif // HAVE_FEATURE_OPENCL
} // end namespace desktop
/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
|