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.
38 #define THREAD_PER_CPU_DEFAULT 1
39 #define THREAD_COUNT_DEFAULT 4
42 static unsigned int _count
;
43 static unsigned int _cpu_count
;
45 static void try_linux() {
46 assert (_cpu_count
== 0);
51 cpuinfo
= fopen("/proc/cpuinfo", "r");
56 while (!feof(cpuinfo
)) {
57 fgets(buffer
, 100, cpuinfo
);
58 if (strncmp("processor", buffer
, strlen("processor")))
66 if (_cpu_count
== 0) {
71 _count
= THREAD_PER_CPU_DEFAULT
* _cpu_count
;
73 _count
= THREAD_COUNT_DEFAULT
;
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");
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");
95 _count
= _cpu_count
* new_per_cpu
;
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");
119 pthread_mutex_t _lock
;
124 /* _lock = PTHREAD_MUTEX_INITIALIZER; */
125 pthread_mutex_init(&_lock
, NULL
);
131 pthread_mutex_lock(&_lock
);
137 pthread_mutex_unlock(&_lock
);
142 class decompose_domain
{
144 int ilg
, ihg
, jlg
, jhg
;
155 virtual void prepare_subdomains(unsigned int threads
) {
157 virtual void subdomain_algorithm(unsigned int thread
,
158 int il
, int ih
, int jl
, int jh
) {
160 virtual void finish_subdomains(unsigned int threads
) {
164 struct thread_data_t
{
165 decompose_domain
*this_ptr
;
166 unsigned int thread_index
;
170 static void *run_thread(void *thread_data
) {
171 thread_data_t
*td
= (thread_data_t
*) thread_data
;
172 td
->this_ptr
->subdomain_algorithm(td
->thread_index
,
173 td
->il
, td
->ih
, td
->jl
, td
->jh
);
178 decompose_domain(int ilg
, int ihg
, int jlg
, int jhg
) {
190 pthread_t
*threads
= (pthread_t
*) malloc(sizeof(pthread_t
) * N
);
191 pthread_attr_t
*thread_attr
= (pthread_attr_t
*)
192 malloc(sizeof(pthread_attr_t
) * N
);
197 prepare_subdomains(N
);
199 thread_data_t
*td
= new thread_data_t
[N
];
201 for (int ti
= 0; ti
< N
; ti
++) {
202 td
[ti
].this_ptr
= this;
203 td
[ti
].thread_index
= ti
;
204 td
[ti
].il
= ilg
+ ((ihg
- ilg
) * ti
) / N
;
205 td
[ti
].ih
= ilg
+ ((ihg
- ilg
) * (ti
+ 1)) / N
;
209 pthread_attr_init(&thread_attr
[ti
]);
210 pthread_attr_setdetachstate(&thread_attr
[ti
],
211 PTHREAD_CREATE_JOINABLE
);
212 pthread_create(&threads
[ti
], &thread_attr
[ti
],
213 run_thread
, &td
[ti
]);
221 for (int ti
= 0; ti
< N
; ti
++) {
222 pthread_join(threads
[ti
], NULL
);
232 finish_subdomains(N
);
235 virtual ~decompose_domain() {