Repository: mahout Updated Branches: refs/heads/master 1eacd7f9d -> 0f4b3d97c
[MAHOUT-1903][MAHOUT-1907] VCL Vector memory copy fix. closes apache/mahout#286 Project: http://git-wip-us.apache.org/repos/asf/mahout/repo Commit: http://git-wip-us.apache.org/repos/asf/mahout/commit/0f4b3d97 Tree: http://git-wip-us.apache.org/repos/asf/mahout/tree/0f4b3d97 Diff: http://git-wip-us.apache.org/repos/asf/mahout/diff/0f4b3d97 Branch: refs/heads/master Commit: 0f4b3d97cfc651a4bc052515b260648cd0093ac2 Parents: 1eacd7f Author: Andrew Palumbo <[email protected]> Authored: Sun Feb 26 13:34:48 2017 -0800 Committer: Andrew Palumbo <[email protected]> Committed: Sun Feb 26 13:36:24 2017 -0800 ---------------------------------------------------------------------- .../viennacl/opencl/javacpp/VCLVector.scala | 10 +-- .../viennacl/opencl/javacpp/VectorBase.scala | 1 + .../viennacl/opencl/ViennaCLSuiteVCL.scala | 68 ++++++++++++-------- 3 files changed, 48 insertions(+), 31 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/mahout/blob/0f4b3d97/viennacl/src/main/scala/org/apache/mahout/viennacl/opencl/javacpp/VCLVector.scala ---------------------------------------------------------------------- diff --git a/viennacl/src/main/scala/org/apache/mahout/viennacl/opencl/javacpp/VCLVector.scala b/viennacl/src/main/scala/org/apache/mahout/viennacl/opencl/javacpp/VCLVector.scala index f0e3010..3926980 100644 --- a/viennacl/src/main/scala/org/apache/mahout/viennacl/opencl/javacpp/VCLVector.scala +++ b/viennacl/src/main/scala/org/apache/mahout/viennacl/opencl/javacpp/VCLVector.scala @@ -112,18 +112,20 @@ final class VCLVector(defaultCtr: Boolean = true) extends VectorBase { @Name(Array("viennacl::vector<double>::self_type")) def selfType:VectorBase = this.asInstanceOf[VectorBase] + // defining this here getting a gcc compilation error when + // adding this method to parent class. + @Name(Array("switch_memory_context")) + @native + def switchMemoryContext(@ByRef ctx: Context) - @native def switch_memory_context(@ByVal context: Context): Unit -// Swaps the handles of two vectors by swapping the OpenCL handles only, no data copy. + // Swaps the handles of two vectors by swapping the OpenCL handles only, no data copy. // @native def fast_swap(@ByVal other: VCLVector): VectorBase // add this operator in for tests many more can be added // @Name(Array("operator*")) // @native @ByPtr def *(i: Int): VectorMultExpression - - } object VCLVector { http://git-wip-us.apache.org/repos/asf/mahout/blob/0f4b3d97/viennacl/src/main/scala/org/apache/mahout/viennacl/opencl/javacpp/VectorBase.scala ---------------------------------------------------------------------- diff --git a/viennacl/src/main/scala/org/apache/mahout/viennacl/opencl/javacpp/VectorBase.scala b/viennacl/src/main/scala/org/apache/mahout/viennacl/opencl/javacpp/VectorBase.scala index 43ae39d..9f45830 100644 --- a/viennacl/src/main/scala/org/apache/mahout/viennacl/opencl/javacpp/VectorBase.scala +++ b/viennacl/src/main/scala/org/apache/mahout/viennacl/opencl/javacpp/VectorBase.scala @@ -47,6 +47,7 @@ class VectorBase extends Pointer { // handle to the vec element buffer @native @Const @ByRef def handle: MemHandle + // // add this operator in for tests many more can be added // @Name(Array("operator* ")) // @native def *(i: Int): VectorMultExpression http://git-wip-us.apache.org/repos/asf/mahout/blob/0f4b3d97/viennacl/src/test/scala/org/apache/mahout/viennacl/opencl/ViennaCLSuiteVCL.scala ---------------------------------------------------------------------- diff --git a/viennacl/src/test/scala/org/apache/mahout/viennacl/opencl/ViennaCLSuiteVCL.scala b/viennacl/src/test/scala/org/apache/mahout/viennacl/opencl/ViennaCLSuiteVCL.scala index c433534..2308307 100644 --- a/viennacl/src/test/scala/org/apache/mahout/viennacl/opencl/ViennaCLSuiteVCL.scala +++ b/viennacl/src/test/scala/org/apache/mahout/viennacl/opencl/ViennaCLSuiteVCL.scala @@ -291,11 +291,11 @@ class ViennaCLSuiteVCL extends FunSuite with Matchers { test("VCL Dense Matrix %*% Dense vector") { val oclCtx = new Context(Context.OPENCL_MEMORY) - val ompCtx = new Context(Context.MAIN_MEMORY) + val mainCtx = new Context(Context.MAIN_MEMORY) - val m = 30 - val s = 10 + val m = 3000 + val s = 1000 val r = new Random(1234) @@ -314,32 +314,47 @@ class ViennaCLSuiteVCL extends FunSuite with Matchers { info(s"Mahout dense matrix %*% dense vector multiplication time: $ms ms.") - /* TODO: CL_OUT_OF_RESOURCES error thrown when trying to read data out of OpenCl GPU Vectors */ - //Test multiplication in OpenCL -// { -// -// ms = System.currentTimeMillis() -// val oclA = toVclDenseRM(mxA, oclCtx) -// val oclVecB = toVclVec(dvecB, oclCtx) -// -// val oclVecC = new VCLVector(prod(oclA, oclVecB)) -// val oclDvecC = fromVClVec(oclVecC) -//// -//// ms = System.currentTimeMillis() - ms -//// info(s"ViennaCL/OpenCL dense matrix %*% dense vector multiplication time: $ms ms.") -//// (oclDvecC.toColMatrix - mDvecC.toColMatrix).norm / s should be < 1e-16 -// -// oclA.close() -// oclVecB.close() -// oclVecC.close() -// } - - //Test multiplication in OpenMP + // Test mx %*% vec multiplication in OpenCL { ms = System.currentTimeMillis() - val ompMxA = toVclDenseRM(mxA, ompCtx) - val ompVecB = toVclVec(dvecB, ompCtx) + + // we must first create vectors in main memory + // when working with vectors at least in ViennaCl + // this is the preferred method + val oclMxA = toVclDenseRM(mxA, mainCtx) + val oclVecB = toVclVec(dvecB, mainCtx) + + // now copy to the OpenCL device + oclMxA.switchMemoryContext(oclCtx) + oclVecB.switchMemoryContext(oclCtx) + + // perform multiplication + val oclVecC = new VCLVector(prod(oclMxA, oclVecB)) + + // copy back to main memory so that we may + // read values out of the result. This must be + // copied back to main memory VCL can not read + // directly from an OpenCL device + oclVecC.switchMemoryContext(mainCtx) + + val oclDvecC = fromVClVec(oclVecC) + + ms = System.currentTimeMillis() - ms + info(s"ViennaCL/OpenCL dense matrix %*% dense vector multiplication time: $ms ms.") + (oclDvecC.toColMatrix - mDvecC.toColMatrix).norm / s should be < 1e-10 + + oclMxA.close() + oclVecB.close() + oclVecC.close() + } + + //Test multiplication in OpenMP + { + + ms = System.currentTimeMillis() + val ompMxA = toVclDenseRM(mxA, mainCtx) + val ompVecB = toVclVec(dvecB, mainCtx) val ompVecC = new VCLVector(prod(ompMxA, ompVecB)) val ompDvecC = fromVClVec(ompVecC) @@ -356,7 +371,6 @@ class ViennaCLSuiteVCL extends FunSuite with Matchers { oclCtx.deallocate() oclCtx.close() - }
