summaryrefslogtreecommitdiff
path: root/cpu_ref/rsCpuScriptGroup2.cpp
blob: 90907d056e5e085845d200cfbbd3594bbe57f592 (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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#include "rsCpuScriptGroup2.h"

#include <dlfcn.h>

#include <string>
#include <vector>

#ifndef RS_COMPATIBILITY_LIB
#include "bcc/Config/Config.h"
#include <sys/wait.h>
#endif

#include "cpu_ref/rsCpuCore.h"
#include "rsClosure.h"
#include "rsContext.h"
#include "rsCpuCore.h"
#include "rsCpuScript.h"
#include "rsScript.h"
#include "rsScriptGroup2.h"
#include "rsScriptIntrinsic.h"

using std::string;
using std::vector;

namespace android {
namespace renderscript {

namespace {

const size_t DefaultKernelArgCount = 2;

void groupRoot(const RsExpandKernelParams *kparams, uint32_t xstart,
               uint32_t xend, uint32_t outstep) {
  const list<CPUClosure*>& closures = *(list<CPUClosure*>*)kparams->usr;
  RsExpandKernelParams *mutable_kparams = (RsExpandKernelParams *)kparams;
  const void **oldIns  = kparams->ins;
  uint32_t *oldStrides = kparams->inEStrides;

  std::vector<const void*> ins(DefaultKernelArgCount);
  std::vector<uint32_t> strides(DefaultKernelArgCount);

  for (CPUClosure* cpuClosure : closures) {
    const Closure* closure = cpuClosure->mClosure;

    auto in_iter = ins.begin();
    auto stride_iter = strides.begin();

    for (const auto& arg : closure->mArgs) {
      const Allocation* a = (const Allocation*)arg;
      const uint32_t eStride = a->mHal.state.elementSizeBytes;
      const uint8_t* ptr = (uint8_t*)(a->mHal.drvState.lod[0].mallocPtr) +
          eStride * xstart;
      if (kparams->dimY > 1) {
        ptr += a->mHal.drvState.lod[0].stride * kparams->y;
      }
      *in_iter++ = ptr;
      *stride_iter++ = eStride;
    }

    mutable_kparams->ins = &ins[0];
    mutable_kparams->inEStrides = &strides[0];

    const Allocation* out = closure->mReturnValue;
    const uint32_t ostep = out->mHal.state.elementSizeBytes;
    const uint8_t* ptr = (uint8_t *)(out->mHal.drvState.lod[0].mallocPtr) +
           ostep * xstart;
    if (kparams->dimY > 1) {
      ptr += out->mHal.drvState.lod[0].stride * kparams->y;
    }

    mutable_kparams->out = (void*)ptr;

    mutable_kparams->usr = cpuClosure->mUsrPtr;

    cpuClosure->mFunc(kparams, xstart, xend, ostep);
  }

  mutable_kparams->ins        = oldIns;
  mutable_kparams->inEStrides = oldStrides;
  mutable_kparams->usr        = &closures;
}

}  // namespace

Batch::~Batch() {
  for (CPUClosure* c : mClosures) {
    delete c;
  }
  if (mScriptObj) {
    dlclose(mScriptObj);
  }
}

bool Batch::conflict(CPUClosure* closure) const {
  if (mClosures.empty()) {
    return false;
  }

  if (closure->mClosure->mKernelID.get() == nullptr ||
      mClosures.front()->mClosure->mKernelID.get() == nullptr) {
    // An invoke should be in a batch by itself, so it conflicts with any other
    // closure.
    return true;
  }

  for (const auto &p : closure->mClosure->mGlobalDeps) {
    const Closure* dep = p.first;
    for (CPUClosure* c : mClosures) {
      if (c->mClosure == dep) {
        ALOGV("ScriptGroup2: closure %p conflicting with closure %p via its global", closure, dep);
        return true;
      }
    }
  }
  for (const auto &p : closure->mClosure->mArgDeps) {
    const Closure* dep = p.first;
    for (CPUClosure* c : mClosures) {
      if (c->mClosure == dep) {
        for (const auto &p1 : *p.second) {
          if (p1.second->get() != nullptr) {
            ALOGV("ScriptGroup2: closure %p conflicting with closure %p via its arg", closure, dep);
            return true;
          }
        }
      }
    }
  }
  return false;
}

CpuScriptGroup2Impl::CpuScriptGroup2Impl(RsdCpuReferenceImpl *cpuRefImpl,
                                         const ScriptGroupBase *sg) :
    mCpuRefImpl(cpuRefImpl), mGroup((const ScriptGroup2*)(sg)) {
  Batch* batch = new Batch(this);
  for (Closure* closure: mGroup->mClosures) {
    const ScriptKernelID* kernelID = closure->mKernelID.get();
    RsdCpuScriptImpl* si =
        (RsdCpuScriptImpl *)mCpuRefImpl->lookupScript(kernelID->mScript);

    MTLaunchStruct mtls;
    si->forEachKernelSetup(kernelID->mSlot, &mtls);
    // TODO: Is mtls.fep.usrLen ever used?
    CPUClosure* cc = new CPUClosure(closure, si, (ExpandFuncTy)mtls.kernel,
                                    mtls.fep.usr, mtls.fep.usrLen);
    if (batch->conflict(cc)) {
      mBatches.push_back(batch);
      batch = new Batch(this);
    }

    batch->mClosures.push_back(cc);
  }

  mBatches.push_back(batch);

#ifndef RS_COMPATIBILITY_LIB
  for (Batch* batch : mBatches) {
    batch->tryToCreateFusedKernel(mGroup->mCacheDir.c_str());
  }
#endif
}

CpuScriptGroup2Impl::~CpuScriptGroup2Impl() {
  for (Batch* batch : mBatches) {
    delete batch;
  }
}

namespace {

#ifndef RS_COMPATIBILITY_LIB

string getFileName(string path) {
  unsigned found = path.find_last_of("/\\");
  return path.substr(found + 1);
}

void setupCompileArguments(
    const vector<string>& inputs, const vector<int>& kernels,
    const string& output_dir, const string& output_filename,
    const string& rsLib, vector<const char*>* args) {
  args->push_back(RsdCpuScriptImpl::BCC_EXE_PATH);
  args->push_back("-fPIC");
  args->push_back("-embedRSInfo");
  args->push_back("-mtriple");
  args->push_back(DEFAULT_TARGET_TRIPLE_STRING);
  args->push_back("-bclib");
  args->push_back(rsLib.c_str());
  for (const string& input : inputs) {
    args->push_back(input.c_str());
  }
  for (int kernel : kernels) {
    args->push_back("-k");
    string strKernel = std::to_string(kernel);
    args->push_back(strKernel.c_str());
  }
  args->push_back("-output_path");
  args->push_back(output_dir.c_str());
  args->push_back("-o");
  args->push_back(output_filename.c_str());
  args->push_back(nullptr);
}

string convertListToString(int n, const char* const* strs) {
  string ret;
  ret.append(strs[0]);
  for (int i = 1; i < n; i++) {
    ret.append(" ");
    ret.append(strs[i]);
  }
  return ret;
}

bool fuseAndCompile(const char** arguments,
                    const string& commandLine) {
  const pid_t pid = fork();

  if (pid == -1) {
    ALOGE("Couldn't fork for bcc execution");
    return false;
  }

  if (pid == 0) {
    // Child process
    ALOGV("Invoking BCC with: %s", commandLine.c_str());
    execv(RsdCpuScriptImpl::BCC_EXE_PATH, (char* const*)arguments);

    ALOGE("execv() failed: %s", strerror(errno));
    abort();
    return false;
  }

  // Parent process
  int status = 0;
  const pid_t w = waitpid(pid, &status, 0);
  if (w == -1) {
    return false;
  }

  if (!WIFEXITED(status) || WEXITSTATUS(status) != 0 ) {
    ALOGE("bcc terminated unexpectedly");
    return false;
  }

  return true;
}
#endif

}  // anonymous namespace

void Batch::tryToCreateFusedKernel(const char *cacheDir) {
#ifndef RS_COMPATIBILITY_LIB
  if (mClosures.size() < 2) {
    ALOGV("Compiler kernel fusion skipped due to only one or zero kernel in"
          " a script group batch.");
    return;
  }

  //===--------------------------------------------------------------------===//
  // Fuse the input kernels and generate native code in an object file
  //===--------------------------------------------------------------------===//

  std::vector<string> inputFiles;
  std::vector<int> slots;

  for (CPUClosure* cpuClosure : mClosures) {
    const Closure* closure = cpuClosure->mClosure;
    const ScriptKernelID* kernelID = closure->mKernelID.get();
    const Script* script = kernelID->mScript;

    if (script->isIntrinsic()) {
      return;
    }

    const RsdCpuScriptImpl *cpuScript =
        (const RsdCpuScriptImpl*)script->mHal.drv;

    const string& bitcodeFilename = cpuScript->getBitcodeFilePath();

    inputFiles.push_back(bitcodeFilename);
    slots.push_back(kernelID->mSlot);
  }

  string outputPath(tempnam(cacheDir, "fused"));
  string outputFileName = getFileName(outputPath);
  string objFilePath(outputPath);
  objFilePath.append(".o");
  string rsLibPath(SYSLIBPATH"/libclcore.bc");
  vector<const char*> arguments;
  setupCompileArguments(inputFiles, slots, cacheDir, outputFileName, rsLibPath,
                        &arguments);
  string commandLine =
      convertListToString(arguments.size() - 1, arguments.data());

  if (!fuseAndCompile(arguments.data(), commandLine)) {
    return;
  }

  //===--------------------------------------------------------------------===//
  // Create and load the shared lib
  //===--------------------------------------------------------------------===//

  const char* resName = outputFileName.c_str();

  if (!SharedLibraryUtils::createSharedLibrary(cacheDir, resName)) {
    ALOGE("Failed to link object file '%s'", resName);
    return;
  }

  void* mSharedObj = SharedLibraryUtils::loadSharedLibrary(cacheDir, resName);
  if (mSharedObj == nullptr) {
    ALOGE("Unable to load '%s'", resName);
    return;
  }

  mExecutable = ScriptExecutable::createFromSharedObject(
      nullptr,  // RS context. Unused.
      mSharedObj);

#endif  // RS_COMPATIBILITY_LIB
}

void CpuScriptGroup2Impl::execute() {
  for (auto batch : mBatches) {
    batch->setGlobalsForBatch();
    batch->run();
  }
}

void Batch::setGlobalsForBatch() {
  for (CPUClosure* cpuClosure : mClosures) {
    const Closure* closure = cpuClosure->mClosure;
    const ScriptKernelID* kernelID = closure->mKernelID.get();
    Script* s = kernelID->mScript;
    for (const auto& p : closure->mGlobals) {
      const void* value = p.second.first;
      int size = p.second.second;
      // We use -1 size to indicate an ObjectBase rather than a primitive type
      if (size < 0) {
        s->setVarObj(p.first->mSlot, (ObjectBase*)value);
      } else {
        s->setVar(p.first->mSlot, (const void*)&value, size);
      }
    }
  }
}

void Batch::run() {
  if (mExecutable != nullptr) {
    MTLaunchStruct mtls;
    const CPUClosure* firstCpuClosure = mClosures.front();
    const CPUClosure* lastCpuClosure = mClosures.back();

    firstCpuClosure->mSi->forEachMtlsSetup(
        (const Allocation**)&firstCpuClosure->mClosure->mArgs[0],
        firstCpuClosure->mClosure->mArgs.size(),
        lastCpuClosure->mClosure->mReturnValue,
        nullptr, 0, nullptr, &mtls);

    mtls.script = nullptr;
    mtls.fep.usr = nullptr;
    mtls.kernel = mExecutable->getForEachFunction(0);

    mGroup->getCpuRefImpl()->launchThreads(
        (const Allocation**)&firstCpuClosure->mClosure->mArgs[0],
        firstCpuClosure->mClosure->mArgs.size(),
        lastCpuClosure->mClosure->mReturnValue,
        nullptr, &mtls);

    return;
  }

  for (CPUClosure* cpuClosure : mClosures) {
    const Closure* closure = cpuClosure->mClosure;
    const ScriptKernelID* kernelID = closure->mKernelID.get();
    cpuClosure->mSi->preLaunch(kernelID->mSlot,
                               (const Allocation**)&closure->mArgs[0],
                               closure->mArgs.size(), closure->mReturnValue,
                               cpuClosure->mUsrPtr, cpuClosure->mUsrSize,
                               nullptr);
  }

  const CPUClosure* cpuClosure = mClosures.front();
  const Closure* closure = cpuClosure->mClosure;
  MTLaunchStruct mtls;

  if (cpuClosure->mSi->forEachMtlsSetup((const Allocation**)&closure->mArgs[0],
                                        closure->mArgs.size(),
                                        closure->mReturnValue,
                                        nullptr, 0, nullptr, &mtls)) {

      mtls.script = nullptr;
      mtls.kernel = (void (*)())&groupRoot;
      mtls.fep.usr = &mClosures;

      mGroup->getCpuRefImpl()->launchThreads(nullptr, 0, nullptr, nullptr, &mtls);
  }

  for (CPUClosure* cpuClosure : mClosures) {
    const Closure* closure = cpuClosure->mClosure;
    const ScriptKernelID* kernelID = closure->mKernelID.get();
    cpuClosure->mSi->postLaunch(kernelID->mSlot,
                                (const Allocation**)&closure->mArgs[0],
                                closure->mArgs.size(), closure->mReturnValue,
                                nullptr, 0, nullptr);
  }
}

}  // namespace renderscript
}  // namespace android