mirror of https://github.com/jlizier/jidt
Add unit tests inside Java for GPU code.
This commit is contained in:
parent
d7fa6814dd
commit
5a07352969
70
java/unittests/infodynamics/measures/continuous/kraskov/MutualInfoMultiVariateTester.java
Executable file → Normal file
70
java/unittests/infodynamics/measures/continuous/kraskov/MutualInfoMultiVariateTester.java
Executable file → Normal file
|
|
@ -461,4 +461,74 @@ public class MutualInfoMultiVariateTester
|
|||
kNNs, expectedFromMILCA_2);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that GPU MI calculations match the CPU version.
|
||||
*
|
||||
* Only runs if CUDA code has been precompiled.
|
||||
*
|
||||
* @throws Exception if something goes wrong
|
||||
*/
|
||||
public void testGPUMutualInfo() throws Exception {
|
||||
|
||||
MutualInfoCalculatorMultiVariateKraskov miCalc =
|
||||
new MutualInfoCalculatorMultiVariateKraskov1();
|
||||
|
||||
boolean gpuLoaded = true;
|
||||
try {
|
||||
miCalc.ensureCudaLibraryLoaded();
|
||||
} catch (Throwable e) {
|
||||
gpuLoaded = false;
|
||||
}
|
||||
|
||||
// This will effectively ignore the test if GPU library not loaded properly
|
||||
if (!gpuLoaded) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Now starts the actual test. Set up variables and properties
|
||||
miCalc.setProperty("NOISE_LEVEL_TO_ADD", "0");
|
||||
ArrayFileReader afr;
|
||||
double[][] data;
|
||||
double cpu_val, gpu_val;
|
||||
|
||||
// Test for low-dimensional data
|
||||
afr = new ArrayFileReader("demos/data/2randomCols-1.txt");
|
||||
data = afr.getDouble2DMatrix();
|
||||
|
||||
miCalc.setProperty("USE_GPU", "false");
|
||||
miCalc.initialise(1,1);
|
||||
miCalc.setObservations(MatrixUtils.selectColumn(data, 0),
|
||||
MatrixUtils.selectColumn(data, 1));
|
||||
cpu_val = miCalc.computeAverageLocalOfObservations();
|
||||
|
||||
miCalc.setProperty("USE_GPU", "true");
|
||||
miCalc.initialise(1,1);
|
||||
miCalc.setObservations(MatrixUtils.selectColumn(data, 0),
|
||||
MatrixUtils.selectColumn(data, 1));
|
||||
gpu_val = miCalc.computeAverageLocalOfObservations();
|
||||
|
||||
assertEquals(cpu_val, gpu_val, 0.00001);
|
||||
|
||||
// Test for slightly higher-dimensional data
|
||||
afr = new ArrayFileReader("demos/data/4ColsPairedNoisyDependence-1.txt");
|
||||
data = afr.getDouble2DMatrix();
|
||||
|
||||
miCalc.setProperty("USE_GPU", "false");
|
||||
miCalc.initialise(2,2);
|
||||
miCalc.setObservations(MatrixUtils.selectColumns(data, new int[]{0,1}),
|
||||
MatrixUtils.selectColumns(data, new int[]{2,3}));
|
||||
cpu_val = miCalc.computeAverageLocalOfObservations();
|
||||
|
||||
miCalc.setProperty("USE_GPU", "true");
|
||||
miCalc.initialise(2,2);
|
||||
miCalc.setObservations(MatrixUtils.selectColumns(data, new int[]{0,1}),
|
||||
MatrixUtils.selectColumns(data, new int[]{2,3}));
|
||||
gpu_val = miCalc.computeAverageLocalOfObservations();
|
||||
|
||||
assertEquals(cpu_val, gpu_val, 0.00001);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue