Testing BeagleLib

The Likelihood class interfaces with BeagleLib to evaluate the likelihood function. Let’s start by just making sure we can access BeagleLib. Create a new header file named likelihood.hpp containing the following:

#pragma once

#include <map>
#include <boost/algorithm/string.hpp>
#include <boost/format.hpp>
#include <boost/shared_ptr.hpp>
#include "libhmsbeagle/beagle.h"
#include "xstrom.hpp"

namespace strom {

template <class T>
class Likelihood
  {
    public:
                                  Likelihood() {}
                                  ~Likelihood() {}
        
      std::string                 availableResources();
    
    public:
      typedef boost::shared_ptr< Likelihood<T> > SharedPtr;
  };

template <class T>
inline std::string Likelihood<T>::availableResources()
    {
    BeagleResourceList * rsrcList = beagleGetResourceList();
	std::string s;
    for (int i = 0; i < rsrcList->length; ++i)
        {
        std::string desc = rsrcList->list[i].description;
        boost::trim(desc);
        if (desc.size() > 0)
            s += boost::str(boost::format("%d: %s (%s)\n") % i % rsrcList->list[i].name % desc);
        else
            s += boost::str(boost::format("%d: %s\n") % i % rsrcList->list[i].name);
        }
    return s;
    }
}

Now modify main.cpp to create a Likelihood object and call its availableResources member function:

#include <iostream>
#include "node.hpp"
#include "likelihood.hpp"

using namespace strom;

int main(int argc, const char * argv[])
    {
    std::cout << "Starting..." << std::endl;

    Likelihood<Node> likelihood;
    std::string s = likelihood.availableResources();
    std::cout << "BeagleLib report:\n" << s << std::endl;

    std::cout << "Finished!" << std::endl;

    return 0;
    }

Running the program will produce a list of resources that BeagleLib could find. We have built only a very basic version of BeagleLib, so you should see just

Starting...
BeagleLib report:
0: CPU

Finished!

The 0: CPU above means that BeagleLib found one resource (the CPU resource), which has an index of 0 in its list of resources. If we had installed the Nvidia CUDA toolkit and had Graphical Processing Units (GPUs) attached to a video card installed on our computer, then one or more GPU resources would have been listed as well. We will not explore GPU usage in this tutorial, but the fact that you are using BeagleLib to compute likelihoods of trees means that in the future it would be easy for you to create applications that make use of available GPU resources.