/normxcorr/trunk

To get this branch, use:
bzr branch http://suren.me/webbzr/normxcorr/trunk

« back to all changes in this revision

Viewing changes to dict_hw/src/hw_sched.c

  • Committer: Suren A. Chilingaryan
  • Date: 2010-04-25 04:39:54 UTC
  • Revision ID: csa@dside.dyndns.org-20100425043954-v7xm2bzohickyl9z
Multi-GPU support

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <stdio.h>
 
2
#include <stdlib.h>
 
3
#include <string.h>
 
4
 
 
5
#include "normxcorr_hw_msg.h"
 
6
#include "hw_sched.h"
 
7
 
 
8
//#include "mrses_ppu.h"
 
9
 
 
10
 
 
11
#define MUTEX_INIT(ctx, name) \
 
12
    if (!err) { \
 
13
        ctx->name##_mutex = g_mutex_new(); \
 
14
        if (!ctx->name##_mutex) err = DICT_ERROR_GLIB; \
 
15
    }
 
16
    
 
17
#define MUTEX_FREE(ctx, name) \
 
18
    if (ctx->name##_mutex) g_mutex_free(ctx->name##_mutex);
 
19
 
 
20
#define COND_INIT(ctx, name) \
 
21
    MUTEX_INIT(ctx, name##_cond) \
 
22
    if (!err) { \
 
23
        ctx->name##_cond = g_cond_new(); \
 
24
        if (!ctx->name##_cond) { \
 
25
            err = DICT_ERROR_GLIB; \
 
26
            MUTEX_FREE(ctx, name##_cond) \
 
27
        } \
 
28
    }
 
29
 
 
30
#define COND_FREE(ctx, name) \
 
31
    if (ctx->name##_cond) g_cond_free(ctx->name##_cond); \
 
32
    MUTEX_FREE(ctx, name##_cond)
 
33
 
 
34
HWRunFunction ppu_run[] = {
 
35
    (HWRunFunction)NULL
 
36
};
 
37
 
 
38
static int hw_sched_initialized = 0;
 
39
 
 
40
int hw_sched_init() {
 
41
    if (!hw_sched_initialized) {
 
42
        g_thread_init(NULL);
 
43
        hw_sched_initialized = 1;
 
44
    }
 
45
 
 
46
    return 0;
 
47
}
 
48
 
 
49
 
 
50
 
 
51
HWSched hw_sched_create(int ppu_count) {
 
52
    int i;
 
53
    int err = 0;
 
54
 
 
55
    HWSched ctx;
 
56
 
 
57
    hw_sched_init();
 
58
    
 
59
    ctx = (HWSched)malloc(sizeof(HWSchedS));
 
60
    if (!ctx) return NULL;
 
61
 
 
62
    memset(ctx, 0, sizeof(HWSchedS));
 
63
 
 
64
    ctx->status = 1;
 
65
 
 
66
    MUTEX_INIT(ctx, data);
 
67
    COND_INIT(ctx, compl);
 
68
    COND_INIT(ctx, job);
 
69
    
 
70
    if (err) {
 
71
        reportError("Error initializing conditions and mutexes");
 
72
        hw_sched_destroy(ctx);
 
73
        return NULL;
 
74
    }
 
75
 
 
76
    ctx->n_threads = 0;
 
77
    for (i = 0; i < ppu_count; i++) {
 
78
        ctx->thread[ctx->n_threads] = hw_thread_create(ctx, ctx->n_threads, NULL, ppu_run, NULL);
 
79
        if (ctx->thread[ctx->n_threads]) ++ctx->n_threads;
 
80
    }
 
81
    
 
82
    return ctx;
 
83
}
 
84
 
 
85
static int hw_sched_wait_threads(HWSched ctx) {
 
86
    int i = 0;
 
87
    
 
88
    hw_sched_lock(ctx, compl_cond);
 
89
    while (i < ctx->n_threads) {
 
90
        for (; i < ctx->n_threads; i++) {
 
91
            if (ctx->thread[i]->status == HW_THREAD_STATUS_INIT) {
 
92
                hw_sched_wait(ctx, compl);
 
93
                break;
 
94
            }
 
95
        }
 
96
        
 
97
    }
 
98
    hw_sched_unlock(ctx, compl_cond);
 
99
    
 
100
    ctx->started = 1;
 
101
 
 
102
    return 0;
 
103
}
 
104
 
 
105
void hw_sched_destroy(HWSched ctx) {
 
106
    int i;
 
107
 
 
108
    if (ctx->n_threads > 0) {
 
109
        if (!ctx->started) {
 
110
            hw_sched_wait_threads(ctx);
 
111
        }
 
112
 
 
113
        ctx->status = 0;
 
114
        hw_sched_lock(ctx, job_cond);
 
115
        hw_sched_broadcast(ctx, job);
 
116
        hw_sched_unlock(ctx, job_cond);
 
117
    
 
118
        for (i = 0; i < ctx->n_threads; i++) {
 
119
            hw_thread_destroy(ctx->thread[i]);
 
120
        }
 
121
    }
 
122
 
 
123
    COND_FREE(ctx, job);
 
124
    COND_FREE(ctx, compl);
 
125
    MUTEX_FREE(ctx, data);
 
126
 
 
127
    free(ctx);
 
128
}
 
129
 
 
130
int hw_sched_set_sequential_mode(HWSched ctx, int *n_blocks, int *cur_block) {
 
131
    ctx->mode = HW_SCHED_MODE_SEQUENTIAL;
 
132
    ctx->n_blocks = n_blocks;
 
133
    ctx->cur_block = cur_block;
 
134
    
 
135
    return 0;
 
136
}
 
137
 
 
138
int hw_sched_get_chunk(HWSched ctx, int thread_id) {
 
139
    int block;
 
140
 
 
141
    switch (ctx->mode) {
 
142
        case HW_SCHED_MODE_PREALLOCATED:
 
143
            if (ctx->thread[thread_id]->status == HW_THREAD_STATUS_IDLE) {
 
144
                return thread_id;
 
145
            } else {
 
146
                return -1;
 
147
            }
 
148
        case HW_SCHED_MODE_SEQUENTIAL:
 
149
            hw_sched_lock(ctx, data);
 
150
            block = *ctx->cur_block;
 
151
            if (block < *ctx->n_blocks) {
 
152
                *ctx->cur_block = *ctx->cur_block + 1;
 
153
            } else {
 
154
                block = -1;
 
155
            }
 
156
            hw_sched_unlock(ctx, data);
 
157
            return block;
 
158
        default:
 
159
            return -1;
 
160
    }
 
161
 
 
162
    return -1;
 
163
}
 
164
 
 
165
    
 
166
int hw_sched_schedule_task(HWSched ctx, void *appctx, HWEntry entry) {
 
167
    if (!ctx->started) {
 
168
        hw_sched_wait_threads(ctx);
 
169
    }
 
170
    
 
171
    ctx->ctx = appctx;
 
172
    ctx->entry = entry;
 
173
    
 
174
    hw_sched_lock(ctx, compl_cond);
 
175
 
 
176
    hw_sched_lock(ctx, job_cond);
 
177
    hw_sched_broadcast(ctx, job);
 
178
    hw_sched_unlock(ctx, job_cond);
 
179
 
 
180
    return 0;
 
181
}
 
182
 
 
183
int hw_sched_wait_task(HWSched ctx) {
 
184
    int i = 0;
 
185
 
 
186
    while (i < ctx->n_threads) {
 
187
        for (; i < ctx->n_threads; i++) {
 
188
            if (ctx->thread[i]->status == HW_THREAD_STATUS_DONE) {
 
189
                ctx->thread[i]->status = HW_THREAD_STATUS_IDLE;
 
190
            } else {
 
191
                hw_sched_wait(ctx, compl);
 
192
                break;
 
193
            }
 
194
        }
 
195
        
 
196
    }
 
197
 
 
198
    hw_sched_unlock(ctx, compl_cond);
 
199
 
 
200
    return 0;
 
201
}