Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

time consuming vs sqp #190

Closed
CliffBao opened this issue Mar 3, 2023 · 11 comments
Closed

time consuming vs sqp #190

CliffBao opened this issue Mar 3, 2023 · 11 comments

Comments

@CliffBao
Copy link

CliffBao commented Mar 3, 2023

Hello , i build examples/cpp/benchmark_dense_qp.cpp and run it, but why resulted setup time and solve time is 0s?

Then I use clock_gettime(CLOCK_THREAD_CPUTIME_ID, &time) to count cpu time consuming, this time I get
image
which is quite different from time declared in this file.

My full code is

/*

Compile this code once with and without vectorization to see performance
difference.

Use vectorization:
g++ -O3 -march=native -DNDEBUG -std=gnu++17 -DPROXSUITE_VECTORIZE
benchmark_dense_qp.cpp -o benchmark_dense_qp $(pkg-config --cflags proxsuite)

Do not use vectorization:
g++ -DNDEBUG -std=gnu++17 benchmark_dense_qp.cpp -o benchmark_dense_qp
$(pkg-config --cflags proxsuite)

Comparison of timings on Intel i7-11850H and ubuntu 20.04 using this file:

With vectorization:
sparsity_factor: 0.1
Setup Time consumption(dense): 0.000237295s
Solve Time consumption(dense): 0.000500206s
sparsity_factor: 0.2
Setup Time consumption(dense): 0.000465961s
Solve Time consumption(dense): 0.000903787s
sparsity_factor: 0.3
Setup Time consumption(dense): 0.000697931s
Solve Time consumption(dense): 0.00136976s
sparsity_factor: 0.4
Setup Time consumption(dense): 0.000931736s
Solve Time consumption(dense): 0.00185252s


Without vectorization:
sparsity_factor: 0.1
Setup Time consumption(dense): 0.0147825s
Solve Time consumption(dense): 0.0277815s
sparsity_factor: 0.2
Setup Time consumption(dense): 0.029592s
Solve Time consumption(dense): 0.0490869s
sparsity_factor: 0.3
Setup Time consumption(dense): 0.0443664s
Solve Time consumption(dense): 0.0746045s
sparsity_factor: 0.4
Setup Time consumption(dense): 0.0592621s
Solve Time consumption(dense): 0.101507s

*/

#include <proxsuite/proxqp/dense/dense.hpp>
#include <proxsuite/proxqp/utils/random_qp_problems.hpp>
#include <time.h>

using namespace proxsuite::proxqp;
using T = double;

uint64_t osal_get_boot_time_us(void)
{
    struct timespec time = {0, 0};
    clock_gettime(CLOCK_MONOTONIC, &time);
    return ((uint64_t)time.tv_sec * 1000000) + ((uint64_t)time.tv_nsec / 1000);
}

uint64_t osal_get_boot_time_ms(void)
{
    struct timespec time = {0, 0};
    clock_gettime(CLOCK_MONOTONIC, &time);
    return ((uint64_t)time.tv_sec * 1000) + ((uint64_t)time.tv_nsec / 1000000);
}

uint64_t osal_get_thread_time_us(void)
{
    struct timespec time = {0, 0};
    clock_gettime(CLOCK_THREAD_CPUTIME_ID, &time);
    return ((uint64_t)time.tv_sec * 1000000) + ((uint64_t)time.tv_nsec / 1000);
}

uint64_t osal_get_thread_time_ms(void)
{
    struct timespec time = {0, 0};
    clock_gettime(CLOCK_THREAD_CPUTIME_ID, &time);
    return ((uint64_t)time.tv_sec * 1000) + ((uint64_t)time.tv_nsec / 1000000);
}

int
main()
{
  double N = 100.0;
  double solve_time = 0.0;
  double setup_time = 0.0;

  dense::isize dim = 100;
  dense::isize n_eq(dim / 2);
  dense::isize n_in(dim / 2);

  for (T sparsity_factor = 0.1; sparsity_factor < 0.5; sparsity_factor += 0.1) {
    T strong_convexity_factor(1.e-2);
    dense::Model<T> qp_random = utils::dense_strongly_convex_qp(
      dim, n_eq, n_in, sparsity_factor, strong_convexity_factor);
    time_t start;
    for (int i = 0; i < N; i++) {
      uint64_t sqp_tt_start      = osal_get_thread_time_us();
      dense::QP<T> qp(dim, n_eq, n_in);
      qp.settings.max_iter = 10000;
      qp.settings.max_iter_in = 1000;
      qp.settings.eps_abs = 1e-4;
      qp.settings.eps_rel = 0;
      qp.init(qp_random.H,
              qp_random.g,
              qp_random.A,
              qp_random.b,
              qp_random.C,
              qp_random.l,
              qp_random.u);
      qp.solve();
      solve_time += (osal_get_thread_time_us() - sqp_tt_start) / N;
      setup_time += qp.results.info.setup_time / N;
    }
    std::cout << "sparsity_factor: " << sparsity_factor << std::endl;
    std::cout << "Setup Time consumption(dense): " << setup_time << " us"
              << std::endl
              << "Solve Time consumption(dense): " << solve_time << " us"
              << std::endl;
  }

  return 0;
}

And the cmake list file is

cmake_minimum_required(VERSION 3.10)

project(Example CXX)
find_package(proxsuite REQUIRED)
set(CMAKE_CXX_STANDARD 17) # set(CMAKE_CXX_STANDARD 14) will work too

