Skip to content

Commit

Permalink
Update silber-1.0 sources
Browse files Browse the repository at this point in the history
  • Loading branch information
ben committed Sep 8, 2024
1 parent 7cfff36 commit 7e28c17
Show file tree
Hide file tree
Showing 14 changed files with 131 additions and 99 deletions.
5 changes: 1 addition & 4 deletions AmApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,16 @@ namespace am
using namespace analyze;

AmApi::AmApi(const Configuration &conf) : loggerPtr(std::make_shared<am::common::Logger>("log.log")),
extractor(loggerPtr)
extractor(loggerPtr)
{
configuration::ConfigurationReader reader;
setConfiguration(conf);
}

algorithm::DescObjects AmApi::compare(const std::string &base_img, const std::string &cmp_img)
{
std::cout << "received before read f\n";
std::vector<Matrix<Color24b>> data = extractor.readFiles({base_img, cmp_img});
std::cout << "received after read f\n";
algorithm::ImagePair pair(data[0], data[1]);
std::cout << "received before getObjectsRects\n";
auto res = detector->getObjectsRects(pair);
return res;
}
Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ ENDIF()

add_executable(aquamarine main.cpp)

target_link_libraries(aquamarine aquamarine_lib shared_mem ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries(aquamarine aquamarine_lib silber-1.0 ${CMAKE_THREAD_LIBS_INIT})
IF (UNIX OR APPLE)
target_link_libraries(aquamarine ${SQLite3_LIBRARIES} ${JPEG_LIBRARIES} )
ENDIF()
4 changes: 0 additions & 4 deletions analyze/algorithm/ObjectDetector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,11 @@ namespace am::analyze::algorithm

DescObjects ObjectDetector::getObjectsRects(ImagePair &pair)
{
std::cout << "getObjectsRects 1\n";
const size_t rowHeight = pair.getHeight() / mThreadsCount;
std::vector<std::vector<ObjectRectangle>> res;
std::vector<std::future<std::vector<ObjectRectangle>>> futures;
mLogger->info("ObjectDetector::getObjectsRects pair threads:%d",
mThreadsCount);
std::cout << "getObjectsRects 2\n";
// threadpool could be replaced with std::async calls
am::common::ThreadPool pool;
for (size_t rowId = 0; rowId < mThreadsCount - 1; ++rowId)
Expand All @@ -109,7 +107,6 @@ std::cout << "getObjectsRects 2\n";

futures.emplace_back(pool.run(std::bind(&startObjectsSearchInPair, pair, row, mConfiguration)));
}
std::cout << "getObjectsRects 3\n";
ImageRowSegment final_row{(mThreadsCount - 1) * rowHeight, pair.getHeight()};
// futures.emplace_back(std::async(std::launch::async, startObjectsSearchInPair,
// pair, final_row, *mConfiguration));
Expand All @@ -118,7 +115,6 @@ std::cout << "getObjectsRects 3\n";
{
res.emplace_back(e.get());
}
std::cout << "getObjectsRects 1\n";
return createObjectRects(res, mConfiguration.MinPixelsForObject);
}
} // namespace am::analyze::algorithm
9 changes: 7 additions & 2 deletions sh_mem/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
cmake_minimum_required (VERSION 3.0)

project (shared_mem)
project (silber)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED True)
set(CMAKE_CXX_EXTENSIONS OFF) # Turn off compiler-specific extensions

