diff --git a/exercises/week12/starting_point/CMakeLists.txt b/exercises/week12/starting_point/CMakeLists.txt index c5359c4..22f170b 100644 --- a/exercises/week12/starting_point/CMakeLists.txt +++ b/exercises/week12/starting_point/CMakeLists.txt @@ -1,70 +1,69 @@ cmake_minimum_required (VERSION 3.1) project (Particles) - +cmake_policy(VERSION 3.3) set(CMAKE_CXX_STANDARD 14) -set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${PROJECT_SOURCE_DIR}/cmake") ################################################################ # libpart ################################################################ add_library(part compute_boundary.cc compute_verlet_integration.cc particle.cc planet.cc compute_gravity.cc csv_reader.cc particles_factory_interface.cc planets_factory.cc compute_contact.cc compute_kinetic_energy.cc csv_writer.cc system.cc compute_energy.cc compute_potential_energy.cc ping_pong_ball.cc material_point.cc system_evolution.cc ping_pong_balls_factory.cc compute_interaction.cc compute_temperature.cc material_points_factory.cc ) add_executable(particles main.cc) target_link_libraries(particles part) ################################################################ # Google test ################################################################ include(GoogleTest) enable_testing() find_package(GTest) if (GTEST_FOUND) include_directories(${GTEST_INCLUDE_DIRS}) add_executable(test_kepler test_kepler.cc) add_executable(test_fft test_fft.cc) target_link_libraries(test_kepler part ${GTEST_BOTH_LIBRARIES} pthread) target_link_libraries(test_fft part ${GTEST_BOTH_LIBRARIES} ${FFTW_LIBRARIES} pthread) gtest_discover_tests(test_kepler) gtest_discover_tests(test_fft) endif() ################################################################ # Doxygen ################################################################ find_package(Doxygen) if (DOXYGEN_FOUND) # to set other options, read: https://cmake.org/cmake/help/v3.9/module/FindDoxygen.html doxygen_add_docs( doxygen ${PROJECT_SOURCE_DIR} COMMENT "Generate html pages" ) add_custom_target(doc DEPENDS doxygen) endif(DOXYGEN_FOUND) diff --git a/exercises/week12/sujet.pdf b/exercises/week12/sujet.pdf index e258eca..43b07b1 100644 Binary files a/exercises/week12/sujet.pdf and b/exercises/week12/sujet.pdf differ diff --git a/lectures/week11.html b/lectures/week11.html index 7d4060a..d545232 100644 --- a/lectures/week11.html +++ b/lectures/week11.html @@ -1,1060 +1,1083 @@ talk slides

External libraries

Python

import numpy as np
 from numpy.linalg import inv
 
    -
  • Python interpreter searches modules in the form *.py files, or directories with init.py files
  • +
  • Python interpreter searches modules in the form *.py files, or directories with __init__.py files
  • Search through standard paths (/usr/lib/python3.6/)
  • Search through path provided in an environment variable PYTHONPATH (edit .bashrc to extend the list)
  • can see such a list with:
import sys
 print(sys.path)
 

Producing executables

  • Compilation: produces .o files
  • Using external libraries first need to include interfaces
#include <iostream>
 #include <cmath>
 #include <gsl/gsl_sf_bessel.h>
 
  • At compilation, g++ searches through standard paths (such as /usr/bin/include)
  • -
  • Extending the list can be done with the option '-I':

    -
    > g++ -c -Imynew_path file.cc
    +
  • Extending the list can be done with the option '-I':
    > g++ -c -Imynew_path file.cc
     
  • -
  • Link edition (Linking): produces executables/libraries from .o files and libraries with '-l' option

    -
  • +
+ +
+
+
+
+ +
+
    +
  • Link edition (Linking): produces executables/libraries from .o files and libraries with '-l' option
  • At compilation, g++ searches through standard paths (such as /usr/lib)
  • -
  • Extend the list can be done with the option '-L'
    > g++ file1.o file2.o -L~/libpath/ -o exec
    +
  • Extend the list can be done with the option '-L'
    > g++ file1.o file2.o -L/some_path/ -lmylib -o exec
     
  • What is this error about ?
-
-
-
+
> g++ tmp.cc
 
 /usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/8/../../../x86_64-linux-gnu/Scrt1.o: in function `_start':
 (.text+0x20): undefined reference to `main'
 collect2: error: ld returned 1 exit status
 
 
  • What is this error about ?