add_executable(example benchmark_dense_qp.cpp)
target_link_libraries(example PUBLIC proxsuite::proxsuite)

# Vectorization support via SIMDE and activated by the compilation options '-march=native' or `-mavx2 -mavx512f`
add_executable(example_with_full_vectorization_support benchmark_dense_qp.cpp)
target_link_libraries(example_with_full_vectorization_support PUBLIC proxsuite::proxsuite-vectorized)
target_compile_options(example_with_full_vectorization_support PUBLIC "-march=native")

My PC is Intel i7-11700 and ubuntu 18.04 ,which should not cost too much time from given reference consumption.

In addition, I wonder how sqp performs under this same dense matrix?

@fabinsch
Copy link
Collaborator

fabinsch commented Mar 3, 2023

Hi @CliffBao, I just opened a PR to fix this example. You have to add one line to turn on the computation of the timings. By default it is off. You do so by:

qp.settings.compute_timings = true

when setting up the qp.

@CliffBao
Copy link
Author

CliffBao commented Mar 3, 2023

Hi, I add this line and this time get correct time output. The summation of setup time and solve time is almost the same with my own calculation
image
My concern is that why it cost so much time ranging from 50ms to 100ms for a 100 dim matrix? This differs a lot from published result. How can I compare it with sqp fairly on my platform?

@fabinsch
Copy link
Collaborator

fabinsch commented Mar 3, 2023

The speed-up you are seeing with vectorization is not the same as for us. How did you install proxsuite? If from source, did you properly compile proxsuite with simde ?
what is the output when you do in your build directory:

cmake .. -DBUILD_WITH_VECTORIZATION_SUPPORT=ON

@jcarpent
Copy link
Member

jcarpent commented Mar 3, 2023

I guess you should compile in Release mode

@CliffBao
Copy link
Author

CliffBao commented Mar 6, 2023

-DBUILD_WITH_VECTORIZATION_SUPPORT=ON

Hi, I install proxsuite from source, following steps is https://github.com/Simple-Robotics/proxsuite/blob/main/doc/5-installation.md. I can locate simde
image

In my build directory, run

cmake .. -DBUILD_WITH_VECTORIZATION_SUPPORT=ON

results in
image

My CMakeLists is set like https://github.com/Simple-Robotics/proxsuite says

cmake_minimum_required(VERSION 3.10)

project(Example CXX)
find_package(proxsuite REQUIRED)
set(CMAKE_CXX_STANDARD 17) # set(CMAKE_CXX_STANDARD 14) will work too

add_executable(example benchmark_dense_qp.cpp)
target_link_libraries(example PUBLIC proxsuite::proxsuite)

# Vectorization support via SIMDE and activated by the compilation options '-march=native' or `-mavx2 -mavx512f`
add_executable(example_with_full_vectorization_support benchmark_dense_qp.cpp)
target_link_libraries(example_with_full_vectorization_support PUBLIC proxsuite::proxsuite-vectorized)
target_compile_options(example_with_full_vectorization_support PUBLIC "-march=native")

It seems it should be modified to support vectorization and release mode compilation?

@jcarpent
Copy link
Member

jcarpent commented Mar 6, 2023

You should add -DCMAKE_BUILD_TYPE=Release when using cmake command. Please look for it on internet if it seems new to you

@jcarpent
Copy link
Member

jcarpent commented Mar 6, 2023

I will close this issue as it seems the missing Release information is the solution

@jcarpent jcarpent closed this as completed Mar 6, 2023
@shbang91
Copy link

@CliffBao, I have one quick question. How did you install SIMDE even on ubuntu 18.04? I think they only support ubuntu 22.04.

@fabinsch
Copy link
Collaborator

@shbang91 you can install via conda. I just tried conda install -c conda-forge -y simde on Ubuntu-18.04 and it works fine.

@shbang91
Copy link

shbang91 commented Mar 23, 2023

@fabinsch I think I need the header files of the simde library because I want to use c++ version of ProxQP. Do you think I can manually copy and paste the headers of simde library into /usr/local/include directory, which would make the FindSimde.cmake find the simde library.

On another note, I tried building the test by triggering BUILD_TESTING =ON, but it failed to build the test binaries like the below:
Screenshot 2023-03-23 at 8 39 01 AM

Is this issue you are already aware of? I'm using ubuntu 18.04.

In addition, assuming the same setting (CMakelists, etc), even though I tried to solve the same problem on MacOS and ubuntu 18.04, MacOS solved the problem successfully, but ubuntu failed. Do you have any idea on this?

Thank you.

@fabinsch
Copy link
Collaborator

hi @shbang91, you can still use conda to install simde (this gets the necessary files) and then build the c++ code of proxsuite from source. You just need to point cmake to the correct location. The easiest is to start from a new conda env, then install all necessary dependencies via conda install -c conda-forge proxsuite --only-deps. Then you can build it from source by specifying

cmake .. -DCMAKE_INSTALL_PREFIX=$CONDA_PREFIX -DCMAKE_BUILD_TYPE=Release -DBUILD_PYTHON_INTERFACE=OFF -DBUILD_TESTING=OFF

I just tried, this works on Ubuntu 18.04.

The other problem is really not related anymore to this issue, please consider opening a new issue or just comment the lines 39 and 40 in test/CMakeLists.txt.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants