rsCpuCore.cpp revision ecf9107edf90ba0e975b9f993274fa92a2388133
1/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "rsCpuCore.h"
18#include "rsCpuScript.h"
19#include "rsCpuScriptGroup.h"
20#include "rsCpuScriptGroup2.h"
21
22#include <malloc.h>
23#include "rsContext.h"
24
25#include <sys/types.h>
26#include <sys/resource.h>
27#include <sched.h>
28#include <sys/syscall.h>
29#include <stdio.h>
30#include <string.h>
31#include <unistd.h>
32
33#if !defined(RS_SERVER) && !defined(RS_COMPATIBILITY_LIB)
34#include <cutils/properties.h>
35#include "utils/StopWatch.h"
36#endif
37
38#ifdef RS_SERVER
39// Android exposes gettid(), standard Linux does not
40static pid_t gettid() {
41    return syscall(SYS_gettid);
42}
43#endif
44
45using namespace android;
46using namespace android::renderscript;
47
48#define REDUCE_ALOGV(mtls, level, ...) do { if ((mtls)->logReduce >= (level)) ALOGV(__VA_ARGS__); } while(0)
49
50static pthread_key_t gThreadTLSKey = 0;
51static uint32_t gThreadTLSKeyCount = 0;
52static pthread_mutex_t gInitMutex = PTHREAD_MUTEX_INITIALIZER;
53
54bool android::renderscript::gArchUseSIMD = false;
55
56RsdCpuReference::~RsdCpuReference() {
57}
58
59RsdCpuReference * RsdCpuReference::create(Context *rsc, uint32_t version_major,
60        uint32_t version_minor, sym_lookup_t lfn, script_lookup_t slfn
61        , RSSelectRTCallback pSelectRTCallback,
62        const char *pBccPluginName
63        ) {
64
65    RsdCpuReferenceImpl *cpu = new RsdCpuReferenceImpl(rsc);
66    if (!cpu) {
67        return nullptr;
68    }
69    if (!cpu->init(version_major, version_minor, lfn, slfn)) {
70        delete cpu;
71        return nullptr;
72    }
73
74    cpu->setSelectRTCallback(pSelectRTCallback);
75    if (pBccPluginName) {
76        cpu->setBccPluginName(pBccPluginName);
77    }
78
79    return cpu;
80}
81
82
83Context * RsdCpuReference::getTlsContext() {
84    ScriptTLSStruct * tls = (ScriptTLSStruct *)pthread_getspecific(gThreadTLSKey);
85    return tls->mContext;
86}
87
88const Script * RsdCpuReference::getTlsScript() {
89    ScriptTLSStruct * tls = (ScriptTLSStruct *)pthread_getspecific(gThreadTLSKey);
90    return tls->mScript;
91}
92
93pthread_key_t RsdCpuReference::getThreadTLSKey(){ return gThreadTLSKey; }
94
95////////////////////////////////////////////////////////////
96///
97
98RsdCpuReferenceImpl::RsdCpuReferenceImpl(Context *rsc) {
99    mRSC = rsc;
100
101    version_major = 0;
102    version_minor = 0;
103    mInKernel = false;
104    memset(&mWorkers, 0, sizeof(mWorkers));
105    memset(&mTlsStruct, 0, sizeof(mTlsStruct));
106    mExit = false;
107    mSelectRTCallback = nullptr;
108    mEmbedGlobalInfo = true;
109    mEmbedGlobalInfoSkipConstant = true;
110}
111
112
113void * RsdCpuReferenceImpl::helperThreadProc(void *vrsc) {
114    RsdCpuReferenceImpl *dc = (RsdCpuReferenceImpl *)vrsc;
115
116    uint32_t idx = __sync_fetch_and_add(&dc->mWorkers.mLaunchCount, 1);
117
118    //ALOGV("RS helperThread starting %p idx=%i", dc, idx);
119
120    dc->mWorkers.mLaunchSignals[idx].init();
121    dc->mWorkers.mNativeThreadId[idx] = gettid();
122
123    memset(&dc->mTlsStruct, 0, sizeof(dc->mTlsStruct));
124    int status = pthread_setspecific(gThreadTLSKey, &dc->mTlsStruct);
125    if (status) {
126        ALOGE("pthread_setspecific %i", status);
127    }
128
129#if 0
130    typedef struct {uint64_t bits[1024 / 64]; } cpu_set_t;
131    cpu_set_t cpuset;
132    memset(&cpuset, 0, sizeof(cpuset));
133    cpuset.bits[idx / 64] |= 1ULL << (idx % 64);
134    int ret = syscall(241, rsc->mWorkers.mNativeThreadId[idx],
135              sizeof(cpuset), &cpuset);
136    ALOGE("SETAFFINITY ret = %i %s", ret, EGLUtils::strerror(ret));
137#endif
138
139    while (!dc->mExit) {
140        dc->mWorkers.mLaunchSignals[idx].wait();
141        if (dc->mWorkers.mLaunchCallback) {
142           // idx +1 is used because the calling thread is always worker 0.
143           dc->mWorkers.mLaunchCallback(dc->mWorkers.mLaunchData, idx+1);
144        }
145        __sync_fetch_and_sub(&dc->mWorkers.mRunningCount, 1);
146        dc->mWorkers.mCompleteSignal.set();
147    }
148
149    //ALOGV("RS helperThread exited %p idx=%i", dc, idx);
150    return nullptr;
151}
152
153// Launch a kernel.
154// The callback function is called to execute the kernel.
155void RsdCpuReferenceImpl::launchThreads(WorkerCallback_t cbk, void *data) {
156    mWorkers.mLaunchData = data;
157    mWorkers.mLaunchCallback = cbk;
158
159    // fast path for very small launches
160    MTLaunchStructCommon *mtls = (MTLaunchStructCommon *)data;
161    if (mtls && mtls->dimPtr->y <= 1 && mtls->end.x <= mtls->start.x + mtls->mSliceSize) {
162        if (mWorkers.mLaunchCallback) {
163            mWorkers.mLaunchCallback(mWorkers.mLaunchData, 0);
164        }
165        return;
166    }
167
168    mWorkers.mRunningCount = mWorkers.mCount;
169    __sync_synchronize();
170
171    for (uint32_t ct = 0; ct < mWorkers.mCount; ct++) {
172        mWorkers.mLaunchSignals[ct].set();
173    }
174
175    // We use the calling thread as one of the workers so we can start without
176    // the delay of the thread wakeup.
177    if (mWorkers.mLaunchCallback) {
178        mWorkers.mLaunchCallback(mWorkers.mLaunchData, 0);
179    }
180
181    while (__sync_fetch_and_or(&mWorkers.mRunningCount, 0) != 0) {
182        mWorkers.mCompleteSignal.wait();
183    }
184}
185
186
187void RsdCpuReferenceImpl::lockMutex() {
188    pthread_mutex_lock(&gInitMutex);
189}
190
191void RsdCpuReferenceImpl::unlockMutex() {
192    pthread_mutex_unlock(&gInitMutex);
193}
194
195// Determine if the CPU we're running on supports SIMD instructions.
196static void GetCpuInfo() {
197    // Read the CPU flags from /proc/cpuinfo.
198    FILE *cpuinfo = fopen("/proc/cpuinfo", "r");
199
200    if (!cpuinfo) {
201        return;
202    }
203
204    char cpuinfostr[4096];
205    // fgets() ends with newline or EOF, need to check the whole
206    // "cpuinfo" file to make sure we can use SIMD or not.
207    while (fgets(cpuinfostr, sizeof(cpuinfostr), cpuinfo)) {
208#if defined(ARCH_ARM_HAVE_VFP) || defined(ARCH_ARM_USE_INTRINSICS)
209        gArchUseSIMD = strstr(cpuinfostr, " neon") || strstr(cpuinfostr, " asimd");
210#elif defined(ARCH_X86_HAVE_SSSE3)
211        gArchUseSIMD = strstr(cpuinfostr, " ssse3");
212#endif
213        if (gArchUseSIMD) {
214            break;
215        }
216    }
217    fclose(cpuinfo);
218}
219
220bool RsdCpuReferenceImpl::init(uint32_t version_major, uint32_t version_minor,
221                               sym_lookup_t lfn, script_lookup_t slfn) {
222    mSymLookupFn = lfn;
223    mScriptLookupFn = slfn;
224
225    lockMutex();
226    if (!gThreadTLSKeyCount) {
227        int status = pthread_key_create(&gThreadTLSKey, nullptr);
228        if (status) {
229            ALOGE("Failed to init thread tls key.");
230            unlockMutex();
231            return false;
232        }
233    }
234    gThreadTLSKeyCount++;
235    unlockMutex();
236
237    mTlsStruct.mContext = mRSC;
238    mTlsStruct.mScript = nullptr;
239    int status = pthread_setspecific(gThreadTLSKey, &mTlsStruct);
240    if (status) {
241        ALOGE("pthread_setspecific %i", status);
242    }
243
244    mPageSize = sysconf(_SC_PAGE_SIZE);
245    // ALOGV("page size = %ld", mPageSize);
246
247    GetCpuInfo();
248
249    int cpu = sysconf(_SC_NPROCESSORS_CONF);
250    if(mRSC->props.mDebugMaxThreads) {
251        cpu = mRSC->props.mDebugMaxThreads;
252    }
253    if (cpu < 2) {
254        mWorkers.mCount = 0;
255        return true;
256    }
257
258    // Subtract one from the cpu count because we also use the command thread as a worker.
259    mWorkers.mCount = (uint32_t)(cpu - 1);
260
261    if (mRSC->props.mLogScripts) {
262      ALOGV("%p Launching thread(s), CPUs %i", mRSC, mWorkers.mCount + 1);
263    }
264
265    mWorkers.mThreadId = (pthread_t *) calloc(mWorkers.mCount, sizeof(pthread_t));
266    mWorkers.mNativeThreadId = (pid_t *) calloc(mWorkers.mCount, sizeof(pid_t));
267    mWorkers.mLaunchSignals = new Signal[mWorkers.mCount];
268    mWorkers.mLaunchCallback = nullptr;
269
270    mWorkers.mCompleteSignal.init();
271
272    mWorkers.mRunningCount = mWorkers.mCount;
273    mWorkers.mLaunchCount = 0;
274    __sync_synchronize();
275
276    pthread_attr_t threadAttr;
277    status = pthread_attr_init(&threadAttr);
278    if (status) {
279        ALOGE("Failed to init thread attribute.");
280        return false;
281    }
282
283    for (uint32_t ct=0; ct < mWorkers.mCount; ct++) {
284        status = pthread_create(&mWorkers.mThreadId[ct], &threadAttr, helperThreadProc, this);
285        if (status) {
286            mWorkers.mCount = ct;
287            ALOGE("Created fewer than expected number of RS threads.");
288            break;
289        }
290    }
291    while (__sync_fetch_and_or(&mWorkers.mRunningCount, 0) != 0) {
292        usleep(100);
293    }
294
295    pthread_attr_destroy(&threadAttr);
296    return true;
297}
298
299
300void RsdCpuReferenceImpl::setPriority(int32_t priority) {
301    for (uint32_t ct=0; ct < mWorkers.mCount; ct++) {
302        setpriority(PRIO_PROCESS, mWorkers.mNativeThreadId[ct], priority);
303    }
304}
305
306RsdCpuReferenceImpl::~RsdCpuReferenceImpl() {
307    mExit = true;
308    mWorkers.mLaunchData = nullptr;
309    mWorkers.mLaunchCallback = nullptr;
310    mWorkers.mRunningCount = mWorkers.mCount;
311    __sync_synchronize();
312    for (uint32_t ct = 0; ct < mWorkers.mCount; ct++) {
313        mWorkers.mLaunchSignals[ct].set();
314    }
315    void *res;
316    for (uint32_t ct = 0; ct < mWorkers.mCount; ct++) {
317        pthread_join(mWorkers.mThreadId[ct], &res);
318    }
319    // b/23109602
320    // TODO: Refactor the implementation with threadpool to
321    // fix the race condition in the destuctor.
322    // rsAssert(__sync_fetch_and_or(&mWorkers.mRunningCount, 0) == 0);
323    free(mWorkers.mThreadId);
324    free(mWorkers.mNativeThreadId);
325    delete[] mWorkers.mLaunchSignals;
326
327    // Global structure cleanup.
328    lockMutex();
329    --gThreadTLSKeyCount;
330    if (!gThreadTLSKeyCount) {
331        pthread_key_delete(gThreadTLSKey);
332    }
333    unlockMutex();
334
335}
336
337// Set up the appropriate input and output pointers to the kernel driver info structure.
338// Inputs:
339//   mtls - The MTLaunchStruct holding information about the kernel launch
340//   fep - The forEach parameters (driver info structure)
341//   x, y, z, lod, face, a1, a2, a3, a4 - The start offsets into each dimension
342static inline void FepPtrSetup(const MTLaunchStructForEach *mtls, RsExpandKernelDriverInfo *fep,
343                               uint32_t x, uint32_t y,
344                               uint32_t z = 0, uint32_t lod = 0,
345                               RsAllocationCubemapFace face = RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X,
346                               uint32_t a1 = 0, uint32_t a2 = 0, uint32_t a3 = 0, uint32_t a4 = 0) {
347    for (uint32_t i = 0; i < fep->inLen; i++) {
348        fep->inPtr[i] = (const uint8_t *)mtls->ains[i]->getPointerUnchecked(x, y, z, lod, face, a1, a2, a3, a4);
349    }
350    if (mtls->aout[0] != nullptr) {
351        fep->outPtr[0] = (uint8_t *)mtls->aout[0]->getPointerUnchecked(x, y, z, lod, face, a1, a2, a3, a4);
352    }
353}
354
355// Set up the appropriate input and output pointers to the kernel driver info structure.
356// Inputs:
357//   mtls - The MTLaunchStruct holding information about the kernel launch
358//   redp - The reduce parameters (driver info structure)
359//   x, y, z - The start offsets into each dimension
360static inline void RedpPtrSetup(const MTLaunchStructReduce *mtls, RsExpandKernelDriverInfo *redp,
361                                uint32_t x, uint32_t y, uint32_t z) {
362    for (uint32_t i = 0; i < redp->inLen; i++) {
363        redp->inPtr[i] = (const uint8_t *)mtls->ains[i]->getPointerUnchecked(x, y, z);
364    }
365}
366
367static uint32_t sliceInt(uint32_t *p, uint32_t val, uint32_t start, uint32_t end) {
368    if (start >= end) {
369        *p = start;
370        return val;
371    }
372
373    uint32_t div = end - start;
374
375    uint32_t n = val / div;
376    *p = (val - (n * div)) + start;
377    return n;
378}
379
380static bool SelectOuterSlice(const MTLaunchStructCommon *mtls, RsExpandKernelDriverInfo* info, uint32_t sliceNum) {
381    uint32_t r = sliceNum;
382    r = sliceInt(&info->current.z, r, mtls->start.z, mtls->end.z);
383    r = sliceInt(&info->current.lod, r, mtls->start.lod, mtls->end.lod);
384    r = sliceInt(&info->current.face, r, mtls->start.face, mtls->end.face);
385    r = sliceInt(&info->current.array[0], r, mtls->start.array[0], mtls->end.array[0]);
386    r = sliceInt(&info->current.array[1], r, mtls->start.array[1], mtls->end.array[1]);
387    r = sliceInt(&info->current.array[2], r, mtls->start.array[2], mtls->end.array[2]);
388    r = sliceInt(&info->current.array[3], r, mtls->start.array[3], mtls->end.array[3]);
389    return r == 0;
390}
391
392static bool SelectZSlice(const MTLaunchStructCommon *mtls, RsExpandKernelDriverInfo* info, uint32_t sliceNum) {
393    return sliceInt(&info->current.z, sliceNum, mtls->start.z, mtls->end.z) == 0;
394}
395
396static void walk_general_foreach(void *usr, uint32_t idx) {
397    MTLaunchStructForEach *mtls = (MTLaunchStructForEach *)usr;
398    RsExpandKernelDriverInfo fep = mtls->fep;
399    fep.lid = idx;
400    ForEachFunc_t fn = mtls->kernel;
401
402    while(1) {
403        uint32_t slice = (uint32_t)__sync_fetch_and_add(&mtls->mSliceNum, 1);
404
405        if (!SelectOuterSlice(mtls, &fep, slice)) {
406            return;
407        }
408
409        for (fep.current.y = mtls->start.y; fep.current.y < mtls->end.y;
410             fep.current.y++) {
411
412            FepPtrSetup(mtls, &fep, mtls->start.x,
413                        fep.current.y, fep.current.z, fep.current.lod,
414                        (RsAllocationCubemapFace)fep.current.face,
415                        fep.current.array[0], fep.current.array[1],
416                        fep.current.array[2], fep.current.array[3]);
417
418            fn(&fep, mtls->start.x, mtls->end.x, mtls->fep.outStride[0]);
419        }
420    }
421}
422
423static void walk_2d_foreach(void *usr, uint32_t idx) {
424    MTLaunchStructForEach *mtls = (MTLaunchStructForEach *)usr;
425    RsExpandKernelDriverInfo fep = mtls->fep;
426    fep.lid = idx;
427    ForEachFunc_t fn = mtls->kernel;
428
429    while (1) {
430        uint32_t slice  = (uint32_t)__sync_fetch_and_add(&mtls->mSliceNum, 1);
431        uint32_t yStart = mtls->start.y + slice * mtls->mSliceSize;
432        uint32_t yEnd   = yStart + mtls->mSliceSize;
433
434        yEnd = rsMin(yEnd, mtls->end.y);
435
436        if (yEnd <= yStart) {
437            return;
438        }
439
440        for (fep.current.y = yStart; fep.current.y < yEnd; fep.current.y++) {
441            FepPtrSetup(mtls, &fep, mtls->start.x, fep.current.y);
442
443            fn(&fep, mtls->start.x, mtls->end.x, fep.outStride[0]);
444        }
445    }
446}
447
448static void walk_1d_foreach(void *usr, uint32_t idx) {
449    MTLaunchStructForEach *mtls = (MTLaunchStructForEach *)usr;
450    RsExpandKernelDriverInfo fep = mtls->fep;
451    fep.lid = idx;
452    ForEachFunc_t fn = mtls->kernel;
453
454    while (1) {
455        uint32_t slice  = (uint32_t)__sync_fetch_and_add(&mtls->mSliceNum, 1);
456        uint32_t xStart = mtls->start.x + slice * mtls->mSliceSize;
457        uint32_t xEnd   = xStart + mtls->mSliceSize;
458
459        xEnd = rsMin(xEnd, mtls->end.x);
460
461        if (xEnd <= xStart) {
462            return;
463        }
464
465        FepPtrSetup(mtls, &fep, xStart, 0);
466
467        fn(&fep, xStart, xEnd, fep.outStride[0]);
468    }
469}
470
471// The function format_bytes() is an auxiliary function to assist in logging.
472//
473// Bytes are read from an input (inBuf) and written (as pairs of hex digits)
474// to an output (outBuf).
475//
476// Output format:
477// - starts with ": "
478// - each input byte is translated to a pair of hex digits
479// - bytes are separated by "." except that every fourth separator is "|"
480// - if the input is sufficiently long, the output is truncated and terminated with "..."
481//
482// Arguments:
483// - outBuf  -- Pointer to buffer of type "FormatBuf" into which output is written
484// - inBuf   -- Pointer to bytes which are to be formatted into outBuf
485// - inBytes -- Number of bytes in inBuf
486//
487// Constant:
488// - kFormatInBytesMax -- Only min(kFormatInBytesMax, inBytes) bytes will be read
489//                        from inBuf
490//
491// Return value:
492// - pointer (const char *) to output (which is part of outBuf)
493//
494static const int kFormatInBytesMax = 16;
495// ": " + 2 digits per byte + 1 separator between bytes + "..." + null
496typedef char FormatBuf[2 + kFormatInBytesMax*2 + (kFormatInBytesMax - 1) + 3 + 1];
497static const char *format_bytes(FormatBuf *outBuf, const uint8_t *inBuf, const int inBytes) {
498  strcpy(*outBuf, ": ");
499  int pos = 2;
500  const int lim = std::min(kFormatInBytesMax, inBytes);
501  for (int i = 0; i < lim; ++i) {
502    if (i) {
503      sprintf(*outBuf + pos, (i % 4 ? "." : "|"));
504      ++pos;
505    }
506    sprintf(*outBuf + pos, "%02x", inBuf[i]);
507    pos += 2;
508  }
509  if (kFormatInBytesMax < inBytes)
510    strcpy(*outBuf + pos, "...");
511  return *outBuf;
512}
513
514static void reduce_get_accumulator(uint8_t *&accumPtr, const MTLaunchStructReduce *mtls,
515                                   const char *walkerName, uint32_t threadIdx) {
516  rsAssert(!accumPtr);
517
518  uint32_t accumIdx = (uint32_t)__sync_fetch_and_add(&mtls->accumCount, 1);
519  if (mtls->outFunc) {
520    accumPtr = mtls->accumAlloc + mtls->accumStride * accumIdx;
521  } else {
522    if (accumIdx == 0) {
523      accumPtr = mtls->redp.outPtr[0];
524    } else {
525      accumPtr = mtls->accumAlloc + mtls->accumStride * (accumIdx - 1);
526    }
527  }
528  REDUCE_ALOGV(mtls, 2, "%s(%p): idx = %u got accumCount %u and accumPtr %p",
529               walkerName, mtls->accumFunc, threadIdx, accumIdx, accumPtr);
530  // initialize accumulator
531  if (mtls->initFunc) {
532    mtls->initFunc(accumPtr);
533  } else {
534    memset(accumPtr, 0, mtls->accumSize);
535  }
536}
537
538static void walk_1d_reduce(void *usr, uint32_t idx) {
539  const MTLaunchStructReduce *mtls = (const MTLaunchStructReduce *)usr;
540  RsExpandKernelDriverInfo redp = mtls->redp;
541
542  // find accumulator
543  uint8_t *&accumPtr = mtls->accumPtr[idx];
544  if (!accumPtr) {
545    reduce_get_accumulator(accumPtr, mtls, __func__, idx);
546  }
547
548  // accumulate
549  const ReduceAccumulatorFunc_t fn = mtls->accumFunc;
550  while (1) {
551    uint32_t slice  = (uint32_t)__sync_fetch_and_add(&mtls->mSliceNum, 1);
552    uint32_t xStart = mtls->start.x + slice * mtls->mSliceSize;
553    uint32_t xEnd   = xStart + mtls->mSliceSize;
554
555    xEnd = rsMin(xEnd, mtls->end.x);
556
557    if (xEnd <= xStart) {
558      return;
559    }
560
561    RedpPtrSetup(mtls, &redp, xStart, 0, 0);
562    fn(&redp, xStart, xEnd, accumPtr);
563
564    // Emit log line after slice has been run, so that we can include
565    // the results of the run on that line.
566    FormatBuf fmt;
567    if (mtls->logReduce >= 3) {
568      format_bytes(&fmt, accumPtr, mtls->accumSize);
569    } else {
570      fmt[0] = 0;
571    }
572    REDUCE_ALOGV(mtls, 2, "walk_1d_reduce(%p): idx = %u, x in [%u, %u)%s",
573                 mtls->accumFunc, idx, xStart, xEnd, fmt);
574  }
575}
576
577static void walk_2d_reduce(void *usr, uint32_t idx) {
578  const MTLaunchStructReduce *mtls = (const MTLaunchStructReduce *)usr;
579  RsExpandKernelDriverInfo redp = mtls->redp;
580
581  // find accumulator
582  uint8_t *&accumPtr = mtls->accumPtr[idx];
583  if (!accumPtr) {
584    reduce_get_accumulator(accumPtr, mtls, __func__, idx);
585  }
586
587  // accumulate
588  const ReduceAccumulatorFunc_t fn = mtls->accumFunc;
589  while (1) {
590    uint32_t slice  = (uint32_t)__sync_fetch_and_add(&mtls->mSliceNum, 1);
591    uint32_t yStart = mtls->start.y + slice * mtls->mSliceSize;
592    uint32_t yEnd   = yStart + mtls->mSliceSize;
593
594    yEnd = rsMin(yEnd, mtls->end.y);
595
596    if (yEnd <= yStart) {
597      return;
598    }
599
600    for (redp.current.y = yStart; redp.current.y < yEnd; redp.current.y++) {
601      RedpPtrSetup(mtls, &redp, mtls->start.x, redp.current.y, 0);
602      fn(&redp, mtls->start.x, mtls->end.x, accumPtr);
603    }
604
605    FormatBuf fmt;
606    if (mtls->logReduce >= 3) {
607      format_bytes(&fmt, accumPtr, mtls->accumSize);
608    } else {
609      fmt[0] = 0;
610    }
611    REDUCE_ALOGV(mtls, 2, "walk_2d_reduce(%p): idx = %u, y in [%u, %u)%s",
612                 mtls->accumFunc, idx, yStart, yEnd, fmt);
613  }
614}
615
616static void walk_3d_reduce(void *usr, uint32_t idx) {
617  const MTLaunchStructReduce *mtls = (const MTLaunchStructReduce *)usr;
618  RsExpandKernelDriverInfo redp = mtls->redp;
619
620  // find accumulator
621  uint8_t *&accumPtr = mtls->accumPtr[idx];
622  if (!accumPtr) {
623    reduce_get_accumulator(accumPtr, mtls, __func__, idx);
624  }
625
626  // accumulate
627  const ReduceAccumulatorFunc_t fn = mtls->accumFunc;
628  while (1) {
629    uint32_t slice  = (uint32_t)__sync_fetch_and_add(&mtls->mSliceNum, 1);
630
631    if (!SelectZSlice(mtls, &redp, slice)) {
632      return;
633    }
634
635    for (redp.current.y = mtls->start.y; redp.current.y < mtls->end.y; redp.current.y++) {
636      RedpPtrSetup(mtls, &redp, mtls->start.x, redp.current.y, redp.current.z);
637      fn(&redp, mtls->start.x, mtls->end.x, accumPtr);
638    }
639
640    FormatBuf fmt;
641    if (mtls->logReduce >= 3) {
642      format_bytes(&fmt, accumPtr, mtls->accumSize);
643    } else {
644      fmt[0] = 0;
645    }
646    REDUCE_ALOGV(mtls, 2, "walk_3d_reduce(%p): idx = %u, z = %u%s",
647                 mtls->accumFunc, idx, redp.current.z, fmt);
648  }
649}
650
651// Launch a general reduce-style kernel.
652// Inputs:
653//   ains[0..inLen-1]: Array of allocations that contain the inputs
654//   aout:             The allocation that will hold the output
655//   mtls:             Holds launch parameters
656void RsdCpuReferenceImpl::launchReduce(const Allocation ** ains,
657                                       uint32_t inLen,
658                                       Allocation * aout,
659                                       MTLaunchStructReduce *mtls) {
660  mtls->logReduce = mRSC->props.mLogReduce;
661  if ((mWorkers.mCount >= 1) && mtls->isThreadable && !mInKernel) {
662    launchReduceParallel(ains, inLen, aout, mtls);
663  } else {
664    launchReduceSerial(ains, inLen, aout, mtls);
665  }
666}
667
668// Launch a general reduce-style kernel, single-threaded.
669// Inputs:
670//   ains[0..inLen-1]: Array of allocations that contain the inputs
671//   aout:             The allocation that will hold the output
672//   mtls:             Holds launch parameters
673void RsdCpuReferenceImpl::launchReduceSerial(const Allocation ** ains,
674                                             uint32_t inLen,
675                                             Allocation * aout,
676                                             MTLaunchStructReduce *mtls) {
677  REDUCE_ALOGV(mtls, 1, "launchReduceSerial(%p): %u x %u x %u", mtls->accumFunc,
678               mtls->redp.dim.x, mtls->redp.dim.y, mtls->redp.dim.z);
679
680  // In the presence of outconverter, we allocate temporary memory for
681  // the accumulator.
682  //
683  // In the absence of outconverter, we use the output allocation as the
684  // accumulator.
685  uint8_t *const accumPtr = (mtls->outFunc
686                             ? static_cast<uint8_t *>(malloc(mtls->accumSize))
687                             : mtls->redp.outPtr[0]);
688
689  // initialize
690  if (mtls->initFunc) {
691    mtls->initFunc(accumPtr);
692  } else {
693    memset(accumPtr, 0, mtls->accumSize);
694  }
695
696  // accumulate
697  const ReduceAccumulatorFunc_t fn = mtls->accumFunc;
698  uint32_t slice = 0;
699  while (SelectOuterSlice(mtls, &mtls->redp, slice++)) {
700    for (mtls->redp.current.y = mtls->start.y;
701         mtls->redp.current.y < mtls->end.y;
702         mtls->redp.current.y++) {
703      RedpPtrSetup(mtls, &mtls->redp, mtls->start.x, mtls->redp.current.y, mtls->redp.current.z);
704      fn(&mtls->redp, mtls->start.x, mtls->end.x, accumPtr);
705    }
706  }
707
708  // outconvert
709  if (mtls->outFunc) {
710    mtls->outFunc(mtls->redp.outPtr[0], accumPtr);
711    free(accumPtr);
712  }
713}
714
715// Launch a general reduce-style kernel, multi-threaded.
716// Inputs:
717//   ains[0..inLen-1]: Array of allocations that contain the inputs
718//   aout:             The allocation that will hold the output
719//   mtls:             Holds launch parameters
720void RsdCpuReferenceImpl::launchReduceParallel(const Allocation ** ains,
721                                               uint32_t inLen,
722                                               Allocation * aout,
723                                               MTLaunchStructReduce *mtls) {
724  // For now, we don't know how to go parallel in the absence of a combiner.
725  if (!mtls->combFunc) {
726    launchReduceSerial(ains, inLen, aout, mtls);
727    return;
728  }
729
730  // Number of threads = "main thread" + number of other (worker) threads
731  const uint32_t numThreads = mWorkers.mCount + 1;
732
733  // In the absence of outconverter, we use the output allocation as
734  // an accumulator, and therefore need to allocate one fewer accumulator.
735  const uint32_t numAllocAccum = numThreads - (mtls->outFunc == nullptr);
736
737  // If mDebugReduceSplitAccum, then we want each accumulator to start
738  // on a page boundary.  (TODO: Would some unit smaller than a page
739  // be sufficient to avoid false sharing?)
740  if (mRSC->props.mDebugReduceSplitAccum) {
741    // Round up accumulator size to an integral number of pages
742    mtls->accumStride =
743        (unsigned(mtls->accumSize) + unsigned(mPageSize)-1) &
744        ~(unsigned(mPageSize)-1);
745    // Each accumulator gets its own page.  Alternatively, if we just
746    // wanted to make sure no two accumulators are on the same page,
747    // we could instead do
748    //   allocSize = mtls->accumStride * (numAllocation - 1) + mtls->accumSize
749    const size_t allocSize = mtls->accumStride * numAllocAccum;
750    mtls->accumAlloc = static_cast<uint8_t *>(memalign(mPageSize, allocSize));
751  } else {
752    mtls->accumStride = mtls->accumSize;
753    mtls->accumAlloc = static_cast<uint8_t *>(malloc(mtls->accumStride * numAllocAccum));
754  }
755
756  const size_t accumPtrArrayBytes = sizeof(uint8_t *) * numThreads;
757  mtls->accumPtr = static_cast<uint8_t **>(malloc(accumPtrArrayBytes));
758  memset(mtls->accumPtr, 0, accumPtrArrayBytes);
759
760  mtls->accumCount = 0;
761
762  rsAssert(!mInKernel);
763  mInKernel = true;
764  REDUCE_ALOGV(mtls, 1, "launchReduceParallel(%p): %u x %u x %u, %u threads, accumAlloc = %p",
765               mtls->accumFunc,
766               mtls->redp.dim.x, mtls->redp.dim.y, mtls->redp.dim.z,
767               numThreads, mtls->accumAlloc);
768  if (mtls->redp.dim.z > 1) {
769    mtls->mSliceSize = 1;
770    launchThreads(walk_3d_reduce, mtls);
771  } else if (mtls->redp.dim.y > 1) {
772    mtls->mSliceSize = rsMax(1U, mtls->redp.dim.y / (numThreads * 4));
773    launchThreads(walk_2d_reduce, mtls);
774  } else {
775    mtls->mSliceSize = rsMax(1U, mtls->redp.dim.x / (numThreads * 4));
776    launchThreads(walk_1d_reduce, mtls);
777  }
778  mInKernel = false;
779
780  // Combine accumulators and identify final accumulator
781  uint8_t *finalAccumPtr = (mtls->outFunc ? nullptr : mtls->redp.outPtr[0]);
782  //   Loop over accumulators, combining into finalAccumPtr.  If finalAccumPtr
783  //   is null, then the first accumulator I find becomes finalAccumPtr.
784  for (unsigned idx = 0; idx < mtls->accumCount; ++idx) {
785    uint8_t *const thisAccumPtr = mtls->accumPtr[idx];
786    if (finalAccumPtr) {
787      if (finalAccumPtr != thisAccumPtr) {
788        if (mtls->combFunc) {
789          if (mtls->logReduce >= 3) {
790            FormatBuf fmt;
791            REDUCE_ALOGV(mtls, 3, "launchReduceParallel(%p): accumulating into%s",
792                         mtls->accumFunc,
793                         format_bytes(&fmt, finalAccumPtr, mtls->accumSize));
794            REDUCE_ALOGV(mtls, 3, "launchReduceParallel(%p):    accumulator[%d]%s",
795                         mtls->accumFunc, idx,
796                         format_bytes(&fmt, thisAccumPtr, mtls->accumSize));
797          }
798          mtls->combFunc(finalAccumPtr, thisAccumPtr);
799        } else {
800          rsAssert(!"expected combiner");
801        }
802      }
803    } else {
804      finalAccumPtr = thisAccumPtr;
805    }
806  }
807  rsAssert(finalAccumPtr != nullptr);
808  if (mtls->logReduce >= 3) {
809    FormatBuf fmt;
810    REDUCE_ALOGV(mtls, 3, "launchReduceParallel(%p): final accumulator%s",
811                 mtls->accumFunc, format_bytes(&fmt, finalAccumPtr, mtls->accumSize));
812  }
813
814  // Outconvert
815  if (mtls->outFunc) {
816    mtls->outFunc(mtls->redp.outPtr[0], finalAccumPtr);
817    if (mtls->logReduce >= 3) {
818      FormatBuf fmt;
819      REDUCE_ALOGV(mtls, 3, "launchReduceParallel(%p): final outconverted result%s",
820                   mtls->accumFunc,
821                   format_bytes(&fmt, mtls->redp.outPtr[0], mtls->redp.outStride[0]));
822    }
823  }
824
825  // Clean up
826  free(mtls->accumPtr);
827  free(mtls->accumAlloc);
828}
829
830
831void RsdCpuReferenceImpl::launchForEach(const Allocation ** ains,
832                                        uint32_t inLen,
833                                        Allocation* aout,
834                                        const RsScriptCall* sc,
835                                        MTLaunchStructForEach* mtls) {
836
837    //android::StopWatch kernel_time("kernel time");
838
839    bool outerDims = (mtls->start.z != mtls->end.z) ||
840                     (mtls->start.face != mtls->end.face) ||
841                     (mtls->start.lod != mtls->end.lod) ||
842                     (mtls->start.array[0] != mtls->end.array[0]) ||
843                     (mtls->start.array[1] != mtls->end.array[1]) ||
844                     (mtls->start.array[2] != mtls->end.array[2]) ||
845                     (mtls->start.array[3] != mtls->end.array[3]);
846
847    if ((mWorkers.mCount >= 1) && mtls->isThreadable && !mInKernel) {
848        const size_t targetByteChunk = 16 * 1024;
849        mInKernel = true;  // NOTE: The guard immediately above ensures this was !mInKernel
850
851        if (outerDims) {
852            // No fancy logic for chunk size
853            mtls->mSliceSize = 1;
854            launchThreads(walk_general_foreach, mtls);
855        } else if (mtls->fep.dim.y > 1) {
856            uint32_t s1 = mtls->fep.dim.y / ((mWorkers.mCount + 1) * 4);
857            uint32_t s2 = 0;
858
859            // This chooses our slice size to rate limit atomic ops to
860            // one per 16k bytes of reads/writes.
861            if ((mtls->aout[0] != nullptr) && mtls->aout[0]->mHal.drvState.lod[0].stride) {
862                s2 = targetByteChunk / mtls->aout[0]->mHal.drvState.lod[0].stride;
863            } else if (mtls->ains[0]) {
864                s2 = targetByteChunk / mtls->ains[0]->mHal.drvState.lod[0].stride;
865            } else {
866                // Launch option only case
867                // Use s1 based only on the dimensions
868                s2 = s1;
869            }
870            mtls->mSliceSize = rsMin(s1, s2);
871
872            if(mtls->mSliceSize < 1) {
873                mtls->mSliceSize = 1;
874            }
875
876            launchThreads(walk_2d_foreach, mtls);
877        } else {
878            uint32_t s1 = mtls->fep.dim.x / ((mWorkers.mCount + 1) * 4);
879            uint32_t s2 = 0;
880
881            // This chooses our slice size to rate limit atomic ops to
882            // one per 16k bytes of reads/writes.
883            if ((mtls->aout[0] != nullptr) && mtls->aout[0]->getType()->getElementSizeBytes()) {
884                s2 = targetByteChunk / mtls->aout[0]->getType()->getElementSizeBytes();
885            } else if (mtls->ains[0]) {
886                s2 = targetByteChunk / mtls->ains[0]->getType()->getElementSizeBytes();
887            } else {
888                // Launch option only case
889                // Use s1 based only on the dimensions
890                s2 = s1;
891            }
892            mtls->mSliceSize = rsMin(s1, s2);
893
894            if (mtls->mSliceSize < 1) {
895                mtls->mSliceSize = 1;
896            }
897
898            launchThreads(walk_1d_foreach, mtls);
899        }
900        mInKernel = false;
901
902    } else {
903        ForEachFunc_t fn = mtls->kernel;
904        uint32_t slice = 0;
905
906
907        while(SelectOuterSlice(mtls, &mtls->fep, slice++)) {
908            for (mtls->fep.current.y = mtls->start.y;
909                 mtls->fep.current.y < mtls->end.y;
910                 mtls->fep.current.y++) {
911
912                FepPtrSetup(mtls, &mtls->fep, mtls->start.x,
913                            mtls->fep.current.y, mtls->fep.current.z, mtls->fep.current.lod,
914                            (RsAllocationCubemapFace) mtls->fep.current.face,
915                            mtls->fep.current.array[0], mtls->fep.current.array[1],
916                            mtls->fep.current.array[2], mtls->fep.current.array[3]);
917
918                fn(&mtls->fep, mtls->start.x, mtls->end.x, mtls->fep.outStride[0]);
919            }
920        }
921    }
922}
923
924RsdCpuScriptImpl * RsdCpuReferenceImpl::setTLS(RsdCpuScriptImpl *sc) {
925    //ALOGE("setTls %p", sc);
926    ScriptTLSStruct * tls = (ScriptTLSStruct *)pthread_getspecific(gThreadTLSKey);
927    rsAssert(tls);
928    RsdCpuScriptImpl *old = tls->mImpl;
929    tls->mImpl = sc;
930    tls->mContext = mRSC;
931    if (sc) {
932        tls->mScript = sc->getScript();
933    } else {
934        tls->mScript = nullptr;
935    }
936    return old;
937}
938
939const RsdCpuReference::CpuSymbol * RsdCpuReferenceImpl::symLookup(const char *name) {
940    return mSymLookupFn(mRSC, name);
941}
942
943
944RsdCpuReference::CpuScript * RsdCpuReferenceImpl::createScript(const ScriptC *s,
945                                    char const *resName, char const *cacheDir,
946                                    uint8_t const *bitcode, size_t bitcodeSize,
947                                    uint32_t flags) {
948
949    RsdCpuScriptImpl *i = new RsdCpuScriptImpl(this, s);
950    if (!i->init(resName, cacheDir, bitcode, bitcodeSize, flags
951        , getBccPluginName()
952        )) {
953        delete i;
954        return nullptr;
955    }
956    return i;
957}
958
959extern RsdCpuScriptImpl * rsdIntrinsic_3DLUT(RsdCpuReferenceImpl *ctx,
960                                             const Script *s, const Element *e);
961extern RsdCpuScriptImpl * rsdIntrinsic_Convolve3x3(RsdCpuReferenceImpl *ctx,
962                                                   const Script *s, const Element *e);
963extern RsdCpuScriptImpl * rsdIntrinsic_ColorMatrix(RsdCpuReferenceImpl *ctx,
964                                                   const Script *s, const Element *e);
965extern RsdCpuScriptImpl * rsdIntrinsic_LUT(RsdCpuReferenceImpl *ctx,
966                                           const Script *s, const Element *e);
967extern RsdCpuScriptImpl * rsdIntrinsic_Convolve5x5(RsdCpuReferenceImpl *ctx,
968                                                   const Script *s, const Element *e);
969extern RsdCpuScriptImpl * rsdIntrinsic_Blur(RsdCpuReferenceImpl *ctx,
970                                            const Script *s, const Element *e);
971extern RsdCpuScriptImpl * rsdIntrinsic_YuvToRGB(RsdCpuReferenceImpl *ctx,
972                                                const Script *s, const Element *e);
973extern RsdCpuScriptImpl * rsdIntrinsic_Blend(RsdCpuReferenceImpl *ctx,
974                                             const Script *s, const Element *e);
975extern RsdCpuScriptImpl * rsdIntrinsic_Histogram(RsdCpuReferenceImpl *ctx,
976                                                 const Script *s, const Element *e);
977extern RsdCpuScriptImpl * rsdIntrinsic_Resize(RsdCpuReferenceImpl *ctx,
978                                              const Script *s, const Element *e);
979extern RsdCpuScriptImpl * rsdIntrinsic_BLAS(RsdCpuReferenceImpl *ctx,
980                                              const Script *s, const Element *e);
981
982RsdCpuReference::CpuScript * RsdCpuReferenceImpl::createIntrinsic(const Script *s,
983                                    RsScriptIntrinsicID iid, Element *e) {
984
985    RsdCpuScriptImpl *i = nullptr;
986    switch (iid) {
987    case RS_SCRIPT_INTRINSIC_ID_3DLUT:
988        i = rsdIntrinsic_3DLUT(this, s, e);
989        break;
990    case RS_SCRIPT_INTRINSIC_ID_CONVOLVE_3x3:
991        i = rsdIntrinsic_Convolve3x3(this, s, e);
992        break;
993    case RS_SCRIPT_INTRINSIC_ID_COLOR_MATRIX:
994        i = rsdIntrinsic_ColorMatrix(this, s, e);
995        break;
996    case RS_SCRIPT_INTRINSIC_ID_LUT:
997        i = rsdIntrinsic_LUT(this, s, e);
998        break;
999    case RS_SCRIPT_INTRINSIC_ID_CONVOLVE_5x5:
1000        i = rsdIntrinsic_Convolve5x5(this, s, e);
1001        break;
1002    case RS_SCRIPT_INTRINSIC_ID_BLUR:
1003        i = rsdIntrinsic_Blur(this, s, e);
1004        break;
1005    case RS_SCRIPT_INTRINSIC_ID_YUV_TO_RGB:
1006        i = rsdIntrinsic_YuvToRGB(this, s, e);
1007        break;
1008    case RS_SCRIPT_INTRINSIC_ID_BLEND:
1009        i = rsdIntrinsic_Blend(this, s, e);
1010        break;
1011    case RS_SCRIPT_INTRINSIC_ID_HISTOGRAM:
1012        i = rsdIntrinsic_Histogram(this, s, e);
1013        break;
1014    case RS_SCRIPT_INTRINSIC_ID_RESIZE:
1015        i = rsdIntrinsic_Resize(this, s, e);
1016        break;
1017    case RS_SCRIPT_INTRINSIC_ID_BLAS:
1018        i = rsdIntrinsic_BLAS(this, s, e);
1019        break;
1020
1021    default:
1022        rsAssert(0);
1023    }
1024
1025    return i;
1026}
1027
1028void* RsdCpuReferenceImpl::createScriptGroup(const ScriptGroupBase *sg) {
1029  switch (sg->getApiVersion()) {
1030    case ScriptGroupBase::SG_V1: {
1031      CpuScriptGroupImpl *sgi = new CpuScriptGroupImpl(this, sg);
1032      if (!sgi->init()) {
1033        delete sgi;
1034        return nullptr;
1035      }
1036      return sgi;
1037    }
1038    case ScriptGroupBase::SG_V2: {
1039      return new CpuScriptGroup2Impl(this, sg);
1040    }
1041  }
1042  return nullptr;
1043}
1044