diff --git a/protected/gatk-protected/src/main/java/org/broadinstitute/sting/utils/pairhmm/VectorLoglessPairHMM.java b/protected/gatk-protected/src/main/java/org/broadinstitute/sting/utils/pairhmm/VectorLoglessPairHMM.java index f27081ae9..b94e7e3ec 100644 --- a/protected/gatk-protected/src/main/java/org/broadinstitute/sting/utils/pairhmm/VectorLoglessPairHMM.java +++ b/protected/gatk-protected/src/main/java/org/broadinstitute/sting/utils/pairhmm/VectorLoglessPairHMM.java @@ -154,6 +154,7 @@ public class VectorLoglessPairHMM extends JNILoglessPairHMM { //Hold the mapping between haplotype and index in the list of Haplotypes passed to initialize //Use this mapping in computeLikelihoods to find the likelihood value corresponding to a given Haplotype private HashMap haplotypeToHaplotypeListIdxMap = new HashMap(); + private JNIHaplotypeDataHolderClass[] mHaplotypeDataArray; @Override public HashMap getHaplotypeToHaplotypeListIdxMap() { return haplotypeToHaplotypeListIdxMap; } @@ -166,17 +167,17 @@ public class VectorLoglessPairHMM extends JNILoglessPairHMM { public void initialize( final List haplotypes, final Map> perSampleReadList, final int readMaxLength, final int haplotypeMaxLength ) { int numHaplotypes = haplotypes.size(); - JNIHaplotypeDataHolderClass[] haplotypeDataArray = new JNIHaplotypeDataHolderClass[numHaplotypes]; + mHaplotypeDataArray = new JNIHaplotypeDataHolderClass[numHaplotypes]; int idx = 0; haplotypeToHaplotypeListIdxMap.clear(); for(final Haplotype currHaplotype : haplotypes) { - haplotypeDataArray[idx] = new JNIHaplotypeDataHolderClass(); - haplotypeDataArray[idx].haplotypeBases = currHaplotype.getBases(); + mHaplotypeDataArray[idx] = new JNIHaplotypeDataHolderClass(); + mHaplotypeDataArray[idx].haplotypeBases = currHaplotype.getBases(); haplotypeToHaplotypeListIdxMap.put(currHaplotype, idx); ++idx; } - jniInitializeHaplotypes(numHaplotypes, haplotypeDataArray); + jniInitializeHaplotypes(numHaplotypes, mHaplotypeDataArray); } /** * Tell JNI to release arrays - really important if native code is directly accessing Java memory, if not @@ -227,8 +228,8 @@ public class VectorLoglessPairHMM extends JNILoglessPairHMM { //for(reads) // for(haplotypes) // compute_full_prob() - jniComputeLikelihoods(readListSize, numHaplotypes, readDataArray, null, mLikelihoodArray, 12); - + jniComputeLikelihoods(readListSize, numHaplotypes, readDataArray, mHaplotypeDataArray, mLikelihoodArray, 12); + final PerReadAlleleLikelihoodMap likelihoodMap = new PerReadAlleleLikelihoodMap(); idx = 0; int idxInsideHaplotypeList = 0; diff --git a/public/VectorPairHMM/src/main/c++/Sandbox.java b/public/VectorPairHMM/src/main/c++/Sandbox.java index d6b7c2eae..698c1b48c 100644 --- a/public/VectorPairHMM/src/main/c++/Sandbox.java +++ b/public/VectorPairHMM/src/main/c++/Sandbox.java @@ -87,20 +87,21 @@ public class Sandbox { } private native void jniInitializeHaplotypes(final int numHaplotypes, JNIHaplotypeDataHolderClass[] haplotypeDataArray); + private JNIHaplotypeDataHolderClass[] mHaplotypeDataArray = null; //Used to transfer data to JNI //Since the haplotypes are the same for all calls to computeLikelihoods within a region, transfer the haplotypes only once to the JNI per region public void initialize(final List haplotypes) { int numHaplotypes = haplotypes.size(); - JNIHaplotypeDataHolderClass[] haplotypeDataArray = new JNIHaplotypeDataHolderClass[numHaplotypes]; + mHaplotypeDataArray = new JNIHaplotypeDataHolderClass[numHaplotypes]; int idx = 0; for(final JNIHaplotypeDataHolderClass currHaplotype : haplotypes) { - haplotypeDataArray[idx] = new JNIHaplotypeDataHolderClass(); - haplotypeDataArray[idx].haplotypeBases = currHaplotype.haplotypeBases; + mHaplotypeDataArray[idx] = new JNIHaplotypeDataHolderClass(); + mHaplotypeDataArray[idx].haplotypeBases = currHaplotype.haplotypeBases; ++idx; } - jniInitializeHaplotypes(numHaplotypes, haplotypeDataArray); + jniInitializeHaplotypes(numHaplotypes, mHaplotypeDataArray); } /** * Tell JNI to release arrays - really important if native code is directly accessing Java memory, if not @@ -144,7 +145,7 @@ public class Sandbox { //for(reads) // for(haplotypes) // compute_full_prob() - jniComputeLikelihoods(readListSize, numHaplotypes, readDataArray, null, mLikelihoodArray, 12); + jniComputeLikelihoods(readListSize, numHaplotypes, readDataArray, mHaplotypeDataArray, mLikelihoodArray, 12); computeTime += (System.nanoTime() - startTime); } diff --git a/public/VectorPairHMM/src/main/c++/jni_common.h b/public/VectorPairHMM/src/main/c++/jni_common.h index 23c323246..ee43da2ec 100644 --- a/public/VectorPairHMM/src/main/c++/jni_common.h +++ b/public/VectorPairHMM/src/main/c++/jni_common.h @@ -26,10 +26,12 @@ #ifndef JNI_COMMON_H #define JNI_COMMON_H +/*#define SINGLE_THREADED_ONLY 1*/ #include /*#define ENABLE_ASSERTIONS 1*/ +#ifdef SINGLE_THREADED_ONLY #define DO_PROFILING 1 -/*#define DEBUG 1*/ +#endif /*#define DEBUG0_1 1*/ /*#define DEBUG3 1*/ /*#define DUMP_TO_SANDBOX 1*/ diff --git a/public/VectorPairHMM/src/main/c++/org_broadinstitute_sting_utils_pairhmm_VectorLoglessPairHMM.cc b/public/VectorPairHMM/src/main/c++/org_broadinstitute_sting_utils_pairhmm_VectorLoglessPairHMM.cc index 0b54c8a81..220b1aa60 100644 --- a/public/VectorPairHMM/src/main/c++/org_broadinstitute_sting_utils_pairhmm_VectorLoglessPairHMM.cc +++ b/public/VectorPairHMM/src/main/c++/org_broadinstitute_sting_utils_pairhmm_VectorLoglessPairHMM.cc @@ -73,20 +73,15 @@ JNIEXPORT void JNICALL Java_org_broadinstitute_sting_utils_pairhmm_VectorLogless } } -//Since the list of haplotypes against which the reads are evaluated in PairHMM is the same for a region, -//transfer the list only once -vector > g_haplotypeBasesArrayVector; -vector g_haplotypeBasesLengths; -JNIEXPORT void JNICALL Java_org_broadinstitute_sting_utils_pairhmm_VectorLoglessPairHMM_jniInitializeHaplotypes - (JNIEnv * env, jobject thisObject, jint numHaplotypes, jobjectArray haplotypeDataArray) +JNIEXPORT void JNICALL initializeHaplotypes + (JNIEnv * env, jobject& thisObject, jint numHaplotypes, jobjectArray& haplotypeDataArray, + vector >& haplotypeBasesArrayVector, vector& haplotypeBasesLengths) { jboolean is_copy = JNI_FALSE; - //To ensure, GET_BYTE_ARRAY_ELEMENTS is invoked only once for each haplotype, store bytearrays in a vector - vector >& haplotypeBasesArrayVector = g_haplotypeBasesArrayVector; haplotypeBasesArrayVector.clear(); - g_haplotypeBasesLengths.clear(); + haplotypeBasesLengths.clear(); haplotypeBasesArrayVector.resize(numHaplotypes); - g_haplotypeBasesLengths.resize(numHaplotypes); + haplotypeBasesLengths.resize(numHaplotypes); jsize haplotypeBasesLength = 0; for(unsigned j=0;j >& haplotypeBasesArrayVector, vector& haplotypeBasesLengths + ) +{ + //Now release haplotype arrays + for(int j=haplotypeBasesArrayVector.size()-1;j>=0;--j) //note the order - reverse of GET + { + RELEASE_BYTE_ARRAY_ELEMENTS(haplotypeBasesArrayVector[j].first, haplotypeBasesArrayVector[j].second, JNI_RO_RELEASE_MODE); + env->DeleteGlobalRef(haplotypeBasesArrayVector[j].first); //free the global reference + } + haplotypeBasesArrayVector.clear(); + haplotypeBasesLengths.clear(); +} + + +vector > g_haplotypeBasesArrayVector; +vector g_haplotypeBasesLengths; +//Since the list of haplotypes against which the reads are evaluated in PairHMM is the same for a region, +//transfer the list only once +//Works only for ST case as the haplotype data is stored in global variables +JNIEXPORT void JNICALL Java_org_broadinstitute_sting_utils_pairhmm_VectorLoglessPairHMM_jniInitializeHaplotypes + (JNIEnv * env, jobject thisObject, jint numHaplotypes, jobjectArray haplotypeDataArray) +{ +#ifdef SINGLE_THREADED_ONLY + //To ensure, GET_BYTE_ARRAY_ELEMENTS is invoked only once for each haplotype, store bytearrays in a vector + initializeHaplotypes(env, thisObject, numHaplotypes, haplotypeDataArray, g_haplotypeBasesArrayVector, g_haplotypeBasesLengths); +#endif +} + + //Create a vector of testcases for computation - copy the references to bytearrays read/readQuals etc into the appropriate //testcase struct inline JNIEXPORT void JNICALL Java_org_broadinstitute_sting_utils_pairhmm_VectorLoglessPairHMM_jniInitializeTestcasesVector (JNIEnv* env, jint numReads, jint numHaplotypes, jobjectArray& readDataArray, - vector > >& readBasesArrayVector, vector& tc_array) + vector > >& readBasesArrayVector, + vector >& haplotypeBasesArrayVector, vector& haplotypeBasesLengths, + vector& tc_array) { jboolean is_copy = JNI_FALSE; - //haplotype vector from earlier store - note the reference to vector, not copying - vector >& haplotypeBasesArrayVector = g_haplotypeBasesArrayVector; unsigned tc_idx = 0; for(unsigned i=0;i >& haplotypeBasesArrayVector = g_haplotypeBasesArrayVector; + vector& haplotypeBasesLengths = g_haplotypeBasesLengths; +#else + vector > l_haplotypeBasesArrayVector; + vector >& haplotypeBasesArrayVector = l_haplotypeBasesArrayVector; + vector l_haplotypeBasesLengths; + vector& haplotypeBasesLengths = l_haplotypeBasesLengths; + initializeHaplotypes(env, thisObject, numHaplotypes, haplotypeDataArray, haplotypeBasesArrayVector, haplotypeBasesLengths); +#endif //Copy byte array references from Java memory into vector of testcase structs Java_org_broadinstitute_sting_utils_pairhmm_VectorLoglessPairHMM_jniInitializeTestcasesVector(env, - numReads, numHaplotypes, readDataArray, readBasesArrayVector, tc_array); + numReads, numHaplotypes, readDataArray, readBasesArrayVector, haplotypeBasesArrayVector, haplotypeBasesLengths, tc_array); + #ifdef DO_PROFILING g_load_time_initializer.m_data_transfer_time += diff_time(start_time); #endif @@ -306,7 +343,6 @@ JNIEXPORT void JNICALL Java_org_broadinstitute_sting_utils_pairhmm_VectorLogless assert(env->GetArrayLength(likelihoodArray) == numTestCases); #endif #ifdef DO_WARMUP //ignore - only for crazy profiling - vector >& haplotypeBasesArrayVector = g_haplotypeBasesArrayVector; for(unsigned i=0;iGetArrayLength(haplotypeBasesArrayVector[i].first); @@ -331,7 +367,7 @@ JNIEXPORT void JNICALL Java_org_broadinstitute_sting_utils_pairhmm_VectorLogless #ifdef DO_PROFILING g_load_time_initializer.m_compute_time += diff_time(start_time); #endif -#ifdef DEBUG +#ifdef DUMP_COMPUTE_VALUES for(unsigned tc_idx=0;tc_idx >& haplotypeBasesArrayVector = g_haplotypeBasesArrayVector; - //Now release haplotype arrays - for(int j=haplotypeBasesArrayVector.size()-1;j>=0;--j) //note the order - reverse of GET - { - RELEASE_BYTE_ARRAY_ELEMENTS(haplotypeBasesArrayVector[j].first, haplotypeBasesArrayVector[j].second, JNI_RO_RELEASE_MODE); - env->DeleteGlobalRef(haplotypeBasesArrayVector[j].first); //free the global reference - } - haplotypeBasesArrayVector.clear(); - g_haplotypeBasesLengths.clear(); +#ifdef SINGLE_THREADED_ONLY + releaseHaplotypes(env, thisObject, g_haplotypeBasesArrayVector, g_haplotypeBasesLengths); +#endif } @@ -375,7 +409,7 @@ JNIEXPORT void JNICALL Java_org_broadinstitute_sting_utils_pairhmm_VectorLogless #ifdef DO_PROFILING g_load_time_initializer.print_profiling(); #endif -#ifdef DEBUG +#ifdef DUMP_COMPUTE_VALUES g_load_time_initializer.debug_close(); #endif } diff --git a/public/VectorPairHMM/src/main/c++/pairhmm-1-base.cc b/public/VectorPairHMM/src/main/c++/pairhmm-1-base.cc index 7ff219b88..d2cc7d903 100644 --- a/public/VectorPairHMM/src/main/c++/pairhmm-1-base.cc +++ b/public/VectorPairHMM/src/main/c++/pairhmm-1-base.cc @@ -22,10 +22,6 @@ *THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ - -//#define DEBUG 1 -//#define DEBUG0_1 1 -//#define DEBUG3 1 #include "headers.h" #include "utils.h" #include "LoadTimeInitializer.h" diff --git a/public/VectorPairHMM/src/main/c++/utils.h b/public/VectorPairHMM/src/main/c++/utils.h index ce9ecd8c9..d384ffbd1 100644 --- a/public/VectorPairHMM/src/main/c++/utils.h +++ b/public/VectorPairHMM/src/main/c++/utils.h @@ -68,7 +68,7 @@ void do_compute(char* filename, bool use_old_read_testcase=true, unsigned chunk_ //#define DO_WARMUP //#define DO_REPEAT_PROFILING -//#define DUMP_COMPUTE_VALUES 1 +/*#define DUMP_COMPUTE_VALUES 1*/ #define BATCH_SIZE 10000 #define RUN_HYBRID diff --git a/public/sting-utils/src/main/resources/org/broadinstitute/sting/utils/pairhmm/libVectorLoglessPairHMM.so b/public/sting-utils/src/main/resources/org/broadinstitute/sting/utils/pairhmm/libVectorLoglessPairHMM.so index 6a7ba0cdf..7cd8b1f73 100644 Binary files a/public/sting-utils/src/main/resources/org/broadinstitute/sting/utils/pairhmm/libVectorLoglessPairHMM.so and b/public/sting-utils/src/main/resources/org/broadinstitute/sting/utils/pairhmm/libVectorLoglessPairHMM.so differ