Skip to content

Commit

Permalink
started python and C++ interface
Browse files Browse the repository at this point in the history
  • Loading branch information
mdorier committed Jun 7, 2024
1 parent 967cd56 commit 99a1ca3
Show file tree
Hide file tree
Showing 14 changed files with 1,095 additions and 1 deletion.
11 changes: 11 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
# See COPYRIGHT in top-level directory.
cmake_minimum_required (VERSION 3.8)
project (flock C CXX)
set (CMAKE_CXX_STANDARD 17)
set (CMAKE_CXX_STANDARD_REQUIRED ON)
enable_testing ()

add_definitions (-Wextra -Wall -Wpedantic)
Expand All @@ -13,6 +15,7 @@ option (ENABLE_EXAMPLES "Build examples" OFF)
option (ENABLE_BEDROCK "Build bedrock module" OFF)
option (ENABLE_COVERAGE "Build with coverage" OFF)
option (ENABLE_MPI "Build with MPI support" OFF)
option (ENABLE_PYTHON "Build with Python support" OFF)

# library version set here (e.g. for shared libs).
set (FLOCK_VERSION_MAJOR 0)
Expand Down Expand Up @@ -67,6 +70,14 @@ if (ENABLE_MPI)
find_package (MPI REQUIRED)
endif ()

# search for Python
if (ENABLE_PYTHON)
set (Python3_FIND_STRATEGY LOCATION)
find_package (Python3 COMPONENTS Interpreter Development REQUIRED)
find_package (pybind11 REQUIRED)
add_subdirectory (python)
endif ()

add_subdirectory (src)
if (${ENABLE_TESTS})
enable_testing ()
Expand Down
2 changes: 1 addition & 1 deletion build.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
SCRIPT_DIR=$(dirname "$0")
cmake .. -DENABLE_TESTS=ON -DENABLE_BEDROCK=ON -DENABLE_EXAMPLES=ON -DENABLE_MPI=ON -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
cmake .. -DENABLE_TESTS=ON -DENABLE_BEDROCK=ON -DENABLE_EXAMPLES=ON -DENABLE_MPI=ON -DENABLE_PYTHON=ON -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
make
78 changes: 78 additions & 0 deletions include/flock/cxx/client.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* (C) 2024 The University of Chicago
*
* See COPYRIGHT in top-level directory.
*/
#ifndef __FLOCK_CLIENT_HPP
#define __FLOCK_CLIENT_HPP

#include <flock/flock-client.h>
#include <flock/flock-group.h>
#include <flock/cxx/exception.hpp>
#include <flock/cxx/group.hpp>

namespace flock {

class Client {

public:

Client() = default;

Client(margo_instance_id mid, ABT_pool pool = ABT_POOL_NULL) {
auto err = flock_client_init(mid, pool, &m_client);
FLOCK_CONVERT_AND_THROW(err);
}

~Client() {
if(m_client != FLOCK_CLIENT_NULL) {
flock_client_finalize(m_client);
}
}

Client(Client&& other)
: m_client(other.m_client) {
other.m_client = FLOCK_CLIENT_NULL;
}

Client(const Client&) = delete;

Client& operator=(const Client&) = delete;

Client& operator=(Client&& other) {
if(this == &other) return *this;
if(m_client != FLOCK_CLIENT_NULL) {
flock_client_finalize(m_client);
}
m_client = other.m_client;
other.m_client = FLOCK_CLIENT_NULL;
return *this;
}

GroupHandle makeGroupHandle(
hg_addr_t addr,
uint16_t provider_id,
uint32_t mode = 0) const {
flock_group_handle_t gh;
auto err = flock_group_handle_create(
m_client, addr, provider_id, mode, &gh);
FLOCK_CONVERT_AND_THROW(err);
return GroupHandle(gh, false);
}

auto handle() const {
return m_client;
}

operator flock_client_t() const {
return m_client;
}

private:

flock_client_t m_client = FLOCK_CLIENT_NULL;
};

}

#endif
46 changes: 46 additions & 0 deletions include/flock/cxx/exception.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* (C) 2024 The University of Chicago
*
* See COPYRIGHT in top-level directory.
*/
#ifndef __FLOCK_EXCEPTION_HPP
#define __FLOCK_EXCEPTION_HPP

#include <flock/flock-common.h>
#include <exception>

