doc/web: Use repo.or.cz terminology for the summary page.
[Ale.git] / thread.h
blob682ac5bddc2e4fb685ad86fbacbb48f6cb817c84
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)) {
57 fgets(buffer, 100, cpuinfo);
58 if (strncmp("processor", buffer, strlen("processor")))
59 continue;
60 _cpu_count++;
64 public:
65 static void init() {
66 if (_cpu_count == 0) {
67 try_linux();
70 if (_cpu_count > 0) {
71 _count = THREAD_PER_CPU_DEFAULT * _cpu_count;
72 } else {
73 _count = THREAD_COUNT_DEFAULT;
76 assert (_count > 0);
79 static void set_per_cpu(unsigned int new_per_cpu) {
80 if (_cpu_count == 0) {
81 fprintf(stderr, "\n\n");
82 fprintf(stderr, "Error: per-cpu thread count specified, but CPU count is unknown.\n");
83 fprintf(stderr, " Try setting the thread count explicitly.\n");
85 exit(1);
87 if (new_per_cpu == 0) {
88 fprintf(stderr, "\n\n");
89 fprintf(stderr, "Error: --per-cpu argument must be positive\n");
90 fprintf(stderr, "\n");
92 exit(1);
95 _count = _cpu_count * new_per_cpu;
96 assert (_count > 0);
99 static void set_count(unsigned int new_count) {
100 if (new_count == 0) {
101 fprintf(stderr, "\n\n");
102 fprintf(stderr, "Error: --thread argument must be positive\n");
103 fprintf(stderr, "\n");
105 exit(1);
108 _count = new_count;
109 assert (_count > 0);
112 static int count() {
113 assert (_count > 0);
114 return _count;
117 class rwlock_t {
118 #ifdef USE_PTHREAD
119 pthread_rwlock_t _lock;
120 #endif
121 public:
122 rwlock_t() {
123 #ifdef USE_PTHREAD
124 pthread_rwlock_init(&_lock, NULL);
125 #endif
128 void wrlock() {
129 #ifdef USE_PTHREAD
130 pthread_rwlock_wrlock(&_lock);
131 #endif
134 void rdlock() {
135 #ifdef USE_PTHREAD
136 pthread_rwlock_rdlock(&_lock);
137 #endif
140 void unlock() {
141 #ifdef USE_PTHREAD
142 pthread_rwlock_unlock(&_lock);
143 #endif
147 class lock_t {
148 #ifdef USE_PTHREAD
149 pthread_mutex_t _lock;
150 #endif
151 public:
152 lock_t() {
153 #ifdef USE_PTHREAD
154 /* _lock = PTHREAD_MUTEX_INITIALIZER; */
155 pthread_mutex_init(&_lock, NULL);
156 #endif
159 void lock() {
160 #ifdef USE_PTHREAD
161 pthread_mutex_lock(&_lock);
162 #endif
165 void unlock() {
166 #ifdef USE_PTHREAD
167 pthread_mutex_unlock(&_lock);
168 #endif
172 class decompose_domain {
173 lock_t _lock;
174 int ilg, ihg, jlg, jhg;
176 protected:
177 void lock() {
178 _lock.lock();
181 void unlock() {
182 _lock.unlock();
185 virtual void prepare_subdomains(unsigned int threads) {
187 virtual void subdomain_algorithm(unsigned int thread,
188 int il, int ih, int jl, int jh) {
190 virtual void finish_subdomains(unsigned int threads) {
193 private:
194 struct thread_data_t {
195 decompose_domain *this_ptr;
196 unsigned int thread_index;
197 int il, ih, jl, jh;
200 static void *run_thread(void *thread_data) {
201 thread_data_t *td = (thread_data_t *) thread_data;
202 td->this_ptr->subdomain_algorithm(td->thread_index,
203 td->il, td->ih, td->jl, td->jh);
204 return NULL;
207 public:
208 decompose_domain(int ilg, int ihg, int jlg, int jhg) {
209 this->ilg = ilg;
210 this->ihg = ihg;
211 this->jlg = jlg;
212 this->jhg = jhg;
215 void run() {
216 int N;
217 #ifdef USE_PTHREAD
218 N = thread::count();
220 pthread_t *threads = (pthread_t *) malloc(sizeof(pthread_t) * N);
221 pthread_attr_t *thread_attr = (pthread_attr_t *)
222 malloc(sizeof(pthread_attr_t) * N);
223 #else
224 N = 1;
225 #endif
227 prepare_subdomains(N);
229 thread_data_t *td = new thread_data_t[N];
231 for (int ti = 0; ti < N; ti++) {
232 td[ti].this_ptr = this;
233 td[ti].thread_index = ti;
234 td[ti].il = ilg + ((ihg - ilg) * ti) / N;
235 td[ti].ih = ilg + ((ihg - ilg) * (ti + 1)) / N;
236 td[ti].jl = jlg;
237 td[ti].jh = jhg;
238 #ifdef USE_PTHREAD
239 pthread_attr_init(&thread_attr[ti]);
240 pthread_attr_setdetachstate(&thread_attr[ti],
241 PTHREAD_CREATE_JOINABLE);
242 pthread_create(&threads[ti], &thread_attr[ti],
243 run_thread, &td[ti]);
244 #else
245 run_thread(&td[ti]);
246 #endif
250 #ifdef USE_PTHREAD
251 for (int ti = 0; ti < N; ti++) {
252 pthread_join(threads[ti], NULL);
255 free(threads);
256 free(thread_attr);
257 #endif
260 delete[] td;
262 finish_subdomains(N);
265 virtual ~decompose_domain() {
270 #endif