ADD_LIBRARY(shared_mem STATIC
ADD_LIBRARY(silber-1.0 STATIC
SharedMemoryReceiver.cpp
SharedMemorySender.cpp
ProcCommunicator.cpp
ClientProcCommunicator.cpp
ServerProcCommunicator.cpp)

install(TARGETS silber-1.0 DESTINATION lib)
install(FILES Message.hpp DESTINATION include )

add_executable(silber_test main.cpp)
target_link_libraries(silber_test silber-1.0)
8 changes: 2 additions & 6 deletions sh_mem/ClientProcCommunicator.cpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
#include "ClientProcCommunicator.h"


ClientProcCommunicator::ClientProcCommunicator(
const std::string &shMemName) : ProcCommunicator(shMemName)
{
printf("1 \n");
m_sender = std::make_unique<SharedMemorySender>( m_master_mem_name.c_str());
printf("2 \n");
m_receiver = std::make_unique<SharedMemoryReceiver>(m_slave_mem_name.c_str());
printf("3 \n");

#ifndef _WIN32
m_master_received = sem_open(m_master_received_s.c_str(), O_CREAT, 0666, SEMAPHORE_DISABLED);
m_slave_received = sem_open(m_slave_received_s.c_str(), O_CREAT, 0666, SEMAPHORE_DISABLED);
m_master_sent = sem_open(m_master_sent_s.c_str(), O_CREAT, 0666, SEMAPHORE_DISABLED);
m_slave_sent = sem_open(m_slave_sent_s.c_str(), O_CREAT, 0666, SEMAPHORE_DISABLED);
m_slave_ready = sem_open(m_slave_ready_s.c_str(), O_CREAT, 0666, SEMAPHORE_ENABLED);
printf("4 \n");

if (m_master_received == SEM_FAILED || m_slave_received == SEM_FAILED ||
m_master_sent == SEM_FAILED || m_slave_sent == SEM_FAILED || m_slave_ready == SEM_FAILED || m_slave_ready == SEM_FAILED)
{
Expand All @@ -39,4 +36,3 @@ ClientProcCommunicator::ClientProcCommunicator(
}
#endif
}

15 changes: 9 additions & 6 deletions sh_mem/ClientProcCommunicator.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#pragma once

#include "ServerProcCommunicator.h"

class ClientProcCommunicator : public ProcCommunicator
Expand All @@ -9,6 +10,8 @@ class ClientProcCommunicator : public ProcCommunicator
virtual ~ClientProcCommunicator() = default;

#ifndef _WIN32
// according to defined message flow user sends request and allocates response
// server will respond and fulfill needed response
template <typename Response>
void sendRequestGetResponse(const Message *request, Response &reponse)
{
Expand All @@ -32,15 +35,15 @@ class ClientProcCommunicator : public ProcCommunicator
template <typename Response>
void sendRequestGetResponse(const Message *request, Response &reponse)
{
WaitForSingleObject(m_slave_ready, INFINITE);
WaitForSingleObject(m_slave_ready, INFINITE);
// sem_wait(m_slave_ready);

m_sender->sendMessage(request);
ReleaseSemaphore(m_master_sent, 1, NULL);
// sem_post(m_master_sent);
WaitForSingleObject(m_slave_received, INFINITE);
WaitForSingleObject(m_slave_received, INFINITE);
// sem_wait(m_slave_received);
WaitForSingleObject(m_slave_sent, INFINITE);
WaitForSingleObject(m_slave_sent, INFINITE);
// sem_wait(m_slave_sent);

Response *repsonsePtr = static_cast<Response *>(m_receiver->receiveMessage());
Expand All @@ -50,10 +53,10 @@ WaitForSingleObject(m_slave_sent, INFINITE);
else
std::cerr << "ClientProcCommunicator::sendRequestGetResponse response type is not expected\n";

ReleaseSemaphore(m_master_received, 1, NULL);
//sem_post(m_master_received);
ReleaseSemaphore(m_master_received, 1, NULL);
// sem_post(m_master_received);
ReleaseSemaphore(m_slave_ready, 1, NULL);
//sem_post(m_slave_ready); // release slave for next messages
// sem_post(m_slave_ready); // release slave for next messages
}
#endif
};
39 changes: 28 additions & 11 deletions sh_mem/Message.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,45 @@ enum MessageType : size_t
DISCONNECT,
DISCONNECT_OK,
DISCONNECT_FAIL,
UNEXPECTED_REQUEST
UNEXPECTED_REQUEST // default value, user must specify type of message
};

struct Configuration
{
size_t AffinityThreshold;
size_t MinPixelsForObject;
uint8_t PixelStep;
double CalculationTimeLimit;
size_t IdleTimeout;
double ThreadsMultiplier;
};

struct Message
{
Message(const size_t aId = 0, const MessageType aType = MessageType::UNEXPECTED_REQUEST)
: id(aId),
type(aType),
size(sizeof(Message)) {}
size_t id;
MessageType type;
size_t size;
};

struct Configuration
{
size_t AffinityThreshold;
size_t MinPixelsForObject;
uint8_t PixelStep;
double CalculationTimeLimit;
size_t IdleTimeout;
double ThreadsMultiplier;
};

struct MessageSetConfig : public Message
{
MessageSetConfig() : Message()
{
size = sizeof(MessageSetConfig);
}
Configuration configuration;
};

struct MessageCompareRequest : public Message
{
MessageCompareRequest() : Message()
{
size = sizeof(MessageCompareRequest);
}
char base[200];
char to_compare[200];
};
Expand All @@ -54,6 +67,10 @@ struct Rect

struct MessageCompareResult : public Message
{
MessageCompareResult() : Message()
{
size = sizeof(MessageCompareResult);
}
Rect payload[100];
size_t payload_bytes;
};
2 changes: 1 addition & 1 deletion sh_mem/ProcCommunicator.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class ProcCommunicator
{
protected:
ProcCommunicator(const std::string &shMemName);
~ProcCommunicator();
virtual ~ProcCommunicator();

protected:
std::unique_ptr<SharedMemorySender> m_sender;
Expand Down
3 changes: 2 additions & 1 deletion sh_mem/ServerProcCommunicator.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#pragma once

#include "ProcCommunicator.h"

class ServerProcCommunicator : public ProcCommunicator
Expand All @@ -9,4 +10,4 @@ class ServerProcCommunicator : public ProcCommunicator

void send(const Message *msg);
Message *receive();
};
};
3 changes: 2 additions & 1 deletion sh_mem/SharedMemoryReceiver.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#pragma once

