diff options
-rw-r--r-- | FEAST/MIToolbox/ArrayOperations.c | 17 | ||||
-rw-r--r-- | FEAST/MIToolbox/CalculateProbability.c | 33 | ||||
-rw-r--r-- | FEAST/MIToolbox/MIToolbox.h | 5 | ||||
-rw-r--r-- | FEAST/MIToolbox/Makefile | 6 | ||||
-rw-r--r-- | FEAST/MIToolbox/MutualInformation.c | 3 | ||||
-rw-r--r-- | FEAST/MIToolbox/RenyiEntropy.c | 7 | ||||
-rw-r--r-- | FEAST/MIToolbox/util.c | 14 | ||||
-rw-r--r-- | FEAST/MIToolbox/util.h | 1 |
8 files changed, 59 insertions, 27 deletions
diff --git a/FEAST/MIToolbox/ArrayOperations.c b/FEAST/MIToolbox/ArrayOperations.c index 00a8324..bd53403 100644 --- a/FEAST/MIToolbox/ArrayOperations.c +++ b/FEAST/MIToolbox/ArrayOperations.c @@ -30,6 +30,7 @@ #include "MIToolbox.h" #include "ArrayOperations.h" +#include "util.h" void printDoubleVector(double *vector, int vectorLength) { @@ -53,7 +54,7 @@ void printIntVector(int *vector, int vectorLength) int numberOfUniqueValues(double *featureVector, int vectorLength) { int uniqueValues = 0; - double *valuesArray = (double *) CALLOC_FUNC(vectorLength,sizeof(double)); + double *valuesArray = safe_calloc(vectorLength,sizeof(double)); int found = 0; int j = 0; @@ -153,8 +154,8 @@ int mergeArrays(double *firstVector, double *secondVector, double *outputVector, int stateCount; int curIndex; - firstNormalisedVector = (int *) CALLOC_FUNC(vectorLength,sizeof(int)); - secondNormalisedVector = (int *) CALLOC_FUNC(vectorLength,sizeof(int)); + firstNormalisedVector = safe_calloc(vectorLength,sizeof(int)); + secondNormalisedVector = safe_calloc(vectorLength,sizeof(int)); firstNumStates = normaliseArray(firstVector,firstNormalisedVector,vectorLength); secondNumStates = normaliseArray(secondVector,secondNormalisedVector,vectorLength); @@ -163,7 +164,7 @@ int mergeArrays(double *firstVector, double *secondVector, double *outputVector, ** printVector(firstNormalisedVector,vectorLength); ** printVector(secondNormalisedVector,vectorLength); */ - stateMap = (int *) CALLOC_FUNC(firstNumStates*secondNumStates,sizeof(int)); + stateMap = safe_calloc(firstNumStates*secondNumStates,sizeof(int)); stateCount = 1; for (i = 0; i < vectorLength; i++) { @@ -196,8 +197,8 @@ int mergeArraysArities(double *firstVector, int numFirstStates, double *secondVe int totalStates; int firstStateCheck, secondStateCheck; - firstNormalisedVector = (int *) CALLOC_FUNC(vectorLength,sizeof(int)); - secondNormalisedVector = (int *) CALLOC_FUNC(vectorLength,sizeof(int)); + firstNormalisedVector = safe_calloc(vectorLength,sizeof(int)); + secondNormalisedVector = safe_calloc(vectorLength,sizeof(int)); firstStateCheck = normaliseArray(firstVector,firstNormalisedVector,vectorLength); secondStateCheck = normaliseArray(secondVector,secondNormalisedVector,vectorLength); @@ -242,7 +243,7 @@ int mergeMultipleArrays(double *inputMatrix, double *outputVector, int matrixWid } else { - normalisedVector = (int *) CALLOC_FUNC(vectorLength,sizeof(int)); + normalisedVector = safe_calloc(vectorLength,sizeof(int)); currentNumStates = normaliseArray(inputMatrix,normalisedVector,vectorLength); for (i = 0; i < vectorLength; i++) { @@ -274,7 +275,7 @@ int mergeMultipleArraysArities(double *inputMatrix, double *outputVector, int ma } else { - normalisedVector = (int *) CALLOC_FUNC(vectorLength,sizeof(int)); + normalisedVector = safe_calloc(vectorLength,sizeof(int)); currentNumStates = normaliseArray(inputMatrix,normalisedVector,vectorLength); for (i = 0; i < vectorLength; i++) { diff --git a/FEAST/MIToolbox/CalculateProbability.c b/FEAST/MIToolbox/CalculateProbability.c index 6d4f19b..89c4e01 100644 --- a/FEAST/MIToolbox/CalculateProbability.c +++ b/FEAST/MIToolbox/CalculateProbability.c @@ -31,6 +31,7 @@ #include "MIToolbox.h" #include "ArrayOperations.h" #include "CalculateProbability.h" +#include "util.h" JointProbabilityState calculateJointProbability(double *firstVector, double *secondVector, int vectorLength) { @@ -49,20 +50,30 @@ JointProbabilityState calculateJointProbability(double *firstVector, double *sec double length = vectorLength; JointProbabilityState state; - firstNormalisedVector = (int *) CALLOC_FUNC(vectorLength,sizeof(int)); - secondNormalisedVector = (int *) CALLOC_FUNC(vectorLength,sizeof(int)); + firstNormalisedVector = safe_calloc(vectorLength,sizeof(int)); + secondNormalisedVector = safe_calloc(vectorLength,sizeof(int)); firstNumStates = normaliseArray(firstVector,firstNormalisedVector,vectorLength); secondNumStates = normaliseArray(secondVector,secondNormalisedVector,vectorLength); jointNumStates = firstNumStates * secondNumStates; - firstStateCounts = (int *) CALLOC_FUNC(firstNumStates,sizeof(int)); - secondStateCounts = (int *) CALLOC_FUNC(secondNumStates,sizeof(int)); - jointStateCounts = (int *) CALLOC_FUNC(jointNumStates,sizeof(int)); + firstStateCounts = safe_calloc(firstNumStates,sizeof(int)); + secondStateCounts = safe_calloc(secondNumStates,sizeof(int)); + jointStateCounts = safe_calloc(jointNumStates,sizeof(int)); - firstStateProbs = (double *) CALLOC_FUNC(firstNumStates,sizeof(double)); - secondStateProbs = (double *) CALLOC_FUNC(secondNumStates,sizeof(double)); - jointStateProbs = (double *) CALLOC_FUNC(jointNumStates,sizeof(double)); + firstStateProbs = safe_calloc(firstNumStates,sizeof(double)); + secondStateProbs =safe_calloc(secondNumStates,sizeof(double)); + jointStateProbs = safe_calloc(jointNumStates,sizeof(double)); + + if(firstNormalisedVector == NULL || secondNormalisedVector == NULL || + firstStateCounts == NULL || secondStateCounts == NULL || jointStateCounts == NULL || + firstStateProbs == NULL || secondStateProbs == NULL || jointStateProbs == NULL) { + + fprintf(stderr, "could not allocate enough memory"); + exit(EXIT_FAILURE); + + } + /* optimised version, less numerically stable double fractionalState = 1.0 / vectorLength; @@ -143,12 +154,12 @@ ProbabilityState calculateProbability(double *dataVector, int vectorLength) int i; double length = vectorLength; - normalisedVector = (int *) CALLOC_FUNC(vectorLength,sizeof(int)); + normalisedVector = safe_calloc(vectorLength,sizeof(int)); numStates = normaliseArray(dataVector,normalisedVector,vectorLength); - stateCounts = (int *) CALLOC_FUNC(numStates,sizeof(int)); - stateProbs = (double *) CALLOC_FUNC(numStates,sizeof(double)); + stateCounts = safe_calloc(numStates,sizeof(int)); + stateProbs = safe_calloc(numStates,sizeof(double)); /* optimised version, may have floating point problems fractionalState = 1.0 / vectorLength; diff --git a/FEAST/MIToolbox/MIToolbox.h b/FEAST/MIToolbox/MIToolbox.h index bba6284..728be31 100644 --- a/FEAST/MIToolbox/MIToolbox.h +++ b/FEAST/MIToolbox/MIToolbox.h @@ -39,15 +39,14 @@ #define C_IMPLEMENTATION #include <stdio.h> #include <stdlib.h> - #define CALLOC_FUNC calloc + #define UNSAFE_CALLOC_FUNC calloc #define FREE_FUNC free #else #define MEX_IMPLEMENTATION #include "mex.h" - #define CALLOC_FUNC mxCalloc + #define UNSAFE_CALLOC_FUNC mxCalloc #define FREE_FUNC mxFree #define printf mexPrintf /*for Octave-3.2*/ #endif #endif - diff --git a/FEAST/MIToolbox/Makefile b/FEAST/MIToolbox/Makefile index 992e7cd..f38a369 100644 --- a/FEAST/MIToolbox/Makefile +++ b/FEAST/MIToolbox/Makefile @@ -25,11 +25,15 @@ PREFIX = /usr CXXFLAGS = -O3 -fPIC COMPILER = gcc objects = ArrayOperations.o CalculateProbability.o Entropy.o \ - MutualInformation.o RenyiEntropy.o RenyiMutualInformation.o + MutualInformation.o RenyiEntropy.o RenyiMutualInformation.o util.o libMIToolbox.so : $(objects) $(COMPILER) $(CXXFLAGS) -shared -o libMIToolbox.so $(objects) + +util.o: util.c + $(COMPILER) $(CXXFLAGS) -DCOMPILE_C -c util.c + RenyiMutualInformation.o: RenyiMutualInformation.c MIToolbox.h ArrayOperations.h \ CalculateProbability.h RenyiEntropy.h $(COMPILER) $(CXXFLAGS) -DCOMPILE_C -c RenyiMutualInformation.c diff --git a/FEAST/MIToolbox/MutualInformation.c b/FEAST/MIToolbox/MutualInformation.c index 0fb4766..d7d10b6 100644 --- a/FEAST/MIToolbox/MutualInformation.c +++ b/FEAST/MIToolbox/MutualInformation.c @@ -35,6 +35,7 @@ #include "CalculateProbability.h" #include "Entropy.h" #include "MutualInformation.h" +#include "util.h" double calculateMutualInformation(double *dataVector, double *targetVector, int vectorLength) { @@ -76,7 +77,7 @@ double calculateConditionalMutualInformation(double *dataVector, double *targetV { double mutualInformation = 0.0; double firstCondition, secondCondition; - double *mergedVector = (double *) CALLOC_FUNC(vectorLength,sizeof(double)); + double *mergedVector = safe_calloc(vectorLength,sizeof(double)); mergeArrays(targetVector,conditionVector,mergedVector,vectorLength); diff --git a/FEAST/MIToolbox/RenyiEntropy.c b/FEAST/MIToolbox/RenyiEntropy.c index 32c5ff9..c0c4bd4 100644 --- a/FEAST/MIToolbox/RenyiEntropy.c +++ b/FEAST/MIToolbox/RenyiEntropy.c @@ -33,6 +33,7 @@ #include "ArrayOperations.h" #include "CalculateProbability.h" #include "Entropy.h" +#include "util.h" double calculateRenyiEntropy(double alpha, double *dataVector, int vectorLength) { @@ -111,8 +112,8 @@ double calcCondRenyiEnt(double alpha, double *dataVector, double *conditionVecto ** first generate the seperate variables */ - double *seperateVectors = (double *) CALLOC_FUNC(uniqueInCondVector*vectorLength,sizeof(double)); - int *seperateVectorCount = (int *) CALLOC_FUNC(uniqueInCondVector,sizeof(int)); + double *seperateVectors = safe_calloc(uniqueInCondVector*vectorLength,sizeof(double)); + int *seperateVectorCount = safe_calloc(uniqueInCondVector,sizeof(int)); double seperateVectorProb = 0.0; int i,j; double entropy = 0.0; @@ -121,7 +122,7 @@ double calcCondRenyiEnt(double alpha, double *dataVector, double *conditionVecto double tempEntropy; ProbabilityState state; - double **seperateVectors2D = (double **) CALLOC_FUNC(uniqueInCondVector,sizeof(double*)); + double **seperateVectors2D = safe_calloc(uniqueInCondVector,sizeof(double*)); for(j=0; j < uniqueInCondVector; j++) seperateVectors2D[j] = seperateVectors + (int)j*vectorLength; diff --git a/FEAST/MIToolbox/util.c b/FEAST/MIToolbox/util.c new file mode 100644 index 0000000..d9d7517 --- /dev/null +++ b/FEAST/MIToolbox/util.c @@ -0,0 +1,14 @@ +#include <string.h> +#include <errno.h> + +#include "MIToolbox.h" + +// a wrapper for calloc that checks if it's allocated +void *safe_calloc(size_t nelem, size_t elsize) { + void *allocated = UNSAFE_CALLOC_FUNC(nelem, elsize); + if(allocated == NULL) { + fprintf(stderr, "Error: %s\n", strerror(errno)); + exit(EXIT_FAILURE); + } + return allocated; +} diff --git a/FEAST/MIToolbox/util.h b/FEAST/MIToolbox/util.h new file mode 100644 index 0000000..6bbddb7 --- /dev/null +++ b/FEAST/MIToolbox/util.h @@ -0,0 +1 @@ +void *safe_calloc(size_t nelem, size_t elsize); |