/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_thread.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
 
 
7
#include "hw_sched.h"
 
8
#include "hw_thread.h"
 
9
 
 
10
#ifdef HW_SINGLE_MODE
 
11
# define hw_run_entry(entry) entry
 
12
#else /* HW_SINGLE_MODE */
 
13
# define hw_run_entry(entry) runs[entry]
 
14
#endif /* HW_SINGLE_MODE */
 
15
 
 
16
static void *hw_thread_function(HWThread ctx) {
 
17
    int err;
 
18
    int chunk_id;
 
19
    
 
20
    HWRunFunction *runs;
 
21
    HWRunFunction run;
 
22
    HWSched sched;
 
23
    void *hwctx;
 
24
    
 
25
    sched = ctx->sched;
 
26
    runs = ctx->run;
 
27
    hwctx = ctx->hwctx;
 
28
    
 
29
    hw_sched_lock(sched, job_cond);
 
30
 
 
31
    hw_sched_lock(sched, compl_cond);
 
32
    ctx->status = HW_THREAD_STATUS_IDLE;    
 
33
    hw_sched_broadcast(sched, compl);
 
34
    hw_sched_unlock(sched, compl_cond);
 
35
    
 
36
    while (sched->status) {
 
37
        hw_sched_wait(sched, job);
 
38
        if (!sched->status) break;
 
39
 
 
40
        hw_sched_unlock(sched, job_cond);
 
41
        
 
42
        run = hw_run_entry(sched->entry);
 
43
        chunk_id = hw_sched_get_chunk(sched, ctx->thread_id);
 
44
 
 
45
            /* Should be after get_chunk, since we can check if it's first time */
 
46
        ctx->status = HW_THREAD_STATUS_RUNNING; 
 
47
        while (chunk_id >= 0) {
 
48
            err = run(ctx, hwctx, chunk_id, sched->ctx);
 
49
            if (err) {
 
50
                reportError("Error exectuing code, %i\n", err);
 
51
                g_thread_exit(NULL);
 
52
            }
 
53
            chunk_id = hw_sched_get_chunk(sched, ctx->thread_id);
 
54
        }
 
55
 
 
56
        hw_sched_lock(sched, job_cond);
 
57
        
 
58
        hw_sched_lock(sched, compl_cond);
 
59
        ctx->status = HW_THREAD_STATUS_DONE;
 
60
        hw_sched_broadcast(sched, compl);
 
61
        hw_sched_unlock(sched, compl_cond);
 
62
    }
 
63
 
 
64
    hw_sched_unlock(sched, job_cond);
 
65
 
 
66
    g_thread_exit(NULL);
 
67
}
 
68
 
 
69
HWThread hw_thread_create(HWSched sched, int thread_id, void *hwctx, HWRunFunction *run_func, HWFreeFunction free_func) {
 
70
    GError *err;
 
71
    
 
72
    HWThread ctx;
 
73
    
 
74
    ctx = (HWThread)malloc(sizeof(HWThreadS));
 
75
    if (!ctx) return ctx;
 
76
    
 
77
    memset(ctx, 0, sizeof(HWThreadS));
 
78
 
 
79
    ctx->sched = sched;
 
80
    ctx->hwctx = hwctx;
 
81
    ctx->run = run_func;
 
82
    ctx->free = free_func;
 
83
    ctx->thread_id = thread_id;
 
84
    ctx->status = HW_THREAD_STATUS_INIT;
 
85
    
 
86
    ctx->thread = g_thread_create((GThreadFunc)hw_thread_function, ctx, 1, &err);
 
87
    if (!ctx->thread) {
 
88
        reportError("Error spawn thread, errno: %i\n", err->message);
 
89
        g_error_free(err);
 
90
 
 
91
        hw_thread_destroy(ctx);
 
92
        return NULL;
 
93
    }
 
94
    
 
95
    return ctx;
 
96
}
 
97
 
 
98
void hw_thread_destroy(HWThread ctx) {
 
99
    if (ctx->thread) {
 
100
        g_thread_join(ctx->thread);
 
101
    }
 
102
    
 
103
    if (ctx->data) {
 
104
        free(ctx->data);
 
105
    }
 
106
    
 
107
    if (ctx->free) {
 
108
        ctx->free(ctx->hwctx);
 
109
    }
 
110
    
 
111
    free(ctx);
 
112
}