Skip to content

Commit

Permalink
[ interface ] rename constants
Browse files Browse the repository at this point in the history
  • Loading branch information
dandoh committed Jan 2, 2021
1 parent 73bede4 commit 2ef6685
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 147 deletions.
38 changes: 21 additions & 17 deletions embed/csimple.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,42 +8,46 @@
%{fftUtils}

// number of (higher dimensional) variables
#define NUM_VARIABLES %{numHigherOrderVariables}
// number of scalar variables (because each higher dimensional var is a grid of scalar variables)
#define NUM_ACTUAL_VARIABLES %{numActualVariables}
#define NUM_HIGH_DIMENSIONAL_VARIABLES %{numHighDimensionalVariables}
// number of scalar variables (because each high dimensional var is a grid of scalar variables)
#define NUM_VARIABLES %{numVariables}
#define MEMORY_NUM_DOUBLES %{totalDoubles}
#define MEMORY_NUM_COMPLEX_DOUBLES %{totalComplexes}
// all the actual double variables are allocated one after another, starts from here
#define VARS_START_OFFSET %{varStartOffset}
#define MAX_NUM_ITERATIONS %{maxNumIterations}
#define NUM_SCALAR_CONSTRAINT %{numScalarConstraints}
#define NUM_GENERAL_CONSTRAINT %{numGeneralConstraints}

const char* var_name[NUM_VARIABLES] = { %{varNames} };
const int var_size[NUM_VARIABLES] = { %{varSizes} };
const char* var_name[NUM_HIGH_DIMENSIONAL_VARIABLES] = { %{varNames} };
const int var_size[NUM_HIGH_DIMENSIONAL_VARIABLES] = { %{varSizes} };

const int var_offset[NUM_VARIABLES] = { %{varOffsets} };
const int partial_derivative_offset[NUM_VARIABLES] = { %{partialDerivativeOffsets} };
const int var_offset[NUM_HIGH_DIMENSIONAL_VARIABLES] = { %{varOffsets} };
const int partial_derivative_offset[NUM_HIGH_DIMENSIONAL_VARIABLES] = { %{partialDerivativeOffsets} };
const int objective_offset = %{objectiveOffset};

double ptr[MEMORY_NUM_DOUBLES];
complex double ptr_c[MEMORY_NUM_COMPLEX_DOUBLES];

double lower_bound[NUM_ACTUAL_VARIABLES];
double upper_bound[NUM_ACTUAL_VARIABLES];
double lower_bound[NUM_VARIABLES];
double upper_bound[NUM_VARIABLES];

double sc_lower_bound[NUM_SCALAR_CONSTRAINT];
double sc_upper_bound[NUM_SCALAR_CONSTRAINT];
const int sc_offset[NUM_SCALAR_CONSTRAINT] = { %{scalarConstraintOffsets} };
double sc_lower_bound[NUM_GENERAL_CONSTRAINT];
double sc_upper_bound[NUM_GENERAL_CONSTRAINT];
const int sc_offset[NUM_GENERAL_CONSTRAINT] = { %{generalConstraintOffsets} };

const int sc_partial_derivative_offset[NUM_SCALAR_CONSTRAINT][NUM_VARIABLES] = { %{scalarConstraintPartialDerivativeOffsets} };
const int sc_partial_derivative_offset[NUM_GENERAL_CONSTRAINT][NUM_HIGH_DIMENSIONAL_VARIABLES] = { %{generalConstraintPartialDerivativeOffsets} };

