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 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
|
// Copyright 2006 David Hilvert <dhilvert@auricle.dyndns.org>,
// <dhilvert@ugcs.caltech.edu>
// <dhilvert@gmail.com>
/* This file is part of the Anti-Lamenessing Engine.
The Anti-Lamenessing Engine is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
The Anti-Lamenessing Engine is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with the Anti-Lamenessing Engine; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
* thread.h: threading details.
*/
#ifndef __thread_h__
#define __thread_h__
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#ifdef USE_PTHREAD
#include <pthread.h>
#endif
#define THREAD_PER_CPU_DEFAULT 1
#define THREAD_COUNT_DEFAULT 4
class thread {
static unsigned int _count;
static unsigned int _cpu_count;
static void try_linux() {
assert (_cpu_count == 0);
FILE *cpuinfo;
char buffer[100];
cpuinfo = fopen("/proc/cpuinfo", "r");
if (!cpuinfo)
return;
while (!feof(cpuinfo)) {
fgets(buffer, 100, cpuinfo);
if (strncmp("processor", buffer, strlen("processor")))
continue;
_cpu_count++;
}
}
public:
static void init() {
if (_cpu_count == 0) {
try_linux();
}
if (_cpu_count > 0) {
_count = THREAD_PER_CPU_DEFAULT * _cpu_count;
} else {
_count = THREAD_COUNT_DEFAULT;
}
assert (_count > 0);
}
static void set_per_cpu(unsigned int new_per_cpu) {
if (_cpu_count == 0) {
fprintf(stderr, "\n\n");
fprintf(stderr, "Error: per-cpu thread count specified, but CPU count is unknown.\n");
fprintf(stderr, " Try setting the thread count explicitly.\n");
exit(1);
}
if (new_per_cpu == 0) {
fprintf(stderr, "\n\n");
fprintf(stderr, "Error: --per-cpu argument must be positive\n");
fprintf(stderr, "\n");
exit(1);
}
_count = _cpu_count * new_per_cpu;
assert (_count > 0);
}
static void set_count(unsigned int new_count) {
if (new_count == 0) {
fprintf(stderr, "\n\n");
fprintf(stderr, "Error: --thread argument must be positive\n");
fprintf(stderr, "\n");
exit(1);
}
_count = new_count;
assert (_count > 0);
}
static int count() {
assert (_count > 0);
return _count;
}
class rwlock_t {
#ifdef USE_PTHREAD
pthread_rwlock_t _lock;
#endif
public:
rwlock_t() {
#ifdef USE_PTHREAD
pthread_rwlock_init(&_lock, NULL);
#endif
}
void wrlock() {
#ifdef USE_PTHREAD
pthread_rwlock_wrlock(&_lock);
#endif
}
void rdlock() {
#ifdef USE_PTHREAD
pthread_rwlock_rdlock(&_lock);
#endif
}
void unlock() {
#ifdef USE_PTHREAD
pthread_rwlock_unlock(&_lock);
#endif
}
};
class lock_t {
#ifdef USE_PTHREAD
pthread_mutex_t _lock;
#endif
public:
lock_t() {
#ifdef USE_PTHREAD
/* _lock = PTHREAD_MUTEX_INITIALIZER; */
pthread_mutex_init(&_lock, NULL);
#endif
}
void lock() {
#ifdef USE_PTHREAD
pthread_mutex_lock(&_lock);
#endif
}
void unlock() {
#ifdef USE_PTHREAD
pthread_mutex_unlock(&_lock);
#endif
}
};
class decompose_domain {
lock_t _lock;
int ilg, ihg, jlg, jhg;
protected:
void lock() {
_lock.lock();
}
void unlock() {
_lock.unlock();
}
virtual void prepare_subdomains(unsigned int threads) {
}
virtual void subdomain_algorithm(unsigned int thread,
int il, int ih, int jl, int jh) {
}
virtual void finish_subdomains(unsigned int threads) {
}
private:
struct thread_data_t {
decompose_domain *this_ptr;
unsigned int thread_index;
int il, ih, jl, jh;
};
static void *run_thread(void *thread_data) {
thread_data_t *td = (thread_data_t *) thread_data;
td->this_ptr->subdomain_algorithm(td->thread_index,
td->il, td->ih, td->jl, td->jh);
return NULL;
}
public:
decompose_domain(int ilg, int ihg, int jlg, int jhg) {
this->ilg = ilg;
this->ihg = ihg;
this->jlg = jlg;
this->jhg = jhg;
}
void run() {
int N;
#ifdef USE_PTHREAD
N = thread::count();
pthread_t *threads = (pthread_t *) malloc(sizeof(pthread_t) * N);
pthread_attr_t *thread_attr = (pthread_attr_t *)
malloc(sizeof(pthread_attr_t) * N);
#else
N = 1;
#endif
prepare_subdomains(N);
thread_data_t *td = new thread_data_t[N];
for (int ti = 0; ti < N; ti++) {
td[ti].this_ptr = this;
td[ti].thread_index = ti;
td[ti].il = ilg + ((ihg - ilg) * ti) / N;
td[ti].ih = ilg + ((ihg - ilg) * (ti + 1)) / N;
td[ti].jl = jlg;
td[ti].jh = jhg;
#ifdef USE_PTHREAD
pthread_attr_init(&thread_attr[ti]);
pthread_attr_setdetachstate(&thread_attr[ti],
PTHREAD_CREATE_JOINABLE);
pthread_create(&threads[ti], &thread_attr[ti],
run_thread, &td[ti]);
#else
run_thread(&td[ti]);
#endif
}
#ifdef USE_PTHREAD
for (int ti = 0; ti < N; ti++) {
pthread_join(threads[ti], NULL);
}
free(threads);
free(thread_attr);
#endif
delete[] td;
finish_subdomains(N);
}
virtual ~decompose_domain() {
}
};
};
#endif
|