bugs: focus stacking by per-element certainty, determined by frequency characteristics.
[Ale.git] / thread.h
blob2986811f054da1b80af71dbe1f36cbc04efecc41
1 // Copyright 2006 David Hilvert <dhilvert@auricle.dyndns.org>,
2 // <dhilvert@ugcs.caltech.edu>
3 // <dhilvert@gmail.com>
5 /* This file is part of the Anti-Lamenessing Engine.
7 The Anti-Lamenessing Engine is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 The Anti-Lamenessing Engine is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with the Anti-Lamenessing Engine; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * thread.h: threading details.
26 #ifndef __thread_h__
27 #define __thread_h__
29 #include <assert.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <stdlib.h>
34 #ifdef USE_PTHREAD
35 #include <pthread.h>
36 #endif
38 #define THREAD_PER_CPU_DEFAULT 1
39 #define THREAD_COUNT_DEFAULT 4
41 class thread {
42 static unsigned int _count;
43 static unsigned int _cpu_count;
45 static void try_linux() {
46 assert (_cpu_count == 0);
48 FILE *cpuinfo;
49 char buffer[100];
51 cpuinfo = fopen("/proc/cpuinfo", "r");
53 if (!cpuinfo)
54 return;
56 while (!feof(cpuinfo) && fgets(buffer, 100, cpuinfo)) {
57 if (strncmp("processor", buffer, strlen("processor")))
58 continue;
59 _cpu_count++;
63 public:
64 static void init() {
65 if (_cpu_count == 0) {
66 try_linux();
69 if (_cpu_count > 0) {
70 _count = THREAD_PER_CPU_DEFAULT * _cpu_count;
71 } else {
72 _count = THREAD_COUNT_DEFAULT;
75 assert (_count > 0);
78 static void set_per_cpu(unsigned int new_per_cpu) {
79 if (_cpu_count == 0) {
80 fprintf(stderr, "\n\n");
81 fprintf(stderr, "Error: per-cpu thread count specified, but CPU count is unknown.\n");
82 fprintf(stderr, " Try setting the thread count explicitly.\n");
84 exit(1);
86 if (new_per_cpu == 0) {
87 fprintf(stderr, "\n\n");
88 fprintf(stderr, "Error: --per-cpu argument must be positive\n");
89 fprintf(stderr, "\n");
91 exit(1);
94 _count = _cpu_count * new_per_cpu;
95 assert (_count > 0);
98 static void set_count(unsigned int new_count) {
99 if (new_count == 0) {
100 fprintf(stderr, "\n\n");
101 fprintf(stderr, "Error: --thread argument must be positive\n");
102 fprintf(stderr, "\n");
104 exit(1);
107 _count = new_count;
108 assert (_count > 0);
111 static int count() {
112 assert (_count > 0);
113 return _count;
116 class rwlock_t {
117 #ifdef USE_PTHREAD
118 pthread_rwlock_t _lock;
119 #endif
120 public:
121 rwlock_t() {
122 #ifdef USE_PTHREAD
123 pthread_rwlock_init(&_lock, NULL);
124 #endif
127 void wrlock() {
128 #ifdef USE_PTHREAD
129 pthread_rwlock_wrlock(&_lock);
130 #endif
133 void rdlock() {
134 #ifdef USE_PTHREAD
135 pthread_rwlock_rdlock(&_lock);
136 #endif
139 void unlock() {
140 #ifdef USE_PTHREAD
141 pthread_rwlock_unlock(&_lock);
142 #endif
146 class lock_t {
147 #ifdef USE_PTHREAD
148 pthread_mutex_t _lock;
149 #endif
150 public:
151 lock_t() {
152 #ifdef USE_PTHREAD
153 /* _lock = PTHREAD_MUTEX_INITIALIZER; */
154 pthread_mutex_init(&_lock, NULL);
155 #endif
158 void lock() {
159 #ifdef USE_PTHREAD
160 pthread_mutex_lock(&_lock);
161 #endif
164 void unlock() {
165 #ifdef USE_PTHREAD
166 pthread_mutex_unlock(&_lock);
167 #endif
171 class decompose_domain {
172 lock_t _lock;
173 int ilg, ihg, jlg, jhg;
175 protected:
176 void lock() {
177 _lock.lock();
180 void unlock() {
181 _lock.unlock();
184 virtual void prepare_subdomains(unsigned int threads) {
186 virtual void subdomain_algorithm(unsigned int thread,
187 int il, int ih, int jl, int jh) {
189 virtual void finish_subdomains(unsigned int threads) {
192 private:
193 struct thread_data_t {
194 decompose_domain *this_ptr;
195 unsigned int thread_index;
196 int il, ih, jl, jh;
199 static void *run_thread(void *thread_data) {
200 thread_data_t *td = (thread_data_t *) thread_data;
201 td->this_ptr->subdomain_algorithm(td->thread_index,
202 td->il, td->ih, td->jl, td->jh);
203 return NULL;
206 public:
207 decompose_domain(int ilg, int ihg, int jlg, int jhg) {
208 this->ilg = ilg;
209 this->ihg = ihg;
210 this->jlg = jlg;
211 this->jhg = jhg;
214 void run() {
215 int N;
216 #ifdef USE_PTHREAD
217 N = thread::count();
219 pthread_t *threads = (pthread_t *) malloc(sizeof(pthread_t) * N);
220 pthread_attr_t *thread_attr = (pthread_attr_t *)
221 malloc(sizeof(pthread_attr_t) * N);
222 #else
223 N = 1;
224 #endif
226 prepare_subdomains(N);
228 thread_data_t *td = new thread_data_t[N];
230 for (int ti = 0; ti < N; ti++) {
231 td[ti].this_ptr = this;
232 td[ti].thread_index = ti;
233 td[ti].il = ilg + ((ihg - ilg) * ti) / N;
234 td[ti].ih = ilg + ((ihg - ilg) * (ti + 1)) / N;
235 td[ti].jl = jlg;
236 td[ti].jh = jhg;
237 #ifdef USE_PTHREAD
238 pthread_attr_init(&thread_attr[ti]);
239 pthread_attr_setdetachstate(&thread_attr[ti],
240 PTHREAD_CREATE_JOINABLE);
241 pthread_create(&threads[ti], &thread_attr[ti],
242 run_thread, &td[ti]);
243 #else
244 run_thread(&td[ti]);
245 #endif
249 #ifdef USE_PTHREAD
250 for (int ti = 0; ti < N; ti++) {
251 pthread_join(threads[ti], NULL);
254 free(threads);
255 free(thread_attr);
256 #endif
259 delete[] td;
261 finish_subdomains(N);
264 virtual ~decompose_domain() {
269 #endif