/* * Class: beagle_BeagleJNIWrapper * Method: getResourceList * Signature: ()[Lbeagle/ResourceDetails; */ JNIEXPORT jobjectArray JNICALL Java_beagle_BeagleJNIWrapper_getResourceList (JNIEnv *env, jobject obj) { BeagleResourceList* rl = beagleGetResourceList(); if (rl == NULL) { return NULL; } jclass objClass = env->FindClass("beagle/ResourceDetails"); if (objClass == NULL) { printf("NULL returned in FindClass: can't find class: beagle/ResourceDetails\n"); return NULL; } jmethodID constructorMethodID = env->GetMethodID(objClass, "<init>","(I)V"); if (constructorMethodID == NULL) { printf("NULL returned in FindClass: can't find constructor for class: beagle/ResourceDetails\n"); return NULL; } jmethodID setNameMethodID = env->GetMethodID(objClass, "setName", "(Ljava/lang/String;)V"); if (setNameMethodID == NULL) { printf("NULL returned in FindClass: can't find 'setName' method in class: beagle/ResourceDetails\n"); return NULL; } jmethodID setDescriptionID = env->GetMethodID(objClass, "setDescription", "(Ljava/lang/String;)V"); if (setDescriptionID == NULL) { printf("NULL returned in FindClass: can't find 'setDescription' method in class: beagle/ResourceDetails\n"); return NULL; } jmethodID setFlagsMethodID = env->GetMethodID(objClass, "setFlags", "(J)V"); if (setFlagsMethodID == NULL) { printf("NULL returned in FindClass: can't find 'setFlags' method in class: beagle/ResourceDetails\n"); return NULL; } jobjectArray resourceArray = env->NewObjectArray(rl->length, objClass, NULL); for (int i = 0; i < rl->length; i++) { jobject resourceObj = env->NewObject(objClass, constructorMethodID, i); jstring jString = env->NewStringUTF(rl->list[i].name); env->CallVoidMethod(resourceObj, setNameMethodID, jString); jString = env->NewStringUTF(rl->list[i].description); env->CallVoidMethod(resourceObj, setDescriptionID, jString); env->CallVoidMethod(resourceObj, setFlagsMethodID, rl->list[i].supportFlags); env->SetObjectArrayElement(resourceArray, i, resourceObj); } return resourceArray; }
void print_beagle_resource_list() { int i; BeagleResourceList* rList; rList = beagleGetResourceList(); fprintf(stdout, "\n\tAvailable resources:\n"); for (i = 0; i < rList->length; i++) { fprintf(stdout, "\t\tResource %i:\n\t\tName : %s\n", i, rList->list[i].name); fprintf(stdout, "\t\t\tDesc : %s\n", rList->list[i].description); fprintf(stdout, "\t\t\tFlags:"); print_beagle_flags(rList->list[i].supportFlags); fprintf(stdout, "\n"); } fflush(stdout); }
void printResourceList() { // print version and citation info fprintf(stdout, "BEAGLE version %s\n", beagleGetVersion()); fprintf(stdout, "%s\n", beagleGetCitation()); // print resource list BeagleResourceList* rList; rList = beagleGetResourceList(); fprintf(stdout, "Available resources:\n"); for (int i = 0; i < rList->length; i++) { fprintf(stdout, "\tResource %i:\n\t\tName : %s\n", i, rList->list[i].name); fprintf(stdout, "\t\tDesc : %s\n", rList->list[i].description); fprintf(stdout, "\t\tFlags:"); printFlags(rList->list[i].supportFlags); fprintf(stdout, "\n"); } fprintf(stdout, "\n"); std::exit(0); }
int main( int argc, const char* argv[] ) { // print resource list BeagleResourceList* rList; rList = beagleGetResourceList(); fprintf(stdout, "Available resources:\n"); for (int i = 0; i < rList->length; i++) { fprintf(stdout, "\tResource %i:\n\t\tName : %s\n", i, rList->list[i].name); fprintf(stdout, "\t\tDesc : %s\n", rList->list[i].description); fprintf(stdout, "\t\tFlags:"); printFlags(rList->list[i].supportFlags); fprintf(stdout, "\n"); } fprintf(stdout, "\n"); bool manualScaling = false; bool autoScaling = false; bool gRates = false; // generalized rate categories, separate root buffers // is nucleotides... int stateCount = 4; // get the number of site patterns int nPatterns = strlen(human); int rateCategoryCount = 4; int nRateCats = (gRates ? 1 : rateCategoryCount); int nRootCount = (!gRates ? 1 : rateCategoryCount); int nPartBuffs = 4 + nRootCount; int scaleCount = (manualScaling ? 2 + nRootCount : 0); // initialize the instance BeagleInstanceDetails instDetails; // create an instance of the BEAGLE library int instance = beagleCreateInstance( 3, /**< Number of tip data elements (input) */ nPartBuffs, /**< Number of partials buffers to create (input) */ 0, /**< Number of compact state representation buffers to create (input) */ stateCount, /**< Number of states in the continuous-time Markov chain (input) */ nPatterns, /**< Number of site patterns to be handled by the instance (input) */ 1, /**< Number of rate matrix eigen-decomposition buffers to allocate (input) */ 4, /**< Number of rate matrix buffers (input) */ nRateCats, /**< Number of rate categories (input) */ scaleCount, /**< Number of scaling buffers */ NULL, /**< List of potential resource on which this instance is allowed (input, NULL implies no restriction */ 0, /**< Length of resourceList list (input) */ BEAGLE_FLAG_PRECISION_DOUBLE | BEAGLE_FLAG_PROCESSOR_GPU | (autoScaling ? BEAGLE_FLAG_SCALING_AUTO : 0), /**< Bit-flags indicating preferred implementation charactertistics, see BeagleFlags (input) */ 0, /**< Bit-flags indicating required implementation characteristics, see BeagleFlags (input) */ &instDetails); if (instance < 0) { fprintf(stderr, "Failed to obtain BEAGLE instance\n\n"); exit(1); } int rNumber = instDetails.resourceNumber; fprintf(stdout, "Using resource %i:\n", rNumber); fprintf(stdout, "\tRsrc Name : %s\n",instDetails.resourceName); fprintf(stdout, "\tImpl Name : %s\n", instDetails.implName); fprintf(stdout, "\tImpl Desc : %s\n", instDetails.implDescription); fprintf(stdout, "\tFlags:"); printFlags(instDetails.flags); fprintf(stdout, "\n\n"); if (!(instDetails.flags & BEAGLE_FLAG_SCALING_AUTO)) autoScaling = false; // beagleSetTipStates(instance, 0, getStates(human)); // beagleSetTipStates(instance, 1, getStates(chimp)); // beagleSetTipStates(instance, 2, getStates(gorilla)); // set the sequences for each tip using partial likelihood arrays double *humanPartials = getPartials(human); double *chimpPartials = getPartials(chimp); double *gorillaPartials = getPartials(gorilla); beagleSetTipPartials(instance, 0, humanPartials); beagleSetTipPartials(instance, 1, chimpPartials); beagleSetTipPartials(instance, 2, gorillaPartials); //#ifdef _WIN32 // std::vector<double> rates(rateCategoryCount); //#else // double rates[rateCategoryCount]; //#endif // for (int i = 0; i < rateCategoryCount; i++) { // rates[i] = 1.0; // } double rates[4] = { 0.03338775, 0.25191592, 0.82026848, 2.89442785 }; // create base frequency array double freqs[16] = { 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25 }; // create an array containing site category weights double* weights = (double*) malloc(sizeof(double) * rateCategoryCount); for (int i = 0; i < rateCategoryCount; i++) { weights[i] = 1.0/rateCategoryCount; } double* patternWeights = (double*) malloc(sizeof(double) * nPatterns); for (int i = 0; i < nPatterns; i++) { patternWeights[i] = 1.0; } // an eigen decomposition for the JC69 model double evec[4 * 4] = { 1.0, 2.0, 0.0, 0.5, 1.0, -2.0, 0.5, 0.0, 1.0, 2.0, 0.0, -0.5, 1.0, -2.0, -0.5, 0.0 }; double ivec[4 * 4] = { 0.25, 0.25, 0.25, 0.25, 0.125, -0.125, 0.125, -0.125, 0.0, 1.0, 0.0, -1.0, 1.0, 0.0, -1.0, 0.0 }; double eval[4] = { 0.0, -1.3333333333333333, -1.3333333333333333, -1.3333333333333333 }; // set the Eigen decomposition beagleSetEigenDecomposition(instance, 0, evec, ivec, eval); beagleSetStateFrequencies(instance, 0, freqs); beagleSetCategoryWeights(instance, 0, weights); beagleSetPatternWeights(instance, patternWeights); // a list of indices and edge lengths int nodeIndices[4] = { 0, 1, 2, 3 }; double edgeLengths[4] = { 0.1, 0.1, 0.2, 0.1 }; int* rootIndices = (int*) malloc(sizeof(int) * nRootCount); int* categoryWeightsIndices = (int*) malloc(sizeof(int) * nRootCount); int* stateFrequencyIndices = (int*) malloc(sizeof(int) * nRootCount); int* cumulativeScalingIndices = (int*) malloc(sizeof(int) * nRootCount); for (int i = 0; i < nRootCount; i++) { rootIndices[i] = 4 + i; categoryWeightsIndices[i] = 0; stateFrequencyIndices[i] = 0; cumulativeScalingIndices[i] = (manualScaling ? 2 + i : BEAGLE_OP_NONE); beagleSetCategoryRates(instance, &rates[i]); // tell BEAGLE to populate the transition matrices for the above edge lengths beagleUpdateTransitionMatrices(instance, // instance 0, // eigenIndex nodeIndices, // probabilityIndices NULL, // firstDerivativeIndices NULL, // secondDerivativeIndices edgeLengths, // edgeLengths 4); // count // create a list of partial likelihood update operations // the order is [dest, destScaling, source1, matrix1, source2, matrix2] BeagleOperation operations[2] = { 3, (manualScaling ? 0 : BEAGLE_OP_NONE), BEAGLE_OP_NONE, 0, 0, 1, 1, rootIndices[i], (manualScaling ? 1 : BEAGLE_OP_NONE), BEAGLE_OP_NONE, 2, 2, 3, 3 }; if (manualScaling) beagleResetScaleFactors(instance, cumulativeScalingIndices[i]); // update the partials beagleUpdatePartials(instance, // instance operations, // eigenIndex 2, // operationCount cumulativeScalingIndices[i]);// cumulative scaling index } if (autoScaling) { int scaleIndices[2] = {3, 4}; beagleAccumulateScaleFactors(instance, scaleIndices, 2, BEAGLE_OP_NONE); } double *patternLogLik = (double*)malloc(sizeof(double) * nPatterns); double logL = 0.0; int returnCode = 0; // calculate the site likelihoods at the root node returnCode = beagleCalculateRootLogLikelihoods(instance, // instance (const int *)rootIndices,// bufferIndices (const int *)categoryWeightsIndices, // weights (const int *)stateFrequencyIndices, // stateFrequencies cumulativeScalingIndices,// cumulative scaling index nRootCount, // count &logL); // outLogLikelihoods if (returnCode < 0) { fprintf(stderr, "Failed to calculate root likelihood\n\n"); } else { beagleGetSiteLogLikelihoods(instance, patternLogLik); double sumLogL = 0.0; for (int i = 0; i < nPatterns; i++) { sumLogL += patternLogLik[i] * patternWeights[i]; // std::cerr << "site lnL[" << i << "] = " << patternLogLik[i] << '\n'; } fprintf(stdout, "logL = %.5f (PAUP logL = -1498.89812)\n", logL); fprintf(stdout, "sumLogL = %.5f\n", sumLogL); } // no rate heterogeneity: // fprintf(stdout, "logL = %.5f (PAUP logL = -1574.63623)\n\n", logL); free(weights); free(patternWeights); free(rootIndices); free(categoryWeightsIndices); free(stateFrequencyIndices); free(cumulativeScalingIndices); free(patternLogLik); free(humanPartials); free(chimpPartials); free(gorillaPartials); beagleFinalizeInstance(instance); #ifdef _WIN32 std::cout << "\nPress ENTER to exit...\n"; fflush( stdout); fflush( stderr); getchar(); #endif }
int beagleCreateInstance(int tipCount, int partialsBufferCount, int compactBufferCount, int stateCount, int patternCount, int eigenBufferCount, int matrixBufferCount, int categoryCount, int scaleBufferCount, int* resourceList, int resourceCount, long preferenceFlags, long requirementFlags, BeagleInstanceDetails* returnInfo) { DEBUG_CREATE_TIME(); try { if (instances == NULL) instances = new std::vector<beagle::BeagleImpl*>; if (rsrcList == NULL) beagleGetResourceList(); if (implFactory == NULL) beagleGetFactoryList(); loaded = 1; // First determine a list of possible resources PairedList* possibleResources = new PairedList; if (resourceList == NULL || resourceCount == 0) { // No list given for(int i=0; i<rsrcList->length; i++) possibleResources->push_back(std::make_pair( scoreFlags(preferenceFlags,rsrcList->list[i].supportFlags), // Score i)); // ID } else { for(int i=0; i<resourceCount; i++) possibleResources->push_back(std::make_pair( scoreFlags(preferenceFlags,rsrcList->list[resourceList[i]].supportFlags), // Score resourceList[i])); // ID } if (requirementFlags != 0) { // If requirements given do restriction for(PairedList::iterator it = possibleResources->begin(); it != possibleResources->end(); ++it) { int resource = (*it).second; long resourceFlag = rsrcList->list[resource].supportFlags; if ( (resourceFlag & requirementFlags) < requirementFlags) { if(it==possibleResources->begin()){ possibleResources->remove(*(it)); it=possibleResources->begin(); }else possibleResources->remove(*(it--)); } if(it==possibleResources->end()) break; } } if (possibleResources->size() == 0) { delete possibleResources; return BEAGLE_ERROR_NO_RESOURCE; } beagle::BeagleImpl* bestBeagle = NULL; possibleResources->sort(compareOnFirst); // Attempt in rank order, lowest score wins int errorCode = BEAGLE_ERROR_NO_RESOURCE; // Score each resource-implementation pair given preferences RsrcImplList* possibleResourceImplementations = new RsrcImplList; for(PairedList::iterator it = possibleResources->begin(); it != possibleResources->end(); ++it) { int resource = (*it).second; long resourceRequiredFlags = rsrcList->list[resource].requiredFlags; long resourceSupportedFlags = rsrcList->list[resource].supportFlags; int resourceScore = (*it).first; #ifdef BEAGLE_DEBUG_FLOW fprintf(stderr,"Possible resource: %s (%d)\n",rsrcList->list[resource].name,resourceScore); #endif for (std::list<beagle::BeagleImplFactory*>::iterator factory = implFactory->begin(); factory != implFactory->end(); factory++) { long factoryFlags = (*factory)->getFlags(); #ifdef BEAGLE_DEBUG_FLOW fprintf(stderr,"\tExamining implementation: %s\n",(*factory)->getName()); #endif if ( ((requirementFlags & factoryFlags) >= requirementFlags) // Factory meets requirementFlags && ((resourceRequiredFlags & factoryFlags) >= resourceRequiredFlags) // Factory meets resourceFlags && ((requirementFlags & resourceSupportedFlags) >= requirementFlags) // Resource meets requirementFlags ) { int implementationScore = scoreFlags(preferenceFlags,factoryFlags); int totalScore = resourceScore + implementationScore; #ifdef BEAGLE_DEBUG_FLOW fprintf(stderr,"\tPossible implementation: %s (%d)\n", (*factory)->getName(),totalScore); #endif possibleResourceImplementations->push_back(std::make_pair(totalScore, std::make_pair(resource, (*factory)))); } } } delete possibleResources; #ifdef BEAGLE_DEBUG_FLOW fprintf(stderr,"\nOriginal list of possible implementations:\n"); for (RsrcImplList::iterator it = possibleResourceImplementations->begin(); it != possibleResourceImplementations->end(); ++it) { beagle::BeagleImplFactory* factory = (*it).second.second; fprintf(stderr,"\t %s (%d)\n", factory->getName(), (*it).first); } #endif possibleResourceImplementations->sort(compareRsrcImpl); #ifdef BEAGLE_DEBUG_FLOW fprintf(stderr,"\nSorted list of possible implementations:\n"); for (RsrcImplList::iterator it = possibleResourceImplementations->begin(); it != possibleResourceImplementations->end(); ++it) { beagle::BeagleImplFactory* factory = (*it).second.second; fprintf(stderr,"\t %s (%d) (%d)\n", factory->getName(), (*it).first, (*it).second.first); } #endif for(RsrcImplList::iterator it = possibleResourceImplementations->begin(); it != possibleResourceImplementations->end(); ++it) { int resource = (*it).second.first; beagle::BeagleImplFactory* factory = (*it).second.second; bestBeagle = factory->createImpl(tipCount, partialsBufferCount, compactBufferCount, stateCount, patternCount, eigenBufferCount, matrixBufferCount, categoryCount, scaleBufferCount, resource, ResourceMap[resource], preferenceFlags, requirementFlags, &errorCode); if (bestBeagle != NULL) break; } delete possibleResourceImplementations; if (bestBeagle != NULL) { int instance = instances->size(); instances->push_back(bestBeagle); int returnValue = bestBeagle->getInstanceDetails(returnInfo); if (returnValue == BEAGLE_SUCCESS) { returnInfo->resourceName = rsrcList->list[returnInfo->resourceNumber].name; // TODO: move implDescription to inside the implementation returnInfo->implDescription = (char*) "none"; returnValue = instance; } return returnValue; } // No implementations found or appropriate, return last error code return errorCode; } catch (std::bad_alloc &) { return BEAGLE_ERROR_OUT_OF_MEMORY; } catch (std::out_of_range &) { return BEAGLE_ERROR_OUT_OF_RANGE; } catch (...) { return BEAGLE_ERROR_UNIDENTIFIED_EXCEPTION; } loaded = 1; }
int main( int argc, const char* argv[] ) { // Default values int stateCount = 4; int ntaxa = 16; int nsites = 10000; bool manualScaling = false; bool autoScaling = false; bool dynamicScaling = false; bool requireDoublePrecision = false; bool requireSSE = false; bool unrooted = false; bool calcderivs = false; int compactTipCount = 0; int randomSeed = 1; int rescaleFrequency = 1; bool logscalers = false; int eigenCount = 1; bool eigencomplex = false; bool ievectrans = false; bool setmatrix = false; int rsrc = -1; int nreps = 5; bool fullTiming = false; int rateCategoryCount = 4; interpretCommandLineParameters(argc, argv, &stateCount, &ntaxa, &nsites, &manualScaling, &autoScaling, &dynamicScaling, &rateCategoryCount, &rsrc, &nreps, &fullTiming, &requireDoublePrecision, &requireSSE, &compactTipCount, &randomSeed, &rescaleFrequency, &unrooted, &calcderivs, &logscalers, &eigenCount, &eigencomplex, &ievectrans, &setmatrix); std::cout << "\nSimulating genomic "; if (stateCount == 4) std::cout << "DNA"; else std::cout << stateCount << "-state data"; std::cout << " with " << ntaxa << " taxa and " << nsites << " site patterns (" << nreps << " rep" << (nreps > 1 ? "s" : ""); std::cout << (manualScaling ? ", manual scaling":(autoScaling ? ", auto scaling":(dynamicScaling ? ", dynamic scaling":""))) << ")\n\n"; if (rsrc != -1) { runBeagle(rsrc, stateCount, ntaxa, nsites, manualScaling, autoScaling, dynamicScaling, rateCategoryCount, nreps, fullTiming, requireDoublePrecision, requireSSE, compactTipCount, randomSeed, rescaleFrequency, unrooted, calcderivs, logscalers, eigenCount, eigencomplex, ievectrans, setmatrix); } else { BeagleResourceList* rl = beagleGetResourceList(); if(rl != NULL){ for(int i=0; i<rl->length; i++){ runBeagle(i, stateCount, ntaxa, nsites, manualScaling, autoScaling, dynamicScaling, rateCategoryCount, nreps, fullTiming, requireDoublePrecision, requireSSE, compactTipCount, randomSeed, rescaleFrequency, unrooted, calcderivs, logscalers, eigenCount, eigencomplex, ievectrans, setmatrix); } }else{ runBeagle(0, stateCount, ntaxa, nsites, manualScaling, autoScaling, dynamicScaling, rateCategoryCount, nreps, fullTiming, requireDoublePrecision, requireSSE, compactTipCount, randomSeed, rescaleFrequency, unrooted, calcderivs, logscalers, eigenCount, eigencomplex, ievectrans, setmatrix); } } //#ifdef _WIN32 // std::cout << "\nPress ENTER to exit...\n"; // fflush( stdout); // fflush( stderr); // getchar(); //#endif }