Skip to content

Commit

Permalink
fixed build systems, added timing, added flags
Browse files Browse the repository at this point in the history
  • Loading branch information
dance858 committed Nov 13, 2024
1 parent 676041d commit 5f3256f
Show file tree
Hide file tree
Showing 22 changed files with 187 additions and 76 deletions.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ if(USE_OPENMP)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fopenmp")
endif()

if(SPECTRAL_TIMING_FLAG)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DSPECTRAL_TIMING_FLAG")
endif()

message(STATUS "COMPILER_OPTS = ${COMPILER_OPTS}")
message(STATUS "CMAKE_C_FLAGS = ${CMAKE_C_FLAGS}")

Expand Down
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
# MAKEFILE for scs
include scs.mk

SCS_OBJECTS = src/util.o src/cones.o src/exp_cone.o src/aa.o src/rw.o src/linalg.o src/ctrlc.o src/scs_version.o src/normalize.o
SCS_OBJECTS = src/util.o src/cones.o src/exp_cone.o src/aa.o src/rw.o src/linalg.o src/ctrlc.o src/scs_version.o src/normalize.o \
src/spectral_cones/logdeterminant/log_cone_Newton.o src/spectral_cones/logdeterminant/log_cone_IPM.o \
src/spectral_cones/logdeterminant/log_cone_wrapper.o src/spectral_cones/logdeterminant/logdet_cone.o \
src/spectral_cones/nuclear/ell1_cone.o src/spectral_cones/nuclear/nuclear_cone.o \
src/spectral_cones/sum-largest/sum_largest_cone.o src/spectral_cones/sum-largest/sum_largest_eval_cone.o \
src/spectral_cones/util_spectral_cones.o
SCS_O = src/scs.o
SCS_INDIR_O = src/scs_indir.o

