summaryrefslogtreecommitdiffstats
path: root/src/roof.c
blob: 505e3aafe84835ce2eb218c3f52a70bfac513b9f (plain)
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
void roof_init() {
    hw_sched_init();
}

Roof *roof_new(UfoRoofConfig *cfg, GError **error) {
    guint i;
    GError *gerr = NULL;

    Roof *ctx = (Roof*)calloc(1, sizeof(Roof));
    if (!ctx) roof_new_error(error, "Can't allocate Roof context");

    ctx->cfg = cfg;

    ctx->n_threads = cfg->n_streams / cfg->sockets_per_thread;
    if (cfg->n_streams % cfg->sockets_per_thread) ctx->n_threads++;

    ctx->rdi = (RoofReadInterface**)calloc(cfg->n_streams, sizeof(RoofReadInterface*));
    ctx->rdc = (RoofReadContext**)calloc(cfg->n_streams, sizeof(RoofReadContext*));
    ctx->rdt = (RoofThreadContext**)calloc(ctx->n_threads, sizeof(RoofThreadContext*));
    ctx->sched = hw_sched_create(cfg->n_threads);

    if ((!ctx->rdi)||(!ctx->rdc)||(!ctx->rdt)||(!ctx->sched)) {
	roof_free(ctx);
	roof_setup_error(error, "Failed to allocate memory for various Roof contexts");
    }

    return ctx;
}

void roof_configure_simulation(Roof *ctx, const gchar *path, guint first_file_number, GError **error) {
    assert(ctx);

    ctx->simulate = 1;
    ctx->path = path;
    ctx->first_file_number = first_file_number;
}

void roof_configure_stop_mode(Roof *ctx, const gulong max, GError **error) {
    assert(ctx);

    ctx->max_datasets = max;
}


void roof_setup(Roof *ctx, GError **error) {
    guint i;
    GError *gerr = NULL;

    assert(ctx);

    RoofConfig *cfg = ctx->cfg;

/*
    ctx->buf = roof_buffer_new(cfg, 2, ctx->max_datasets, &gerr);
    if ((gerr)||(!ctx->buf))
        roof_propagate_error(error, gerr, "roof_buffer_new: ");
*/

    for (i = 0; i < cfg->n_streams; i++) {
	if (ctx->simulate) {
	    if (!ctx->path)
		roof_setup_error(error, "Path to simulated data should be specified");

	    ctx->rdi[i] = roof_read_file_new(cfg, ctx->path, ctx->first_file_number + i, &gerr);
	} else {
	    ctx->rdi[i] = roof_read_socket_new(cfg, i, &gerr);
	}

	if (!ctx->rdi[i]) 
	    roof_propagate_error(error, gerr, "roof_read_interface_new: ");

        ctx->rdc[i] = roof_read_context_new(cfg, ctx->rdi[i], &gerr);
	if (!ctx->rdc[i]) 
	    roof_propagate_error(error, gerr, "roof_read_context_new: ");
    }

	// We try to distribute sockets uniformly respecting sockets_per_thread as maximum limit
    guint extra = 0, sockets_per_thread = cfg->n_streams / ctx->n_threads;
    if (cfg->n_streams % ctx->n_threads) extra = cfg->n_streams - ctx->n_threads * sockets_per_thread;

    guint from, to;
    for (i = 0; i < ctx->n_threads; i++) {
	guint to = from + sockets_per_thread;
	if (i < extra) to++;

	ctx->thr[i]= roof_thread_new(cfg, ctx, from, to, &gerr);
	if (!ctx->thr[i]) roof_propagate_error(error, gerr, "roof_thread_new (%i): ", i);
    }
}


void roof_free(Roof *ctx) {
    guint i;

    if (ctx) {
	RoofConfig *cfg = ctx->cfg;
	if (ctx->sched) hw_sched_destroy(priv->sched);

	if (ctx->rdt) {
	    for (i = 0; i < ctx->n_threads; i++)
		if (ctx->rdt[i]) roof_thread_context_free(ctx->rdt[i]);
	    free(ctx->rdt);
	}

	if (ctx->rdc) {
	    for (i = 0; i < cfg->n_streams; i++)
		if (ctx->rdc[i]) roof_read_context_free(ctx->rdc[i]);
	    free(ctx->rdc);
	}

	if (ctx->rdi) {
	    for (i = 0; i < cfg->n_streams; i++)
		if (ctx->rdi[i]) roof_read_interface_free(ctx->rdi[i]);
	    free(ctx->rdi);
	}

	if (ctx->buf) roof_buffer_free(ctx->buf);
	free(ctx);
    }
}


void roof_read_dataset(Roof *ctx, void *buffer, GError **error) {
    priv->current_dataset;
    priv->current_buffer = buffer;

    err = hw_sched_schedule_thread_task(sched, (void*)ctx, roof_thread_read);
    if (!err) err = hw_sched_wait_task(sched);
    if (err) { fprintf(stderr, "Error %i scheduling init threads", err); exit(-1); }
}


}