Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/cmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, macos-latest]
os: [ubuntu-latest, macos-latest, windows-latest]
build_type: [Release, Debug]

steps:
- uses: actions/checkout@v3

- name: Configure CMake
run: cmake -B build -S . -DCMAKE_BUILD_TYPE=${{ matrix.build_type }}
run: cmake -B build -S . -DCMAKE_BUILD_TYPE=${{ matrix.build_type }} -DCMAKE_VERBOSE_MAKEFILE=ON

- name: Build
run: cmake --build build --config ${{ matrix.build_type }}

- name: Run tests
run: ./build/all_tests
run: cd build && ctest -C ${{ matrix.build_type }} --verbose
59 changes: 38 additions & 21 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,23 @@ else()
message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")
endif()

# Warning flags (always enabled)
add_compile_options(
-Wall # Enable most warnings
-Wextra # Extra warnings
-Wpedantic # Strict ISO C compliance
-Wshadow # Warn about variable shadowing
-Wformat=2 # Extra format string checks
-Wcast-qual # Warn about cast that removes qualifiers
-Wcast-align # Warn about pointer cast alignment issues
-Wunused # Warn about unused variables/functions
-Wdouble-promotion # Warn about float->double promotion
-Wnull-dereference # Warn about null pointer dereference
)
# Warning flags (compiler-specific)
if(MSVC)
add_compile_options(/W4 /WX)
else()
add_compile_options(
-Wall # Enable most warnings
-Wextra # Extra warnings
-Wpedantic # Strict ISO C compliance
-Wshadow # Warn about variable shadowing
-Wformat=2 # Extra format string checks
-Wcast-qual # Warn about cast that removes qualifiers
-Wcast-align # Warn about pointer cast alignment issues
-Wunused # Warn about unused variables/functions
-Wdouble-promotion # Warn about float to double promotion
-Wnull-dereference # Warn about null pointer dereference
)
endif()

# Include directories
include_directories(${PROJECT_SOURCE_DIR}/include)
Expand All @@ -43,15 +47,28 @@ file(GLOB_RECURSE SOURCES "src/*.c")

# Create core library
add_library(dnlp_diff ${SOURCES})
target_link_libraries(dnlp_diff m)

# Config-specific compile options
target_compile_options(dnlp_diff PRIVATE
$<$<CONFIG:Debug>:-g -O0>
$<$<CONFIG:Release>:-O3 -DNDEBUG>
$<$<CONFIG:RelWithDebInfo>:-O2 -g -DNDEBUG>
$<$<CONFIG:MinSizeRel>:-Os -DNDEBUG>
)
# Link math library (Unix/Linux only)
if(NOT MSVC)
target_link_libraries(dnlp_diff m)
endif()

# Config-specific compile options (compiler-specific)
if(MSVC)
target_compile_options(dnlp_diff PRIVATE
$<$<CONFIG:Debug>:/Od /Zi>
$<$<CONFIG:Release>:/O2 /DNDEBUG>
$<$<CONFIG:RelWithDebInfo>:/O2 /Zi /DNDEBUG>
$<$<CONFIG:MinSizeRel>:/Os /DNDEBUG>
)
else()
target_compile_options(dnlp_diff PRIVATE
$<$<CONFIG:Debug>:-g -O0>
$<$<CONFIG:Release>:-O3 -DNDEBUG>
$<$<CONFIG:RelWithDebInfo>:-O2 -g -DNDEBUG>
$<$<CONFIG:MinSizeRel>:-Os -DNDEBUG>
)
endif()

