summaryrefslogtreecommitdiffstats
path: root/desktop/source/app/opencl.cxx
blob: fb5c3723ed8b9955a75c4d5721139a7e9db7f200 (plain)
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/* -*- 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_feature_opencl.h>
#include <config_folders.h>

#include <rtl/bootstrap.hxx>
#include <sal/log.hxx>

#include <officecfg/Office/Calc.hxx>
#include <officecfg/Office/Common.hxx>

#include <comphelper/propertyvalue.hxx>
#include <svl/documentlockfile.hxx>
#include <tools/diagnose_ex.h>

#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>
#include <osl/process.h>

using namespace ::osl;
using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::frame;

namespace desktop {

#if HAVE_FEATURE_OPENCL

static bool testOpenCLDriver()
{
    // A simple OpenCL test run in a separate process in order to test
    // whether the driver crashes (asserts,etc.) when trying to use OpenCL.
    SAL_INFO("opencl", "Starting CL driver test");

    OUString testerURL("$BRAND_BASE_DIR/" LIBO_BIN_FOLDER "/opencltest");
    rtl::Bootstrap::expandMacros(testerURL); //TODO: detect failure

    OUString deviceName, platformName;
    openclwrapper::getOpenCLDeviceName( deviceName, platformName );
    rtl_uString* args[] = { deviceName.pData, platformName.pData };
    sal_Int32 numArgs = 2;

    oslProcess process;
    oslSecurity security = osl_getCurrentSecurity();
    oslProcessError error = osl_executeProcess(testerURL.pData, args, numArgs,
        osl_Process_SEARCHPATH | osl_Process_HIDDEN, security,
        nullptr, nullptr, 0, &process );
    osl_freeSecurityHandle( security );
    if( error != osl_Process_E_None )
    {
        SAL_WARN( "opencl", "failed to start CL driver test: " << error );
        return false;
    }
    // If the driver takes more than 10 seconds, it's probably broken/useless.
    TimeValue timeout( 10, 0 );
    error = osl_joinProcessWithTimeout( process, &timeout );
    if( error == osl_Process_E_None )
    {
        oslProcessInfo info;
        info.Size = sizeof( info );
        error = osl_getProcessInfo( process, osl_Process_EXITCODE, &info );
        if( error == osl_Process_E_None )
        {
            if( info.Code == 0 )
            {
                SAL_INFO( "opencl", "CL driver test passed" );
                osl_freeProcessHandle( process );
                return true;
            }
            else
            {
                SAL_WARN( "opencl", "CL driver test failed - disabling: " << info.Code );
                osl_freeProcessHandle( process );
                return false;
            }
        }
    }
    SAL_WARN( "opencl", "CL driver test did not finish - disabling: " << error );
    osl_terminateProcess( process );
    osl_freeProcessHandle( process );
    return false;
}

static bool testOpenCLCompute(const Reference< XDesktop2 > &xDesktop, const OUString &rURL)
{
    bool bSuccess = false;
    css::uno::Reference< css::lang::XComponent > xComponent;

    sal_uInt64 nKernelFailures = openclwrapper::kernelFailures;

    SAL_INFO("opencl", "Starting CL test spreadsheet");

    // A stale lock file would make the loading fail, so make sure to remove it.
    try {
        ::svt::DocumentLockFile lockFile( rURL );
        lockFile.RemoveFileDirectly();
    }
    catch (const css::uno::Exception&)
    {
    }

    try {
        css::uno::Reference< css::frame::XComponentLoader > xLoader(xDesktop, css::uno::UNO_QUERY_THROW);

        css::uno::Sequence< css::beans::PropertyValue > aArgs{ comphelper::makePropertyValue("Hidden",
                                                                                             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_SET_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 &)
    {
        TOOLS_WARN_EXCEPTION("opencl", "OpenCL testing failed - disabling");
    }

    if (nKernelFailures != openclwrapper::kernelFailures)
    {
        // tdf#100883 - defeat SEH exception handling fallbacks.
        SAL_WARN("opencl", "OpenCL kernels failed to compile, "
                 "or took SEH exceptions "
                 << nKernelFailures << " != " << openclwrapper::kernelFailures);
        bSuccess = false;
    }

    if (!bSuccess)
        OpenCLZone::hardDisable();
    if (xComponent.is())
        xComponent->dispose();


    return bSuccess;
}

void Desktop::CheckOpenCLCompute(const Reference< XDesktop2 > &xDesktop)
{
    if (!openclwrapper::canUseOpenCL() || Application::IsSafeModeEnabled())
        return;

    SAL_INFO("opencl", "Initiating test of OpenCL device");
    OpenCLZone aZone;
    OpenCLInitialZone aInitialZone;

    OUString aDevice = officecfg::Office::Calc::Formula::Calculation::OpenCLDevice::get();
    OUString aSelectedCLDeviceVersionID;
    if (!openclwrapper::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 += "--" 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;
    (void)DirectoryItem::get( aURL, aItem );
    FileStatus aFileStatus( osl_FileStatus_Mask_ModifyTime );
    (void)aItem.getFileStatus( aFileStatus );
    TimeValue aTimeVal = aFileStatus.getModifyTime();
    aSelectedCLDeviceVersionID += "--" +
        OUString::number(aTimeVal.Seconds);

    if (aSelectedCLDeviceVersionID == officecfg::Office::Common::Misc::SelectedOpenCLDeviceIdentifier::get())
        return;

    // OpenCL device changed - sanity check it and disable if bad.

    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();
    }

    // Hopefully at least basic functionality always works and broken OpenCL implementations break
    // only when they are used to compute something. If this assumptions turns out to be not true,
    // the driver check needs to be moved sooner.
    bool bSucceeded = testOpenCLDriver() && 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: */