From 64f1142b4080002d75a32f0615bfac6fcaeade86 Mon Sep 17 00:00:00 2001 From: Pedro Martinez Mediano Date: Tue, 19 Dec 2017 20:42:58 +0000 Subject: [PATCH] Added argument to choose which variable to reorder in GPU CMI code. --- cuda/gpuCMILibrary.c | 31 ++++++++++++++----- cuda/gpuCMILibrary.h | 6 ++-- cuda/kraskovCuda.c | 10 +++--- cuda/unittest.cpp | 12 +++---- ...tualInfoCalculatorMultiVariateKraskov.java | 26 ++++++++-------- 5 files changed, 52 insertions(+), 33 deletions(-) diff --git a/cuda/gpuCMILibrary.c b/cuda/gpuCMILibrary.c index bb7566f..870507b 100644 --- a/cuda/gpuCMILibrary.c +++ b/cuda/gpuCMILibrary.c @@ -10,9 +10,11 @@ jidt_error_t CMIKraskov_C(int N, float *source, int dimx, float *dest, int dimy, float *cond, int dimz, int k, int thelier, int nb_surrogates, - int returnLocals, int useMaxNorm, int isAlgorithm1, float *result) { + int returnLocals, int useMaxNorm, int isAlgorithm1, float *result, + int variableToReorder) { return CMIKraskovWithReorderings(N, source, dimx, dest, dimy, cond, dimz, - k, thelier, nb_surrogates, returnLocals, useMaxNorm, isAlgorithm1, result, 0, NULL); + k, thelier, nb_surrogates, returnLocals, useMaxNorm, isAlgorithm1, result, + 0, NULL, variableToReorder); } /** @@ -21,7 +23,8 @@ jidt_error_t CMIKraskov_C(int N, float *source, int dimx, float *dest, int dimy, jidt_error_t CMIKraskovWithReorderings(int N, float *source, int dimx, float *dest, int dimy, float *cond, int dimz, int k, int thelier, int nb_surrogates, int returnLocals, int useMaxNorm, - int isAlgorithm1, float *result, int reorderingsGiven, int **reorderings) { + int isAlgorithm1, float *result, int reorderingsGiven, int **reorderings, + int variableToReorder) { CPerfTimer pt = startTimer("Rearranging pointset"); @@ -70,12 +73,24 @@ jidt_error_t CMIKraskovWithReorderings(int N, float *source, int dimx, } for (int i = 0; i < N; i++) { - for (int j = 0; j < dimx; j++) { - pointset[(s+1)*N + N*j*nchunks + i] = source[N*j + order[i]]; - } + if (variableToReorder == 1) { + for (int j = 0; j < dimx; j++) { + pointset[(s+1)*N + N*j*nchunks + i] = source[N*j + order[i]]; + } + + for (int j = 0; j < dimz; j++) { + pointset[nchunks*N*dimx + (s+1)*N + N*j*nchunks + i] = cond[N*j + i]; + } + + } else { + for (int j = 0; j < dimx; j++) { + pointset[(s+1)*N + N*j*nchunks + i] = source[N*j + i]; + } + + for (int j = 0; j < dimz; j++) { + pointset[nchunks*N*dimx + (s+1)*N + N*j*nchunks + i] = cond[N*j + order[i]]; + } - for (int j = 0; j < dimz; j++) { - pointset[nchunks*N*dimx + (s+1)*N + N*j*nchunks + i] = cond[N*j + i]; } for (int j = 0; j < dimy; j++) { diff --git a/cuda/gpuCMILibrary.h b/cuda/gpuCMILibrary.h index 9b46e38..9acf45a 100644 --- a/cuda/gpuCMILibrary.h +++ b/cuda/gpuCMILibrary.h @@ -11,11 +11,13 @@ extern "C" { jidt_error_t CMIKraskovWithReorderings(int N, float *source, int dimx, float *dest, int dimy, float *cond, int dimz, int k, int thelier, int nb_surrogates, int returnLocals, int useMaxNorm, - int isAlgorithm1, float *result, int reorderingsGiven, int **reorderings); + int isAlgorithm1, float *result, int reorderingsGiven, int **reorderings, + int variableToReorder); jidt_error_t CMIKraskov_C(int N, float *source, int dimx, float *dest, int dimy, float *cond, int dimz, int k, int thelier, int nb_surrogates, - int returnLocals, int useMaxNorm, int isAlgorithm1, float *result); + int returnLocals, int useMaxNorm, int isAlgorithm1, float *result, + int variableToReorder); jidt_error_t CMIKraskovByPointsetChunks(int N, float *source, int dimx, float *dest, int dimy, float *cond, int dimz, int k, int thelier, int nb_surrogates, diff --git a/cuda/kraskovCuda.c b/cuda/kraskovCuda.c index 471e0d4..af848d3 100644 --- a/cuda/kraskovCuda.c +++ b/cuda/kraskovCuda.c @@ -200,7 +200,7 @@ JNIEXPORT jdoubleArray JNICALL /* * Class: infodynamics_measures_continuous_kraskov_ConditionalMutualInfoCalculatorMultiVariateKraskov * Method: CMIKraskov - * Signature: (I[DI[DI[DIIIZZZIZ[I)[D + * Signature: (I[DI[DI[DIIIZZZIZ[II)[D */ JNIEXPORT jdoubleArray JNICALL Java_infodynamics_measures_continuous_kraskov_ConditionalMutualInfoCalculatorMultiVariateKraskov_CMIKraskov( @@ -210,7 +210,8 @@ JNIEXPORT jdoubleArray JNICALL jobjectArray j_condArray, jint j_dimz, jint j_k, jint j_theiler, jboolean j_returnLocals, jboolean j_useMaxNorm, jboolean j_isAlgorithm1, jint j_nbSurrogates, - jboolean j_reorderingsGiven, jobjectArray j_orderings) { + jboolean j_reorderingsGiven, jobjectArray j_orderings, + jint j_variableToReorder) { // Check that incoming data has correct size // ===================== @@ -247,6 +248,7 @@ JNIEXPORT jdoubleArray JNICALL int isAlgorithm1 = j_isAlgorithm1 ? 1 : 0; int nb_surrogates = j_nbSurrogates; int reorderingsGiven = j_reorderingsGiven ? 1 : 0; + int variableToReorder = j_variableToReorder; CPerfTimer pt = startTimer("Java array copy"); @@ -334,13 +336,13 @@ JNIEXPORT jdoubleArray JNICALL if (!reorderingsGiven) { ret = CMIKraskov_C(N, source, dimx, dest, dimy, cond, dimz, k, theiler, nb_surrogates, returnLocals, useMaxNorm, - isAlgorithm1, result); + isAlgorithm1, result, variableToReorder); } else { ret = CMIKraskovWithReorderings(N, source, dimx, dest, dimy, cond, dimz, k, theiler, nb_surrogates, returnLocals, useMaxNorm, isAlgorithm1, result, reorderingsGiven, - reorderings); + reorderings, variableToReorder); } if (JIDT_ERROR == ret) { diff --git a/cuda/unittest.cpp b/cuda/unittest.cpp index 4949690..20078f0 100644 --- a/cuda/unittest.cpp +++ b/cuda/unittest.cpp @@ -632,7 +632,7 @@ CASE("Test correct pointset arrangement without reorderings in CMI") jidt_error_t err; err = CMIKraskov_C(N, source, dimx, dest, dimy, cond, dimz, k, thelier, - 0, returnLocals, useMaxNorm, isAlgorithm1, result1); + 0, returnLocals, useMaxNorm, isAlgorithm1, result1, 1); EXPECT(err == JIDT_SUCCESS); err = CMIKraskovByPointsetChunks(N, source, dimx, dest, dimy, cond, dimz, k, thelier, @@ -741,7 +741,7 @@ CASE("Test correct pointset arrangement in more than one dimension for CMI") jidt_error_t err; err = CMIKraskov_C(N, source, dimx, dest, dimy, cond, dimz, k, thelier, - 0, returnLocals, useMaxNorm, isAlgorithm1, result1); + 0, returnLocals, useMaxNorm, isAlgorithm1, result1, 1); EXPECT(err == JIDT_SUCCESS); err = CMIKraskovByPointsetChunks(N, source, dimx, dest, dimy, cond, dimz, k, thelier, @@ -820,7 +820,7 @@ CASE("Test that same sample in repeated chunks gives same result in CMI") jidt_error_t err; err = CMIKraskov_C(N, source, dimx, dest, dimy, cond, dimz, k, thelier, - 0, returnLocals, useMaxNorm, isAlgorithm1, result1); + 0, returnLocals, useMaxNorm, isAlgorithm1, result1, 1); err = CMIKraskovByPointsetChunks(N*2, source, dimx, dest, dimy, cond, dimz, k, thelier, 2, returnLocals, useMaxNorm, isAlgorithm1, result2, double_pointset); @@ -935,7 +935,7 @@ CASE("Test that sample and identity reordering have same CMI") jidt_error_t err; err = CMIKraskovWithReorderings(N, source, dimx, dest, dimy, cond, dimz, k, thelier, - 1, returnLocals, useMaxNorm, isAlgorithm1, result, reorderingsGiven, reorderings); + 1, returnLocals, useMaxNorm, isAlgorithm1, result, reorderingsGiven, reorderings, 1); EXPECT(err == JIDT_SUCCESS); EXPECT(result[0] == approx(result[1])); @@ -1191,14 +1191,14 @@ CASE("Test that the first result of calculation with surrogates is the same as w jidt_error_t err; err = CMIKraskov_C(N, source, dimx, dest, dimy, cond, dimz, k, thelier, - 0, returnLocals, useMaxNorm, isAlgorithm1, result1); + 0, returnLocals, useMaxNorm, isAlgorithm1, result1, 1); EXPECT(err == JIDT_SUCCESS); float CMI1 = cpuDigamma(k) + result1[0]/((double) N); err = CMIKraskov_C(N, source, dimx, dest, dimy, cond, dimz, k, thelier, - 1, returnLocals, useMaxNorm, isAlgorithm1, result2); + 1, returnLocals, useMaxNorm, isAlgorithm1, result2, 1); EXPECT(err == JIDT_SUCCESS); EXPECT(CMI1 == approx(result2[0])); diff --git a/java/source/infodynamics/measures/continuous/kraskov/ConditionalMutualInfoCalculatorMultiVariateKraskov.java b/java/source/infodynamics/measures/continuous/kraskov/ConditionalMutualInfoCalculatorMultiVariateKraskov.java index 5b25a10..2648bae 100755 --- a/java/source/infodynamics/measures/continuous/kraskov/ConditionalMutualInfoCalculatorMultiVariateKraskov.java +++ b/java/source/infodynamics/measures/continuous/kraskov/ConditionalMutualInfoCalculatorMultiVariateKraskov.java @@ -669,7 +669,7 @@ public abstract class ConditionalMutualInfoCalculatorMultiVariateKraskov */ protected double[] gpuComputeFromObservations(int startTimePoint, int numTimePoints, boolean returnLocals, int nb_surrogates, - int[][] newOrderings) throws Exception { + int[][] newOrderings, int variableToReorder) throws Exception { if (debug) { System.out.println("Start GPU calculation"); @@ -695,8 +695,8 @@ public abstract class ConditionalMutualInfoCalculatorMultiVariateKraskov } res = CMIKraskov(totalObservations, var1Observations, dimensionsVar1, var2Observations, dimensionsVar2, condObservations, dimensionsCond, - k, dynCorrExclTime, returnLocals, useMaxNorm, - isAlgorithm1, nb_surrogates, null!=newOrderings, newOrderings); + k, dynCorrExclTime, returnLocals, useMaxNorm, isAlgorithm1, + nb_surrogates, null!=newOrderings, newOrderings, variableToReorder); if (debug) { System.out.println("GPU calculation finished successfully. Returning results"); } @@ -725,7 +725,7 @@ public abstract class ConditionalMutualInfoCalculatorMultiVariateKraskov double[][] cond, int dimz, int k, int theiler, boolean returnLocals, boolean useMaxNorm, boolean isAlgorithm1, int nb_surrogates, boolean orderingsGiven, - int[][] newOrderings); + int[][] newOrderings, int variableToReorder); /** * Internal method to ensure that the Cuda native library has been correctly @@ -807,17 +807,17 @@ public abstract class ConditionalMutualInfoCalculatorMultiVariateKraskov } /** - * {@inheritDoc} + * {@inheritDoc} */ @Override - public EmpiricalMeasurementDistribution computeSignificance(int numPermutationsToCheck) - throws Exception { + public EmpiricalMeasurementDistribution computeSignificance( + int variableToReorder, int numPermutationsToCheck) throws Exception { if (useGPU) { - double[] res = gpuComputeFromObservations(0, totalObservations, false, numPermutationsToCheck, null); + double[] res = gpuComputeFromObservations(0, totalObservations, false, numPermutationsToCheck, null, variableToReorder); return new EmpiricalMeasurementDistribution( MatrixUtils.select(res, 1, res.length - 1), res[0]); } else { - return super.computeSignificance(numPermutationsToCheck); + return super.computeSignificance(variableToReorder, numPermutationsToCheck); } } @@ -825,14 +825,14 @@ public abstract class ConditionalMutualInfoCalculatorMultiVariateKraskov * {@inheritDoc} */ @Override - public EmpiricalMeasurementDistribution computeSignificance(int[][] newOrderings) - throws Exception { + public EmpiricalMeasurementDistribution computeSignificance( + int variableToReorder, int[][] newOrderings) throws Exception { if (useGPU) { - double[] res = gpuComputeFromObservations(0, totalObservations, false, newOrderings.length, newOrderings); + double[] res = gpuComputeFromObservations(0, totalObservations, false, newOrderings.length, newOrderings, variableToReorder); return new EmpiricalMeasurementDistribution( MatrixUtils.select(res, 1, res.length - 1), res[0]); } else { - return super.computeSignificance(newOrderings); + return super.computeSignificance(variableToReorder, newOrderings); } }