Expand Down
14 changes: 13 additions & 1 deletion include/cones.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
extern "C" {
#endif

// macro for time measurements of SpectralSCS
#ifdef SPECTRAL_TIMING_FLAG
#define SPECTRAL_TIMING(action) action
#else
#define SPECTRAL_TIMING(action)
#endif

#include "glbopts.h"
#include "scs.h"
#include "scs_blas.h"
Expand All @@ -14,6 +21,7 @@ extern "C" {




/* private data to help cone projection step */
struct SCS_CONE_WORK {
/*
Expand All @@ -31,12 +39,16 @@ struct SCS_CONE_WORK {
scs_float box_t_warm_start;

/* if the projection onto the logarithmic cone should be warmstarted*/
bool log_cone_warmstart;
bool *log_cone_warmstarts;

/* Needed for ell1 norm cone projection */
Value_index *work_ell1;
scs_float *work_ell1_proj;

// used for timing spectral vector cone and spectral matrix cone projections
SPECTRAL_TIMING(scs_float tot_time_mat_cone_proj;)
SPECTRAL_TIMING(scs_float tot_time_vec_cone_proj;)

#ifdef USE_LAPACK
/* workspace for eigenvector decompositions: */
scs_float *Xs, *Z, *e, *work;
Expand Down
6 changes: 6 additions & 0 deletions include/scs.h
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,12 @@ typedef struct {
scs_float cone_time;
/** Total time (milliseconds) spent in the acceleration routine. */
scs_float accel_time;
#ifdef SPECTRAL_TIMING_FLAG
/** Average time (milliseconds) per iteration matrix cone projection */
scs_float ave_time_matrix_cone_proj;
/** Average time (milliseconds) per iteration for spectral vector cone projection */
scs_float ave_time_vector_cone_proj;
#endif
} ScsInfo;

/*
Expand Down
2 changes: 1 addition & 1 deletion include/util_spectral_cones.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ bool is_pos(const scs_float *x, scs_int n);
bool is_negative(const scs_float *x, scs_int n);
void non_neg_proj(const scs_float *src, scs_float *dst, scs_int n);
scs_float sum_log(const scs_float *x, scs_int n);

scs_float min_vec(const scs_float *vec, scs_int n);

// used for sorting in ell1-norm cone and sum of largest cone.
typedef struct
Expand Down
29 changes: 21 additions & 8 deletions src/cones.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ scs_float SCS(proj_pd_exp_cone)(scs_float *v0, scs_int primal);

// Forward declare spectral matrix cone projections
scs_int SCS(proj_logdet_cone)(scs_float *tvX, const scs_int n, ScsConeWork *c,
Newton_stats *stats, scs_int offset);
Newton_stats *stats, scs_int offset,
bool *warmstart);
scs_int SCS(proj_nuclear_cone)(scs_float *tX, size_t m, size_t n, ScsConeWork *c);
void SCS(proj_ell_one)(scs_float *tx, size_t n, ScsConeWork *c);
scs_int SCS(proj_sum_largest_evals)(scs_float *tX, scs_int n, scs_int k,
Expand Down Expand Up @@ -448,6 +449,9 @@ void SCS(finish_cone)(ScsConeWork *c) {
if (c->work_sum_of_largest) {
scs_free(c->work_sum_of_largest);
}
if (c->log_cone_warmstarts) {
scs_free(c->log_cone_warmstarts);
}
#endif
if (c->work_ell1) {
scs_free(c->work_ell1);
Expand Down Expand Up @@ -556,6 +560,9 @@ static scs_int set_up_sd_cone_work_space(ScsConeWork *c, const ScsCone *k) {
#define _STR(tok) _STR_EXPAND(tok)
scs_printf("BLAS(func) = '%s'\n", _STR(BLAS(func)));
#endif

c->log_cone_warmstarts = (bool *)scs_calloc(k->dsize, sizeof(bool));

// ----------------------------------------------------------------------
// compute max dimension needed for eigenvalue decomposition workspace
// (all cones appearing in the next section of code require eigenvalue
Expand Down Expand Up @@ -776,10 +783,6 @@ static scs_int proj_semi_definite_cone(scs_float *X, const scs_int n,
return 0;
}

#ifdef DEBUG
scs_printf("number of positive eigenvalues: %d \n", n - first_idx);
#endif

/* Z is matrix of eigenvectors with positive eigenvalues */
memcpy(Z, &Xs[first_idx * n], sizeof(scs_float) * n * (n - first_idx));

Expand Down Expand Up @@ -1005,8 +1008,9 @@ static scs_int proj_cone(scs_float *x, const ScsCone *k, ScsConeWork *c,
scs_int normalize, scs_float *r_y) {
scs_int i, status;
scs_int count = 0;
scs_int offset_logdet_cone = 0; /* used for warmstarting log-cone projections */
scs_int offset_log_cone = 0; /* used for warmstarting log-cone projections */
scs_float *r_box = SCS_NULL;
SPECTRAL_TIMING(SCS(timer) spec_mat_proj_timer;)

if (k->z) { /* doesn't use r_y */
/* project onto primal zero / dual free cone */
Expand Down Expand Up @@ -1043,7 +1047,9 @@ static scs_int proj_cone(scs_float *x, const ScsCone *k, ScsConeWork *c,
if (k->ssize && k->s) { /* doesn't use r_y */
/* project onto PSD cones */
for (i = 0; i < k->ssize; ++i) {
SPECTRAL_TIMING(SCS(tic)(&spec_mat_proj_timer);)
status = proj_semi_definite_cone(&(x[count]), k->s[i], c);
SPECTRAL_TIMING(c->tot_time_mat_cone_proj += SCS(tocq)(&spec_mat_proj_timer);)
if (status < 0) {
return status;
}
Expand Down Expand Up @@ -1093,9 +1099,12 @@ static scs_int proj_cone(scs_float *x, const ScsCone *k, ScsConeWork *c,
/* project onto logdet cones */
if (k->dsize && k->d) {
for (i = 0; i < k->dsize; ++i) {
SPECTRAL_TIMING(SCS(tic)(&spec_mat_proj_timer);)
status = SCS(proj_logdet_cone)(&(x[count]), k->d[i], c, &(c->newton_stats),
offset_logdet_cone);
offset_logdet_cone += k->d[i] + 2;
offset_log_cone,
c->log_cone_warmstarts + i);
SPECTRAL_TIMING(c->tot_time_mat_cone_proj += SCS(tocq)(&spec_mat_proj_timer);)
offset_log_cone += k->d[i] + 2;
if (status < 0) {
return status;
}
Expand All @@ -1105,7 +1114,9 @@ static scs_int proj_cone(scs_float *x, const ScsCone *k, ScsConeWork *c,
/* project onto nuclear norm cones */
if (k->nucsize && k->nuc_m && k->nuc_n) {
for (i = 0; i < k->nucsize; ++i) {
SPECTRAL_TIMING(SCS(tic)(&spec_mat_proj_timer);)
status = SCS(proj_nuclear_cone)(&(x[count]), k->nuc_m[i], k->nuc_n[i], c);
SPECTRAL_TIMING(c->tot_time_mat_cone_proj += SCS(tocq)(&spec_mat_proj_timer);)
if (status < 0) {
return status;
}
Expand All @@ -1122,7 +1133,9 @@ static scs_int proj_cone(scs_float *x, const ScsCone *k, ScsConeWork *c,
/* project onto sum-of-largest eigenvalues cone */
if (k->sl_size && k->sl_n && k->sl_k) {
for (i = 0; i < k->sl_size; ++i) {
SPECTRAL_TIMING(SCS(tic)(&spec_mat_proj_timer);)
status = SCS(proj_sum_largest_evals)(&(x[count]), k->sl_n[i], k->sl_k[i], c);
SPECTRAL_TIMING(c->tot_time_mat_cone_proj += SCS(tocq)(&spec_mat_proj_timer);)
if (status < 0) {
return status;
}
Expand Down
6 changes: 5 additions & 1 deletion src/scs.c
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,10 @@ static void finalize(ScsWork *w, ScsSolution *sol, ScsInfo *info,
info->rejected_accel_steps = w->rejected_accel_steps;
info->accepted_accel_steps = w->accepted_accel_steps;
info->comp_slack = ABS(sty);
#ifdef SPECTRAL_TIMING_FLAG
info->ave_time_matrix_cone_proj = w->cone_work->tot_time_mat_cone_proj / iter;
info->ave_time_vector_cone_proj = w->cone_work->tot_time_vec_cone_proj / iter;
#endif
if (info->comp_slack > 1e-5 * MAX(nm_s, nm_y)) {
scs_printf("WARNING - large complementary slackness residual: %f\n",
info->comp_slack);
Expand Down Expand Up @@ -1050,7 +1054,7 @@ scs_int scs_solve(ScsWork *w, ScsSolution *sol, ScsInfo *info,
ScsSettings *stgs = w->stgs;
/* set warm start */
stgs->warm_start = warm_start;
w->cone_work->log_cone_warmstart = false;


/* initialize ctrl-c support */
scs_start_interrupt_listener();
Expand Down
6 changes: 3 additions & 3 deletions src/spectral_cones/logdeterminant/log_cone_IPM.c
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ scs_int log_cone_IPM(scs_float t0, scs_float v0, scs_float *x0, scs_float *u1,
scs_float pres0, dres0;
scs_float phi0 = 0.0, dphi0 = 0.0, step_size0 = 0.0;

#ifdef DEBUG
#ifdef SPECTRAL_DEBUG
printf("%-3s%-15s%-15s%-15s%-10s%-10s\n", "", "gap", "pres", "dres", "sig",
"step");
#endif
Expand Down Expand Up @@ -498,7 +498,7 @@ scs_int log_cone_IPM(scs_float t0, scs_float v0, scs_float *x0, scs_float *u1,

if (dres < FEASTOL_IPM && pres < FEASTOL_IPM && (gap < ABSTOL_IPM || relgap <= RELTOL_IPM))
{
#ifdef DEBUG
#ifdef SPECTRAL_DEBUG
printf("optimal solution found: \n");
printf("gap / pres / dres: %.7e, %.7e, %.7e \n", gap, pres, dres);
#endif
Expand Down Expand Up @@ -710,7 +710,7 @@ scs_int log_cone_IPM(scs_float t0, scs_float v0, scs_float *x0, scs_float *u1,
r = rnew;
memcpy(z, z_new, 3 * sizeof(*z));
memcpy(s, s_new, 3 * sizeof(*s));
#ifdef DEBUG
#ifdef SPECTRAL_DEBUG
printf("%ld: %.7e, %.7e, %.7e, %f, %.3f \n",
iter, gap, pres, dres, sigma, step_size);
#endif
Expand Down
Loading

0 comments on commit 5f3256f

Please sign in to comment.