rsCpuCore.cpp revision 7c4b888f2147edf99690b6af75470774ff31c43b
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 21#include <malloc.h> 22#include "rsContext.h" 23 24#include <sys/types.h> 25#include <sys/resource.h> 26#include <sched.h> 27#include <cutils/properties.h> 28#include <sys/syscall.h> 29#include <string.h> 30#include "utils/StopWatch.h" 31 32using namespace android; 33using namespace android::renderscript; 34 35typedef void (*outer_foreach_t)( 36 const android::renderscript::RsForEachStubParamStruct *, 37 uint32_t x1, uint32_t x2, 38 uint32_t instep, uint32_t outstep); 39 40 41static pthread_key_t gThreadTLSKey = 0; 42static uint32_t gThreadTLSKeyCount = 0; 43static pthread_mutex_t gInitMutex = PTHREAD_MUTEX_INITIALIZER; 44 45RsdCpuReference::~RsdCpuReference() { 46} 47 48RsdCpuReference * RsdCpuReference::create(Context *rsc, uint32_t version_major, 49 uint32_t version_minor, sym_lookup_t lfn, 50 script_lookup_t slfn) { 51 52 RsdCpuReferenceImpl *cpu = new RsdCpuReferenceImpl(rsc); 53 if (!cpu) { 54 return NULL; 55 } 56 if (!cpu->init(version_major, version_minor, lfn, slfn)) { 57 delete cpu; 58 return NULL; 59 } 60 return cpu; 61} 62 63 64Context * RsdCpuReference::getTlsContext() { 65 ScriptTLSStruct * tls = (ScriptTLSStruct *)pthread_getspecific(gThreadTLSKey); 66 return tls->mContext; 67} 68 69const Script * RsdCpuReference::getTlsScript() { 70 ScriptTLSStruct * tls = (ScriptTLSStruct *)pthread_getspecific(gThreadTLSKey); 71 return tls->mScript; 72} 73 74 75//////////////////////////////////////////////////////////// 76/// 77 78RsdCpuReferenceImpl::RsdCpuReferenceImpl(Context *rsc) { 79 mRSC = rsc; 80 81 version_major = 0; 82 version_minor = 0; 83 mInForEach = false; 84 memset(&mWorkers, 0, sizeof(mWorkers)); 85 memset(&mTlsStruct, 0, sizeof(mTlsStruct)); 86 mExit = false; 87 88} 89 90 91void * RsdCpuReferenceImpl::helperThreadProc(void *vrsc) { 92 RsdCpuReferenceImpl *dc = (RsdCpuReferenceImpl *)vrsc; 93 94 95 uint32_t idx = (uint32_t)android_atomic_inc(&dc->mWorkers.mLaunchCount); 96 97 //ALOGV("RS helperThread starting %p idx=%i", dc, idx); 98 99 dc->mWorkers.mLaunchSignals[idx].init(); 100 dc->mWorkers.mNativeThreadId[idx] = gettid(); 101 102 memset(&dc->mTlsStruct, 0, sizeof(dc->mTlsStruct)); 103 int status = pthread_setspecific(gThreadTLSKey, &dc->mTlsStruct); 104 if (status) { 105 ALOGE("pthread_setspecific %i", status); 106 } 107 108#if 0 109 typedef struct {uint64_t bits[1024 / 64]; } cpu_set_t; 110 cpu_set_t cpuset; 111 memset(&cpuset, 0, sizeof(cpuset)); 112 cpuset.bits[idx / 64] |= 1ULL << (idx % 64); 113 int ret = syscall(241, rsc->mWorkers.mNativeThreadId[idx], 114 sizeof(cpuset), &cpuset); 115 ALOGE("SETAFFINITY ret = %i %s", ret, EGLUtils::strerror(ret)); 116#endif 117 118 while (!dc->mExit) { 119 dc->mWorkers.mLaunchSignals[idx].wait(); 120 if (dc->mWorkers.mLaunchCallback) { 121 // idx +1 is used because the calling thread is always worker 0. 122 dc->mWorkers.mLaunchCallback(dc->mWorkers.mLaunchData, idx+1); 123 } 124 android_atomic_dec(&dc->mWorkers.mRunningCount); 125 dc->mWorkers.mCompleteSignal.set(); 126 } 127 128 //ALOGV("RS helperThread exited %p idx=%i", dc, idx); 129 return NULL; 130} 131 132void RsdCpuReferenceImpl::launchThreads(WorkerCallback_t cbk, void *data) { 133 mWorkers.mLaunchData = data; 134 mWorkers.mLaunchCallback = cbk; 135 136 // fast path for very small launches 137 MTLaunchStruct *mtls = (MTLaunchStruct *)data; 138 if (mtls && mtls->fep.dimY <= 1 && mtls->xEnd <= mtls->xStart + mtls->mSliceSize) { 139 if (mWorkers.mLaunchCallback) { 140 mWorkers.mLaunchCallback(mWorkers.mLaunchData, 0); 141 } 142 return; 143 } 144 145 android_atomic_release_store(mWorkers.mCount, &mWorkers.mRunningCount); 146 for (uint32_t ct = 0; ct < mWorkers.mCount; ct++) { 147 mWorkers.mLaunchSignals[ct].set(); 148 } 149 150 // We use the calling thread as one of the workers so we can start without 151 // the delay of the thread wakeup. 152 if (mWorkers.mLaunchCallback) { 153 mWorkers.mLaunchCallback(mWorkers.mLaunchData, 0); 154 } 155 156 while (android_atomic_acquire_load(&mWorkers.mRunningCount) != 0) { 157 mWorkers.mCompleteSignal.wait(); 158 } 159} 160 161 162void RsdCpuReferenceImpl::lockMutex() { 163 pthread_mutex_lock(&gInitMutex); 164} 165 166void RsdCpuReferenceImpl::unlockMutex() { 167 pthread_mutex_unlock(&gInitMutex); 168} 169 170bool RsdCpuReferenceImpl::init(uint32_t version_major, uint32_t version_minor, 171 sym_lookup_t lfn, script_lookup_t slfn) { 172 173 mSymLookupFn = lfn; 174 mScriptLookupFn = slfn; 175 176 lockMutex(); 177 if (!gThreadTLSKeyCount) { 178 int status = pthread_key_create(&gThreadTLSKey, NULL); 179 if (status) { 180 ALOGE("Failed to init thread tls key."); 181 unlockMutex(); 182 return false; 183 } 184 } 185 gThreadTLSKeyCount++; 186 unlockMutex(); 187 188 mTlsStruct.mContext = mRSC; 189 mTlsStruct.mScript = NULL; 190 int status = pthread_setspecific(gThreadTLSKey, &mTlsStruct); 191 if (status) { 192 ALOGE("pthread_setspecific %i", status); 193 } 194 195 int cpu = sysconf(_SC_NPROCESSORS_ONLN); 196 if(mRSC->props.mDebugMaxThreads) { 197 cpu = mRSC->props.mDebugMaxThreads; 198 } 199 if (cpu < 2) { 200 mWorkers.mCount = 0; 201 return true; 202 } 203 204 // Subtract one from the cpu count because we also use the command thread as a worker. 205 mWorkers.mCount = (uint32_t)(cpu - 1); 206 207 ALOGV("%p Launching thread(s), CPUs %i", mRSC, mWorkers.mCount); 208 209 mWorkers.mThreadId = (pthread_t *) calloc(mWorkers.mCount, sizeof(pthread_t)); 210 mWorkers.mNativeThreadId = (pid_t *) calloc(mWorkers.mCount, sizeof(pid_t)); 211 mWorkers.mLaunchSignals = new Signal[mWorkers.mCount]; 212 mWorkers.mLaunchCallback = NULL; 213 214 mWorkers.mCompleteSignal.init(); 215 216 android_atomic_release_store(mWorkers.mCount, &mWorkers.mRunningCount); 217 android_atomic_release_store(0, &mWorkers.mLaunchCount); 218 219 pthread_attr_t threadAttr; 220 status = pthread_attr_init(&threadAttr); 221 if (status) { 222 ALOGE("Failed to init thread attribute."); 223 return false; 224 } 225 226 for (uint32_t ct=0; ct < mWorkers.mCount; ct++) { 227 status = pthread_create(&mWorkers.mThreadId[ct], &threadAttr, helperThreadProc, this); 228 if (status) { 229 mWorkers.mCount = ct; 230 ALOGE("Created fewer than expected number of RS threads."); 231 break; 232 } 233 } 234 while (android_atomic_acquire_load(&mWorkers.mRunningCount) != 0) { 235 usleep(100); 236 } 237 238 pthread_attr_destroy(&threadAttr); 239 return true; 240} 241 242 243void RsdCpuReferenceImpl::setPriority(int32_t priority) { 244 for (uint32_t ct=0; ct < mWorkers.mCount; ct++) { 245 setpriority(PRIO_PROCESS, mWorkers.mNativeThreadId[ct], priority); 246 } 247} 248 249RsdCpuReferenceImpl::~RsdCpuReferenceImpl() { 250 mExit = true; 251 mWorkers.mLaunchData = NULL; 252 mWorkers.mLaunchCallback = NULL; 253 android_atomic_release_store(mWorkers.mCount, &mWorkers.mRunningCount); 254 for (uint32_t ct = 0; ct < mWorkers.mCount; ct++) { 255 mWorkers.mLaunchSignals[ct].set(); 256 } 257 void *res; 258 for (uint32_t ct = 0; ct < mWorkers.mCount; ct++) { 259 pthread_join(mWorkers.mThreadId[ct], &res); 260 } 261 rsAssert(android_atomic_acquire_load(&mWorkers.mRunningCount) == 0); 262 263 // Global structure cleanup. 264 lockMutex(); 265 --gThreadTLSKeyCount; 266 if (!gThreadTLSKeyCount) { 267 pthread_key_delete(gThreadTLSKey); 268 } 269 unlockMutex(); 270 271} 272 273typedef void (*rs_t)(const void *, void *, const void *, uint32_t, uint32_t, uint32_t, uint32_t); 274 275static void wc_xy(void *usr, uint32_t idx) { 276 MTLaunchStruct *mtls = (MTLaunchStruct *)usr; 277 RsForEachStubParamStruct p; 278 memcpy(&p, &mtls->fep, sizeof(p)); 279 p.lid = idx; 280 uint32_t sig = mtls->sig; 281 282 outer_foreach_t fn = (outer_foreach_t) mtls->kernel; 283 while (1) { 284 uint32_t slice = (uint32_t)android_atomic_inc(&mtls->mSliceNum); 285 uint32_t yStart = mtls->yStart + slice * mtls->mSliceSize; 286 uint32_t yEnd = yStart + mtls->mSliceSize; 287 yEnd = rsMin(yEnd, mtls->yEnd); 288 if (yEnd <= yStart) { 289 return; 290 } 291 292 //ALOGE("usr idx %i, x %i,%i y %i,%i", idx, mtls->xStart, mtls->xEnd, yStart, yEnd); 293 //ALOGE("usr ptr in %p, out %p", mtls->fep.ptrIn, mtls->fep.ptrOut); 294 295 for (p.y = yStart; p.y < yEnd; p.y++) { 296 p.out = mtls->fep.ptrOut + (mtls->fep.yStrideOut * p.y) + 297 (mtls->fep.eStrideOut * mtls->xStart); 298 p.in = mtls->fep.ptrIn + (mtls->fep.yStrideIn * p.y) + 299 (mtls->fep.eStrideIn * mtls->xStart); 300 fn(&p, mtls->xStart, mtls->xEnd, mtls->fep.eStrideIn, mtls->fep.eStrideOut); 301 } 302 } 303} 304 305static void wc_x(void *usr, uint32_t idx) { 306 MTLaunchStruct *mtls = (MTLaunchStruct *)usr; 307 RsForEachStubParamStruct p; 308 memcpy(&p, &mtls->fep, sizeof(p)); 309 p.lid = idx; 310 uint32_t sig = mtls->sig; 311 312 outer_foreach_t fn = (outer_foreach_t) mtls->kernel; 313 while (1) { 314 uint32_t slice = (uint32_t)android_atomic_inc(&mtls->mSliceNum); 315 uint32_t xStart = mtls->xStart + slice * mtls->mSliceSize; 316 uint32_t xEnd = xStart + mtls->mSliceSize; 317 xEnd = rsMin(xEnd, mtls->xEnd); 318 if (xEnd <= xStart) { 319 return; 320 } 321 322 //ALOGE("usr slice %i idx %i, x %i,%i", slice, idx, xStart, xEnd); 323 //ALOGE("usr ptr in %p, out %p", mtls->fep.ptrIn, mtls->fep.ptrOut); 324 325 p.out = mtls->fep.ptrOut + (mtls->fep.eStrideOut * xStart); 326 p.in = mtls->fep.ptrIn + (mtls->fep.eStrideIn * xStart); 327 fn(&p, xStart, xEnd, mtls->fep.eStrideIn, mtls->fep.eStrideOut); 328 } 329} 330 331void RsdCpuReferenceImpl::launchThreads(const Allocation * ain, Allocation * aout, 332 const RsScriptCall *sc, MTLaunchStruct *mtls) { 333 334 //android::StopWatch kernel_time("kernel time"); 335 336 if ((mWorkers.mCount >= 1) && mtls->isThreadable && !mInForEach) { 337 const size_t targetByteChunk = 16 * 1024; 338 mInForEach = true; 339 if (mtls->fep.dimY > 1) { 340 uint32_t s1 = mtls->fep.dimY / ((mWorkers.mCount + 1) * 4); 341 uint32_t s2 = 0; 342 343 // This chooses our slice size to rate limit atomic ops to 344 // one per 16k bytes of reads/writes. 345 if (mtls->fep.yStrideOut) { 346 s2 = targetByteChunk / mtls->fep.yStrideOut; 347 } else { 348 s2 = targetByteChunk / mtls->fep.yStrideIn; 349 } 350 mtls->mSliceSize = rsMin(s1, s2); 351 352 if(mtls->mSliceSize < 1) { 353 mtls->mSliceSize = 1; 354 } 355 356 // mtls->mSliceSize = 2; 357 launchThreads(wc_xy, mtls); 358 } else { 359 uint32_t s1 = mtls->fep.dimX / ((mWorkers.mCount + 1) * 4); 360 uint32_t s2 = 0; 361 362 // This chooses our slice size to rate limit atomic ops to 363 // one per 16k bytes of reads/writes. 364 if (mtls->fep.eStrideOut) { 365 s2 = targetByteChunk / mtls->fep.eStrideOut; 366 } else { 367 s2 = targetByteChunk / mtls->fep.eStrideIn; 368 } 369 mtls->mSliceSize = rsMin(s1, s2); 370 371 if(mtls->mSliceSize < 1) { 372 mtls->mSliceSize = 1; 373 } 374 375 launchThreads(wc_x, mtls); 376 } 377 mInForEach = false; 378 379 //ALOGE("launch 1"); 380 } else { 381 RsForEachStubParamStruct p; 382 memcpy(&p, &mtls->fep, sizeof(p)); 383 uint32_t sig = mtls->sig; 384 385 //ALOGE("launch 3"); 386 outer_foreach_t fn = (outer_foreach_t) mtls->kernel; 387 for (p.ar[0] = mtls->arrayStart; p.ar[0] < mtls->arrayEnd; p.ar[0]++) { 388 for (p.z = mtls->zStart; p.z < mtls->zEnd; p.z++) { 389 for (p.y = mtls->yStart; p.y < mtls->yEnd; p.y++) { 390 uint32_t offset = mtls->fep.dimY * mtls->fep.dimZ * p.ar[0] + 391 mtls->fep.dimY * p.z + p.y; 392 p.out = mtls->fep.ptrOut + (mtls->fep.yStrideOut * offset) + 393 (mtls->fep.eStrideOut * mtls->xStart); 394 p.in = mtls->fep.ptrIn + (mtls->fep.yStrideIn * offset) + 395 (mtls->fep.eStrideIn * mtls->xStart); 396 fn(&p, mtls->xStart, mtls->xEnd, mtls->fep.eStrideIn, mtls->fep.eStrideOut); 397 } 398 } 399 } 400 } 401} 402 403RsdCpuScriptImpl * RsdCpuReferenceImpl::setTLS(RsdCpuScriptImpl *sc) { 404 //ALOGE("setTls %p", sc); 405 ScriptTLSStruct * tls = (ScriptTLSStruct *)pthread_getspecific(gThreadTLSKey); 406 rsAssert(tls); 407 RsdCpuScriptImpl *old = tls->mImpl; 408 tls->mImpl = sc; 409 tls->mContext = mRSC; 410 if (sc) { 411 tls->mScript = sc->getScript(); 412 } else { 413 tls->mScript = NULL; 414 } 415 return old; 416} 417 418const RsdCpuReference::CpuSymbol * RsdCpuReferenceImpl::symLookup(const char *name) { 419 return mSymLookupFn(mRSC, name); 420} 421 422 423RsdCpuReference::CpuScript * RsdCpuReferenceImpl::createScript(const ScriptC *s, 424 char const *resName, char const *cacheDir, 425 uint8_t const *bitcode, size_t bitcodeSize, 426 uint32_t flags) { 427 428 RsdCpuScriptImpl *i = new RsdCpuScriptImpl(this, s); 429 if (!i->init(resName, cacheDir, bitcode, bitcodeSize, flags)) { 430 delete i; 431 return NULL; 432 } 433 return i; 434} 435 436extern RsdCpuScriptImpl * rsdIntrinsic_3DLUT(RsdCpuReferenceImpl *ctx, 437 const Script *s, const Element *e); 438extern RsdCpuScriptImpl * rsdIntrinsic_Convolve3x3(RsdCpuReferenceImpl *ctx, 439 const Script *s, const Element *e); 440extern RsdCpuScriptImpl * rsdIntrinsic_ColorMatrix(RsdCpuReferenceImpl *ctx, 441 const Script *s, const Element *e); 442extern RsdCpuScriptImpl * rsdIntrinsic_LUT(RsdCpuReferenceImpl *ctx, 443 const Script *s, const Element *e); 444extern RsdCpuScriptImpl * rsdIntrinsic_Convolve5x5(RsdCpuReferenceImpl *ctx, 445 const Script *s, const Element *e); 446extern RsdCpuScriptImpl * rsdIntrinsic_Blur(RsdCpuReferenceImpl *ctx, 447 const Script *s, const Element *e); 448extern RsdCpuScriptImpl * rsdIntrinsic_YuvToRGB(RsdCpuReferenceImpl *ctx, 449 const Script *s, const Element *e); 450extern RsdCpuScriptImpl * rsdIntrinsic_Blend(RsdCpuReferenceImpl *ctx, 451 const Script *s, const Element *e); 452 453RsdCpuReference::CpuScript * RsdCpuReferenceImpl::createIntrinsic(const Script *s, 454 RsScriptIntrinsicID iid, Element *e) { 455 456 RsdCpuScriptImpl *i = NULL; 457 switch (iid) { 458 case RS_SCRIPT_INTRINSIC_ID_3DLUT: 459 i = rsdIntrinsic_3DLUT(this, s, e); 460 break; 461 case RS_SCRIPT_INTRINSIC_ID_CONVOLVE_3x3: 462 i = rsdIntrinsic_Convolve3x3(this, s, e); 463 break; 464 case RS_SCRIPT_INTRINSIC_ID_COLOR_MATRIX: 465 i = rsdIntrinsic_ColorMatrix(this, s, e); 466 break; 467 case RS_SCRIPT_INTRINSIC_ID_LUT: 468 i = rsdIntrinsic_LUT(this, s, e); 469 break; 470 case RS_SCRIPT_INTRINSIC_ID_CONVOLVE_5x5: 471 i = rsdIntrinsic_Convolve5x5(this, s, e); 472 break; 473 case RS_SCRIPT_INTRINSIC_ID_BLUR: 474 i = rsdIntrinsic_Blur(this, s, e); 475 break; 476 case RS_SCRIPT_INTRINSIC_ID_YUV_TO_RGB: 477 i = rsdIntrinsic_YuvToRGB(this, s, e); 478 break; 479 case RS_SCRIPT_INTRINSIC_ID_BLEND: 480 i = rsdIntrinsic_Blend(this, s, e); 481 break; 482 483 default: 484 rsAssert(0); 485 } 486 487 return i; 488} 489 490RsdCpuReference::CpuScriptGroup * RsdCpuReferenceImpl::createScriptGroup(const ScriptGroup *sg) { 491 CpuScriptGroupImpl *sgi = new CpuScriptGroupImpl(this, sg); 492 if (!sgi->init()) { 493 delete sgi; 494 return NULL; 495 } 496 return sgi; 497} 498 499 500