Last active
December 15, 2015 01:19
-
-
Save ochafik/5179076 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.nativelibs4java.opencl.*; | |
import org.bridj.*; | |
import static org.bridj.Pointer.*; | |
/* | |
Put these lines in `build.sbt`, and run with `sbt ~run`: | |
fork := true | |
libraryDependencies += "com.nativelibs4java" % "javacl" % "1.0.0-RC3" | |
*/ | |
public class TestOpenCLDataTransfers { | |
public static void main(String[] args) { | |
int n = args.length == 1 ? Integer.parseInt(args[0]) : 100000; | |
for (CLPlatform platform : JavaCL.listPlatforms()) { | |
System.out.println("# Platform " + platform); | |
for (CLDevice device : platform.listAllDevices(true)) { | |
System.out.println("# CLDevice " + device); | |
CLContext context = JavaCL.createContext(null, device); | |
CLQueue queue = context.createDefaultQueue(); | |
CLBuffer<Integer> buffer = context.createIntBuffer(CLMem.Usage.InputOutput, n); | |
Pointer<Integer> p = allocateInts(n); | |
p.set(10); | |
for (int j = 0; j < 10; j++) { | |
int times = 10; | |
double timeMap, timeBlockingWrite, timeNonBlockingWrite; | |
{ | |
long start = System.nanoTime(); | |
for (int i = 0; i < times; i++) { | |
Pointer<Integer> pBuffer = buffer.map(queue, CLMem.MapFlags.Write); | |
p.copyTo(pBuffer); | |
buffer.unmap(queue, pBuffer, (CLEvent[])null); | |
queue.finish(); | |
} | |
long time = System.nanoTime() - start; | |
timeMap = time / 1000000.0; | |
} | |
queue.finish(); | |
{ | |
long start = System.nanoTime(); | |
for (int i = 0; i < times; i++) { | |
buffer.write(queue, p, true, (CLEvent[])null); | |
//queue.finish(); | |
} | |
long time = System.nanoTime() - start; | |
timeBlockingWrite = time / 1000000.0; | |
} | |
queue.finish(); | |
{ | |
long start = System.nanoTime(); | |
for (int i = 0; i < times; i++) { | |
buffer.write(queue, p, false, (CLEvent[])null); | |
queue.finish(); | |
} | |
long time = System.nanoTime() - start; | |
timeNonBlockingWrite = time / 1000000.0; | |
} | |
queue.finish(); | |
System.out.println("Time millis [ map / blocking write / non-blocking write ]:"); | |
System.out.println("\t" + timeMap + " / " + timeBlockingWrite + " / " + timeNonBlockingWrite); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment