| The Gaudi Framework v28r2p1 (f1a77ff4)

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

Classes | Functions | Variables
mergesort.cpp File Reference

Mergesort example. More...

`#include <boost/threadpool.hpp>`
`#include <boost/thread/mutex.hpp>`
`#include <boost/smart_ptr.hpp>`
`#include <iostream>`
`#include <sstream>`
`#include <algorithm>`
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 48 of file mergesort.cpp.

49 {
50 boost::xtime::xtime_sec_t start_ms = start.sec * 1000 + start.nsec/1000000;
51 boost::xtime::xtime_sec_t end_ms = end.sec * 1000 + end.nsec/1000000;
52 return static_cast<unsigned long>(end_ms - start_ms);
53 }
IOTest.start
start
Definition: IOTest.py:88
end
auto end(reverse_wrapper< T > &w)
Definition: reverse.h:50

int main | ( | int | argc,
|  | char *const | argv[]
| ) |  |

Definition at line 125 of file mergesort.cpp.

126 {
127 print("MAIN: construct thread pool\n");
128
129
130
131 boost::xtime start;
132 boost::xtime_get(&start, boost::TIME_UTC);
133
134 int exponent = 7;
135 int data_len = 1 << exponent; // = pow(2, exponent)
136
137 print("MAIN: sort array with "+ to_string(data_len) + " elements.\n");
138
139 boost::shared_array<image> data(new image[data_len]);
140
141 // fill array with arbitrary values (not sorted ascendingly)
142 for(int i = 0; i < data_len; i++)
143 {
144 data[i] = image((data_len - i - 1) % 23);
145 }
146
147
148 /***************************/
149 /* Standard implementation */
150 /***************************/
151
152 pool tp;
153 tp.size_controller().resize(5);
154
155 // merge data array
156 for(int step = 1; step <= exponent; step++)
157 {
158 print("\nMAIN: merge step "+ to_string(step)+"\n");
159
160 // divide array into partitions
161 int partition_size = 1 << step;
162 for(int partition = 0; partition < data_len/partition_size; partition++)
163 {
164 // sort partition
165 boost::shared_ptr<merge_job<image> > job(new merge_job<image>(data, partition*partition_size, partition_size));
166 //tp->schedule(prio_task_func(5, boost::bind(&merge_job<image>::run, job)));
167 schedule(tp, boost::bind(&merge_job<image>::run, job));
168 // schedule(tp, job);
169 }
170 tp.wait(); // wait until all partitions are sorted
171 }
172
173 boost::xtime end;
174 boost::xtime_get(&end, boost::TIME_UTC);
175
176 print("\nMAIN: duration " + to_string(get_ms_diff(start, end)) + " ms \n");
177
178 print("\nMAIN: check if array is sorted... \n");
179
180 // check if array is sorted ascendingly
181 bool ascending = true;
182 for(int i = 0; i < data_len-1; i++)
183 {
184 if(data[i+1] < data[i])
185 {
186 ascending = false;
187 }
188 }
189
190 if(ascending)
191 {
192 print("\nMAIN: array is sorted\n");
193 }
194 else
195 {
196 print("\nMAIN: array is NOT sorted!\n");
197 }
198
199 return 0;
200 }
boost::threadpool::thread_pool::wait
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
to_string
string to_string(const T &value)
Definition: mergesort.cpp:40
merge_job
Definition: mergesort.cpp:85
get_ms_diff
unsigned long get_ms_diff(boost::xtime &start, boost::xtime &end)
Definition: mergesort.cpp:48
image
Definition: mergesort.cpp:55
std::partition
T partition(T...args)
pool
Definition: PoolClasses.h:7
boost::threadpool::schedule
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
IOTest.start
start
Definition: IOTest.py:88
boost::threadpool::thread_pool::size_controller
size_controller_type size_controller()
Gets the size controller which manages the number of threads in the pool.
Definition: pool.hpp:111
end
auto end(reverse_wrapper< T > &w)
Definition: reverse.h:50
print
void print(string text)
Definition: mergesort.cpp:33

void print | ( | string | text | ) |

Definition at line 33 of file mergesort.cpp.

34 {
35 boost::mutex::scoped_lock lock(m_io_monitor);
36 cout << text;
37 }
std::lock
T lock(T...args)
m_io_monitor
boost::mutex m_io_monitor
Definition: mergesort.cpp:31
std::cout
template<class T >

string to_string | ( | const T & | value | ) |

Definition at line 40 of file mergesort.cpp.

41 {
42 ostringstream ost;
43 ost << value;
44 ost.flush();
45 return ost.str();
46 }
std::ostringstream
STL class.
std::ostringstream::str
T str(T...args)
std::ostringstream::flush
T flush(T...args)

## Variable Documentation

boost::mutex m_io_monitor

Definition at line 31 of file mergesort.cpp.