Skip to content

Commit

Permalink
Repo reorganization with Laboratory 3 included
Browse files Browse the repository at this point in the history
  • Loading branch information
dariosg committed Oct 27, 2020
1 parent 1555936 commit 862419d
Show file tree
Hide file tree
Showing 12 changed files with 214 additions and 45 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
build
build*
26 changes: 26 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 2 additions & 0 deletions Laboratory-3/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
54 changes: 54 additions & 0 deletions Laboratory-3/pi_taylor_parallel.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include <iomanip>
#include <iostream>
#include <numeric>
#include <string>
#include <thread>
#include <utility>
#include <vector>

using my_float = long double;

void
pi_taylor_chunk(std::vector<my_float> &output,
size_t thread_id, size_t start_step, size_t stop_step) {



}

std::pair<size_t, size_t>
usage(int argc, const char *argv[]) {
// read the number of steps from the command line
if (argc != 3) {
std::cerr << "Invalid syntax: pi_taylor <steps> <threads>" << 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<long double>::digits10 + 1)
<< pi << std::endl;
}

26 changes: 26 additions & 0 deletions Laboratory-3/pi_taylor_sequential.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <iomanip>
#include <iostream>

// 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 <steps>" << 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<my_float>::digits10 + 1)
<< pi << std::endl;
}
48 changes: 39 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
9 changes: 9 additions & 0 deletions cmake/AddPacsExecutable.cmake
Original file line number Diff line number Diff line change
@@ -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)
44 changes: 9 additions & 35 deletions code_examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
25 changes: 25 additions & 0 deletions code_examples/mutex.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <iostream>
#include <mutex>
#include <thread>

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;
}
Binary file added code_examples/pi_taylor
Binary file not shown.
10 changes: 10 additions & 0 deletions code_examples/snippet_mutex.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <mutex>

std::mutex m;
int shared_var = 0;

void increment() {
m.lock();
++shared_var;
m.unlock();
}
13 changes: 13 additions & 0 deletions code_examples/snippet_mutex_exception.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <mutex>

std::mutex m;
int shared_var = 0;

void increment() {

int* siete = new int(7);

m.lock();
++shared_var;
m.unlock();
}

0 comments on commit 862419d

Please sign in to comment.