# This is needed for clock_gettime on Linux without compiler extensions
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
Expand Down
64 changes: 50 additions & 14 deletions include/utils/Timer.h
Original file line number Diff line number Diff line change
@@ -1,25 +1,61 @@
/*
* Copyright 2025 Daniel Cederberg
*
* This file is part of the PSLP project (LP Presolver).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef TIMER_H
#define TIMER_H

#include "time.h"

#if defined(_WIN32) || defined(_WIN64)
#include <windows.h>
typedef struct
{
LARGE_INTEGER start, end;
} Timer;
static inline void clock_gettime_monotonic(Timer *timer, int is_start)
{
if (is_start)
{
QueryPerformanceCounter(&timer->start);
}
else
{
QueryPerformanceCounter(&timer->end);
}
}
static inline double get_elapsed_seconds(const Timer *timer)
{
LARGE_INTEGER freq;
QueryPerformanceFrequency(&freq);
return (double) (timer->end.QuadPart - timer->start.QuadPart) /
(double) freq.QuadPart;
}
#define clock_gettime(CLOCK, PTR) \
clock_gettime_monotonic((Timer *) (PTR), ((PTR) == &((Timer *) (PTR))->start))
#define GET_ELAPSED_SECONDS(timer) get_elapsed_seconds(&(timer))
#else
#include <time.h>
typedef struct
{
struct timespec start, end;
} Timer;

// Macro to compute elapsed time in seconds
#define GET_ELAPSED_SECONDS(timer) \
(((timer).end.tv_sec - (timer).start.tv_sec) + \
((double) ((timer).end.tv_nsec - (timer).start.tv_nsec) * 1e-9))

#define RUN_AND_TIME(func, timer, time_variable, result_var, ...) \
do \
{ \
clock_gettime(CLOCK_MONOTONIC, &timer.start); \
(result_var) = func(__VA_ARGS__); \
clock_gettime(CLOCK_MONOTONIC, &timer.end); \
(time_variable) += GET_ELAPSED_SECONDS(timer); \
} while (0)
((double) ((timer).end.tv_sec - (timer).start.tv_sec) + \
(double) ((timer).end.tv_nsec - (timer).start.tv_nsec) / 1e9)
#endif

#endif // TIMER_H
32 changes: 20 additions & 12 deletions include/utils/Vec_macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,23 @@
#include <stdlib.h>
#include <string.h>

// Portable UNUSED macro
#if defined(_MSC_VER)
#define PSLP_UNUSED
#else
#define PSLP_UNUSED __attribute__((unused))
#endif

// Macro to define a generic vector class
#define DEFINE_VECTOR(TYPE, TYPE_NAME) \
typedef struct TYPE_NAME##Vec \
{ \
TYPE *data; \
size_t len; \
size_t capacity; \
int len; \
int capacity; \
} TYPE_NAME##Vec; \
\
__attribute__((unused)) static TYPE_NAME##Vec *TYPE_NAME##Vec_new( \
size_t capacity) \
PSLP_UNUSED static TYPE_NAME##Vec *TYPE_NAME##Vec_new(int capacity) \
{ \
assert(capacity > 0); \
TYPE_NAME##Vec *vec = (TYPE_NAME##Vec *) malloc(sizeof(TYPE_NAME##Vec)); \
Expand Down Expand Up @@ -68,7 +74,8 @@
{ \
vec->capacity *= 2; \
assert(vec->capacity > 0); \
TYPE *temp = (TYPE *) realloc(vec->data, vec->capacity * sizeof(TYPE)); \
TYPE *temp = \
(TYPE *) realloc(vec->data, (size_t) vec->capacity * sizeof(TYPE)); \
if (temp == NULL) \
{ \
TYPE_NAME##Vec_free(vec); \
Expand All @@ -83,17 +90,18 @@
} \
\
static inline void TYPE_NAME##Vec_append_array(TYPE_NAME##Vec *vec, \
const TYPE *values, size_t n) \
const TYPE *values, int n) \
{ \
if (vec->len + n > vec->capacity) \
{ \
size_t new_capacity = vec->capacity > 0 ? vec->capacity : 1; \
int new_capacity = vec->capacity > 0 ? vec->capacity : 1; \
while (vec->len + n > new_capacity) \
{ \
new_capacity *= 2; \
} \
\
TYPE *temp = (TYPE *) realloc(vec->data, new_capacity * sizeof(TYPE)); \
TYPE *temp = \
(TYPE *) realloc(vec->data, (size_t) new_capacity * sizeof(TYPE)); \
if (temp == NULL) \
{ \
TYPE_NAME##Vec_free(vec); \
Expand All @@ -105,13 +113,13 @@
vec->capacity = new_capacity; \
} \
\
memcpy(vec->data + vec->len, values, n * sizeof(TYPE)); \
memcpy(vec->data + vec->len, values, (size_t) n * sizeof(TYPE)); \
vec->len += n; \
} \
__attribute__((unused)) static int TYPE_NAME##Vec_contains( \
const TYPE_NAME##Vec *vec, TYPE value) \
PSLP_UNUSED static int TYPE_NAME##Vec_contains(const TYPE_NAME##Vec *vec, \
TYPE value) \
{ \
for (size_t i = 0; i < vec->len; ++i) \
for (int i = 0; i < vec->len; ++i) \
{ \
if (vec->data[i] == value) \
{ \
Expand Down
3 changes: 0 additions & 3 deletions src/affine/add.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,5 @@ expr *new_add(expr *left, expr *right)
expr_retain(left);
expr_retain(right);

// just for debugging, should be removed
strcpy(node->name, "add");

return node;
}
3 changes: 0 additions & 3 deletions src/bivariate/const_scalar_mult.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,5 @@ expr *new_const_scalar_mult(double a, expr *child)
mult_node->a = a;
expr_retain(child);

// just for debugging, should be removed
strcpy(node->name, "const_scalar_mult");

return node;
}
2 changes: 0 additions & 2 deletions src/elementwise_univariate/exp.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,5 @@ expr *new_exp(expr *child)
node->local_jacobian = local_jacobian;
node->local_wsum_hess = local_wsum_hess;

// just for debugging, should be removed
strcpy(node->name, "exp");
return node;
}
2 changes: 0 additions & 2 deletions src/elementwise_univariate/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,5 @@ expr *new_log(expr *child)
node->local_jacobian = local_jacobian;
node->local_wsum_hess = local_wsum_hess;

// just for debugging, should be removed
strcpy(node->name, "log");
return node;
}
4 changes: 0 additions & 4 deletions tests/wsum_hess/test_hstack.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,6 @@ const char *test_wsum_hess_hstack()

free_expr(hstack_node);
return 0;

return 0;
}

const char *test_wsum_hess_hstack_matrix()
Expand Down Expand Up @@ -209,6 +207,4 @@ const char *test_wsum_hess_hstack_matrix()

free_expr(hstack_node);
return 0;

return 0;
}