-
Notifications
You must be signed in to change notification settings - Fork 0
/
RProxy.c
221 lines (184 loc) · 5.36 KB
/
RProxy.c
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
#include <dlfcn.h>
/** Possbile status values */
#define STATUS_ERROR 0
#define STATUS_SUCCESS 1
/** libbbcomp.so reference **/
void* LIB_BBCOMP = 0;
/** Functions from libbbcomp **/
int(*BB_configure)(int, const char*) = 0;
int(*BB_login)(const char* username, const char* password) = 0;
int(*BB_numberOfTracks)(void) = 0;
const char*(*BB_trackName)(int) = 0;
int(*BB_setTrack)(const char*) = 0;
int(*BB_numberOfProblems)(void) = 0;
int(*BB_setProblem)(int) = 0;
int(*BB_dimension)(void) = 0;
int(*BB_budget)(void) = 0;
int(*BB_evaluations)(void) = 0;
int(*BB_evaluate)(double*, double*) = 0;
int(*BB_history)(int, double*, double*) = 0;
const char*(*BB_errorMessage)(void) = 0;
/**
* Gets function pointers from *.so"
**/
void R_init(int* status) {
*status = STATUS_SUCCESS;
LIB_BBCOMP = dlopen("libbbcomp.so", RTLD_LAZY);
if (!LIB_BBCOMP) goto fail;
BB_configure = dlsym(LIB_BBCOMP, "configure");
if (!BB_configure) goto fail;
BB_login = dlsym(LIB_BBCOMP, "login");
if (!BB_login) goto fail;
BB_numberOfTracks = dlsym(LIB_BBCOMP, "numberOfTracks");
if (!BB_numberOfTracks) goto fail;
BB_trackName = dlsym(LIB_BBCOMP, "trackName");
if (!BB_trackName) goto fail;
BB_setTrack = dlsym(LIB_BBCOMP, "setTrack");
if (!BB_setTrack) goto fail;
BB_numberOfProblems = dlsym(LIB_BBCOMP, "numberOfProblems");
if (!BB_numberOfProblems) goto fail;
BB_setProblem = dlsym(LIB_BBCOMP, "setProblem");
if (!BB_setProblem) goto fail;
BB_dimension = dlsym(LIB_BBCOMP, "dimension");
if (!BB_dimension) goto fail;
BB_budget = dlsym(LIB_BBCOMP, "budget");
if (!BB_budget) goto fail;
BB_evaluations = dlsym(LIB_BBCOMP, "evaluations");
if (!BB_evaluations) goto fail;
BB_evaluate = dlsym(LIB_BBCOMP, "evaluate");
if (!BB_evaluate) goto fail;
BB_history = dlsym(LIB_BBCOMP, "history");
if (!BB_history) goto fail;
BB_errorMessage = dlsym(LIB_BBCOMP, "errorMessage");
if (!BB_errorMessage) goto fail;
return;
fail:
*status = STATUS_ERROR;
return;
}
void R_configure(int* history, const char** logPath, int* status) {
if (!LIB_BBCOMP || !BB_configure) {
*status = STATUS_ERROR;
return;
}
*status = (BB_configure(*history, *logPath) != 0) ? STATUS_SUCCESS : STATUS_ERROR;
}
void R_login(char** log, char** pass, int* status) {
if (!LIB_BBCOMP || !BB_login) {
*status = STATUS_ERROR;
return;
}
*status = (BB_login(*log, *pass) != 0) ? STATUS_SUCCESS : STATUS_ERROR;
}
void R_numberOfTracks(int* noTracks, int* status) {
if (!LIB_BBCOMP || !BB_numberOfTracks) {
*status = STATUS_ERROR;
return;
}
*noTracks = BB_numberOfTracks();
*status = ((*noTracks) != 0) ? STATUS_SUCCESS : STATUS_ERROR;
}
void R_trackName(int* trackIndex, const char** name, int* status) {
if (!LIB_BBCOMP || !BB_trackName) {
*status = STATUS_ERROR;
return;
}
const char* ret_name = BB_trackName(*trackIndex);
if (ret_name)
*name = ret_name;
*status = (ret_name) ? STATUS_SUCCESS : STATUS_ERROR;
}
void R_setTrack(const char** name, int* status) {
if (!LIB_BBCOMP || !BB_setTrack) {
*status = STATUS_ERROR;
return;
}
*status = (BB_setTrack(*name) != 0) ? STATUS_SUCCESS : STATUS_ERROR;
}
void R_numberOfProblems(int* n, int* status) {
if (!LIB_BBCOMP || !BB_numberOfProblems) {
*status = STATUS_ERROR;
return;
}
*n = BB_numberOfProblems();
*status = (*n != 0) ? STATUS_SUCCESS : STATUS_ERROR;
}
void R_setProblem(int* id, int* status) {
if (!LIB_BBCOMP || !BB_setProblem) {
*status = STATUS_ERROR;
return;
}
*status = (BB_setProblem(*id) != 0) ? STATUS_SUCCESS : STATUS_ERROR;
}
void R_dimension(int* n, int* status) {
if (!LIB_BBCOMP || !BB_dimension) {
*status = STATUS_ERROR;
return;
}
*n = BB_dimension();
*status = (*n != 0) ? STATUS_SUCCESS : STATUS_ERROR;
}
void R_budget(int* n, int* status) {
if (!LIB_BBCOMP || !BB_budget) {
*status = STATUS_ERROR;
return;
}
*n = BB_budget();
*status = (*n != 0) ? STATUS_SUCCESS : STATUS_ERROR;
}
void R_evaluations(int* n, int* status) {
if (!LIB_BBCOMP || !BB_evaluations) {
*status = STATUS_ERROR;
return;
}
*n = BB_evaluations();
*status = (*n != -1) ? STATUS_SUCCESS : STATUS_ERROR;
}
void R_evaluate(double* point, double* value, int* data_length, int* status) {
if (!LIB_BBCOMP || !BB_evaluate) {
*status = STATUS_ERROR;
return;
}
const int n = BB_dimension();
if (*data_length % n != 0 || *data_length <= 0 || n <= 0) {
*status = STATUS_ERROR;
return;
}
const int points = *data_length / n;
int i;
for (i = 0; i < points; ++i) {
if (BB_evaluate(point + (i*n), value + i) == 0)
break;
}
*status = i;
return;
}
void R_history(int* index, double* point, double* value, int* data_length, int* status) {
if (!LIB_BBCOMP || !BB_history || *index >= BB_evaluations()) {
*status = STATUS_ERROR;
return;
}
const int n = BB_dimension();
if (*data_length % n != 0 || *data_length <= 0 || n <= 0) {
*status = STATUS_ERROR;
return;
}
const int points = *data_length / n;
int i;
for (i = 0; i < points; ++i) {
if (BB_history(*(index + i), point + (i*n), value + i) == 0)
break;
}
*status = i;
return;
}
void R_errorMessage(const char** msg, int* status) {
*status = STATUS_ERROR;
if (!LIB_BBCOMP || !BB_errorMessage)
return;
const char* ret_val = BB_errorMessage();
if (!ret_val)
return;
*status = STATUS_SUCCESS;
*msg = ret_val;
}