#include <string>
#include "Message.hpp"

Expand All @@ -19,4 +20,4 @@ class SharedMemoryReceiver

void *m_ptr;
std::string m_name;
};
};
19 changes: 3 additions & 16 deletions sh_mem/SharedMemorySender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,8 @@ void SharedMemorySender::finish()
void SharedMemorySender::sendMessage(const Message *msg)
{
// std::cout<<"sendMessage\n";
if (msg->type == MessageType::SET_CONFIG)
std::memcpy(m_ptr, msg, sizeof(MessageSetConfig));
else if (msg->type == MessageType::COMPARE_REQUEST)
std::memcpy(m_ptr, msg, sizeof(MessageCompareRequest));
else if (msg->type == MessageType::COMPARE_RESULT)
std::memcpy(m_ptr, msg, sizeof(MessageCompareResult));
else
std::memcpy(m_ptr, msg, sizeof(Message));

std::memcpy(m_ptr, msg, msg->size);
}
#else
void SharedMemorySender::init()
Expand Down Expand Up @@ -122,13 +116,6 @@ void SharedMemorySender::finish()

void SharedMemorySender::sendMessage(const Message *msg)
{
if (msg->type == MessageType::SET_CONFIG)
CopyMemory(m_ptr, msg, sizeof(MessageSetConfig));
else if (msg->type == MessageType::COMPARE_REQUEST)
CopyMemory(m_ptr, msg, sizeof(MessageCompareRequest));
else if (msg->type == MessageType::COMPARE_RESULT)
CopyMemory(m_ptr, msg, sizeof(MessageCompareResult));
else
CopyMemory(m_ptr, msg, sizeof(Message));
CopyMemory(m_ptr, msg, msg->size);
}
#endif
2 changes: 2 additions & 0 deletions sh_mem/SharedMemorySender.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#pragma once

#include <string>
#include "Message.hpp"
#ifdef _WIN32
Expand All @@ -8,6 +9,7 @@
#include <tchar.h>
#pragma comment(lib, "user32.lib")
#endif

class SharedMemorySender
{
public:
Expand Down
Loading

0 comments on commit 7e28c17

Please sign in to comment.