Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 19 additions & 18 deletions Tensors/src/test/scala/com/thoughtworks/compute/TensorsSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,34 +16,35 @@ import org.scalatest._
* @author 杨博 (Yang Bo)
*/
class TensorsSpec extends AsyncFreeSpec with Matchers {
private val tensors: Tensors =
private def doTensors: Do[Tensors] = Do.monadicCloseable(
Factory[
OpenCL.GlobalExecutionContext with OpenCL.UseAllDevices with OpenCL.UseFirstPlatform with OpenCL.CommandQueuePool with Tensors]
.newInstance(
handleOpenCLNotification = handleOpenCLNotification,
numberOfCommandQueuesForDevice = { (deviceId: Long, capabilities: CLCapabilities) =>
5
}
)
))

"create a tensor of a constant" in {
val shape = Array(2, 3, 5)
val element = 42.0f
val zeros = tensors.Tensor.fill(element, shape)

for {
pendingBuffer <- zeros.enqueue
floatBuffer <- pendingBuffer.toHostBuffer
} yield {
for (i <- 0 until floatBuffer.capacity()) {
floatBuffer.get(i) should be(element)
doTensors.flatMap { tensors =>
val shape = Array(2, 3, 5)
val element = 42.0f
val zeros = tensors.Tensor.fill(element, shape)
for {
pendingBuffer <- zeros.enqueue
floatBuffer <- pendingBuffer.toHostBuffer
} yield {
for (i <- 0 until floatBuffer.capacity()) {
floatBuffer.get(i) should be(element)
}
floatBuffer.position() should be(0)
floatBuffer.limit() should be(shape.product)
floatBuffer.capacity() should be(shape.product)
tensors.kernelCache.getIfPresent(zeros.closure) should not be null
val zeros2 = tensors.Tensor.fill(element, shape)
tensors.kernelCache.getIfPresent(zeros2.closure) should not be null
}
floatBuffer.position() should be(0)
floatBuffer.limit() should be(shape.product)
floatBuffer.capacity() should be(shape.product)
tensors.kernelCache.getIfPresent(zeros.closure) should not be null
val zeros2 = tensors.Tensor.fill(element, shape)
tensors.kernelCache.getIfPresent(zeros2.closure) should not be null
}
}.run.toScalaFuture

Expand Down