From 5afbff71f4165e3d941f06e350e93e07f2fc0ee2 Mon Sep 17 00:00:00 2001 From: Yang Bo Date: Wed, 9 May 2018 16:47:23 +0800 Subject: [PATCH] Move Tensors.UnsafeMathOptimizations and Tensors.SuppressWarnings to OpenCL --- .../com/thoughtworks/compute/OpenCL.scala | 25 +++++++++++++- .../com/thoughtworks/compute/Tensors.scala | 34 ++++--------------- .../com/thoughtworks/compute/benchmarks.scala | 4 +-- .../scala/com/thoughtworks/compute/cpu.scala | 2 +- .../scala/com/thoughtworks/compute/gpu.scala | 2 +- 5 files changed, 34 insertions(+), 33 deletions(-) diff --git a/OpenCL/src/main/scala/com/thoughtworks/compute/OpenCL.scala b/OpenCL/src/main/scala/com/thoughtworks/compute/OpenCL.scala index f76ac0f9..8d5e40a6 100644 --- a/OpenCL/src/main/scala/com/thoughtworks/compute/OpenCL.scala +++ b/OpenCL/src/main/scala/com/thoughtworks/compute/OpenCL.scala @@ -929,7 +929,7 @@ object OpenCL { checkBuildErrorCode(None, clBuildProgram(handle, null, options, null, NULL)) } - def build(): Unit = build("") + def build()(implicit witness: Witness.Aux[Owner]): Unit = build(witness.value.defaultProgramOptions) def monadicClose = UnitContinuation.delay { OpenCL.checkErrorCode(clReleaseProgram(handle)) @@ -1113,11 +1113,34 @@ object OpenCL { } } + /** A plug-in of Tensors to suppress warnings during compiling a OpenCL kernel for non-AMD platforms. */ + trait SuppressWarnings extends OpenCL { + @transient + private lazy val _defaultProgramOptions = { + if (platformCapabilities.cl_amd_compile_options) { + // AMD SDK does not support -w flag in OpenCL specification. + super.defaultProgramOptions + } else { + super.defaultProgramOptions + " -w" + } + } + + override protected def defaultProgramOptions: CharSequence = _defaultProgramOptions + } + + trait UnsafeMathOptimizations extends OpenCL { + private lazy val _defaultProgramOptions = super.defaultProgramOptions + " -cl-unsafe-math-optimizations" + + abstract override protected def defaultProgramOptions: CharSequence = _defaultProgramOptions + } + } trait OpenCL extends MonadicCloseable[UnitContinuation] with DefaultCloseable { import OpenCL._ + protected def defaultProgramOptions: CharSequence = "" + protected def createKernels(program: Program): Seq[Kernel] = { val stack = stackPush() try { diff --git a/Tensors/src/main/scala/com/thoughtworks/compute/Tensors.scala b/Tensors/src/main/scala/com/thoughtworks/compute/Tensors.scala index 8816b382..31d1a0a4 100644 --- a/Tensors/src/main/scala/com/thoughtworks/compute/Tensors.scala +++ b/Tensors/src/main/scala/com/thoughtworks/compute/Tensors.scala @@ -41,26 +41,6 @@ object Tensors { private final val MaxWorkItemDimensions = 3 - /** A plug-in of Tensors to suppress warnings during compiling a OpenCL kernel for non-AMD platforms. */ - trait SuppressWarnings extends Tensors { - @transient - private lazy val _openclCompilerFlags = { - if (platformCapabilities.cl_amd_compile_options) { - // AMD SDK does not support -w flag in OpenCL specification. - super.openclCompilerFlags - } else { - super.openclCompilerFlags + " -w" - } - } - - override protected def openclCompilerFlags: String = _openclCompilerFlags - } - - trait UnsafeMathOptimizations extends Tensors { - private lazy val _openclCompilerFlags = super.openclCompilerFlags + " -cl-unsafe-math-optimizations" - override protected def openclCompilerFlags: String = _openclCompilerFlags - } - trait TensorBuilder[Data] { type Element def flatten(a: Data): Seq[Element] @@ -320,8 +300,6 @@ trait Tensors extends OpenCL { protected def hashSourceCode: Fastring - protected def openclCompilerFlags: String = "" - protected object PlusPrograms extends MonoidPrograms { def append(leftHandSide: Fastring, rightHandSide: Fastring): Fastring = fast"(($leftHandSide) + ($rightHandSide))" def zero: Fastring = fast"0.0f" @@ -368,7 +346,7 @@ trait Tensors extends OpenCL { } } """) - program.build(openclCompilerFlags) + program.build() program } @@ -409,7 +387,7 @@ trait Tensors extends OpenCL { } } """) - program.build(openclCompilerFlags) + program.build() program } } @@ -446,7 +424,7 @@ trait Tensors extends OpenCL { buffer[i * 2 + 1] = z1; } """) - program.build(openclCompilerFlags) + program.build() program } @@ -460,7 +438,7 @@ trait Tensors extends OpenCL { buffer[i] = hash(i ^ seed) / 4294967296.0f; } """) - program.build(openclCompilerFlags) + program.build() program } @@ -1049,7 +1027,7 @@ trait Tensors extends OpenCL { /** * @group delayed */ - def transpose: TransformedTensor = { permute(shape.indices.reverse.toArray)} + def transpose: TransformedTensor = { permute(shape.indices.reverse.toArray) } /** * @group delayed @@ -1216,7 +1194,7 @@ trait Tensors extends OpenCL { } val program = createProgramWithSource(sourceCode) - program.build(openclCompilerFlags) + program.build() val compiledKernel = new CompiledKernel { diff --git a/benchmarks/src/jmh/scala/com/thoughtworks/compute/benchmarks.scala b/benchmarks/src/jmh/scala/com/thoughtworks/compute/benchmarks.scala index 1b0ddf22..6ca194e4 100644 --- a/benchmarks/src/jmh/scala/com/thoughtworks/compute/benchmarks.scala +++ b/benchmarks/src/jmh/scala/com/thoughtworks/compute/benchmarks.scala @@ -33,8 +33,8 @@ object benchmarks { trait BenchmarkTensors extends StrictLogging - with Tensors.UnsafeMathOptimizations - with Tensors.SuppressWarnings + with OpenCL.UnsafeMathOptimizations + with OpenCL.SuppressWarnings with OpenCL.LogContextNotification with OpenCL.UseAllDevicesByType with OpenCL.GlobalExecutionContext diff --git a/cpu/src/main/scala/com/thoughtworks/compute/cpu.scala b/cpu/src/main/scala/com/thoughtworks/compute/cpu.scala index 59709ffe..7c1da656 100644 --- a/cpu/src/main/scala/com/thoughtworks/compute/cpu.scala +++ b/cpu/src/main/scala/com/thoughtworks/compute/cpu.scala @@ -102,7 +102,7 @@ import org.lwjgl.opencl.CL10.CL_DEVICE_TYPE_CPU */ object cpu extends StrictLogging - with Tensors.UnsafeMathOptimizations + with OpenCL.UnsafeMathOptimizations with OpenCL.LogContextNotification with OpenCL.GlobalExecutionContext with OpenCL.CommandQueuePool diff --git a/gpu/src/main/scala/com/thoughtworks/compute/gpu.scala b/gpu/src/main/scala/com/thoughtworks/compute/gpu.scala index 60e48863..96641e6f 100644 --- a/gpu/src/main/scala/com/thoughtworks/compute/gpu.scala +++ b/gpu/src/main/scala/com/thoughtworks/compute/gpu.scala @@ -14,7 +14,7 @@ import com.typesafe.scalalogging.StrictLogging */ object gpu extends StrictLogging - with Tensors.UnsafeMathOptimizations + with OpenCL.UnsafeMathOptimizations with OpenCL.LogContextNotification with OpenCL.GlobalExecutionContext with OpenCL.CommandQueuePool