/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
/*
 * 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/>.
 */

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

#include "debug.h"

#include "hw_sched.h"
#include "hw_thread.h"

#ifdef HW_USE_THREADS
static void *hw_thread_function(HWThread ctx) {
    int err;
    int chunk_id;
    
    HWRunFunction *runs;
    HWRunFunction run;
    HWSched sched;
    void *hwctx;
    
    sched = ctx->sched;
    runs = ctx->run;
    hwctx = ctx->hwctx;
    
    hw_sched_lock(sched, job_cond);

    hw_sched_lock(sched, compl_cond);
    ctx->status = HW_THREAD_STATUS_IDLE;    
    hw_sched_broadcast(sched, compl);
    hw_sched_unlock(sched, compl_cond);
    
    while (sched->status) {
	hw_sched_wait(sched, job);
	if (!sched->status) break;

	ctx->err = 0;
	ctx->status = HW_THREAD_STATUS_STARTING;
	hw_sched_unlock(sched, job_cond);
	
	run = hw_run_entry(runs, sched->entry);
#if 0
	// Offset to interleave transfers if the GPUBox is used
	// Just check with CUDA_LAUNCH_BLOCKED the togpu time and put it here
	// It should be still significantly less than BP time
	// We can do callibration during initilization in future
	
	usleep(12000 * ctx->thread_id);
#endif
	chunk_id = hw_sched_get_chunk(sched, ctx->thread_id);

	    /* Should be after get_chunk, since we can check if it's first time */
	ctx->status = HW_THREAD_STATUS_RUNNING; 
	while (chunk_id != HW_SCHED_CHUNK_INVALID) {
	    //printf("Thread %i processing slice %i\n", ctx->thread_id, chunk_id);
	    err = run(ctx, hwctx, chunk_id, sched->ctx);
	    if (err) {
		ctx->err = err;
		break;
	    }
	    chunk_id = hw_sched_get_chunk(sched, ctx->thread_id);
	}

	hw_sched_lock(sched, job_cond);
	
	hw_sched_lock(sched, compl_cond);
	ctx->status = HW_THREAD_STATUS_DONE;
	hw_sched_broadcast(sched, compl);
	hw_sched_unlock(sched, compl_cond);
    }

    hw_sched_unlock(sched, job_cond);

    g_thread_exit(NULL);
    return NULL; /* TODO: check this */
}
#endif /* HW_USE_THREADS */


HWThread hw_thread_create(HWSched sched, int thread_id, void *hwctx, HWRunFunction *run_func, HWFreeFunction free_func) {
    GError *err;
    
    HWThread ctx;
    
    ctx = (HWThread)malloc(sizeof(HWThreadS));
    if (!ctx) return ctx;
    
    memset(ctx, 0, sizeof(HWThreadS));

    ctx->sched = sched;
    ctx->hwctx = hwctx;
    ctx->run = run_func;
    ctx->free = free_func;
    ctx->thread_id = thread_id;
    ctx->status = HW_THREAD_STATUS_INIT;
    
#ifdef HW_USE_THREADS
    ctx->thread = g_thread_create((GThreadFunc)hw_thread_function, ctx, 1, &err);
    if (!ctx->thread) {
	pyhst_error("Error spawn thread, errno: %i\n", err->message);
	g_error_free(err);

	hw_thread_destroy(ctx);
	return NULL;
    }
#endif /* HW_USE_THREADS */
    
    return ctx;
}

void hw_thread_destroy(HWThread ctx) {
#ifdef HW_USE_THREADS
    if (ctx->thread) {
	g_thread_join(ctx->thread);
    }
#endif /* HW_USE_THREADS */
    
    if (ctx->data) {
	free(ctx->data);
    }
    
    if (ctx->free) {
	ctx->free(ctx->hwctx);
    }
    
    free(ctx);
}