The Gaudi Framework  v29r0 (ff2e7097)
mergesort.cpp File Reference

Mergesort example. More...

#include <algorithm>
#include <boost/smart_ptr.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/threadpool.hpp>
#include <iostream>
#include <sstream>
Include dependency graph for mergesort.cpp:

Go to the source code of this file.

Classes

class  image
 
class  merge_job< T >
 

Functions

void print (string text)
 
template<class T >
string to_string (const T &value)
 
unsigned long get_ms_diff (boost::xtime &start, boost::xtime &end)
 
int main (int argc, char *const argv[])
 

Variables

boost::mutex m_io_monitor
 

Detailed Description

Mergesort example.

This example shows how to use the threadpool library.

Copyright (c) 2005-2006 Philipp Henkel

Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

http://threadpool.sourceforge.net

Definition in file mergesort.cpp.

Function Documentation

unsigned long get_ms_diff ( boost::xtime &  start,
boost::xtime &  end 
)

Definition at line 45 of file mergesort.cpp.

46 {
47  boost::xtime::xtime_sec_t start_ms = start.sec * 1000 + start.nsec / 1000000;
48  boost::xtime::xtime_sec_t end_ms = end.sec * 1000 + end.nsec / 1000000;
49  return static_cast<unsigned long>( end_ms - start_ms );
50 }
start
Definition: IOTest.py:99
auto end(reverse_wrapper< T > &w)
Definition: reverse.h:64
int main ( int  argc,
char *const  argv[] 
)

Definition at line 113 of file mergesort.cpp.

114 {
115  print( "MAIN: construct thread pool\n" );
116 
117  boost::xtime start;
118  boost::xtime_get( &start, boost::TIME_UTC );
119 
120  int exponent = 7;
121  int data_len = 1 << exponent; // = pow(2, exponent)
122 
123  print( "MAIN: sort array with " + to_string( data_len ) + " elements.\n" );
124 
125  boost::shared_array<image> data( new image[data_len] );
126 
127  // fill array with arbitrary values (not sorted ascendingly)
128  for ( int i = 0; i < data_len; i++ ) {
129  data[i] = image( ( data_len - i - 1 ) % 23 );
130  }
131 
132  /***************************/
133  /* Standard implementation */
134  /***************************/
135 
136  pool tp;
137  tp.size_controller().resize( 5 );
138 
139  // merge data array
140  for ( int step = 1; step <= exponent; step++ ) {
141  print( "\nMAIN: merge step " + to_string( step ) + "\n" );
142 
143  // divide array into partitions
144  int partition_size = 1 << step;
145  for ( int partition = 0; partition < data_len / partition_size; partition++ ) {
146  // sort partition
147  boost::shared_ptr<merge_job<image>> job(
148  new merge_job<image>( data, partition * partition_size, partition_size ) );
149  // tp->schedule(prio_task_func(5, boost::bind(&merge_job<image>::run, job)));
150  schedule( tp, boost::bind( &merge_job<image>::run, job ) );
151  // schedule(tp, job);
152  }
153  tp.wait(); // wait until all partitions are sorted
154  }
155 
156  boost::xtime end;
157  boost::xtime_get( &end, boost::TIME_UTC );
158 
159  print( "\nMAIN: duration " + to_string( get_ms_diff( start, end ) ) + " ms \n" );
160 
161  print( "\nMAIN: check if array is sorted... \n" );
162 
163  // check if array is sorted ascendingly
164  bool ascending = true;
165  for ( int i = 0; i < data_len - 1; i++ ) {
166  if ( data[i + 1] < data[i] ) {
167  ascending = false;
168  }
169  }
170 
171  if ( ascending ) {
172  print( "\nMAIN: array is sorted\n" );
173  } else {
174  print( "\nMAIN: array is NOT sorted!\n" );
175  }
176 
177  return 0;
178 }
void wait(size_t task_threshold=0) const
The current thread of execution is blocked until the sum of all active and pending tasks is equal or ...
Definition: pool.hpp:176
string to_string(const T &value)
Definition: mergesort.cpp:37
unsigned long get_ms_diff(boost::xtime &start, boost::xtime &end)
Definition: mergesort.cpp:45
T partition(T...args)
disable_if< is_void< typename result_of< Function() >::type >, future< typename result_of< Function() >::type >>::type schedule(Pool &pool, const Function &task)
Definition: future.hpp:111
start
Definition: IOTest.py:99
size_controller_type size_controller()
Gets the size controller which manages the number of threads in the pool.
Definition: pool.hpp:111
auto end(reverse_wrapper< T > &w)
Definition: reverse.h:64
void print(string text)
Definition: mergesort.cpp:30
void print ( string  text)

Definition at line 30 of file mergesort.cpp.

31 {
32  boost::mutex::scoped_lock lock( m_io_monitor );
33  cout << text;
34 }
T lock(T...args)
boost::mutex m_io_monitor
Definition: mergesort.cpp:28
template<class T >
string to_string ( const T &  value)

Definition at line 37 of file mergesort.cpp.

38 {
39  ostringstream ost;
40  ost << value;
41  ost.flush();
42  return ost.str();
43 }
T flush(T...args)

Variable Documentation

boost::mutex m_io_monitor

Definition at line 28 of file mergesort.cpp.