diff --git a/.gitignore b/.gitignore index 378eac2..1b2211d 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1 @@ -build +build* diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..d827524 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,26 @@ +cmake_minimum_required (VERSION 3.1) +list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake") +include(addPacsExecutable) + +project (PACS_EXAMPLES) + +set(CMAKE_CXX_STANDARD 11) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +# disable clang-tidy to avoid compilation issues +# set(CMAKE_CXX_CLANG_TIDY "clang-tidy;-checks=*") + + +set(CMAKE_THREAD_PREFER_PTHREAD TRUE) +set(THREADS_PREFER_PTHREAD_FLAG TRUE) +find_package(Threads REQUIRED) + +if (MSVC) + # warning level 4 and all warnings as errors + add_compile_options(/W4 /WX) +else() + # lots of warnings and all warnings as errors + add_compile_options(-Wall -Wextra -pedantic -Werror) +endif() + +add_subdirectory(code_examples) +add_subdirectory(Laboratory-3) diff --git a/Laboratory-3/CMakeLists.txt b/Laboratory-3/CMakeLists.txt new file mode 100644 index 0000000..a5f452d --- /dev/null +++ b/Laboratory-3/CMakeLists.txt @@ -0,0 +1,2 @@ +ADD_PACS_EXECUTABLE(TARGET pi_taylor_sequential SOURCES pi_taylor_sequential.cc) +ADD_PACS_EXECUTABLE(TARGET pi_taylor_parallel SOURCES pi_taylor_parallel.cc) diff --git a/Laboratory-3/pi_taylor_parallel.cc b/Laboratory-3/pi_taylor_parallel.cc new file mode 100644 index 0000000..f837b5d --- /dev/null +++ b/Laboratory-3/pi_taylor_parallel.cc @@ -0,0 +1,54 @@ +#include +#include +#include +#include +#include +#include +#include + +using my_float = long double; + +void +pi_taylor_chunk(std::vector &output, + size_t thread_id, size_t start_step, size_t stop_step) { + + + +} + +std::pair +usage(int argc, const char *argv[]) { + // read the number of steps from the command line + if (argc != 3) { + std::cerr << "Invalid syntax: pi_taylor " << std::endl; + exit(1); + } + + size_t steps = std::stoll(argv[1]); + size_t threads = std::stoll(argv[2]); + + if (steps < threads ){ + std::cerr << "The number of steps should be larger than the number of threads" << std::endl; + exit(1); + + } + return std::make_pair(steps, threads); +} + +int main(int argc, const char *argv[]) { + + + auto ret_pair = usage(argc, argv); + auto steps = ret_pair.first; + auto threads = ret_pair.second; + + my_float pi; + + // please complete missing parts + + + std::cout << "For " << steps << ", pi value: " + << std::setprecision(std::numeric_limits::digits10 + 1) + << pi << std::endl; +} + diff --git a/Laboratory-3/pi_taylor_sequential.cc b/Laboratory-3/pi_taylor_sequential.cc new file mode 100644 index 0000000..069976e --- /dev/null +++ b/Laboratory-3/pi_taylor_sequential.cc @@ -0,0 +1,26 @@ +#include +#include + +// Allow to change the floating point type +using my_float = long double; + +my_float pi_taylor(size_t steps) { + return 0.0f; +} + +int main(int argc, const char *argv[]) { + + // read the number of steps from the command line + if (argc != 2) { + std::cerr << "Invalid syntax: pi_taylor " << std::endl; + exit(1); + + } + + size_t steps = std::stoll(argv[1]); + auto pi = pi_taylor(steps); + + std::cout << "For " << steps << ", pi value: " + << std::setprecision(std::numeric_limits::digits10 + 1) + << pi << std::endl; +} diff --git a/README.md b/README.md index 7a55285..c0cd44a 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,27 @@ Official repository of the Programming and Architecture of Computing Systems course of the MS in Robotics, Graphics, and Computer Vision +## Platform support + +The code examples and some laboratories rely on +[cmake](https://cmake.org/overview/) to enable multi-platform building support, +so there is no need to manually write any Makefile or other build system. Linux +is the prefered operating system to work on the labs, but the binaries have +been also compiled on mac OS. + +Although, we do not provide support for Windows. If you want to build and run +the test on Windows, please consider using [CMake with Visual +Studio](https://docs.microsoft.com/es-es/cpp/build/cmake-projects-in-visual-studio?view=vs-2019). + +You can download the free community edition of [Visual Studio +2019](https://visualstudio.microsoft.com/downloads/), and then with you github +creentials download this repo and compile it. + +One advantage of cmake is the separation between source and binary files. To +generate the programs, you always have to create a build directory first, then +invoke cmake to generate the build files, `Makefiles` for Linux, and, after +than, use the standard `make` tool for obtaining the binaries. By default, cmake builds programs in release mode with optimizations enabled, if you want to build them with debug support; e.g., to use them with `gdb`, you can set the variable `CMAKE_BUILD_TYPE` to `Debug`. + ## Code snippets from Class The directory `code_examples` contains many of the small C++ programs and @@ -11,18 +32,27 @@ fragments from the slides. To compile them, you can use `cmake`. For example: ```bash -cd code_examples -mkdir build -cd build +mkdir build-release +cd build-release cmake ../ # generate the Makefile with cmake make # compile the examples ls # list the examples ``` -Although, we do not provide support for Windows. If you want to build and run -the test on Windows, please consider using [CMake with Visual -Studio](https://docs.microsoft.com/es-es/cpp/build/cmake-projects-in-visual-studio?view=vs-2019). +If you want to compile for debugging, please note that at this point, we will +have two sets of binaries, set the variable `CMAKE_BUILD_TYPE` when calling +`cmake`: + +```bash +mkdir build-debug +cd build-debug +cmake -DCMAKE_BUILD_TYPE=Debug ../ # generate the Makefile with cmake +make -j4 # compile the examples in parallel with 4 jobs +``` + +## Laboratories + +Each laboratory includes its own directory, but all of them can be built at the +same time if required. To build a single laboratory; e.g., the third laboratory, you can invoke c + -You can download the free community edition of [Visual Studio -2019](https://visualstudio.microsoft.com/downloads/), and then with you github -creentials download this repo and compile it. diff --git a/cmake/AddPacsExecutable.cmake b/cmake/AddPacsExecutable.cmake new file mode 100644 index 0000000..e4d9a6d --- /dev/null +++ b/cmake/AddPacsExecutable.cmake @@ -0,0 +1,9 @@ +macro(ADD_PACS_EXECUTABLE) + set(options "") + set(oneValueArgs TARGET KERNEL) + set(multiValueArgs SOURCES) + cmake_parse_arguments(ADD_PACS_EXECUTABLE "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN} ) + + add_executable("${ADD_PACS_EXECUTABLE_TARGET}" ${ADD_PACS_EXECUTABLE_SOURCES}) + target_link_libraries("${ADD_PACS_EXECUTABLE_TARGET}" Threads::Threads) +endmacro(ADD_PACS_EXECUTABLE) diff --git a/code_examples/CMakeLists.txt b/code_examples/CMakeLists.txt index b6603b2..ed8748c 100644 --- a/code_examples/CMakeLists.txt +++ b/code_examples/CMakeLists.txt @@ -1,35 +1,9 @@ -cmake_minimum_required (VERSION 3.1) -project (PACS_EXAMPLES) - -set(CMAKE_CXX_STANDARD 11) -set(CMAKE_CXX_STANDARD_REQUIRED ON) - -set(CMAKE_THREAD_PREFER_PTHREAD TRUE) -set(THREADS_PREFER_PTHREAD_FLAG TRUE) -find_package(Threads REQUIRED) - -if (MSVC) - # warning level 4 and all warnings as errors - add_compile_options(/W4 /WX) -else() - # lots of warnings and all warnings as errors - add_compile_options(-Wall -Wextra -pedantic -Werror) -endif() - -#FIXME add macro for the two steps -add_executable(hello_pacs hello_pacs.cc) -target_link_libraries(hello_pacs Threads::Threads) -add_executable(thread_joinable thread_joinable.cc) -target_link_libraries(thread_joinable Threads::Threads) -add_executable(hello_pacs_two_threads hello_pacs_two_threads.cc) -target_link_libraries(hello_pacs_two_threads Threads::Threads) -add_executable(hello_pacs_args hello_pacs_args.cc) -target_link_libraries(hello_pacs_args Threads::Threads) -add_executable(lambda_example lambda_example.cc) -add_executable(saxpy saxpy.cc) -target_link_libraries(saxpy Threads::Threads) -add_executable(saxpy_scaling saxpy_scaling.cc) -target_link_libraries(saxpy_scaling Threads::Threads) - -add_executable(synchronization synchronization.cc) -target_link_libraries(synchronization Threads::Threads) +ADD_PACS_EXECUTABLE(TARGET hello SOURCES hello_pacs.cc) +ADD_PACS_EXECUTABLE(TARGET thread_joinable SOURCES thread_joinable.cc) +ADD_PACS_EXECUTABLE(TARGET hello_pacs_two_threads SOURCES hello_pacs_two_threads.cc) +ADD_PACS_EXECUTABLE(TARGET hello_pacs_args SOURCES hello_pacs_args.cc) +ADD_PACS_EXECUTABLE(TARGET lambda_example SOURCES lambda_example.cc) +ADD_PACS_EXECUTABLE(TARGET saxpy SOURCES saxpy.cc) +ADD_PACS_EXECUTABLE(TARGET saxpy_scaling SOURCES saxpy_scaling.cc) +ADD_PACS_EXECUTABLE(TARGET synchronization SOURCES synchronization.cc) +ADD_PACS_EXECUTABLE(TARGET mutex SOURCES mutex.cc) diff --git a/code_examples/mutex.cc b/code_examples/mutex.cc new file mode 100644 index 0000000..39b4259 --- /dev/null +++ b/code_examples/mutex.cc @@ -0,0 +1,25 @@ +#include +#include +#include + +namespace { + + std::mutex m; + int shared_var = 0; +} + +void increment() { + m.lock(); + ++shared_var; + m.unlock(); +} + +int main() { + std::thread t0{increment}; + std::thread t1{increment}; + + t0.join(); + t1.join(); + + std::cout << shared_var << std::endl; +} diff --git a/code_examples/pi_taylor b/code_examples/pi_taylor new file mode 100755 index 0000000..78c3ae3 Binary files /dev/null and b/code_examples/pi_taylor differ diff --git a/code_examples/snippet_mutex.cc b/code_examples/snippet_mutex.cc new file mode 100644 index 0000000..5ed21bf --- /dev/null +++ b/code_examples/snippet_mutex.cc @@ -0,0 +1,10 @@ +#include + +std::mutex m; +int shared_var = 0; + +void increment() { + m.lock(); + ++shared_var; + m.unlock(); +} diff --git a/code_examples/snippet_mutex_exception.cc b/code_examples/snippet_mutex_exception.cc new file mode 100644 index 0000000..f8de009 --- /dev/null +++ b/code_examples/snippet_mutex_exception.cc @@ -0,0 +1,13 @@ +#include + +std::mutex m; +int shared_var = 0; + +void increment() { + + int* siete = new int(7); + + m.lock(); + ++shared_var; + m.unlock(); +}