General
Memory-mapped files using the boost library
9The objective of memory mapping files is to increase I/O performance. Memory mapping a file creates a pointer to a segment in virtual memory and the actual loading is performed by the Operating System one page at a time. For large files, this is much faster than using traditional methods in C such as fopen/fread/fwrite.
In this post, I show an example of how to use the boost iostreams library to create a memory mapped file that, unlike mmap, works for both Windows and Linux.
Start with installing the boost iostreams library. On ubuntu this is done by installing the libboost-iostreams-dev package.
sudo apt-get install libboost-iostreams-dev
The example below will create a memory mapping of 1000000 integers for the file filename.raw. The integers will be available from the pointer called data.
#include <boost/iostreams/device/mapped_file.hpp> #include <iostream> int main() { boost::iostreams::mapped_file_source file; int numberOfElements = 1000000; int numberOfBytes = numberOfElements*sizeof(int); file.open("filename.raw", numberOfBytes); // Check if file was successfully opened if(file.is_open()) { // Get pointer to the data int * data = (int *)file.data(); // Do something with the data for(int i = 0; i < numberOfElements; i++) std::cout << data[i] << " "; // Remember to unmap the file file.close(); } else { std::cout << "could not map the file filename.raw" << std::endl; } }
Here is a minimal CMakeLists.txt file for compiling this example together with the boost iostreams library.
cmake_minimum_required(VERSION 2.8) find_package(Boost COMPONENTS iostreams REQUIRED) add_executable(memory-map main.cpp) target_link_libraries(memory-map ${Boost_LIBRARIES})
As usual you can download/clone the code and the sample raw file from my GitHub page
Measuring runtime in milliseconds using the C++ 11 chrono library
0I have been playing around with the new C++ 11 standard. It includes a nice new library called chrono which includes some useful clocks and timers. Below is an example of some macros you can use to time your applications in milliseconds and print out the result. Timing can be turned off by removing the #define TIMING line. Remember to compile the program with C++11 (or C++0x) enabled. For GCC this should be:
g++ main.cpp -std=c++0x
#include <iostream> #include <chrono> #define TIMING #ifdef TIMING #define INIT_TIMER auto start = std::chrono::high_resolution_clock::now(); #define START_TIMER start = std::chrono::high_resolution_clock::now(); #define STOP_TIMER(name) std::cout << "RUNTIME of " << name << ": " << \ std::chrono::duration_cast<std::chrono::milliseconds>( \ std::chrono::high_resolution_clock::now()-start \ ).count() << " ms " << std::endl; #else #define INIT_TIMER #define START_TIMER #define STOP_TIMER(name) #endif int main() { INIT_TIMER START_TIMER sleep(2); STOP_TIMER("sleeping for 2 seconds") START_TIMER long unsigned int b = 0; for(int i = 0; i < 10000000; i++) { b += i; } STOP_TIMER("some long loop") }
Example output:
RUNTIME of sleeping for 2 seconds: 2000 ms RUNTIME of some long loop: 24 ms
Getting started with Google Test (GTest) on Ubuntu
7Google test is a framework for writing C++ unit tests. In this short post, I explain how to set it up in Ubuntu.
Start by installing the gtest development package:
sudo apt-get install libgtest-dev
Note that this package only install source files. You have to compile the code yourself to create the necessary library files. These source files should be located at /usr/src/gtest. Browse to this folder and use cmake to compile the library:
sudo apt-get install cmake # install cmake cd /usr/src/gtest sudo cmake CMakeLists.txt sudo make # copy or symlink libgtest.a and libgtest_main.a to your /usr/lib folder sudo cp *.a /usr/lib
Lets say we now want to test the following simple squareRoot function:
// whattotest.cpp #include <math.h> double squareRoot(const double a) { double b = sqrt(a); if(b != b) { // nan check return -1.0; }else{ return sqrt(a); } }
In the following code, we create two tests that test the function using a simple assertion. There exists many other assertion macros in the framework (see http://code.google.com/p/googletest/wiki/Primer#Assertions). The code contains a small main function that will run all of the tests automatically. Nice and simple!
// tests.cpp #include "whattotest.cpp" #include <gtest/gtest.h> TEST(SquareRootTest, PositiveNos) { ASSERT_EQ(6, squareRoot(36.0)); ASSERT_EQ(18.0, squareRoot(324.0)); ASSERT_EQ(25.4, squareRoot(645.16)); ASSERT_EQ(0, squareRoot(0.0)); } TEST(SquareRootTest, NegativeNos) { ASSERT_EQ(-1.0, squareRoot(-15.0)); ASSERT_EQ(-1.0, squareRoot(-0.2)); } int main(int argc, char **argv) { testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); }
The next step is to compile the code. I’ve set up a small CMakeLists.txt file below to compile the tests. This file locates the google test library and links it with the test application. Note that we also have to link to the pthread library or the application won’t compile.
cmake_minimum_required(VERSION 2.6) # Locate GTest find_package(GTest REQUIRED) include_directories(${GTEST_INCLUDE_DIRS}) # Link runTests with what we want to test and the GTest and pthread library add_executable(runTests tests.cpp) target_link_libraries(runTests ${GTEST_LIBRARIES} pthread)
Compile and run the tests:
cmake CMakeLists.txt make ./runTests
Have fun testing! You can download all of the code above at my Github page: https://github.com/smistad/GTest
References
http://code.google.com/p/googletest/wiki/Documentation
http://www.ibm.com/developerworks/aix/library/au-googletestingframework.html
Gjenskapingen av Hamar domkirke – Del 2
3Da var faget Eksperter i Team over og jeg og resten av gruppa mi er stolte over å presentere resultatet av vårt arbeid. Som sagt i et tidligere blogginnlegg gikk oppgaven vår ut på å gjenskape Hamar domkirke i virtuell virkelighet. I den sammenheng har vi laget en film over gamle Hamar domkirke som vi har lagt ut på Youtube, se under. Filmen viser tre ulike byggefaser kirka gikk gjennom.
3D modellene av kirka er laget i programmet 3D studio max 2009, landskapet i Grome 2, teksturene er funnet på cgtextures.com og fantastiske gratis Unreal Development Kit er brukt til å sette det hele sammen med realistisk grafikk.
Gjenskapingen av Hamar domkirke – Del 1
5I det obligatoriske faget Eksperter i Team har jeg og en gruppe på 3 andre valgt oppgaven med å gjenskape Hamar domkirke i virtuell virkelighet. Det vil si at vi skal modellere den i 3D på datamaskiner og så plassere den i et 3D landskap. Kirken ble bygget rundt år 1200 og ble rasert i 1567 av svenskene under den nordiske 7 års krigen.
Etter andre dag med jobbing har gruppa fått modellert hovedtrekkene til både fase 1 og 2.
Neste steg er å legge mer detaljer til modellene. Som f.eks. dører, vinduer, takpynt o.l.
Pseudo-random numbers and sampling from probability distributions
3One often needs a source of random numbers for use in stochastic simulations. Since a computer is deterministic, it can’t generate real random numbers, unless one happen to have a quantum random generator at hand, like these. But computers can generate sequences of pseudo-random numbers which are deterministic sequences of numbers which has the same statistical properties as sequences of real random numbers. All of the different random functions in various programming languages, like rand() in C, create sequences of pseudo-random numbers. This post contains my exam notes for the course TDT4270 Statistical image analysis and learning and explains how to generate these sequences, what sampling is and how to sample from any probability distribution.
Fix for HTTP Error when uploading images in WordPress 2.8
4When installing this wordpress blog I encountered a problem when trying to upload files. When googling the problem I found that some had been trying to mess around with the .htaccess file. I tried that, but it didn’t seem to do the trick for this version of wordpress. So after a while I found a solution which worked on my version of wordpress.
(more…)




Recent Comments
int numBytes =[…] 1 hour ago