Posts Tagged ‘Boost’

Boost and Xcode, united through CMake

Friday, May 7th, 2010

A while back I complained about my problems with developing applications using Boost in Xcode.  Between then and now I had a long period where I couldn’t even get Boost compiled on my Mac.

Now, however, I’ve found that there is a CMake based distribution of Boost.  This is great since CMake can Xcode projects directly, which makes it easy to make sure the libraries build will work with projects I make directly in Xcode.  Which incidentally is also why I was happy to see Bio++ move to CMake.

My only problem now is that I have a bunch of old software based on Automake that uses Boost.  They don’t seem to play well together on my Mac.  I should port those to CMake, but first I need to learn how to use CMake, and time is not something I have plenty of at the moment.  Maybe this should be a project for the summer holiday.

Does anyone know of any scripts that can assist me in converting Autoconf/Automake configurations to CMake?

Problems with boost::interval

Saturday, May 23rd, 2009

I need to work with exponentiation and logarithms with boost interval arithmetic, but nothing seems to work for me.

Here’s a simple little test program:

#include <iostream>
#include <boost/numeric/interval.hpp>
using namespace boost::numeric;
using namespace interval_lib;

int main()
{
  interval<double> a = 1, b = 1;
  std::cout << width(exp(a + b)) << std::endl;
  return 0;
}

If I try to compile this,I get the errors:

/usr/include/boost/numeric/interval/transc.hpp: In function ‘boost::numeric::interval<T, Policies> boost::numeric::exp(const boost::numeric::interval<T, Policies>&) [with T = double, Policies = boost::numeric::interval_lib::policies<boost::numeric::interval_lib::rounded_math<double>, boost::numeric::interval_lib::checking_strict<double> >]‘:
foo.cc:9:   instantiated from here
/usr/include/boost/numeric/interval/transc.hpp:34: error: ‘struct boost::numeric::interval_lib::rounded_math<double>’ has no member named ‘exp_down’
/usr/include/boost/numeric/interval/transc.hpp:34: error: ‘struct boost::numeric::interval_lib::rounded_math<double>’ has no member named ‘exp_up’

Why aren’t exp_down and exp_up defined?  According to the documentation, they should be defined for the built in types:

By default, it is interval_lib::rounded_math<T>. The class interval_lib::rounded_math is already specialized for the standard floating types (float , double and long double). So if the base type of your intervals is not one of these, a good solution would probably be to provide a specialization of this class. But if the default specialization of rounded_math<T> for float, double, or long double is not what you seek, or you do not want to specialize interval_lib::rounded_math<T> (say because you prefer to work in your own namespace) you can also define your own rounding policy and pass it directly to interval_lib::policies.

This is really annoying me, ’cause I have no idea what I am doing wrong or how to fix it.  No idea whatsoever, and I cannot continue with my project before figuring it out.

Sometimes, the boost library is just a little too complex for me, I guess…

142-152=-10

This just isn’t right

Friday, April 10th, 2009

So, I’m working on some genome scale analysis and am a bit worried about numerical precision issues.  After all, even relatively stable operations might run into problems after milions of additions and multiplications.

So I figured I could try using interval arithmetic to get a feeling for the uncertainty in the numerical values.

Boost, as so often, is helpful here and has a library for interval arithmetic.

Great, I thought, I’ll just chuck in the interval type in my algorithm (that is parameterised with the numerical type anyway so I can use either float, double or long double) and see how it goes.

Well, no luck.  All my intervals ends up being a single value, and that just isn’t right.

A simple example like this, that that adds 1000 random floats, results in an empty interval:

#include <iostream>

#include <boost/numeric/interval.hpp>
using namespace boost::numeric;
using namespace interval_lib;

#include <ctime>
#include <boost/random.hpp>

int main (int argc, char * const argv[])
{
	typedef interval<double> I;

	static boost::mt19937 rng(std::time(0));
	static boost::uniform_real<> uni_dist(0,1);
	static boost::variate_generator<boost::mt19937&, boost::uniform_real<> > random_01(rng, uni_dist);

	I x = 0.0;
	for (int i = 0; i < 1000; ++i) {
		x += random_01();
	}
	std::cout << '[' << x.lower() << ',' << x.upper()
                  << "](" << (x.upper() - x.lower()) << ')'
                  << std::endl;

    return 0;
}

There is no way there is no precision lost in adding 1000 values!

Clearly I am doing something wrong, but what?

100-122=-22

Xcode and Boost unit tests

Thursday, April 9th, 2009

Andreas sent me this link yesterday:

He has just configured our HMM framework with this.   It is really nice to have the unit tests integrated with the IDE.

I am still only very slowly getting used to working with an IDE.  I have used emacs and a terminal for my programming for 15 years and I never really saw the point of an IDE.  Then when I got a Mac, I figured I should give Xcode a try.  After all, it is just a shell on top of GCC, so I can recognize the error messages and such, so the change should be manageable.

All in all, I am growing to like Xcode.  I still use Automake to set up the build system, since I want my code to compile on Linux as well (I run most of my computations on a Linux grid), but I do my editing in Xcode exclusively now.

Until now I ran my unit tests in the terminal, but it is much nicer to run them from Xcode and get directed to the test violations.

99-121=-22

Trying out Boost.Test

Wednesday, October 8th, 2008

I’ve just started a new programming project — a library for dense HMMs that uses parallel hardware for its computations, if you want to know — and I decided to use Boost.Test for my unit testing.

Normally, I just write my unit tests with asserts and maybe a few home-made macros, but since I am going to use Boost heavily in the code anyway (I do more and more these days) I figured I might as well try out its unit testing framework.

Problems with the documentation

To my great surprise, I had some problems with the documentation of the framework.

Usually, the documentation for the boost libraries is excellent — at least compared to most libraries I use — and if you just read the documentation for Boost.Test it looks great.

There is a lot of it, with detailed descriptions of this and that and with tutorials to get you started.

It’s just that the examples there do not work.

Take for example this program from the tutorial:

#define BOOST_TEST_MODULE MyTest
#include <boost/test/unit_test.hpp>

int add( int i, int j ) { return i+j; }

BOOST_AUTO_TEST_CASE( my_test )
{
    // seven ways to detect and report the same error:
    BOOST_CHECK( add( 2,2 ) == 4 );        // #1 continues on error

    BOOST_REQUIRE( add( 2,2 ) == 4 );      // #2 throws on error

    if( add( 2,2 ) != 4 )
      BOOST_ERROR( "Ouch..." );            // #3 continues on error

    if( add( 2,2 ) != 4 )
      BOOST_FAIL( "Ouch..." );             // #4 throws on error

    if( add( 2,2 ) != 4 ) throw "Ouch..."; // #5 throws on error

    BOOST_CHECK_MESSAGE( add( 2,2 ) == 4,  // #6 continues on error
                         "add(..) result: " << add( 2,2 ) );

    BOOST_CHECK_EQUAL( add( 2,2 ), 4 );	  // #7 continues on error
}

The BOOST_AUTO_TEST_CASE() macro should create a test function and plug it into the framework, and after compiling the file (and linking with -lboost_unit_test_framework) you should have a test program.

Well, you can compile the program, but you cannot link it.  There is no main() function.

Oh well, if you read the header file boost/test/unit_test.hpp you find these lines:

#if defined(BOOST_TEST_DYN_LINK) && defined(BOOST_TEST_MAIN) && !defined(BOOST_TEST_NO_MAIN)
int BOOST_TEST_CALL_DECL
main( int argc, char* argv[] )
{
    return ::boost::unit_test::unit_test_main( &init_unit_test, argc, argv );
}

so it seems that the framework will define main, if only you have defined the right symbols, and yes, adding

#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MAIN

to the top of the program makes it run.

There were a few other cases like this, where I couldn’t figure out how to use the framework.  Like testing template functions with a list of different template parameters, but I just worked my way around that problem with my own macros.

Using the framework

There’s a lot of different things you can do with Boost.Test, but so far I’ve just used the very basic functionality.

I use the BOOST_AUTO_TEST_CASE() macro for my test functions.  The different cases are automatically grouped into test suites — one per file (compilation unit) — so I don’t worry about the larger framework.  I write a few test cases per code unit I need to test and the rely on the default behaviour of the framework.

The actual testing is done through various BOOST_CHECK_* macros like in the program above.

Among the macros are tests for floating point numbers that lets you test that two numbers are equal up to a certain accuracy.  This is what you want to check, since testing equality of floating point numbers is rarely a good idea.

So far I’m happy with Boost.Test, and I’m going to try out some of the more advanced features as my project progresses, I think.