void read_bounds() {
for (int i = 0; i < NUM_ACTUAL_VARIABLES; i++) {
for (int i = 0; i < NUM_VARIABLES; i++) {
lower_bound[i] = -INFINITY;
upper_bound[i] = INFINITY;
}
for (int i = 0; i < NUM_GENERAL_CONSTRAINT; i++) {
sc_lower_bound[i] = -INFINITY;
sc_upper_bound[i] = INFINITY;
}
%{readBounds}
%{readBoundScalarConstraints}
%{readBoundGeneralConstraints}
}

void read_values() {
Expand All @@ -70,4 +74,4 @@ void evaluate_scalar_constraints() {
}
void evaluate_scalar_constraints_jacobian() {
%{evaluateScalarConstraintsJacobian}
}
}
40 changes: 20 additions & 20 deletions solvers/gradient_descent/gradient_descent.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@

#define oo 10000000

extern const char* var_name[NUM_VARIABLES];
extern const int var_num_dim[NUM_VARIABLES];
extern const int var_shape[NUM_VARIABLES][3];
extern const int var_size[NUM_VARIABLES];
extern const int var_offset[NUM_VARIABLES];
extern const int partial_derivative_offset[NUM_VARIABLES];
extern const char* var_name[NUM_HIGH_DIMENSIONAL_VARIABLES];
extern const int var_num_dim[NUM_HIGH_DIMENSIONAL_VARIABLES];
extern const int var_shape[NUM_HIGH_DIMENSIONAL_VARIABLES][3];
extern const int var_size[NUM_HIGH_DIMENSIONAL_VARIABLES];
extern const int var_offset[NUM_HIGH_DIMENSIONAL_VARIABLES];
extern const int partial_derivative_offset[NUM_HIGH_DIMENSIONAL_VARIABLES];
extern const int objective_offset;
extern double ptr[MEM_SIZE];

Expand All @@ -34,7 +34,7 @@ double random_in(double min, double max) {

bool any_partial_derivative_NaN() {
int i, j;
for (i = 0; i < NUM_VARIABLES; i++) {
for (i = 0; i < NUM_HIGH_DIMENSIONAL_VARIABLES; i++) {
for (j = 0; j < var_size[i]; j++) {
if (isnan(ptr[partial_derivative_offset[i] + j])) {
return true;
Expand All @@ -49,7 +49,7 @@ int main() {
srand(time(NULL));
int i, j, cnt, k;
int total_variable_size = 0;
for (i = 0; i < NUM_VARIABLES; i++) {
for (i = 0; i < NUM_HIGH_DIMENSIONAL_VARIABLES; i++) {
total_variable_size = total_variable_size + var_size[i];
}

Expand All @@ -61,7 +61,7 @@ int main() {

while (true) {
// initialize optimizing variables
for (i = 0; i < NUM_VARIABLES; i++) {
for (i = 0; i < NUM_HIGH_DIMENSIONAL_VARIABLES; i++) {
for (j = 0; j < var_size[i]; j++) {
ptr[var_offset[i] + j] = random_in(0, 1);
}
Expand All @@ -77,7 +77,7 @@ int main() {

// save the state of all variables
cnt = 0;
for (i = 0; i < NUM_VARIABLES; i++) {
for (i = 0; i < NUM_HIGH_DIMENSIONAL_VARIABLES; i++) {
for (j = 0; j < var_size[i]; j++) {
var_temp[cnt] = ptr[var_offset[i] + j];
grad_temp[cnt] = ptr[partial_derivative_offset[i] + j];
Expand All @@ -88,7 +88,7 @@ int main() {
// fx
double fx = ptr[objective_offset];
double minus_dot_grad = 0;
for (i = 0; i < NUM_VARIABLES; i++) {
for (i = 0; i < NUM_HIGH_DIMENSIONAL_VARIABLES; i++) {
for (j = 0; j < var_size[i]; j++) {
minus_dot_grad = minus_dot_grad - (ptr[partial_derivative_offset[i] + j] * ptr[partial_derivative_offset[i] + j]);
}
Expand All @@ -102,7 +102,7 @@ int main() {
// after while, x still the same
while (true) {
// write updated variables
for (i = 0; i < NUM_VARIABLES; i++) {
for (i = 0; i < NUM_HIGH_DIMENSIONAL_VARIABLES; i++) {
for (j = 0; j < var_size[i]; j++) {
ptr[var_offset[i] + j] -= t * ptr[partial_derivative_offset[i] + j];
}
Expand All @@ -111,7 +111,7 @@ int main() {
double fnew = ptr[objective_offset];

cnt = 0;
for (i = 0; i < NUM_VARIABLES; i++) {
for (i = 0; i < NUM_HIGH_DIMENSIONAL_VARIABLES; i++) {
for (j = 0; j < var_size[i]; j++) {
ptr[var_offset[i] + j] = var_temp[cnt];
cnt++;
Expand All @@ -124,7 +124,7 @@ int main() {
// acc = - grad(old) <.> grad(new)
double acc = 0;
cnt = 0;
for (i = 0; i < NUM_VARIABLES; i++) {
for (i = 0; i < NUM_HIGH_DIMENSIONAL_VARIABLES; i++) {
for (j = 0; j < var_size[i]; j++) {
acc = acc - grad_temp[cnt] * ptr[partial_derivative_offset[i] + j];
cnt++;
Expand Down Expand Up @@ -154,23 +154,23 @@ int main() {
while (t > 0 && any_partial_derivative_NaN()) {
t = t / 2;
cnt = 0;
for (i = 0; i < NUM_VARIABLES; i++) {
for (i = 0; i < NUM_HIGH_DIMENSIONAL_VARIABLES; i++) {
for (j = 0; j < var_size[i]; j++) {
var_temp[cnt] = ptr[var_offset[i] + j];
cnt++;
}
}

cnt = 0;
for (i = 0; i < NUM_VARIABLES; i++) {
for (i = 0; i < NUM_HIGH_DIMENSIONAL_VARIABLES; i++) {
for (j = 0; j < var_size[i]; j++) {
ptr[var_offset[i] + j] -= t * grad_temp[cnt];
cnt++;
}
}
evaluate_partial_derivatives_and_objective();
cnt = 0;
for (i = 0; i < NUM_VARIABLES; i++) {
for (i = 0; i < NUM_HIGH_DIMENSIONAL_VARIABLES; i++) {
for (j = 0; j < var_size[i]; j++) {
ptr[var_offset[i] + j] = var_temp[cnt];
cnt++;
Expand All @@ -187,7 +187,7 @@ int main() {
double max_step = 0;
// OK now we have a good t, let's update
cnt = 0;
for (i = 0; i < NUM_VARIABLES; i++) {
for (i = 0; i < NUM_HIGH_DIMENSIONAL_VARIABLES; i++) {
for (j = 0; j < var_size[i]; j++) {
double step = -t * grad_temp[cnt];
ptr[var_offset[i] + j] += step;
Expand All @@ -209,7 +209,7 @@ int main() {

if (!isnan(ptr[objective_offset])) {
printf("f_min = %f\n", ptr[objective_offset]);
for (i = 0; i < NUM_VARIABLES; i++) {
for (i = 0; i < NUM_HIGH_DIMENSIONAL_VARIABLES; i++) {
char* var_file_name = malloc(strlen(var_name[i]) + 4);
strcpy(var_file_name, var_name[i]);
strcat(var_file_name, ".txt");
Expand Down Expand Up @@ -248,4 +248,4 @@ int main() {

free(var_temp);
free(grad_temp);
}
}
38 changes: 19 additions & 19 deletions solvers/gradient_descent/gradient_descent_backtracking.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@

#define oo 10000000

extern const char* var_name[NUM_VARIABLES];
extern const int var_num_dim[NUM_VARIABLES];
extern const int var_shape[NUM_VARIABLES][3];
extern const int var_size[NUM_VARIABLES];
extern const int var_offset[NUM_VARIABLES];
extern const int partial_derivative_offset[NUM_VARIABLES];
extern const char* var_name[NUM_HIGH_DIMENSIONAL_VARIABLES];
extern const int var_num_dim[NUM_HIGH_DIMENSIONAL_VARIABLES];
extern const int var_shape[NUM_HIGH_DIMENSIONAL_VARIABLES][3];
extern const int var_size[NUM_HIGH_DIMENSIONAL_VARIABLES];
extern const int var_offset[NUM_HIGH_DIMENSIONAL_VARIABLES];
extern const int partial_derivative_offset[NUM_HIGH_DIMENSIONAL_VARIABLES];
extern const int objective_offset;
extern double ptr[MEM_SIZE];

Expand All @@ -33,7 +33,7 @@ double random_in(double min, double max) {

bool any_partial_derivative_NaN() {
int i, j;
for (i = 0; i < NUM_VARIABLES; i++) {
for (i = 0; i < NUM_HIGH_DIMENSIONAL_VARIABLES; i++) {
for (j = 0; j < var_size[i]; j++) {
if (isnan(ptr[partial_derivative_offset[i] + j])) {
return true;
Expand All @@ -48,7 +48,7 @@ int main() {
srand(time(NULL));
int i, j, cnt;
int total_variable_size = 0;
for (i = 0; i < NUM_VARIABLES; i++) {
for (i = 0; i < NUM_HIGH_DIMENSIONAL_VARIABLES; i++) {
total_variable_size = total_variable_size + var_size[i];
}

Expand All @@ -60,7 +60,7 @@ int main() {

while (true) {
// initialize optimizing variables
for (i = 0; i < NUM_VARIABLES; i++) {
for (i = 0; i < NUM_HIGH_DIMENSIONAL_VARIABLES; i++) {
for (j = 0; j < var_size[i]; j++) {
ptr[var_offset[i] + j] = random_in(0, 1);
}
Expand All @@ -73,7 +73,7 @@ int main() {
while (iter < MAX_ITER) {
// save the state of all variables
cnt = 0;
for (i = 0; i < NUM_VARIABLES; i++) {
for (i = 0; i < NUM_HIGH_DIMENSIONAL_VARIABLES; i++) {
for (j = 0; j < var_size[i]; j++) {
var_temp[cnt] = ptr[var_offset[i] + j];
grad_temp[cnt] = ptr[partial_derivative_offset[i] + j];
Expand All @@ -84,7 +84,7 @@ int main() {
// fx
double fx = ptr[objective_offset];
double minus_dot_grad = 0;
for (i = 0; i < NUM_VARIABLES; i++) {
for (i = 0; i < NUM_HIGH_DIMENSIONAL_VARIABLES; i++) {
for (j = 0; j < var_size[i]; j++) {
minus_dot_grad = minus_dot_grad - (ptr[partial_derivative_offset[i] + j] * ptr[partial_derivative_offset[i] + j]);
}
Expand All @@ -96,7 +96,7 @@ int main() {
// after while, x still the same
while (t > 0) {
cnt = 0;
for (i = 0; i < NUM_VARIABLES; i++) {
for (i = 0; i < NUM_HIGH_DIMENSIONAL_VARIABLES; i++) {
for (j = 0; j < var_size[i]; j++) {
ptr[var_offset[i] + j] -= t * grad_temp[cnt];
cnt++;
Expand All @@ -106,7 +106,7 @@ int main() {


cnt = 0;
for (i = 0; i < NUM_VARIABLES; i++) {
for (i = 0; i < NUM_HIGH_DIMENSIONAL_VARIABLES; i++) {
for (j = 0; j < var_size[i]; j++) {
ptr[var_offset[i] + j] = var_temp[cnt];
cnt++;
Expand All @@ -127,23 +127,23 @@ int main() {
while (t > 0 && any_partial_derivative_NaN()) {
t = t / 2;
cnt = 0;
for (i = 0; i < NUM_VARIABLES; i++) {
for (i = 0; i < NUM_HIGH_DIMENSIONAL_VARIABLES; i++) {
for (j = 0; j < var_size[i]; j++) {
var_temp[cnt] = ptr[var_offset[i] + j];
cnt++;
}
}

cnt = 0;
for (i = 0; i < NUM_VARIABLES; i++) {
for (i = 0; i < NUM_HIGH_DIMENSIONAL_VARIABLES; i++) {
for (j = 0; j < var_size[i]; j++) {
ptr[var_offset[i] + j] -= t * grad_temp[cnt];
cnt++;
}
}
evaluate_partial_derivatives_and_objective();
cnt = 0;
for (i = 0; i < NUM_VARIABLES; i++) {
for (i = 0; i < NUM_HIGH_DIMENSIONAL_VARIABLES; i++) {
for (j = 0; j < var_size[i]; j++) {
ptr[var_offset[i] + j] = var_temp[cnt];
cnt++;
Expand All @@ -160,7 +160,7 @@ int main() {
double max_step = 0;
// OK now we have a good t, let's update
cnt = 0;
for (i = 0; i < NUM_VARIABLES; i++) {
for (i = 0; i < NUM_HIGH_DIMENSIONAL_VARIABLES; i++) {
for (j = 0; j < var_size[i]; j++) {
double step = -t * grad_temp[cnt];
ptr[var_offset[i] + j] += step;
Expand Down Expand Up @@ -189,7 +189,7 @@ int main() {
printf("Writing result to output.txt...\n");
FILE *fp = fopen("output.txt", "w");
if (fp) {
for (i = 0; i < NUM_VARIABLES; i++) {
for (i = 0; i < NUM_HIGH_DIMENSIONAL_VARIABLES; i++) {
for (j = 0; j < var_size[i]; j++) {
fprintf(fp, "%f ", ptr[var_offset[i] + j]);
}
Expand All @@ -203,4 +203,4 @@ int main() {

free(var_temp);
free(grad_temp);
}
}
Loading

0 comments on commit 2ef6685

Please sign in to comment.