-
-
-
+
#include <gsl/gsl_sf_bessel.h>
 #include <iostream>
 
 int main(void) {
   double x = 5.0;
   double y = gsl_sf_bessel_J0(x);
   std::cout << "J0(" << x << ") = " << y;
   return 0;
 }
 
> g++ tmp.cc
 
-/usr/bin/ld: /tmp/ccXkvsyR.o: in function `main':
+/usr/bin/ld: /tmp/ccN3AVEO.o: in function `main':
 tmp.cc:(.text+0x1b): undefined reference to `gsl_sf_bessel_J0'
 collect2: error: ld returned 1 exit status
 
 
-
+
+
+
  • Need to specify the "library" where the files reside
-
-
-
+
> g++ -lgsl tmp.cc
 
 
 
-
+
+
+
  • Why does this show no error at link ?
-
-
-
+
#include <cmath>
 
 int main() { return int(sqrt(2.)); }
 
-
> gcc tmp.cc
+
> g++ tmp.cc
 
 
 
-
+
+
+

Static libraries

  • A Static libraries is a set of routines, external functions and variables which are resolved by linker/binder to produce a stand-alone executable.
  • Generally with extension "libXXXX.a" (e.g. /usr/lib/x86_64-linux-gnu/libm.a).
  • The final executable statically linked includes all the libraries (big executable file)
  • carry dependencies but not portable
+ +
+
+
+
+ +

Dynamic libraries

  • Dynamic/Shared libraries#Shared_libraries) are loaded on demand by executables.
  • Shared libraries can be statically linked or dynamically loaded.
  • libXXX.so (or libXXX.dll on windows). e.g. /usr/lib/x86_64-linux-gnu/libm.so
-
+
+
+
#include <cmath>
 #include <gsl/gsl_sf_bessel.h>
 #include <iostream>
 
 int main(void) {
   double x = 5.0;
   double y = gsl_sf_bessel_J0(x);
   std::cout << "J0(" << x << ") = " << y;
   return int(sqrt(2.));
 }
 
-
-
-
+
> ldd exe
 
-	linux-vdso.so.1 (0x00007ffeb9b0c000)
-	libgsl.so.23 => /lib/x86_64-linux-gnu/libgsl.so.23 (0x00007f98cd54c000)
-	libstdc++.so.6 => /lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f98cd3c9000)
-	libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f98cd235000)
-	libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f98cd21b000)
-	libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f98cd05e000)
-	libgslcblas.so.0 => /lib/x86_64-linux-gnu/libgslcblas.so.0 (0x00007f98cd01d000)
-	/lib64/ld-linux-x86-64.so.2 (0x00007f98cd806000)
+	linux-vdso.so.1 (0x00007ffe0acfa000)
+	libgsl.so.23 => /lib/x86_64-linux-gnu/libgsl.so.23 (0x00007f434ca37000)
+	libstdc++.so.6 => /lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f434c8b4000)
+	libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f434c720000)
+	libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f434c706000)
+	libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f434c549000)
+	libgslcblas.so.0 => /lib/x86_64-linux-gnu/libgslcblas.so.0 (0x00007f434c508000)
+	/lib64/ld-linux-x86-64.so.2 (0x00007f434ccf1000)
 
 
-
+
+
+

Scrutinizing a library

  • What to do if you have a link error Undefined symbol
  • For an internal symbol: check missing inline, check missing implementation
  • For an external symbol: search the library file !

Listing the Symbols from a library:

-
-
-
+
> nm -DAC /lib/x86_64-linux-gnu/libm.so.6
 
  • option "-A": precede the symbol with library name
  • option "-D": for dynamic libraries (not needed if .o or .a files)
  • option "-C": demangle C++ symbols
+
+
+
> nm -DAC /lib/x86_64-linux-gnu/libm.so.6
+
+
+
+ +
+
+ +
+
/lib/x86_64-linux-gnu/libm.so.6:000000000000e910 W acos
 /lib/x86_64-linux-gnu/libm.so.6:0000000000011f40 W acosf
 /lib/x86_64-linux-gnu/libm.so.6:0000000000074b30 W acosf128
 /lib/x86_64-linux-gnu/libm.so.6:0000000000056b30 T __acosf128_finite
 /lib/x86_64-linux-gnu/libm.so.6:0000000000011f40 W acosf32
 ...
 
  • U: undefined symbol $\Rightarrow$ used in the library not defined
  • T: text symbol $\Rightarrow$ used in the library and defined in the library
  • -
  • W: weak symbol $\Rightarrow$ used in the library, defined but can be overloaded with a T symbol ()
  • +
  • W: weak symbol $\Rightarrow$ used in the library, defined but can be overloaded with a T symbol

Finding a symbol from a list of files

> find /usr/lib/x86_64-linux-gnu/ -name '*.so' -exec nm -DAC '{}' ';' | grep 'gsl_sf_bessel_J0'
 
/usr/lib/x86_64-linux-gnu/libgsl.so:0000000000164250 T gsl_sf_bessel_J0
 /usr/lib/x86_64-linux-gnu/libgsl.so:0000000000163dd0 T gsl_sf_bessel_J0_e
 
 

Advanced CMake

Creating an executable

cmake_minimum_required (VERSION 3.1)
 project (Wonderland)
 add_executable(exe file1.cc file2.cc)
 
  • compiles file1.o and file2.o and link into an executable exe

Variables

set(VAR_NAME "my variable")
 # example: setting version number 
 set(Wonderland_VERSION_MAJOR 1)
 set(Wonderland_VERSION_MINOR 0))
 # variable appearing in the ccmake menu
 set(CACHED_VARIABLE CACHE STRING "a variable appearing in cmake menu")
 
  • types are BOOL, FILEPATH, PATH, STRING
  • using variables
message("VAR_NAME = ${VAR_NAME}")
 
-
example: ${PROJECT_BINARY_DIR} ${PROJECT_SOURCE_DIR}
-
+ +
example: ${PROJECT_BINARY_DIR} ${PROJECT_SOURCE_DIR}

Adding paths to the include search list

include_directories('/my_preferred_path')
 include_directories("${PROJECT_BINARY_DIR}")
 

Adding an external library

add_executable(exe file1.cc file2.cc)
 target_link_libraries (exe gsl)
 # or with full path
 target_link_libraries (exe /usr/lib/x86_64-linux-gnu/libgsl.so)
 

Creating a library

add_library(mylib file1.cc file2.cc)
-add_executable(exe)
+add_executable(exe main.cc)
 target_link_libraries (exe gsl mylib)
 
-
+
+
+

Making options

# should we use our own math functions?
 option (USE_GSL "Use gsl library" ON) 
 
 if (USE_GSL)
     target_link_libraries (exe gsl mylib)
 endif (USE_GSL)
 
-
+
+
+

Finding libraries

  • For the library files
set(GSL_LIBRARY_PATH CACHE PATH "library where to search libgsl")
-find_library (GSL_LIBRARY libgsl /usr/include/ ${GSL_LIBRARY_PATH})
+find_library (GSL_LIBRARY libgsl ${GSL_LIBRARY_PATH} /usr/lib)
 
  • For the include files
set(GSL_INCLUDE_PATH CACHE PATH "path where to search gsl include files")
-find_file(GSL_INCLUDE gsl_root.h /usr/include/ ${GSL_INCLUDE_PATH})
+find_path(GSL_INCLUDE gsl_root.h ${GSL_INCLUDE_PATH} /usr/include/)
 

Discrete Fourier Transform

  • Taking a signal (vector of points) $(x_i)$, the Discrete Fourier Transform is gien as the signal:
$$[DFT(x)]_k = \hat{x}_k = \sum_{i=0}^{N-1} x_i \cdot e^{-\frac{2\pi i kn}{N}}$$
  • It forms an approximation of Fourier series complex coefficients
  • The inverse DFT is given with
$$[DFT^{-1}(\hat{x})]_i = x_i = \frac{1}{N}\sum_{i=0}^{N-1} \hat{x}_k \cdot e^{\frac{2\pi kin}{N}}$$

Fast Fourier Transform

  • Hierarchical algorithm ($N \log N$)
  • FFTW library: mainstream library implementating the FFT transform (Fastest Fourier Transform in the West)
  • Documentation
  • example
    #include <fftw3.h>
     {
       fftw_complex *in, *out;
       fftw_plan p;
       ...
       in = new fftw_complex[N];
       out = new fftw_complex[N];
       p = fftw_plan_dft_1d(N, in, out, FFTW_FORWARD, FFTW_ESTIMATE);
       ...
       fftw_execute(p); /* repeat as needed */
       ...
       fftw_destroy_plan(p);
       delete [] in;
       delete [] out;
     }
     
-
-
- -
-
diff --git a/work/week11/particle-trajectory-optimization/starting_point/CMakeLists.txt b/work/week11/particle-trajectory-optimization/starting_point/CMakeLists.txt index cfec95a..bc5b122 100644 --- a/work/week11/particle-trajectory-optimization/starting_point/CMakeLists.txt +++ b/work/week11/particle-trajectory-optimization/starting_point/CMakeLists.txt @@ -1,36 +1,37 @@ cmake_minimum_required (VERSION 2.6) project (Particles) set(CMAKE_CXX_STANDARD 14) add_library(part vector.cc compute_boundary.cc compute_verlet_integration.cc particle.cc planet.cc compute_gravity.cc csv_reader.cc particles_factory_interface.cc planets_factory.cc compute_contact.cc compute_kinetic_energy.cc csv_writer.cc system.cc compute_energy.cc compute_potential_energy.cc ping_pong_ball.cc system_evolution.cc ping_pong_balls_factory.cc compute_interaction.cc compute_dissipative_force.cc compute_vertical_gravity.cc) add_executable(particles main.cc) target_link_libraries(particles gtest_main gtest pthread part) add_executable(test_kepler test_kepler.cc) target_link_libraries(test_kepler gtest_main gtest pthread part) + add_custom_target(test ./test_kepler DEPENDS part test_kepler) diff --git a/work/week12/starting_point/CMakeLists.txt b/work/week12/starting_point/CMakeLists.txt index e9c16a2..5d1c840 100644 --- a/work/week12/starting_point/CMakeLists.txt +++ b/work/week12/starting_point/CMakeLists.txt @@ -1,71 +1,92 @@ cmake_minimum_required (VERSION 3.1) project (Particles) cmake_policy(VERSION 3.3) set(CMAKE_CXX_STANDARD 14) set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${PROJECT_SOURCE_DIR}/cmake") ################################################################ # libpart ################################################################ add_library(part compute_boundary.cc compute_verlet_integration.cc particle.cc planet.cc compute_gravity.cc csv_reader.cc particles_factory_interface.cc planets_factory.cc compute_contact.cc compute_kinetic_energy.cc csv_writer.cc system.cc compute_energy.cc compute_potential_energy.cc ping_pong_ball.cc material_point.cc system_evolution.cc ping_pong_balls_factory.cc compute_interaction.cc compute_temperature.cc material_points_factory.cc ) + + +# Create executable add_executable(particles main.cc) target_link_libraries(particles part) + ################################################################ # Google test ################################################################ include(GoogleTest) enable_testing() find_package(GTest) if (GTEST_FOUND) include_directories(${GTEST_INCLUDE_DIRS}) add_executable(test_kepler test_kepler.cc) add_executable(test_fft test_fft.cc) target_link_libraries(test_kepler part ${GTEST_BOTH_LIBRARIES} pthread) target_link_libraries(test_fft part ${GTEST_BOTH_LIBRARIES} ${FFTW_LIBRARIES} pthread) gtest_discover_tests(test_kepler) gtest_discover_tests(test_fft) endif() +################################################################ +# FFTW +################################################################ + + +set(FFTW_LIBRARY_PATH CACHE PATH "library where to search FFTW") +find_library (FFTW_LIBRARY fftw3 /usr/include/ ${FFTW_LIBRARY_PATH}) + +option (USE_FFTW "Use FFTW library" ON) + + +if (USE_FFTW) + target_link_libraries (particles fftw3) +endif (USE_FFTW) + + + ################################################################ # Doxygen ################################################################ find_package(Doxygen) if (DOXYGEN_FOUND) # to set other options, read: https://cmake.org/cmake/help/v3.9/module/FindDoxygen.html doxygen_add_docs( doxygen ${PROJECT_SOURCE_DIR} COMMENT "Generate html pages" ) add_custom_target(doc DEPENDS doxygen) endif(DOXYGEN_FOUND)