Skip to content
Snippets Groups Projects
Commit 6e08e207 authored by Saurabh Rawat's avatar Saurabh Rawat
Browse files

add tests for scala

parent c2e44ad3
No related branches found
No related tags found
No related merge requests found
......@@ -98,6 +98,11 @@
<artifactId>bcel</artifactId>
<version>6.2</version>
</dependency>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>2.12.6</version>
</dependency>
</dependencies>
<build>
......@@ -107,6 +112,22 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.4.1</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
<configuration>
<scalaVersion>2.12.6</scalaVersion>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
......
package com.aparapi
import com.aparapi.codegen.Diff
import com.aparapi.internal.model.ClassModel
import com.aparapi.internal.writer.KernelWriter
import org.junit.Test
import scala.util.Random
class SimpleScalaTest {
def runKernel(inA: Array[Float], inB: Array[Float]): Array[Float] = {
val result = new Array[Float](inA.length)
val kernel = new Kernel() {
override def run() {
val i = getGlobalId()
result(i) = ((inA(i) + inB(i)) / (inA(i) / inB(i))) * ((inA(i) - inB(i)) / (inA(
i) * inB(i))) -
((inB(i) - inA(i)) * (inB(i) + inA(i))) * ((inB(i) - inA(i)) / (inB(i) * inA(
i)))
}
}
kernel.execute(inA.length)
result
}
def generateCL(inA: Array[Float], inB: Array[Float]): String = {
val result = new Array[Float](inA.length)
val kernel = new Kernel() {
override def run() {
val i = getGlobalId()
result(i) = ((inA(i) + inB(i)) / (inA(i) / inB(i))) * ((inA(i) - inB(i)) / (inA(
i) * inB(i))) -
((inB(i) - inA(i)) * (inB(i) + inA(i))) * ((inB(i) - inA(i)) / (inB(i) * inA(
i)))
}
}
val classModel = ClassModel.createClassModel(kernel.getClass)
val entryPoint = classModel.getEntrypoint("run", kernel)
KernelWriter.writeToString(entryPoint)
}
@Test def testKernel(): Unit = {
val a = Array.fill(50000)(Random.nextFloat())
val b = Array.fill(50000)(Random.nextFloat())
assert(runKernel(a, b).length == 50000)
}
@Test def testCL(): Unit = {
val a = Array.fill(50000)(Random.nextFloat())
val b = Array.fill(50000)(Random.nextFloat())
val expected =
"""typedef struct This_s{
| __global float *result$2;
| __global float *inA$2;
| __global float *inB$2;
| int passid;
|}This;
|int get_pass_id(This *this){
| return this->passid;
|}
|__kernel void run(
| __global float *result$2,
| __global float *inA$2,
| __global float *inB$2,
| int passid
|){
| This thisStruct;
| This* this=&thisStruct;
| this->result$2 = result$2;
| this->inA$2 = inA$2;
| this->inB$2 = inB$2;
| this->passid = passid;
| {
| {
| int i = get_global_id(0);
| this->result$2[i] = (((this->inA$2[i] + this->inB$2[i]) / (this->inA$2[i] / this->inB$2[i])) * ((this->inA$2[i] - this->inB$2[i]) / (this->inA$2[i] * this->inB$2[i]))) - (((this->inB$2[i] - this->inA$2[i]) * (this->inB$2[i] + this->inA$2[i])) * ((this->inB$2[i] - this->inA$2[i]) / (this->inB$2[i] * this->inA$2[i])));
| }
| return;
| }
|}""".stripMargin
assert(Diff.same(expected, generateCL(a, b)))
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment