-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* concurrency: -refactor cuncurrency into it's own target and folder, move old files into older for cleanup * src: -refactor cmake files and includes to more consistent naming scheme for including headers. -rename matrix_multiplier to matrix_operations -rename multiplication_utils namespace to math_utils nmespace
- Loading branch information
1 parent
57fe949
commit e74d0dc
Showing
52 changed files
with
305 additions
and
305 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
add_library(concurrency_utils channel.hpp waitgroup.hpp waitgroup.cpp) | ||
cmake_path(GET CMAKE_CURRENT_SOURCE_DIR PARENT_PATH MY_PARENT_DIR) | ||
target_include_directories(concurrency_utils PUBLIC ${MY_PARENT_DIR}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
The files here are related to local concurrency |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
#pragma once | ||
|
||
|
||
#include <queue> | ||
#include <mutex> | ||
#include <condition_variable> | ||
namespace concurrency { | ||
|
||
template<class T> | ||
struct Result { | ||
T answer; | ||
bool ok; | ||
}; | ||
|
||
|
||
template<class T> | ||
class Channel { | ||
public: | ||
Channel() : q(), m(), c() {} | ||
|
||
~Channel() { | ||
close(); | ||
} | ||
|
||
// not allowing copy or moving of a channel. | ||
Channel(const Channel &) = delete; | ||
|
||
Channel(Channel &&) = delete; | ||
|
||
|
||
/** | ||
* Adds an element to the queue. | ||
*/ | ||
void write(T t) { | ||
std::lock_guard<std::mutex> lock(m); | ||
q.push(t); | ||
c.notify_one(); | ||
} | ||
|
||
/** | ||
* Get the "front"-element. | ||
* If there is nothing to read from the channel, wait till an element was written on another thread. | ||
*/ | ||
Result<T> read() { | ||
std::unique_lock<std::mutex> lock(m); | ||
// returns if q is not empty, and if channel is not closed. // todo verify this expression. | ||
c.wait(lock, [&] { return (!q.empty() || closed); }); | ||
if (closed) { return Result<T>{T(), false}; } // result is not OK. | ||
T val = q.front(); | ||
q.pop(); | ||
return Result<T>{val, true}; | ||
} | ||
|
||
/** | ||
* read_for behaves like read(), but ensures the caller does not block forever on the channel. | ||
* After the given duration the channel returns a result of read failure. | ||
*/ // TODO: should return something that indicates timeout. | ||
template<typename _Rep, typename _Period> | ||
Result<T> read_for(const std::chrono::duration<_Rep, _Period> &dur) { | ||
auto max_timeout = std::chrono::steady_clock::now() + dur; | ||
|
||
std::unique_lock<std::mutex> lock(m); | ||
// returns if q is not empty, and if channel is not closed. | ||
c.wait_until(lock, max_timeout, [&] { return (!q.empty() || closed); }); | ||
|
||
if (std::chrono::steady_clock::now() > max_timeout) { | ||
return Result<T>{T(), false}; // timeout passed.. | ||
} | ||
|
||
if (closed) { | ||
return Result<T>{T(), false}; | ||
} // result is not OK. | ||
|
||
T val = q.front(); | ||
q.pop(); | ||
return Result<T>{val, true}; | ||
} | ||
|
||
/** | ||
* Closes the channel, anyone attempting to read from a closed channel should quickly receive read failure. | ||
*/ | ||
void close() { | ||
std::lock_guard<std::mutex> lock(m); | ||
closed = true; | ||
c.notify_all(); | ||
} | ||
|
||
|
||
private: | ||
bool closed = false; | ||
std::queue<T> q; | ||
mutable std::mutex m; | ||
std::condition_variable c; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#include "waitgroup.hpp" | ||
|
||
namespace concurrency { | ||
void WaitGroup::add(int delta) { | ||
{ | ||
count.fetch_add(delta); // TODO: understand order of operation parameter in these funcs. | ||
} | ||
} | ||
|
||
|
||
void WaitGroup::done() { | ||
{ | ||
auto prev_val = count.fetch_add(-1); | ||
if (prev_val == 1) { | ||
cv.notify_all(); | ||
return; | ||
} | ||
if (prev_val <= 0) { | ||
throw std::runtime_error("negative counter in wait group!"); | ||
} | ||
} | ||
} | ||
|
||
void WaitGroup::wait() { | ||
{ | ||
std::unique_lock<std::mutex> lock(m); | ||
cv.wait(lock, [this] { return count.load() == 0; }); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#pragma once | ||
|
||
#include <queue> | ||
#include <condition_variable> | ||
|
||
namespace concurrency { | ||
class WaitGroup { | ||
private: | ||
std::mutex m; | ||
std::condition_variable cv; | ||
std::atomic_int32_t count = 0; | ||
public: | ||
WaitGroup() : m(), cv() {} | ||
|
||
// not allowing move or copy. | ||
WaitGroup(const WaitGroup &) = delete; | ||
|
||
WaitGroup(WaitGroup &&) = delete; | ||
|
||
void add(int delta); | ||
|
||
void done(); | ||
|
||
void wait(); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
add_library(marshal_package marshal.hpp marshal.cpp) | ||
|
||
target_include_directories(marshal_package PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) | ||
cmake_path(GET CMAKE_CURRENT_SOURCE_DIR PARENT_PATH MY_PARENT_DIR) | ||
|
||
target_include_directories(marshal_package PUBLIC ${MY_PARENT_DIR}) | ||
target_link_libraries(marshal_package SEAL::seal) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
add_library(multiplication_utils matrix_multiplier.hpp matrix_multiplier.cpp evaluator_wrapper.hpp evaluator_wrapper.cpp query_expander.hpp query_expander.cpp matrix.h channel.h) | ||
add_library(multiplication_utils matrix_operations.hpp matrix_operations.cpp evaluator_wrapper.hpp evaluator_wrapper.cpp query_expander.hpp query_expander.cpp matrix.h) | ||
|
||
target_include_directories(multiplication_utils PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) | ||
target_link_libraries(multiplication_utils SEAL::seal) | ||
cmake_path(GET CMAKE_CURRENT_SOURCE_DIR PARENT_PATH MY_PARENT_DIR) | ||
|
||
target_include_directories(multiplication_utils PUBLIC ${MY_PARENT_DIR}) | ||
target_link_libraries(multiplication_utils concurrency_utils SEAL::seal) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.