summaryrefslogtreecommitdiff
path: root/graphics/asymptote/LspCpp/third_party/threadpool/libs/threadpool/example/mergesort/mergesort.cpp
blob: 338153453b43ab6be92c6641336362e70f806670 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*! \file
 * \brief 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
 *
 */


#include <boost/threadpool.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/smart_ptr.hpp>
#include <iostream>
#include <sstream>
#include <algorithm>



using namespace std;
using namespace boost::threadpool;

//
// Helpers
boost::mutex m_io_monitor;

void print(string text)
{
  boost::mutex::scoped_lock lock(m_io_monitor);
  cout << text;
}

template<class T>
string to_string(const T& value)
{
  ostringstream ost;
  ost << value;
  ost.flush();
  return ost.str();
}

unsigned long get_ms_diff(boost::xtime& start, boost::xtime& end)
{
  boost::xtime::xtime_sec_t start_ms = start.sec * 1000	+ start.nsec/1000000; 
  boost::xtime::xtime_sec_t end_ms = end.sec * 1000	+ end.nsec/1000000; 
  return static_cast<unsigned long>(end_ms - start_ms);
}

class image
{
public:
  image() : m_content(0)	{}
  image(int content) : m_content(content)	{}

  image(const image& src)
  {
    m_content = src.m_content;
  }

  bool operator<(const image& l) const
  {
    {	// simulate time needed for image comparision
      boost::xtime xt;
      boost::xtime_get(&xt, boost::TIME_UTC);
      int duration = 1+(m_content % 4);
      xt.nsec += 250 * 1000 * duration;	
      boost::thread::sleep(xt); 
	    print(".");
    }	
    return m_content < l.m_content;
  }

protected:
  int m_content;	// represents image data in this example
};


template<class T>
class merge_job
{
public:
  merge_job(boost::shared_array<T> data, unsigned int position, unsigned int length) 
    : m_data(data)
    , m_position(position)
    , m_length(length) 
  {
    print("merge job created : " + to_string(m_position) +", "+ to_string(m_length) +"\n");
  }

  void run()
  {	
    print("merge job running :   " + to_string(m_position) +", "+ to_string(m_length) +"\n");

    T* begin = m_data.get();
    std::advance(begin, m_position);

    T* mid = m_data.get();
    std::advance(mid, m_position + m_length/2);

    T* end = m_data.get();
    std::advance(end, m_position + m_length);

    std::inplace_merge(begin, mid, end);

    print("\nmerge job finished:     "  + to_string(m_position) +", "+ to_string(m_length) +"\n");
  }

protected:
  boost::shared_array<T> m_data;
  unsigned int m_position;
  unsigned int m_length;
};




//
// A demonstration of the thread_pool class
int main (int argc, char * const argv[]) 
{
  print("MAIN: construct thread pool\n");

		

  boost::xtime start;
  boost::xtime_get(&start, boost::TIME_UTC);

  int exponent = 7;
  int data_len = 1 << exponent;  // = pow(2, exponent) 

  print("MAIN: sort array with "+ to_string(data_len) + " elements.\n");

  boost::shared_array<image> data(new image[data_len]);

  // fill array with arbitrary values (not sorted ascendingly)
  for(int i = 0; i < data_len; i++)
  {
    data[i] = image((data_len - i - 1) % 23);
  }


  /***************************/
  /* Standard implementation */
  /***************************/

  pool tp;
  tp.size_controller().resize(5);	

// merge data array
  for(int step = 1; step <= exponent; step++)
  {
    print("\nMAIN: merge step "+ to_string(step)+"\n");

    // divide array into partitions
    int partition_size = 1 << step;
    for(int partition = 0; partition < data_len/partition_size; partition++)
    {
      // sort partition
      boost::shared_ptr<merge_job<image> > job(new merge_job<image>(data, partition*partition_size, partition_size));
      //tp->schedule(prio_task_func(5, boost::bind(&merge_job<image>::run, job)));
      schedule(tp, boost::bind(&merge_job<image>::run, job));
     // schedule(tp, job);
    }
    tp.wait();	// wait until all partitions are sorted
  } 

  boost::xtime end;
  boost::xtime_get(&end, boost::TIME_UTC);				

  print("\nMAIN: duration " + to_string(get_ms_diff(start, end)) + " ms \n");

  print("\nMAIN: check if array is sorted... \n");

  // check if array is sorted ascendingly 
  bool ascending = true;
  for(int i = 0; i < data_len-1; i++)
  {
    if(data[i+1] < data[i])
    {
      ascending = false;
    }
  }

  if(ascending)
  {
    print("\nMAIN: array is sorted\n");
  }
  else
  {
    print("\nMAIN: array is NOT sorted!\n");
  }

  return 0;
}