namespace flock {

class Exception : public std::exception {

public:

Exception(flock_return_t code)
: m_code(code) {}

const char* what() const noexcept override {
#define X(__err__, __msg__) case __err__: return __msg__;
switch(m_code) {
FLOCK_RETURN_VALUES
}
#undef X
return "Unknown error";
}

auto code() const {
return m_code;
}

private:

flock_return_t m_code;
};

#define FLOCK_CONVERT_AND_THROW(__err__) do { \
if((__err__) != FLOCK_SUCCESS) { \
throw ::flock::Exception((__err__)); \
} \
} while(0)

}
#endif
148 changes: 148 additions & 0 deletions include/flock/cxx/group-view.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/*
* (C) 2024 The University of Chicago
*
* See COPYRIGHT in top-level directory.
*/
#ifndef __FLOCK_GROUP_VIEW_HPP
#define __FLOCK_GROUP_VIEW_HPP

#include <flock/flock-group.h>
#include <flock/cxx/exception.hpp>
#include <memory>
#include <map>
#include <vector>
#include <string>
#include <functional>

namespace flock {

class GroupHandle;
class Provider;

class GroupView {

friend class GroupHandle;
friend class Provider;

public:

struct Member {
uint64_t rank;
uint16_t provider_id;
std::string address;
};

GroupView() = default;

GroupView(flock_group_view_t view) {
FLOCK_GROUP_VIEW_MOVE(&view, &m_view);
}

~GroupView() {
flock_group_view_clear(&m_view);
}

GroupView(GroupView&& other) {
FLOCK_GROUP_VIEW_MOVE(&other.m_view, &m_view);
}

GroupView& operator=(GroupView&& other) {
if(this == &other) return *this;
flock_group_view_clear(&m_view);
FLOCK_GROUP_VIEW_MOVE(&other.m_view, &m_view);
return *this;
}

auto digest() const {
return m_view.digest;
}

void lock() {
FLOCK_GROUP_VIEW_LOCK(&m_view);
}

void unlock() {
FLOCK_GROUP_VIEW_UNLOCK(&m_view);
}

void clear() {
flock_group_view_clear(&m_view);
}

void addMember(uint64_t rank, const char* address, uint16_t provider_id) {
auto member = flock_group_view_add_member(&m_view, rank, provider_id, address);
if(!member) throw Exception{FLOCK_ERR_RANK_USED};
}

void removeMember(uint64_t rank) {
if(!flock_group_view_remove_member(&m_view, rank))
throw Exception{FLOCK_ERR_NO_MEMBER};
}

Member findMember(uint64_t rank) const {
auto member = flock_group_view_find_member(&m_view, rank);
if(!member) throw Exception{FLOCK_ERR_NO_MEMBER};
return Member{member->rank, member->provider_id, member->address};
}

std::vector<Member> members() const {
std::vector<Member> result;
result.reserve(m_view.members.size);
for(size_t i = 0; i < m_view.members.size; ++i) {
result.push_back(Member{
m_view.members.data[i].rank,
m_view.members.data[i].provider_id,
m_view.members.data[i].address
});
}
return result;
}

size_t maxNumMembers() const {
return m_view.members.data[m_view.members.size-1].rank;
}

size_t numLiveMembers() const {
return m_view.members.size;
}

void setMetadata(const char* key, const char* value) {
flock_group_view_add_metadata(&m_view, key, value);
}

void removeMetadata(const char* key) {
if(!flock_group_view_remove_metadata(&m_view, key))
throw Exception{FLOCK_ERR_NO_METADATA};
}

std::map<std::string, std::string> metadata() const {
std::map<std::string, std::string> result;
for(size_t i = 0; i < m_view.metadata.size; ++i) {
result.insert(
std::make_pair<const std::string, std::string>(
m_view.metadata.data[i].key,
m_view.metadata.data[i].value));
}
return result;
}

std::string toString(margo_instance_id mid, uint64_t credentials = 0) const {
std::string result;
flock_return_t ret = flock_group_view_serialize(
mid, credentials, &m_view,
[](void* ctx, const char* content, size_t size) {
auto str = static_cast<decltype(&result)>(ctx);
str->assign(content, size);
}, static_cast<void*>(&result));
if(ret != FLOCK_SUCCESS) throw Exception{ret};
return result;
}

private:

flock_group_view_t m_view = FLOCK_GROUP_VIEW_INITIALIZER;
};

}

#endif
Loading

0 comments on commit 99a1ca3

Please sign in to comment.