/tomo/pyhst

To get this branch, use:
bzr branch http://darksoft.org/webbzr/tomo/pyhst
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*
 * The PyHST program is Copyright (C) 2002-2011 of the
 * European Synchrotron Radiation Facility (ESRF) and
 * Karlsruhe Institute of Technology (KIT).
 *
 * PyHST 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.
 * 
 * hst 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifdef HW_HAVE_SCHED_HEADERS
# include <sys/types.h>
# include <unistd.h>
# include <sched.h>
#endif /* HW_HAVE_SCHED_HEADERS */

#include "debug.h"
#include "hw_sched.h"




#ifdef HW_USE_THREADS
# define MUTEX_INIT(ctx, name) \
    if (!err) { \
	ctx->name##_mutex = g_mutex_new(); \
	if (!ctx->name##_mutex) err = 1; \
    }
    
# define MUTEX_FREE(ctx, name) \
    if (ctx->name##_mutex) g_mutex_free(ctx->name##_mutex);

# define COND_INIT(ctx, name) \
    MUTEX_INIT(ctx, name##_cond) \
    if (!err) { \
	ctx->name##_cond = g_cond_new(); \
	if (!ctx->name##_cond) { \
	    err = 1; \
	    MUTEX_FREE(ctx, name##_cond) \
	} \
    }

# define COND_FREE(ctx, name) \
    if (ctx->name##_cond) g_cond_free(ctx->name##_cond); \
    MUTEX_FREE(ctx, name##_cond)
#else /* HW_USE_THREADS */
# define MUTEX_INIT(ctx, name)
# define MUTEX_FREE(ctx, name)
# define COND_INIT(ctx, name)
# define COND_FREE(ctx, name)
#endif /* HW_USE_THREADS */


HWRunFunction ppu_run[] = {
    (HWRunFunction)NULL
};

static int hw_sched_initialized = 0;

int hw_sched_init(void) {
    if (!hw_sched_initialized) {
#ifdef HW_USE_THREADS
	g_thread_init(NULL);
#endif /* HW_USE_THREADS */
	hw_sched_initialized = 1;
    }

    return 0;
}


int hw_sched_get_cpu_count(void) {
#ifdef HW_HAVE_SCHED_HEADERS
    int err;

    int cpu_count;
    cpu_set_t mask;

    err = sched_getaffinity(getpid(), sizeof(mask), &mask);
    if (err) return 1;

# ifdef CPU_COUNT
    cpu_count = CPU_COUNT(&mask);
# else
    for (cpu_count = 0; cpu_count < CPU_SETSIZE; cpu_count++) {
	if (!CPU_ISSET(cpu_count, &mask)) break;
    }
# endif

    if (!cpu_count) cpu_count = 1;
    return cpu_count;    
#else /* HW_HAVE_SCHED_HEADERS */
    return 1;
#endif /* HW_HAVE_SCHED_HEADERS */
}


HWSched hw_sched_create(int cpu_count) {
    int i;
    int err = 0;

    HWSched ctx;

    //hw_sched_init();
    
    ctx = (HWSched)malloc(sizeof(HWSchedS));
    if (!ctx) return NULL;

    memset(ctx, 0, sizeof(HWSchedS));

    ctx->status = 1;

    MUTEX_INIT(ctx, sync);
    MUTEX_INIT(ctx, data);
    COND_INIT(ctx, compl);
    COND_INIT(ctx, job);
    
    if (err) {
	pyhst_error("Error initializing conditions and mutexes");
	hw_sched_destroy(ctx);
	return NULL;
    }
    
    if (!cpu_count) cpu_count = hw_sched_get_cpu_count();
    if (cpu_count > HW_MAX_THREADS) {
	pyhst_warning("Amount of requested threads %i is above limit, using %i", cpu_count, HW_MAX_THREADS);
	cpu_count = HW_MAX_THREADS;
    }

    ctx->n_threads = 0;
    for (i = 0; i < cpu_count; i++) {
	ctx->thread[ctx->n_threads] = hw_thread_create(ctx, ctx->n_threads, NULL, ppu_run, NULL);
	if (ctx->thread[ctx->n_threads]) {
#ifndef HW_USE_THREADS
	    ctx->thread[ctx->n_threads]->status = HW_THREAD_STATUS_STARTING;
#endif /* HW_USE_THREADS */
	    ++ctx->n_threads;
	}
    }
    
    if (!ctx->n_threads) {
	hw_sched_destroy(ctx);
	return NULL;
    }
    
    return ctx;
}

static int hw_sched_wait_threads(HWSched ctx) {
#ifdef HW_USE_THREADS
    int i = 0;
    
    hw_sched_lock(ctx, compl_cond);
    while (i < ctx->n_threads) {
        for (; i < ctx->n_threads; i++) {
	    if (ctx->thread[i]->status == HW_THREAD_STATUS_INIT) {
		hw_sched_wait(ctx, compl);
		break;
	    }
	}
	
    }
    hw_sched_unlock(ctx, compl_cond);
#endif /* HW_USE_THREADS */
    
    ctx->started = 1;

    return 0;
}

void hw_sched_destroy(HWSched ctx) {
    int i;

    if (ctx->n_threads > 0) {
	if (!ctx->started) {
	    hw_sched_wait_threads(ctx);
	}

	ctx->status = 0;
	hw_sched_lock(ctx, job_cond);
	hw_sched_broadcast(ctx, job);
	hw_sched_unlock(ctx, job_cond);
    
	for (i = 0; i < ctx->n_threads; i++) {
	    hw_thread_destroy(ctx->thread[i]);
	}
    }

    COND_FREE(ctx, job);
    COND_FREE(ctx, compl);
    MUTEX_FREE(ctx, data);
    MUTEX_FREE(ctx, sync);

    free(ctx);
}

int hw_sched_set_sequential_mode(HWSched ctx, int *n_blocks, int *cur_block, HWSchedFlags flags) {
    ctx->mode = HW_SCHED_MODE_SEQUENTIAL;
    ctx->n_blocks = n_blocks;
    ctx->cur_block = cur_block;
    ctx->flags = flags;
    
    return 0;
}

int hw_sched_get_chunk(HWSched ctx, int thread_id) {
    int block;

    switch (ctx->mode) {
	case HW_SCHED_MODE_PREALLOCATED:
	    if (ctx->thread[thread_id]->status == HW_THREAD_STATUS_STARTING) {
#ifndef HW_USE_THREADS
	        ctx->thread[thread_id]->status = HW_THREAD_STATUS_DONE;
#endif /* HW_USE_THREADS */
                return thread_id;
	    } else {
		return HW_SCHED_CHUNK_INVALID;
	    }
	case HW_SCHED_MODE_SEQUENTIAL:
	    if ((ctx->flags&HW_SCHED_FLAG_INIT_CALL)&&(ctx->thread[thread_id]->status == HW_THREAD_STATUS_STARTING)) {
	        return HW_SCHED_CHUNK_INIT;
	    }
	    hw_sched_lock(ctx, data);
	    block = *ctx->cur_block;
	    if (block < *ctx->n_blocks) {
		*ctx->cur_block = *ctx->cur_block + 1;
	    } else {
		block = HW_SCHED_CHUNK_INVALID;
	    }
	    hw_sched_unlock(ctx, data);
	    if (block == HW_SCHED_CHUNK_INVALID) {
	        if (((ctx->flags&HW_SCHED_FLAG_FREE_CALL)&&(ctx->thread[thread_id]->status == HW_THREAD_STATUS_RUNNING))) {
	            ctx->thread[thread_id]->status = HW_THREAD_STATUS_FINISHING;
	            return HW_SCHED_CHUNK_FREE;
	        }
	        if ((ctx->flags&HW_SCHED_FLAG_TERMINATOR_CALL)&&((ctx->thread[thread_id]->status == HW_THREAD_STATUS_RUNNING)||(ctx->thread[thread_id]->status == HW_THREAD_STATUS_FINISHING))) {
	            int i;
	            hw_sched_lock(ctx, data);
	            for (i = 0; i < ctx->n_threads; i++) {
	                if (thread_id == i) continue;
	                if ((ctx->thread[i]->status != HW_THREAD_STATUS_DONE)&&(ctx->thread[i]->status != HW_THREAD_STATUS_FINISHING2)&&(ctx->thread[i]->status != HW_THREAD_STATUS_IDLE)) {
	            	    break;
	            	}
	            }
	            ctx->thread[thread_id]->status  = HW_THREAD_STATUS_FINISHING2;
	            hw_sched_unlock(ctx, data);
	            if (i == ctx->n_threads) {
	                return HW_SCHED_CHUNK_TERMINATOR;
	            } 
	        }
	    }
	    return block;
	default:
	    return HW_SCHED_CHUNK_INVALID;
    }

    return -1;
}

    
int hw_sched_schedule_task(HWSched ctx, void *appctx, HWEntry entry) {
#ifdef HW_USE_THREADS
    if (!ctx->started) {
	hw_sched_wait_threads(ctx);
    }
#else /* HW_USE_THREADS */
    int err;
    int i, chunk_id, n_threads;
    HWRunFunction run;
    HWThread thrctx;
#endif /* HW_USE_THREADS */
    
    ctx->ctx = appctx;
    ctx->entry = entry;

    switch (ctx->mode) {
	case HW_SCHED_MODE_SEQUENTIAL:
	    *ctx->cur_block = 0;
	break;
	default:
	    ;
    }
        
#ifdef HW_USE_THREADS
    hw_sched_lock(ctx, compl_cond);

    hw_sched_lock(ctx, job_cond);
    hw_sched_broadcast(ctx, job);
    hw_sched_unlock(ctx, job_cond);
#else  /* HW_USE_THREADS */
    n_threads = ctx->n_threads;
    
    for (i = 0; i < n_threads; i++) {
	thrctx = ctx->thread[i];
	thrctx->err = 0;
    }

    i = 0;
    thrctx = ctx->thread[i];
    chunk_id = hw_sched_get_chunk(ctx, thrctx->thread_id);

    while (chunk_id >= 0) {
	run = hw_run_entry(thrctx->runs, entry);
        err = run(thrctx, thrctx->hwctx, chunk_id, appctx);
	if (err) {
	    thrctx->err = err;
	    break;
	}
	
	if ((++i) == n_threads) i = 0;
	thrctx = ctx->thread[i];
        chunk_id = hw_sched_get_chunk(ctx, thrctx->thread_id);
    }
#endif /* HW_USE_THREADS */

    return 0;
}

int hw_sched_wait_task(HWSched ctx) {
    int err = 0;
    int i = 0, n_threads = ctx->n_threads;

#ifdef HW_USE_THREADS
    while (i < ctx->n_threads) {
        for (; i < ctx->n_threads; i++) {
	    if (ctx->thread[i]->status == HW_THREAD_STATUS_DONE) {
		ctx->thread[i]->status = HW_THREAD_STATUS_IDLE;
	    } else {
		hw_sched_wait(ctx, compl);
		break;
	    }
	}
	
    }

    hw_sched_unlock(ctx, compl_cond);
#endif /* HW_USE_THREADS */

    for (i = 0; i < n_threads; i++) {
	HWThread thrctx = ctx->thread[i];
	if (thrctx->err) return err = thrctx->err;

#ifndef HW_USE_THREADS
        thrctx->status = HW_THREAD_STATUS_IDLE;
#endif /* HW_USE_THREADS */
    }

    return err;
}

int hw_sched_execute_task(HWSched ctx, void *appctx, HWEntry entry) {
    int err;
    
    err = hw_sched_schedule_task(ctx, appctx, entry);
    if (err) return err;
    
    return hw_sched_wait_task(ctx);
}

int hw_sched_schedule_thread_task(HWSched ctx, void *appctx, HWEntry entry) {
    int err;
    
    ctx->saved_mode = ctx->mode;
    ctx->mode = HW_SCHED_MODE_PREALLOCATED;
    err = hw_sched_schedule_task(ctx, appctx, entry);
    
    return err;
}


int hw_sched_wait_thread_task(HWSched ctx) {
    int err;

    err = hw_sched_wait_task(ctx);
    ctx->mode = ctx->saved_mode;

    return err;
}

int hw_sched_execute_thread_task(HWSched ctx, void *appctx, HWEntry entry) {
    int err;
    int saved_mode = ctx->mode;

    ctx->mode = HW_SCHED_MODE_PREALLOCATED;
    err = hw_sched_execute_task(ctx, appctx, entry);
    ctx->mode = saved_mode;
    
    return err;
}