Skip to content

Commit 65f5003

Browse files
committed
Enable HandleEventInExecutionContext only for AMD and Intel's platform
1 parent fdf386d commit 65f5003

File tree

4 files changed

+35
-9
lines changed

4 files changed

+35
-9
lines changed

OpenCL/src/main/scala/com/thoughtworks/compute/OpenCL.scala

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import scala.collection.JavaConverters._
44
import java.nio.{ByteBuffer, IntBuffer}
55
import java.util.concurrent.{ConcurrentHashMap, Executors}
66
import java.util.concurrent.atomic.AtomicReference
7+
import java.util.regex.Pattern
78

89
import com.typesafe.scalalogging.{CanLog, Logger, StrictLogging}
910
import org.lwjgl.opencl._
@@ -55,7 +56,21 @@ object OpenCL {
5556

5657
final case class PlatformId[Owner <: Singleton with OpenCL](handle: Long) extends AnyVal {
5758

58-
final def deviceIdsByType(deviceType: Int): Seq[DeviceId[Owner]] = {
59+
def name: String = {
60+
val stack = stackPush()
61+
try {
62+
val sizeBuffer = stack.mallocPointer(1)
63+
checkErrorCode(clGetPlatformInfo(handle, CL_PLATFORM_NAME, null: ByteBuffer, sizeBuffer))
64+
val size = sizeBuffer.get(0).toInt
65+
val nameBuffer = stack.malloc(size)
66+
checkErrorCode(clGetPlatformInfo(handle, CL_PLATFORM_NAME, nameBuffer, null))
67+
memUTF8(nameBuffer)
68+
} finally {
69+
stack.close()
70+
}
71+
}
72+
73+
def deviceIdsByType(deviceType: Int): Seq[DeviceId[Owner]] = {
5974
val Array(numberOfDevices) = {
6075
val a = Array(0)
6176
checkErrorCode(clGetDeviceIDs(handle, deviceType, null, a))
@@ -358,8 +373,10 @@ object OpenCL {
358373

359374
}
360375

376+
private val AmdOrIntelRegex = """AMD|Intel""".r
377+
361378
/**
362-
* @note [[HandleEventInExecutionContext]] __should__ be unnecessary because
379+
* @note [[HandleEventInExecutionContextForIntelAndAMDPlatform]] __should__ be unnecessary because
363380
* only OpenCL calls to create contexts or command-queues, or blocking OpenCL operations are undefined behavior,
364381
* according to https://www.khronos.org/registry/OpenCL/sdk/1.2/docs/man/xhtml/clSetEventCallback.html
365382
* and we don't use those forbidden functions.
@@ -370,18 +387,27 @@ object OpenCL {
370387
*
371388
* There is also similar bug in Intel's OpenCL implementation
372389
*
373-
* As a workaround, always enable this [[HandleEventInExecutionContext]] for
390+
* As a workaround, always enable this [[HandleEventInExecutionContextForIntelAndAMDPlatform]] for
374391
* Intel's and AMD's OpenCL implementation.
375392
*/
376-
trait HandleEventInExecutionContext extends OpenCL {
393+
trait HandleEventInExecutionContextForIntelAndAMDPlatform extends OpenCL {
377394

378395
// FIXME: this plug-in will cause Nvidia OpenCL hang up. Need investigation.
379396

380397
protected val executionContext: ExecutionContext
381398

399+
@transient
400+
private lazy val isEnabled = {
401+
AmdOrIntelRegex.findAllMatchIn(platformId.name).nonEmpty
402+
}
403+
382404
override protected def waitForStatus(event: Event, callbackType: Status): UnitContinuation[Status] =
383-
super.waitForStatus(event, callbackType).flatMap { status =>
384-
UnitContinuation.execute(status)(executionContext)
405+
if (isEnabled) {
406+
super.waitForStatus(event, callbackType).flatMap { status =>
407+
UnitContinuation.execute(status)(executionContext)
408+
}
409+
} else {
410+
super.waitForStatus(event, callbackType)
385411
}
386412
}
387413

benchmarks/src/jmh/scala/com/thoughtworks/compute/benchmarks.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ object benchmarks {
4040
with OpenCL.CommandQueuePool
4141
with OpenCL.DontReleaseEventTooEarly
4242
with OpenCL.SynchronizedCreatingKernel
43-
with OpenCL.HandleEventInExecutionContext
43+
with OpenCL.HandleEventInExecutionContextForIntelAndAMDPlatform
4444
with Tensors.WangHashingRandomNumberGenerator {
4545

4646
protected val deviceType: Int =

cpu/src/main/scala/com/thoughtworks/compute/cpu.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ object cpu
1414
with OpenCL.UseAllCpuDevices
1515
with OpenCL.DontReleaseEventTooEarly
1616
with OpenCL.SynchronizedCreatingKernel
17-
with OpenCL.HandleEventInExecutionContext
17+
with OpenCL.HandleEventInExecutionContextForIntelAndAMDPlatform
1818
with Tensors.WangHashingRandomNumberGenerator {
1919

2020
protected val numberOfCommandQueuesPerDevice = 5

gpu/src/main/scala/com/thoughtworks/compute/gpu.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ object gpu
1414
with OpenCL.UseAllGpuDevices
1515
with OpenCL.DontReleaseEventTooEarly
1616
with OpenCL.SynchronizedCreatingKernel
17-
with OpenCL.HandleEventInExecutionContext
17+
with OpenCL.HandleEventInExecutionContextForIntelAndAMDPlatform
1818
with Tensors.WangHashingRandomNumberGenerator {
1919
protected val numberOfCommandQueuesPerDevice: Int = 5
2020
}

0 commit comments

Comments
 (0)