Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 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" |
Yang Ni | 1ffd86b | 2015-01-07 09:16:40 -0800 | [diff] [blame] | 20 | #include "rsCpuScriptGroup2.h" |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 21 | |
| 22 | #include <malloc.h> |
| 23 | #include "rsContext.h" |
| 24 | |
| 25 | #include <sys/types.h> |
| 26 | #include <sys/resource.h> |
| 27 | #include <sched.h> |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 28 | #include <sys/syscall.h> |
Matt Wala | 11fd9ec | 2015-07-10 16:40:12 -0700 | [diff] [blame] | 29 | #include <stdio.h> |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 30 | #include <string.h> |
Stephen Hines | b0934b6 | 2013-07-03 17:27:38 -0700 | [diff] [blame] | 31 | #include <unistd.h> |
Tim Murray | 0b575de | 2013-03-15 15:56:43 -0700 | [diff] [blame] | 32 | |
Stephen Hines | b0934b6 | 2013-07-03 17:27:38 -0700 | [diff] [blame] | 33 | #if !defined(RS_SERVER) && !defined(RS_COMPATIBILITY_LIB) |
Tim Murray | 0b575de | 2013-03-15 15:56:43 -0700 | [diff] [blame] | 34 | #include <cutils/properties.h> |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 35 | #include "utils/StopWatch.h" |
Tim Murray | 0b575de | 2013-03-15 15:56:43 -0700 | [diff] [blame] | 36 | #endif |
| 37 | |
| 38 | #ifdef RS_SERVER |
| 39 | // Android exposes gettid(), standard Linux does not |
| 40 | static pid_t gettid() { |
| 41 | return syscall(SYS_gettid); |
| 42 | } |
| 43 | #endif |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 44 | |
| 45 | using namespace android; |
| 46 | using namespace android::renderscript; |
| 47 | |
David Gross | ae2ec3f | 2016-06-01 14:45:47 -0700 | [diff] [blame] | 48 | #define REDUCE_ALOGV(mtls, level, ...) do { if ((mtls)->logReduce >= (level)) ALOGV(__VA_ARGS__); } while(0) |
David Gross | 35dbc8c | 2016-03-29 13:48:41 -0700 | [diff] [blame] | 49 | |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 50 | static pthread_key_t gThreadTLSKey = 0; |
| 51 | static uint32_t gThreadTLSKeyCount = 0; |
| 52 | static pthread_mutex_t gInitMutex = PTHREAD_MUTEX_INITIALIZER; |
| 53 | |
Jason Sams | f5ef8df | 2013-08-06 13:49:25 -0700 | [diff] [blame] | 54 | bool android::renderscript::gArchUseSIMD = false; |
| 55 | |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 56 | RsdCpuReference::~RsdCpuReference() { |
| 57 | } |
| 58 | |
| 59 | RsdCpuReference * RsdCpuReference::create(Context *rsc, uint32_t version_major, |
Jason Sams | cadfac4 | 2013-03-06 18:09:08 -0800 | [diff] [blame] | 60 | uint32_t version_minor, sym_lookup_t lfn, script_lookup_t slfn |
David Gross | b043df0 | 2015-05-29 11:38:15 -0700 | [diff] [blame] | 61 | , RSSelectRTCallback pSelectRTCallback, |
Stephen Hines | 0051132 | 2014-01-31 11:20:23 -0800 | [diff] [blame] | 62 | const char *pBccPluginName |
Jason Sams | cadfac4 | 2013-03-06 18:09:08 -0800 | [diff] [blame] | 63 | ) { |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 64 | |
| 65 | RsdCpuReferenceImpl *cpu = new RsdCpuReferenceImpl(rsc); |
| 66 | if (!cpu) { |
Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 67 | return nullptr; |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 68 | } |
| 69 | if (!cpu->init(version_major, version_minor, lfn, slfn)) { |
| 70 | delete cpu; |
Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 71 | return nullptr; |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 72 | } |
Stephen Hines | f218bf1 | 2013-02-12 19:32:38 -0800 | [diff] [blame] | 73 | |
Stephen Hines | 1d47662 | 2013-03-29 22:08:49 -0700 | [diff] [blame] | 74 | cpu->setSelectRTCallback(pSelectRTCallback); |
Stephen Hines | 0051132 | 2014-01-31 11:20:23 -0800 | [diff] [blame] | 75 | if (pBccPluginName) { |
| 76 | cpu->setBccPluginName(pBccPluginName); |
| 77 | } |
Stephen Hines | f218bf1 | 2013-02-12 19:32:38 -0800 | [diff] [blame] | 78 | |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 79 | return cpu; |
| 80 | } |
| 81 | |
| 82 | |
| 83 | Context * RsdCpuReference::getTlsContext() { |
| 84 | ScriptTLSStruct * tls = (ScriptTLSStruct *)pthread_getspecific(gThreadTLSKey); |
| 85 | return tls->mContext; |
| 86 | } |
| 87 | |
| 88 | const Script * RsdCpuReference::getTlsScript() { |
| 89 | ScriptTLSStruct * tls = (ScriptTLSStruct *)pthread_getspecific(gThreadTLSKey); |
| 90 | return tls->mScript; |
| 91 | } |
| 92 | |
Stephen Hines | f218bf1 | 2013-02-12 19:32:38 -0800 | [diff] [blame] | 93 | pthread_key_t RsdCpuReference::getThreadTLSKey(){ return gThreadTLSKey; } |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 94 | |
| 95 | //////////////////////////////////////////////////////////// |
| 96 | /// |
| 97 | |
| 98 | RsdCpuReferenceImpl::RsdCpuReferenceImpl(Context *rsc) { |
| 99 | mRSC = rsc; |
| 100 | |
| 101 | version_major = 0; |
| 102 | version_minor = 0; |
David Gross | 35dbc8c | 2016-03-29 13:48:41 -0700 | [diff] [blame] | 103 | mInKernel = false; |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 104 | memset(&mWorkers, 0, sizeof(mWorkers)); |
| 105 | memset(&mTlsStruct, 0, sizeof(mTlsStruct)); |
| 106 | mExit = false; |
Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 107 | mSelectRTCallback = nullptr; |
Stephen Hines | 8409d64 | 2015-04-28 18:49:56 -0700 | [diff] [blame] | 108 | mEmbedGlobalInfo = true; |
| 109 | mEmbedGlobalInfoSkipConstant = true; |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | |
| 113 | void * RsdCpuReferenceImpl::helperThreadProc(void *vrsc) { |
| 114 | RsdCpuReferenceImpl *dc = (RsdCpuReferenceImpl *)vrsc; |
| 115 | |
Tim Murray | 0b575de | 2013-03-15 15:56:43 -0700 | [diff] [blame] | 116 | uint32_t idx = __sync_fetch_and_add(&dc->mWorkers.mLaunchCount, 1); |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 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 | } |
Tim Murray | 0b575de | 2013-03-15 15:56:43 -0700 | [diff] [blame] | 145 | __sync_fetch_and_sub(&dc->mWorkers.mRunningCount, 1); |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 146 | dc->mWorkers.mCompleteSignal.set(); |
| 147 | } |
| 148 | |
| 149 | //ALOGV("RS helperThread exited %p idx=%i", dc, idx); |
Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 150 | return nullptr; |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 151 | } |
| 152 | |
Matt Wala | 14ce007 | 2015-07-30 17:30:25 -0700 | [diff] [blame] | 153 | // Launch a kernel. |
| 154 | // The callback function is called to execute the kernel. |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 155 | void RsdCpuReferenceImpl::launchThreads(WorkerCallback_t cbk, void *data) { |
| 156 | mWorkers.mLaunchData = data; |
| 157 | mWorkers.mLaunchCallback = cbk; |
Tim Murray | 4d252d6 | 2012-11-29 14:37:59 -0800 | [diff] [blame] | 158 | |
| 159 | // fast path for very small launches |
Matt Wala | 14ce007 | 2015-07-30 17:30:25 -0700 | [diff] [blame] | 160 | MTLaunchStructCommon *mtls = (MTLaunchStructCommon *)data; |
| 161 | if (mtls && mtls->dimPtr->y <= 1 && mtls->end.x <= mtls->start.x + mtls->mSliceSize) { |
Tim Murray | 4d252d6 | 2012-11-29 14:37:59 -0800 | [diff] [blame] | 162 | if (mWorkers.mLaunchCallback) { |
| 163 | mWorkers.mLaunchCallback(mWorkers.mLaunchData, 0); |
| 164 | } |
| 165 | return; |
| 166 | } |
| 167 | |
Tim Murray | 0b575de | 2013-03-15 15:56:43 -0700 | [diff] [blame] | 168 | mWorkers.mRunningCount = mWorkers.mCount; |
| 169 | __sync_synchronize(); |
| 170 | |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 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) { |
Tim Murray | 4d252d6 | 2012-11-29 14:37:59 -0800 | [diff] [blame] | 178 | mWorkers.mLaunchCallback(mWorkers.mLaunchData, 0); |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 179 | } |
| 180 | |
Tim Murray | 0b575de | 2013-03-15 15:56:43 -0700 | [diff] [blame] | 181 | while (__sync_fetch_and_or(&mWorkers.mRunningCount, 0) != 0) { |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 182 | mWorkers.mCompleteSignal.wait(); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | |
| 187 | void RsdCpuReferenceImpl::lockMutex() { |
| 188 | pthread_mutex_lock(&gInitMutex); |
| 189 | } |
| 190 | |
| 191 | void RsdCpuReferenceImpl::unlockMutex() { |
| 192 | pthread_mutex_unlock(&gInitMutex); |
| 193 | } |
| 194 | |
Matt Wala | 11fd9ec | 2015-07-10 16:40:12 -0700 | [diff] [blame] | 195 | // Determine if the CPU we're running on supports SIMD instructions. |
Jason Sams | f5ef8df | 2013-08-06 13:49:25 -0700 | [diff] [blame] | 196 | static void GetCpuInfo() { |
Matt Wala | 11fd9ec | 2015-07-10 16:40:12 -0700 | [diff] [blame] | 197 | // Read the CPU flags from /proc/cpuinfo. |
| 198 | FILE *cpuinfo = fopen("/proc/cpuinfo", "r"); |
Jason Sams | f5ef8df | 2013-08-06 13:49:25 -0700 | [diff] [blame] | 199 | |
Matt Wala | 11fd9ec | 2015-07-10 16:40:12 -0700 | [diff] [blame] | 200 | if (!cpuinfo) { |
Jason Sams | f5ef8df | 2013-08-06 13:49:25 -0700 | [diff] [blame] | 201 | return; |
| 202 | } |
| 203 | |
Matt Wala | 11fd9ec | 2015-07-10 16:40:12 -0700 | [diff] [blame] | 204 | char cpuinfostr[4096]; |
Miao Wang | 5d70cb5 | 2015-07-17 11:53:04 -0700 | [diff] [blame] | 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 | } |
Matt Wala | 11fd9ec | 2015-07-10 16:40:12 -0700 | [diff] [blame] | 216 | } |
| 217 | fclose(cpuinfo); |
Jason Sams | f5ef8df | 2013-08-06 13:49:25 -0700 | [diff] [blame] | 218 | } |
Jason Sams | f5ef8df | 2013-08-06 13:49:25 -0700 | [diff] [blame] | 219 | |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 220 | bool RsdCpuReferenceImpl::init(uint32_t version_major, uint32_t version_minor, |
| 221 | sym_lookup_t lfn, script_lookup_t slfn) { |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 222 | mSymLookupFn = lfn; |
| 223 | mScriptLookupFn = slfn; |
| 224 | |
| 225 | lockMutex(); |
| 226 | if (!gThreadTLSKeyCount) { |
Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 227 | int status = pthread_key_create(&gThreadTLSKey, nullptr); |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 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; |
Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 238 | mTlsStruct.mScript = nullptr; |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 239 | int status = pthread_setspecific(gThreadTLSKey, &mTlsStruct); |
| 240 | if (status) { |
| 241 | ALOGE("pthread_setspecific %i", status); |
| 242 | } |
| 243 | |
David Gross | 35dbc8c | 2016-03-29 13:48:41 -0700 | [diff] [blame] | 244 | mPageSize = sysconf(_SC_PAGE_SIZE); |
David Gross | 013ff53 | 2016-04-01 12:46:58 -0700 | [diff] [blame] | 245 | // ALOGV("page size = %ld", mPageSize); |
David Gross | 35dbc8c | 2016-03-29 13:48:41 -0700 | [diff] [blame] | 246 | |
Jason Sams | f5ef8df | 2013-08-06 13:49:25 -0700 | [diff] [blame] | 247 | GetCpuInfo(); |
Jason Sams | f5ef8df | 2013-08-06 13:49:25 -0700 | [diff] [blame] | 248 | |
Jason Sams | 77d57a3 | 2014-10-23 17:43:53 -0700 | [diff] [blame] | 249 | int cpu = sysconf(_SC_NPROCESSORS_CONF); |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 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 | |
Yang Ni | 554054c | 2016-04-20 09:04:02 -0700 | [diff] [blame] | 261 | if (mRSC->props.mLogScripts) { |
| 262 | ALOGV("%p Launching thread(s), CPUs %i", mRSC, mWorkers.mCount + 1); |
| 263 | } |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 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]; |
Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 268 | mWorkers.mLaunchCallback = nullptr; |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 269 | |
| 270 | mWorkers.mCompleteSignal.init(); |
| 271 | |
Tim Murray | 0b575de | 2013-03-15 15:56:43 -0700 | [diff] [blame] | 272 | mWorkers.mRunningCount = mWorkers.mCount; |
| 273 | mWorkers.mLaunchCount = 0; |
| 274 | __sync_synchronize(); |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 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 | } |
Tim Murray | 0b575de | 2013-03-15 15:56:43 -0700 | [diff] [blame] | 291 | while (__sync_fetch_and_or(&mWorkers.mRunningCount, 0) != 0) { |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 292 | usleep(100); |
| 293 | } |
| 294 | |
| 295 | pthread_attr_destroy(&threadAttr); |
| 296 | return true; |
| 297 | } |
| 298 | |
| 299 | |
| 300 | void 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 | |
| 306 | RsdCpuReferenceImpl::~RsdCpuReferenceImpl() { |
| 307 | mExit = true; |
Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 308 | mWorkers.mLaunchData = nullptr; |
| 309 | mWorkers.mLaunchCallback = nullptr; |
Tim Murray | 0b575de | 2013-03-15 15:56:43 -0700 | [diff] [blame] | 310 | mWorkers.mRunningCount = mWorkers.mCount; |
| 311 | __sync_synchronize(); |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 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 | } |
Miao Wang | 19601aa | 2016-04-21 20:36:57 -0700 | [diff] [blame] | 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); |
Jens Gulin | 07ef704 | 2014-02-19 18:16:01 +0100 | [diff] [blame] | 323 | free(mWorkers.mThreadId); |
| 324 | free(mWorkers.mNativeThreadId); |
| 325 | delete[] mWorkers.mLaunchSignals; |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 326 | |
| 327 | // Global structure cleanup. |
| 328 | lockMutex(); |
| 329 | --gThreadTLSKeyCount; |
| 330 | if (!gThreadTLSKeyCount) { |
| 331 | pthread_key_delete(gThreadTLSKey); |
| 332 | } |
| 333 | unlockMutex(); |
| 334 | |
| 335 | } |
| 336 | |
Matt Wala | 14ce007 | 2015-07-30 17:30:25 -0700 | [diff] [blame] | 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 |
| 342 | static inline void FepPtrSetup(const MTLaunchStructForEach *mtls, RsExpandKernelDriverInfo *fep, |
Jason Sams | c0d6847 | 2015-01-20 14:29:52 -0800 | [diff] [blame] | 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) { |
Jason Sams | c0d6847 | 2015-01-20 14:29:52 -0800 | [diff] [blame] | 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 | } |
Jason Sams | c0d6847 | 2015-01-20 14:29:52 -0800 | [diff] [blame] | 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 | |
David Gross | 6c1876b | 2016-01-15 11:52:14 -0800 | [diff] [blame] | 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 |
David Gross | ae2ec3f | 2016-06-01 14:45:47 -0700 | [diff] [blame] | 360 | static inline void RedpPtrSetup(const MTLaunchStructReduce *mtls, RsExpandKernelDriverInfo *redp, |
David Gross | 6c1876b | 2016-01-15 11:52:14 -0800 | [diff] [blame] | 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 | |
Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 367 | static 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 | |
David Gross | 6c1876b | 2016-01-15 11:52:14 -0800 | [diff] [blame] | 380 | static bool SelectOuterSlice(const MTLaunchStructCommon *mtls, RsExpandKernelDriverInfo* info, uint32_t sliceNum) { |
Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 381 | uint32_t r = sliceNum; |
David Gross | 6c1876b | 2016-01-15 11:52:14 -0800 | [diff] [blame] | 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]); |
Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 389 | return r == 0; |
| 390 | } |
| 391 | |
David Gross | f9bd1f2 | 2016-04-06 16:45:26 -0700 | [diff] [blame] | 392 | static 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 | } |
Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 395 | |
David Gross | f9bd1f2 | 2016-04-06 16:45:26 -0700 | [diff] [blame] | 396 | static void walk_general_foreach(void *usr, uint32_t idx) { |
Matt Wala | 14ce007 | 2015-07-30 17:30:25 -0700 | [diff] [blame] | 397 | MTLaunchStructForEach *mtls = (MTLaunchStructForEach *)usr; |
Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 398 | RsExpandKernelDriverInfo fep = mtls->fep; |
| 399 | fep.lid = idx; |
Matt Wala | 14ce007 | 2015-07-30 17:30:25 -0700 | [diff] [blame] | 400 | ForEachFunc_t fn = mtls->kernel; |
Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 401 | |
Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 402 | while(1) { |
| 403 | uint32_t slice = (uint32_t)__sync_fetch_and_add(&mtls->mSliceNum, 1); |
| 404 | |
Jason Sams | 59b35f2 | 2015-03-17 12:59:17 -0700 | [diff] [blame] | 405 | if (!SelectOuterSlice(mtls, &fep, slice)) { |
Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 406 | return; |
| 407 | } |
| 408 | |
Jason Sams | 59b35f2 | 2015-03-17 12:59:17 -0700 | [diff] [blame] | 409 | for (fep.current.y = mtls->start.y; fep.current.y < mtls->end.y; |
| 410 | fep.current.y++) { |
Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 411 | |
Jason Sams | 59b35f2 | 2015-03-17 12:59:17 -0700 | [diff] [blame] | 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]); |
Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 417 | |
Jason Sams | 59b35f2 | 2015-03-17 12:59:17 -0700 | [diff] [blame] | 418 | fn(&fep, mtls->start.x, mtls->end.x, mtls->fep.outStride[0]); |
Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 419 | } |
| 420 | } |
Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 421 | } |
Jason Sams | c0d6847 | 2015-01-20 14:29:52 -0800 | [diff] [blame] | 422 | |
David Gross | f9bd1f2 | 2016-04-06 16:45:26 -0700 | [diff] [blame] | 423 | static void walk_2d_foreach(void *usr, uint32_t idx) { |
Matt Wala | 14ce007 | 2015-07-30 17:30:25 -0700 | [diff] [blame] | 424 | MTLaunchStructForEach *mtls = (MTLaunchStructForEach *)usr; |
Jason Sams | c0d6847 | 2015-01-20 14:29:52 -0800 | [diff] [blame] | 425 | RsExpandKernelDriverInfo fep = mtls->fep; |
| 426 | fep.lid = idx; |
Matt Wala | 14ce007 | 2015-07-30 17:30:25 -0700 | [diff] [blame] | 427 | ForEachFunc_t fn = mtls->kernel; |
Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 428 | |
Jason Sams | c0d6847 | 2015-01-20 14:29:52 -0800 | [diff] [blame] | 429 | while (1) { |
| 430 | uint32_t slice = (uint32_t)__sync_fetch_and_add(&mtls->mSliceNum, 1); |
Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 431 | uint32_t yStart = mtls->start.y + slice * mtls->mSliceSize; |
Jason Sams | c0d6847 | 2015-01-20 14:29:52 -0800 | [diff] [blame] | 432 | uint32_t yEnd = yStart + mtls->mSliceSize; |
Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 433 | |
Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 434 | yEnd = rsMin(yEnd, mtls->end.y); |
Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 435 | |
Jason Sams | c0d6847 | 2015-01-20 14:29:52 -0800 | [diff] [blame] | 436 | if (yEnd <= yStart) { |
| 437 | return; |
Stephen Hines | 4b2bea3 | 2014-08-13 17:32:10 +0000 | [diff] [blame] | 438 | } |
Jason Sams | c0d6847 | 2015-01-20 14:29:52 -0800 | [diff] [blame] | 439 | |
| 440 | for (fep.current.y = yStart; fep.current.y < yEnd; fep.current.y++) { |
Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 441 | FepPtrSetup(mtls, &fep, mtls->start.x, fep.current.y); |
Jason Sams | c0d6847 | 2015-01-20 14:29:52 -0800 | [diff] [blame] | 442 | |
David Gross | b0abb14 | 2015-03-12 15:23:03 -0700 | [diff] [blame] | 443 | fn(&fep, mtls->start.x, mtls->end.x, fep.outStride[0]); |
Jason Sams | c0d6847 | 2015-01-20 14:29:52 -0800 | [diff] [blame] | 444 | } |
| 445 | } |
Stephen Hines | 4b2bea3 | 2014-08-13 17:32:10 +0000 | [diff] [blame] | 446 | } |
| 447 | |
David Gross | 35dbc8c | 2016-03-29 13:48:41 -0700 | [diff] [blame] | 448 | static void walk_1d_foreach(void *usr, uint32_t idx) { |
Matt Wala | 14ce007 | 2015-07-30 17:30:25 -0700 | [diff] [blame] | 449 | MTLaunchStructForEach *mtls = (MTLaunchStructForEach *)usr; |
Jason Sams | c0d6847 | 2015-01-20 14:29:52 -0800 | [diff] [blame] | 450 | RsExpandKernelDriverInfo fep = mtls->fep; |
| 451 | fep.lid = idx; |
Matt Wala | 14ce007 | 2015-07-30 17:30:25 -0700 | [diff] [blame] | 452 | ForEachFunc_t fn = mtls->kernel; |
Chris Wailes | f371213 | 2014-07-16 15:18:30 -0700 | [diff] [blame] | 453 | |
Jason Sams | c0d6847 | 2015-01-20 14:29:52 -0800 | [diff] [blame] | 454 | while (1) { |
| 455 | uint32_t slice = (uint32_t)__sync_fetch_and_add(&mtls->mSliceNum, 1); |
Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 456 | uint32_t xStart = mtls->start.x + slice * mtls->mSliceSize; |
Jason Sams | c0d6847 | 2015-01-20 14:29:52 -0800 | [diff] [blame] | 457 | uint32_t xEnd = xStart + mtls->mSliceSize; |
Chris Wailes | f371213 | 2014-07-16 15:18:30 -0700 | [diff] [blame] | 458 | |
Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 459 | xEnd = rsMin(xEnd, mtls->end.x); |
Chris Wailes | f371213 | 2014-07-16 15:18:30 -0700 | [diff] [blame] | 460 | |
Jason Sams | c0d6847 | 2015-01-20 14:29:52 -0800 | [diff] [blame] | 461 | if (xEnd <= xStart) { |
| 462 | return; |
Chris Wailes | f371213 | 2014-07-16 15:18:30 -0700 | [diff] [blame] | 463 | } |
Jason Sams | c0d6847 | 2015-01-20 14:29:52 -0800 | [diff] [blame] | 464 | |
Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 465 | FepPtrSetup(mtls, &fep, xStart, 0); |
Jason Sams | c0d6847 | 2015-01-20 14:29:52 -0800 | [diff] [blame] | 466 | |
David Gross | b0abb14 | 2015-03-12 15:23:03 -0700 | [diff] [blame] | 467 | fn(&fep, xStart, xEnd, fep.outStride[0]); |
Jason Sams | c0d6847 | 2015-01-20 14:29:52 -0800 | [diff] [blame] | 468 | } |
Chris Wailes | f371213 | 2014-07-16 15:18:30 -0700 | [diff] [blame] | 469 | } |
| 470 | |
David Gross | 35dbc8c | 2016-03-29 13:48:41 -0700 | [diff] [blame] | 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 | // |
| 494 | static const int kFormatInBytesMax = 16; |
| 495 | // ": " + 2 digits per byte + 1 separator between bytes + "..." + null |
| 496 | typedef char FormatBuf[2 + kFormatInBytesMax*2 + (kFormatInBytesMax - 1) + 3 + 1]; |
| 497 | static 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 | |
David Gross | ae2ec3f | 2016-06-01 14:45:47 -0700 | [diff] [blame] | 514 | static void reduce_get_accumulator(uint8_t *&accumPtr, const MTLaunchStructReduce *mtls, |
| 515 | const char *walkerName, uint32_t threadIdx) { |
David Gross | f9bd1f2 | 2016-04-06 16:45:26 -0700 | [diff] [blame] | 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 | } |
David Gross | ae2ec3f | 2016-06-01 14:45:47 -0700 | [diff] [blame] | 528 | REDUCE_ALOGV(mtls, 2, "%s(%p): idx = %u got accumCount %u and accumPtr %p", |
| 529 | walkerName, mtls->accumFunc, threadIdx, accumIdx, accumPtr); |
David Gross | f9bd1f2 | 2016-04-06 16:45:26 -0700 | [diff] [blame] | 530 | // initialize accumulator |
| 531 | if (mtls->initFunc) { |
| 532 | mtls->initFunc(accumPtr); |
| 533 | } else { |
| 534 | memset(accumPtr, 0, mtls->accumSize); |
| 535 | } |
| 536 | } |
| 537 | |
David Gross | ae2ec3f | 2016-06-01 14:45:47 -0700 | [diff] [blame] | 538 | static void walk_1d_reduce(void *usr, uint32_t idx) { |
| 539 | const MTLaunchStructReduce *mtls = (const MTLaunchStructReduce *)usr; |
David Gross | 35dbc8c | 2016-03-29 13:48:41 -0700 | [diff] [blame] | 540 | RsExpandKernelDriverInfo redp = mtls->redp; |
| 541 | |
| 542 | // find accumulator |
| 543 | uint8_t *&accumPtr = mtls->accumPtr[idx]; |
| 544 | if (!accumPtr) { |
David Gross | ae2ec3f | 2016-06-01 14:45:47 -0700 | [diff] [blame] | 545 | reduce_get_accumulator(accumPtr, mtls, __func__, idx); |
David Gross | 35dbc8c | 2016-03-29 13:48:41 -0700 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | // accumulate |
David Gross | ae2ec3f | 2016-06-01 14:45:47 -0700 | [diff] [blame] | 549 | const ReduceAccumulatorFunc_t fn = mtls->accumFunc; |
David Gross | 35dbc8c | 2016-03-29 13:48:41 -0700 | [diff] [blame] | 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 | |
David Gross | f9bd1f2 | 2016-04-06 16:45:26 -0700 | [diff] [blame] | 564 | // Emit log line after slice has been run, so that we can include |
| 565 | // the results of the run on that line. |
David Gross | 35dbc8c | 2016-03-29 13:48:41 -0700 | [diff] [blame] | 566 | FormatBuf fmt; |
David Gross | 013ff53 | 2016-04-01 12:46:58 -0700 | [diff] [blame] | 567 | if (mtls->logReduce >= 3) { |
David Gross | 35dbc8c | 2016-03-29 13:48:41 -0700 | [diff] [blame] | 568 | format_bytes(&fmt, accumPtr, mtls->accumSize); |
| 569 | } else { |
| 570 | fmt[0] = 0; |
| 571 | } |
David Gross | ae2ec3f | 2016-06-01 14:45:47 -0700 | [diff] [blame] | 572 | REDUCE_ALOGV(mtls, 2, "walk_1d_reduce(%p): idx = %u, x in [%u, %u)%s", |
| 573 | mtls->accumFunc, idx, xStart, xEnd, fmt); |
David Gross | 35dbc8c | 2016-03-29 13:48:41 -0700 | [diff] [blame] | 574 | } |
| 575 | } |
| 576 | |
David Gross | ae2ec3f | 2016-06-01 14:45:47 -0700 | [diff] [blame] | 577 | static void walk_2d_reduce(void *usr, uint32_t idx) { |
| 578 | const MTLaunchStructReduce *mtls = (const MTLaunchStructReduce *)usr; |
David Gross | f9bd1f2 | 2016-04-06 16:45:26 -0700 | [diff] [blame] | 579 | RsExpandKernelDriverInfo redp = mtls->redp; |
| 580 | |
| 581 | // find accumulator |
| 582 | uint8_t *&accumPtr = mtls->accumPtr[idx]; |
| 583 | if (!accumPtr) { |
David Gross | ae2ec3f | 2016-06-01 14:45:47 -0700 | [diff] [blame] | 584 | reduce_get_accumulator(accumPtr, mtls, __func__, idx); |
David Gross | f9bd1f2 | 2016-04-06 16:45:26 -0700 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | // accumulate |
David Gross | ae2ec3f | 2016-06-01 14:45:47 -0700 | [diff] [blame] | 588 | const ReduceAccumulatorFunc_t fn = mtls->accumFunc; |
David Gross | f9bd1f2 | 2016-04-06 16:45:26 -0700 | [diff] [blame] | 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 | } |
David Gross | ae2ec3f | 2016-06-01 14:45:47 -0700 | [diff] [blame] | 611 | REDUCE_ALOGV(mtls, 2, "walk_2d_reduce(%p): idx = %u, y in [%u, %u)%s", |
| 612 | mtls->accumFunc, idx, yStart, yEnd, fmt); |
David Gross | f9bd1f2 | 2016-04-06 16:45:26 -0700 | [diff] [blame] | 613 | } |
| 614 | } |
| 615 | |
David Gross | ae2ec3f | 2016-06-01 14:45:47 -0700 | [diff] [blame] | 616 | static void walk_3d_reduce(void *usr, uint32_t idx) { |
| 617 | const MTLaunchStructReduce *mtls = (const MTLaunchStructReduce *)usr; |
David Gross | f9bd1f2 | 2016-04-06 16:45:26 -0700 | [diff] [blame] | 618 | RsExpandKernelDriverInfo redp = mtls->redp; |
| 619 | |
| 620 | // find accumulator |
| 621 | uint8_t *&accumPtr = mtls->accumPtr[idx]; |
| 622 | if (!accumPtr) { |
David Gross | ae2ec3f | 2016-06-01 14:45:47 -0700 | [diff] [blame] | 623 | reduce_get_accumulator(accumPtr, mtls, __func__, idx); |
David Gross | f9bd1f2 | 2016-04-06 16:45:26 -0700 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | // accumulate |
David Gross | ae2ec3f | 2016-06-01 14:45:47 -0700 | [diff] [blame] | 627 | const ReduceAccumulatorFunc_t fn = mtls->accumFunc; |
David Gross | f9bd1f2 | 2016-04-06 16:45:26 -0700 | [diff] [blame] | 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 | } |
David Gross | ae2ec3f | 2016-06-01 14:45:47 -0700 | [diff] [blame] | 646 | REDUCE_ALOGV(mtls, 2, "walk_3d_reduce(%p): idx = %u, z = %u%s", |
| 647 | mtls->accumFunc, idx, redp.current.z, fmt); |
David Gross | f9bd1f2 | 2016-04-06 16:45:26 -0700 | [diff] [blame] | 648 | } |
| 649 | } |
| 650 | |
David Gross | 6c1876b | 2016-01-15 11:52:14 -0800 | [diff] [blame] | 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 |
David Gross | ae2ec3f | 2016-06-01 14:45:47 -0700 | [diff] [blame] | 656 | void RsdCpuReferenceImpl::launchReduce(const Allocation ** ains, |
| 657 | uint32_t inLen, |
| 658 | Allocation * aout, |
| 659 | MTLaunchStructReduce *mtls) { |
David Gross | 013ff53 | 2016-04-01 12:46:58 -0700 | [diff] [blame] | 660 | mtls->logReduce = mRSC->props.mLogReduce; |
David Gross | 35dbc8c | 2016-03-29 13:48:41 -0700 | [diff] [blame] | 661 | if ((mWorkers.mCount >= 1) && mtls->isThreadable && !mInKernel) { |
David Gross | ae2ec3f | 2016-06-01 14:45:47 -0700 | [diff] [blame] | 662 | launchReduceParallel(ains, inLen, aout, mtls); |
David Gross | 35dbc8c | 2016-03-29 13:48:41 -0700 | [diff] [blame] | 663 | } else { |
David Gross | ae2ec3f | 2016-06-01 14:45:47 -0700 | [diff] [blame] | 664 | launchReduceSerial(ains, inLen, aout, mtls); |
David Gross | 35dbc8c | 2016-03-29 13:48:41 -0700 | [diff] [blame] | 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 |
David Gross | ae2ec3f | 2016-06-01 14:45:47 -0700 | [diff] [blame] | 673 | void 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); |
David Gross | 35dbc8c | 2016-03-29 13:48:41 -0700 | [diff] [blame] | 679 | |
David Gross | 6c1876b | 2016-01-15 11:52:14 -0800 | [diff] [blame] | 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 |
David Gross | ae2ec3f | 2016-06-01 14:45:47 -0700 | [diff] [blame] | 697 | const ReduceAccumulatorFunc_t fn = mtls->accumFunc; |
David Gross | 6c1876b | 2016-01-15 11:52:14 -0800 | [diff] [blame] | 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 | |
David Gross | 35dbc8c | 2016-03-29 13:48:41 -0700 | [diff] [blame] | 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 |
David Gross | ae2ec3f | 2016-06-01 14:45:47 -0700 | [diff] [blame] | 720 | void RsdCpuReferenceImpl::launchReduceParallel(const Allocation ** ains, |
| 721 | uint32_t inLen, |
| 722 | Allocation * aout, |
| 723 | MTLaunchStructReduce *mtls) { |
David Gross | f9bd1f2 | 2016-04-06 16:45:26 -0700 | [diff] [blame] | 724 | // For now, we don't know how to go parallel in the absence of a combiner. |
| 725 | if (!mtls->combFunc) { |
David Gross | ae2ec3f | 2016-06-01 14:45:47 -0700 | [diff] [blame] | 726 | launchReduceSerial(ains, inLen, aout, mtls); |
David Gross | 35dbc8c | 2016-03-29 13:48:41 -0700 | [diff] [blame] | 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; |
David Gross | ae2ec3f | 2016-06-01 14:45:47 -0700 | [diff] [blame] | 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); |
David Gross | f9bd1f2 | 2016-04-06 16:45:26 -0700 | [diff] [blame] | 768 | if (mtls->redp.dim.z > 1) { |
| 769 | mtls->mSliceSize = 1; |
David Gross | ae2ec3f | 2016-06-01 14:45:47 -0700 | [diff] [blame] | 770 | launchThreads(walk_3d_reduce, mtls); |
David Gross | f9bd1f2 | 2016-04-06 16:45:26 -0700 | [diff] [blame] | 771 | } else if (mtls->redp.dim.y > 1) { |
| 772 | mtls->mSliceSize = rsMax(1U, mtls->redp.dim.y / (numThreads * 4)); |
David Gross | ae2ec3f | 2016-06-01 14:45:47 -0700 | [diff] [blame] | 773 | launchThreads(walk_2d_reduce, mtls); |
David Gross | f9bd1f2 | 2016-04-06 16:45:26 -0700 | [diff] [blame] | 774 | } else { |
| 775 | mtls->mSliceSize = rsMax(1U, mtls->redp.dim.x / (numThreads * 4)); |
David Gross | ae2ec3f | 2016-06-01 14:45:47 -0700 | [diff] [blame] | 776 | launchThreads(walk_1d_reduce, mtls); |
David Gross | f9bd1f2 | 2016-04-06 16:45:26 -0700 | [diff] [blame] | 777 | } |
David Gross | 35dbc8c | 2016-03-29 13:48:41 -0700 | [diff] [blame] | 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) { |
David Gross | 013ff53 | 2016-04-01 12:46:58 -0700 | [diff] [blame] | 789 | if (mtls->logReduce >= 3) { |
David Gross | 35dbc8c | 2016-03-29 13:48:41 -0700 | [diff] [blame] | 790 | FormatBuf fmt; |
David Gross | ae2ec3f | 2016-06-01 14:45:47 -0700 | [diff] [blame] | 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)); |
David Gross | 35dbc8c | 2016-03-29 13:48:41 -0700 | [diff] [blame] | 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); |
David Gross | 013ff53 | 2016-04-01 12:46:58 -0700 | [diff] [blame] | 808 | if (mtls->logReduce >= 3) { |
David Gross | 35dbc8c | 2016-03-29 13:48:41 -0700 | [diff] [blame] | 809 | FormatBuf fmt; |
David Gross | ae2ec3f | 2016-06-01 14:45:47 -0700 | [diff] [blame] | 810 | REDUCE_ALOGV(mtls, 3, "launchReduceParallel(%p): final accumulator%s", |
| 811 | mtls->accumFunc, format_bytes(&fmt, finalAccumPtr, mtls->accumSize)); |
David Gross | 35dbc8c | 2016-03-29 13:48:41 -0700 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | // Outconvert |
| 815 | if (mtls->outFunc) { |
| 816 | mtls->outFunc(mtls->redp.outPtr[0], finalAccumPtr); |
David Gross | 013ff53 | 2016-04-01 12:46:58 -0700 | [diff] [blame] | 817 | if (mtls->logReduce >= 3) { |
David Gross | 35dbc8c | 2016-03-29 13:48:41 -0700 | [diff] [blame] | 818 | FormatBuf fmt; |
David Gross | ae2ec3f | 2016-06-01 14:45:47 -0700 | [diff] [blame] | 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])); |
David Gross | 35dbc8c | 2016-03-29 13:48:41 -0700 | [diff] [blame] | 822 | } |
| 823 | } |
| 824 | |
| 825 | // Clean up |
| 826 | free(mtls->accumPtr); |
| 827 | free(mtls->accumAlloc); |
| 828 | } |
| 829 | |
| 830 | |
Matt Wala | 14ce007 | 2015-07-30 17:30:25 -0700 | [diff] [blame] | 831 | void RsdCpuReferenceImpl::launchForEach(const Allocation ** ains, |
Chris Wailes | f371213 | 2014-07-16 15:18:30 -0700 | [diff] [blame] | 832 | uint32_t inLen, |
| 833 | Allocation* aout, |
| 834 | const RsScriptCall* sc, |
Matt Wala | 14ce007 | 2015-07-30 17:30:25 -0700 | [diff] [blame] | 835 | MTLaunchStructForEach* mtls) { |
Stephen Hines | 4b2bea3 | 2014-08-13 17:32:10 +0000 | [diff] [blame] | 836 | |
| 837 | //android::StopWatch kernel_time("kernel time"); |
| 838 | |
Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 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 | |
David Gross | 35dbc8c | 2016-03-29 13:48:41 -0700 | [diff] [blame] | 847 | if ((mWorkers.mCount >= 1) && mtls->isThreadable && !mInKernel) { |
Stephen Hines | 4b2bea3 | 2014-08-13 17:32:10 +0000 | [diff] [blame] | 848 | const size_t targetByteChunk = 16 * 1024; |
David Gross | 35dbc8c | 2016-03-29 13:48:41 -0700 | [diff] [blame] | 849 | mInKernel = true; // NOTE: The guard immediately above ensures this was !mInKernel |
Chris Wailes | f371213 | 2014-07-16 15:18:30 -0700 | [diff] [blame] | 850 | |
Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 851 | if (outerDims) { |
| 852 | // No fancy logic for chunk size |
| 853 | mtls->mSliceSize = 1; |
David Gross | f9bd1f2 | 2016-04-06 16:45:26 -0700 | [diff] [blame] | 854 | launchThreads(walk_general_foreach, mtls); |
Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 855 | } else if (mtls->fep.dim.y > 1) { |
Jason Sams | c0d6847 | 2015-01-20 14:29:52 -0800 | [diff] [blame] | 856 | uint32_t s1 = mtls->fep.dim.y / ((mWorkers.mCount + 1) * 4); |
Stephen Hines | 4b2bea3 | 2014-08-13 17:32:10 +0000 | [diff] [blame] | 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. |
Jason Sams | c0d6847 | 2015-01-20 14:29:52 -0800 | [diff] [blame] | 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; |
Jason Sams | a9139c7 | 2015-04-16 15:11:04 -0700 | [diff] [blame] | 863 | } else if (mtls->ains[0]) { |
Jason Sams | c0d6847 | 2015-01-20 14:29:52 -0800 | [diff] [blame] | 864 | s2 = targetByteChunk / mtls->ains[0]->mHal.drvState.lod[0].stride; |
Jason Sams | a9139c7 | 2015-04-16 15:11:04 -0700 | [diff] [blame] | 865 | } else { |
| 866 | // Launch option only case |
| 867 | // Use s1 based only on the dimensions |
| 868 | s2 = s1; |
Stephen Hines | 4b2bea3 | 2014-08-13 17:32:10 +0000 | [diff] [blame] | 869 | } |
| 870 | mtls->mSliceSize = rsMin(s1, s2); |
| 871 | |
| 872 | if(mtls->mSliceSize < 1) { |
| 873 | mtls->mSliceSize = 1; |
| 874 | } |
| 875 | |
David Gross | f9bd1f2 | 2016-04-06 16:45:26 -0700 | [diff] [blame] | 876 | launchThreads(walk_2d_foreach, mtls); |
Stephen Hines | 4b2bea3 | 2014-08-13 17:32:10 +0000 | [diff] [blame] | 877 | } else { |
Jason Sams | c0d6847 | 2015-01-20 14:29:52 -0800 | [diff] [blame] | 878 | uint32_t s1 = mtls->fep.dim.x / ((mWorkers.mCount + 1) * 4); |
Stephen Hines | 4b2bea3 | 2014-08-13 17:32:10 +0000 | [diff] [blame] | 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. |
Jason Sams | c0d6847 | 2015-01-20 14:29:52 -0800 | [diff] [blame] | 883 | if ((mtls->aout[0] != nullptr) && mtls->aout[0]->getType()->getElementSizeBytes()) { |
| 884 | s2 = targetByteChunk / mtls->aout[0]->getType()->getElementSizeBytes(); |
Jason Sams | a9139c7 | 2015-04-16 15:11:04 -0700 | [diff] [blame] | 885 | } else if (mtls->ains[0]) { |
Jason Sams | c0d6847 | 2015-01-20 14:29:52 -0800 | [diff] [blame] | 886 | s2 = targetByteChunk / mtls->ains[0]->getType()->getElementSizeBytes(); |
Jason Sams | a9139c7 | 2015-04-16 15:11:04 -0700 | [diff] [blame] | 887 | } else { |
| 888 | // Launch option only case |
| 889 | // Use s1 based only on the dimensions |
| 890 | s2 = s1; |
Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 891 | } |
| 892 | mtls->mSliceSize = rsMin(s1, s2); |
| 893 | |
| 894 | if (mtls->mSliceSize < 1) { |
| 895 | mtls->mSliceSize = 1; |
| 896 | } |
| 897 | |
David Gross | 35dbc8c | 2016-03-29 13:48:41 -0700 | [diff] [blame] | 898 | launchThreads(walk_1d_foreach, mtls); |
Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 899 | } |
David Gross | 35dbc8c | 2016-03-29 13:48:41 -0700 | [diff] [blame] | 900 | mInKernel = false; |
Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 901 | |
Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 902 | } else { |
Matt Wala | 14ce007 | 2015-07-30 17:30:25 -0700 | [diff] [blame] | 903 | ForEachFunc_t fn = mtls->kernel; |
Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 904 | uint32_t slice = 0; |
Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 905 | |
Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 906 | |
Jason Sams | 59b35f2 | 2015-03-17 12:59:17 -0700 | [diff] [blame] | 907 | while(SelectOuterSlice(mtls, &mtls->fep, slice++)) { |
Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 908 | for (mtls->fep.current.y = mtls->start.y; |
| 909 | mtls->fep.current.y < mtls->end.y; |
| 910 | mtls->fep.current.y++) { |
Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 911 | |
Jason Sams | bf2111d | 2015-01-26 18:13:41 -0800 | [diff] [blame] | 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]); |
Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 917 | |
David Gross | b0abb14 | 2015-03-12 15:23:03 -0700 | [diff] [blame] | 918 | fn(&mtls->fep, mtls->start.x, mtls->end.x, mtls->fep.outStride[0]); |
Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 919 | } |
| 920 | } |
Chris Wailes | 4b3c34e | 2014-06-11 12:00:29 -0700 | [diff] [blame] | 921 | } |
| 922 | } |
| 923 | |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 924 | RsdCpuScriptImpl * 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 { |
Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 934 | tls->mScript = nullptr; |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 935 | } |
| 936 | return old; |
| 937 | } |
| 938 | |
| 939 | const RsdCpuReference::CpuSymbol * RsdCpuReferenceImpl::symLookup(const char *name) { |
| 940 | return mSymLookupFn(mRSC, name); |
| 941 | } |
| 942 | |
| 943 | |
| 944 | RsdCpuReference::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); |
Stephen Hines | 0051132 | 2014-01-31 11:20:23 -0800 | [diff] [blame] | 950 | if (!i->init(resName, cacheDir, bitcode, bitcodeSize, flags |
Stephen Hines | 0051132 | 2014-01-31 11:20:23 -0800 | [diff] [blame] | 951 | , getBccPluginName() |
Stephen Hines | 0051132 | 2014-01-31 11:20:23 -0800 | [diff] [blame] | 952 | )) { |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 953 | delete i; |
Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 954 | return nullptr; |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 955 | } |
| 956 | return i; |
| 957 | } |
| 958 | |
Jason Sams | 7c4b888 | 2013-01-04 10:50:05 -0800 | [diff] [blame] | 959 | extern RsdCpuScriptImpl * rsdIntrinsic_3DLUT(RsdCpuReferenceImpl *ctx, |
| 960 | const Script *s, const Element *e); |
Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 961 | extern RsdCpuScriptImpl * rsdIntrinsic_Convolve3x3(RsdCpuReferenceImpl *ctx, |
| 962 | const Script *s, const Element *e); |
| 963 | extern RsdCpuScriptImpl * rsdIntrinsic_ColorMatrix(RsdCpuReferenceImpl *ctx, |
| 964 | const Script *s, const Element *e); |
| 965 | extern RsdCpuScriptImpl * rsdIntrinsic_LUT(RsdCpuReferenceImpl *ctx, |
| 966 | const Script *s, const Element *e); |
| 967 | extern RsdCpuScriptImpl * rsdIntrinsic_Convolve5x5(RsdCpuReferenceImpl *ctx, |
| 968 | const Script *s, const Element *e); |
| 969 | extern RsdCpuScriptImpl * rsdIntrinsic_Blur(RsdCpuReferenceImpl *ctx, |
| 970 | const Script *s, const Element *e); |
| 971 | extern RsdCpuScriptImpl * rsdIntrinsic_YuvToRGB(RsdCpuReferenceImpl *ctx, |
| 972 | const Script *s, const Element *e); |
| 973 | extern RsdCpuScriptImpl * rsdIntrinsic_Blend(RsdCpuReferenceImpl *ctx, |
| 974 | const Script *s, const Element *e); |
Jason Sams | 2282e28 | 2013-06-17 16:52:01 -0700 | [diff] [blame] | 975 | extern RsdCpuScriptImpl * rsdIntrinsic_Histogram(RsdCpuReferenceImpl *ctx, |
| 976 | const Script *s, const Element *e); |
Jason Sams | 39ab94a | 2014-04-16 17:14:05 -0700 | [diff] [blame] | 977 | extern RsdCpuScriptImpl * rsdIntrinsic_Resize(RsdCpuReferenceImpl *ctx, |
| 978 | const Script *s, const Element *e); |
Tim Murray | 64c682b | 2015-01-09 12:08:43 -0800 | [diff] [blame] | 979 | extern RsdCpuScriptImpl * rsdIntrinsic_BLAS(RsdCpuReferenceImpl *ctx, |
| 980 | const Script *s, const Element *e); |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 981 | |
| 982 | RsdCpuReference::CpuScript * RsdCpuReferenceImpl::createIntrinsic(const Script *s, |
| 983 | RsScriptIntrinsicID iid, Element *e) { |
| 984 | |
Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 985 | RsdCpuScriptImpl *i = nullptr; |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 986 | switch (iid) { |
Jason Sams | 7c4b888 | 2013-01-04 10:50:05 -0800 | [diff] [blame] | 987 | case RS_SCRIPT_INTRINSIC_ID_3DLUT: |
| 988 | i = rsdIntrinsic_3DLUT(this, s, e); |
| 989 | break; |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 990 | case RS_SCRIPT_INTRINSIC_ID_CONVOLVE_3x3: |
Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 991 | i = rsdIntrinsic_Convolve3x3(this, s, e); |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 992 | break; |
| 993 | case RS_SCRIPT_INTRINSIC_ID_COLOR_MATRIX: |
Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 994 | i = rsdIntrinsic_ColorMatrix(this, s, e); |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 995 | break; |
| 996 | case RS_SCRIPT_INTRINSIC_ID_LUT: |
Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 997 | i = rsdIntrinsic_LUT(this, s, e); |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 998 | break; |
| 999 | case RS_SCRIPT_INTRINSIC_ID_CONVOLVE_5x5: |
Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 1000 | i = rsdIntrinsic_Convolve5x5(this, s, e); |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 1001 | break; |
| 1002 | case RS_SCRIPT_INTRINSIC_ID_BLUR: |
Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 1003 | i = rsdIntrinsic_Blur(this, s, e); |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 1004 | break; |
| 1005 | case RS_SCRIPT_INTRINSIC_ID_YUV_TO_RGB: |
Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 1006 | i = rsdIntrinsic_YuvToRGB(this, s, e); |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 1007 | break; |
| 1008 | case RS_SCRIPT_INTRINSIC_ID_BLEND: |
Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 1009 | i = rsdIntrinsic_Blend(this, s, e); |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 1010 | break; |
Jason Sams | 2282e28 | 2013-06-17 16:52:01 -0700 | [diff] [blame] | 1011 | case RS_SCRIPT_INTRINSIC_ID_HISTOGRAM: |
| 1012 | i = rsdIntrinsic_Histogram(this, s, e); |
| 1013 | break; |
Jason Sams | 39ab94a | 2014-04-16 17:14:05 -0700 | [diff] [blame] | 1014 | case RS_SCRIPT_INTRINSIC_ID_RESIZE: |
| 1015 | i = rsdIntrinsic_Resize(this, s, e); |
| 1016 | break; |
Tim Murray | 64c682b | 2015-01-09 12:08:43 -0800 | [diff] [blame] | 1017 | case RS_SCRIPT_INTRINSIC_ID_BLAS: |
| 1018 | i = rsdIntrinsic_BLAS(this, s, e); |
| 1019 | break; |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 1020 | |
| 1021 | default: |
| 1022 | rsAssert(0); |
| 1023 | } |
| 1024 | |
| 1025 | return i; |
| 1026 | } |
| 1027 | |
Yang Ni | 1ffd86b | 2015-01-07 09:16:40 -0800 | [diff] [blame] | 1028 | void* 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()) { |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 1033 | delete sgi; |
Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 1034 | return nullptr; |
Yang Ni | 1ffd86b | 2015-01-07 09:16:40 -0800 | [diff] [blame] | 1035 | } |
| 1036 | return sgi; |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 1037 | } |
Yang Ni | 1ffd86b | 2015-01-07 09:16:40 -0800 | [diff] [blame] | 1038 | case ScriptGroupBase::SG_V2: { |
| 1039 | return new CpuScriptGroup2Impl(this, sg); |
| 1040 | } |
| 1041 | } |
| 1042 | return nullptr; |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 1043 | } |