/ani/mrses

To get this branch, use:
bzr branch http://suren.me/webbzr/ani/mrses
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
#define _GNU_SOURCE

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <sched.h>
#include <pthread.h>
#include <errno.h>

#include "msg.h"
#include "hw_sched.h"

#include "mrses_ppu.h"

#ifdef HW_HAVE_SPU
# include <libspe2.h>
# include "mrses_spu.h"

extern spe_program_handle_t mrses_spe;
#endif /* HW_HAVE_SPU */


#define MUTEX_INIT(ctx, name) \
    if (!err) err = pthread_mutex_init(&ctx->name##_mutex, NULL);
    
#define MUTEX_FREE(ctx, name) \
    pthread_mutex_destroy(&ctx->name##_mutex);

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

#define COND_FREE(ctx, name) \
    pthread_cond_destroy(&ctx->name##_cond); \
    MUTEX_FREE(ctx, name##_cond)

HWRunFunction ppu_run[] = {
    (HWRunFunction)mrses_ppu_run,
    (HWRunFunction)mrses_ppu_iterate,
    NULL
};

#ifdef HW_HAVE_SPU
HWRunFunction spu_run[] = {
    (HWRunFunction)mrses_spu_run,
    (HWRunFunction)mrses_spu_iterate,
    NULL
};
#endif /* HW_HAVE_SPU */

HWSched hw_sched_create() {
    int i;
    int err = 0;
    cpu_set_t mask;

    int ppu_count;
    HWSched ctx;
    
#ifdef HW_HAVE_SPU
    int spu_count;
    spe_context_ptr_t spe;
#endif /* HW_HAVE_SPU */

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

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

    ctx->status = 1;

    MUTEX_INIT(ctx, data);

    COND_INIT(ctx, compl);
    if (err) {
	MUTEX_FREE(ctx, data);
    }

    COND_INIT(ctx, job);
    if (err) {
	COND_FREE(ctx, compl);
	MUTEX_FREE(ctx, data);
    } else {
	ctx->sync_init = 1;
    }
    
    if (err) {
	fprintf(stderr, "Error initializing conditions and mutexes, errnon: %i\n", errno);
	hw_sched_destroy(ctx);
	return NULL;
    }


    err = sched_getaffinity(getpid(), sizeof(mask), &mask);

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

#ifdef HW_MAX_PPU
    if (ppu_count > HW_MAX_PPU) ppu_count = HW_MAX_PPU;
#endif /* HW_MAX_PPU */

    ctx->n_threads = 0;
    for (i = 0; i < ppu_count; i++) {
	ctx->thread[ctx->n_threads] = hw_thread_create(ctx, ctx->n_threads, NULL, ppu_run, NULL);
	if (ctx->thread[ctx->n_threads]) ++ctx->n_threads;
    }
    
#ifdef HW_HAVE_SPU
    spu_count = spe_cpu_info_get(SPE_COUNT_USABLE_SPES, -1);
    if ((spu_count + ctx->n_threads) > HW_MAX_THREADS) spu_count = HW_MAX_THREADS - ctx->n_threads;

#ifdef HW_MAX_SPU
    if (spu_count > HW_MAX_SPU) spu_count = HW_MAX_SPU;
#endif /* HW_MAX_SPU */

    for (i = 0; i < spu_count; i++) {
	spe = spe_context_create (0, NULL);
	if (spe == NULL) {
    	    reportError("Failed to create SPE context");
	    hw_sched_destroy(ctx);
	    return NULL;
	}
	
	err = spe_program_load (spe, &mrses_spe);
        if (err) {
    	    reportError("Failed to load program into the SPE, error: %i", err);
	    return NULL;
	}

	ctx->thread[ctx->n_threads] = hw_thread_create(ctx, ctx->n_threads, spe, spu_run, (HWFreeFunction)spe_context_destroy);
	if (ctx->thread[ctx->n_threads]) ++ctx->n_threads;
    }

    reportMessage("ppu: %i, spu: %i", ppu_count, spu_count);
#else /* HW_HAVE_SPU */
    reportMessage("threads: %i", ppu_count);
#endif /* HW_HAVE_SPU */


    return ctx;
}

static int hw_sched_wait_threads(HWSched ctx) {
    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);
    
    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]);
	}
    }

    if (ctx->sync_init) {
        COND_FREE(ctx, job);
	COND_FREE(ctx, compl);
	MUTEX_FREE(ctx, data);
    }

    free(ctx);
}

int hw_sched_set_sequential_mode(HWSched ctx, int *n_blocks, int *cur_block) {
    ctx->mode = HW_SCHED_MODE_SEQUENTIAL;
    ctx->n_blocks = n_blocks;
    ctx->cur_block = cur_block;
    
    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_IDLE) {
		return thread_id;
	    } else {
		return -1;
	    }
	case HW_SCHED_MODE_SEQUENTIAL:
	    hw_sched_lock(ctx, data);
	    block = *ctx->cur_block;
	    if (block < *ctx->n_blocks) {
		*ctx->cur_block = *ctx->cur_block + 1;
	    } else {
		block = -1;
	    }
	    hw_sched_unlock(ctx, data);
	    return block;
	default:
	    return -1;
    }

    return -1;
}

    
int hw_sched_schedule_task(HWSched ctx, void *appctx, int entry) {
    if (!ctx->started) {
	hw_sched_wait_threads(ctx);
    }
    
    ctx->ctx = appctx;
    ctx->entry = entry;
    
    hw_sched_lock(ctx, compl_cond);

    hw_sched_lock(ctx, job_cond);
    hw_sched_broadcast(ctx, job);
    hw_sched_unlock(ctx, job_cond);

    return 0;
}

int hw_sched_wait_task(HWSched ctx) {
    int i = 0;

    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;
	    }
	}
	
    }

/*        
    int i, running = 1;

	//wait all threads set running mode

    while (running) {
	hw_sched_wait(ctx, compl);
	
	running = 0;
        for (i = 0; i < ctx->n_threads; i++) {
	    if (ctx->thread[i]->status) {
		if (ctx->thread[i]->status == HW_THREAD_STATUS_DONE) {
		    ctx->thread[i]->status = HW_THREAD_STATUS_IDLE;
		} else {
		    running = 1;
		    break;
		}
	    }
	}
    }
*/

    hw_sched_unlock(ctx, compl_cond);


    return 0;
}