Skip to content

Commit

Permalink
Try #1837:
Browse files Browse the repository at this point in the history
  • Loading branch information
bors[bot] authored Jun 10, 2022
2 parents 066dd38 + 9243b72 commit a9c52c4
Show file tree
Hide file tree
Showing 21 changed files with 216 additions and 125 deletions.
2 changes: 0 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -355,8 +355,6 @@ endif()
#--------------

if(ARB_WITH_GPU)
target_compile_definitions(arbor-config-defs INTERFACE ARB_HAVE_GPU)

if(ARB_WITH_NVCC OR ARB_WITH_CUDA_CLANG)
target_include_directories(arborenv-private-deps INTERFACE ${CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES})
add_compile_options(
Expand Down
8 changes: 4 additions & 4 deletions arbor/fvm_layout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,7 @@ fvm_mechanism_data fvm_build_mechanism_data(

fvm_mechanism_config config;
if (info.kind != arb_mechanism_kind_density) {
throw cable_cell_error("expected density mechanism, got " +name +" which has " +arb_mechsnism_kind_str(info.kind));
throw cable_cell_error("expected density mechanism, got " +name +" which has " +arb_mechanism_kind_str(info.kind));
}
config.kind = arb_mechanism_kind_density;

Expand Down Expand Up @@ -918,7 +918,7 @@ fvm_mechanism_data fvm_build_mechanism_data(
mechanism_info info = catalogue[name];

if (info.kind != arb_mechanism_kind_point) {
throw cable_cell_error("expected point mechanism, got " +name +" which has " +arb_mechsnism_kind_str(info.kind));
throw cable_cell_error("expected point mechanism, got " +name +" which has " +arb_mechanism_kind_str(info.kind));
}

post_events |= info.post_events;
Expand Down Expand Up @@ -1077,7 +1077,7 @@ fvm_mechanism_data fvm_build_mechanism_data(
for (const auto& [name, placements]: cell.junctions()) {
mechanism_info info = catalogue[name];
if (info.kind != arb_mechanism_kind_gap_junction) {
throw cable_cell_error("expected gap_junction mechanism, got " +name +" which has " +arb_mechsnism_kind_str(info.kind));
throw cable_cell_error("expected gap_junction mechanism, got " +name +" which has " +arb_mechanism_kind_str(info.kind));
}

fvm_mechanism_config config;
Expand Down Expand Up @@ -1268,7 +1268,7 @@ fvm_mechanism_data fvm_build_mechanism_data(
const mechanism_desc& revpot = *maybe_revpot;
mechanism_info info = catalogue[revpot.name()];
if (info.kind != arb_mechanism_kind_reversal_potential) {
throw cable_cell_error("expected reversal potential mechanism for ion " +ion +", got "+ revpot.name() +" which has " +arb_mechsnism_kind_str(info.kind));
throw cable_cell_error("expected reversal potential mechanism for ion " +ion +", got "+ revpot.name() +" which has " +arb_mechanism_kind_str(info.kind));
}

verify_mechanism(info, revpot);
Expand Down
5 changes: 3 additions & 2 deletions arbor/fvm_lowered_cell_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@

#include <arbor/arbexcept.hpp>
#include <arbor/common_types.hpp>
#include <arbor/version.hpp>

#include "backends/multicore/fvm.hpp"
#ifdef ARB_HAVE_GPU
#ifdef ARB_GPU_ENABLED
#include "backends/gpu/fvm.hpp"
#endif
#include "fvm_lowered_cell_impl.hpp"
Expand All @@ -17,7 +18,7 @@ fvm_lowered_cell_ptr make_fvm_lowered_cell(backend_kind p, const execution_conte
case backend_kind::multicore:
return fvm_lowered_cell_ptr(new fvm_lowered_cell_impl<multicore::backend>(ctx));
case backend_kind::gpu:
#ifdef ARB_HAVE_GPU
#ifdef ARB_GPU_ENABLED
return fvm_lowered_cell_ptr(new fvm_lowered_cell_impl<gpu::backend>(ctx));
#endif
; // fall through
Expand Down
6 changes: 3 additions & 3 deletions arbor/gpu_context.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include <memory>

#include <arbor/arbexcept.hpp>

#include <arbor/version.hpp>
#include "gpu_context.hpp"

#ifdef ARB_HAVE_GPU
#ifdef ARB_GPU_ENABLED
#include <arbor/gpu/gpu_api.hpp>
#endif

Expand All @@ -30,7 +30,7 @@ bool gpu_context::has_gpu() const {
return id_ != -1;
}

#ifndef ARB_HAVE_GPU
#ifndef ARB_GPU_ENABLED

void gpu_context::set_gpu() const {
throw arbor_exception("Arbor must be compiled with CUDA/HIP support to set a GPU.");
Expand Down
3 changes: 2 additions & 1 deletion arbor/hardware/memory.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#include "memory.hpp"
#include <arbor/version.hpp>

#ifdef __linux__
extern "C" {
#include <malloc.h>
}
#endif

#ifdef ARB_HAVE_GPU
#ifdef ARB_GPU_ENABLED
#include <arbor/gpu/gpu_api.hpp>
#endif

Expand Down
16 changes: 13 additions & 3 deletions arbor/include/arbor/mechanism_abi.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ extern "C" {

// Version
#define ARB_MECH_ABI_VERSION_MAJOR 0
#define ARB_MECH_ABI_VERSION_MINOR 0
#define ARB_MECH_ABI_VERSION_PATCH 1
#define ARB_MECH_ABI_VERSION_MINOR 1
#define ARB_MECH_ABI_VERSION_PATCH 0
#define ARB_MECH_ABI_VERSION ((ARB_MECH_ABI_VERSION_MAJOR * 10000L * 10000L) + (ARB_MECH_ABI_VERSION_MAJOR * 10000L) + ARB_MECH_ABI_VERSION_PATCH)

typedef const char* arb_mechanism_fingerprint;
Expand All @@ -28,7 +28,7 @@ typedef uint32_t arb_backend_kind;
#define arb_backend_kind_cpu 1
#define arb_backend_kind_gpu 2

inline const char* arb_mechsnism_kind_str(const arb_mechanism_kind& mech) {
inline const char* arb_mechanism_kind_str(const arb_mechanism_kind& mech) {
switch (mech) {
case arb_mechanism_kind_density: return "density mechanism kind";
case arb_mechanism_kind_point: return "point mechanism kind";
Expand Down Expand Up @@ -208,6 +208,16 @@ typedef struct arb_mechanism_type {
arb_size_type n_ions;
} arb_mechanism_type;

// Bundle a type and its interfaces
typedef arb_mechanism_type (*arb_get_mechanism_type)();
typedef arb_mechanism_interface* (*arb_get_mechanism_interface)();

typedef struct arb_mechanism {
arb_get_mechanism_type type;
arb_get_mechanism_interface i_cpu;
arb_get_mechanism_interface i_gpu;
} arb_mechanism;

#ifdef __cplusplus
}
#endif
Expand Down
2 changes: 1 addition & 1 deletion arbor/include/arbor/mechcat.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,6 @@ ARB_ARBOR_API const mechanism_catalogue& global_allen_catalogue();
ARB_ARBOR_API const mechanism_catalogue& global_bbp_catalogue();

// Load catalogue from disk.
ARB_ARBOR_API const mechanism_catalogue& load_catalogue(const std::string&);
ARB_ARBOR_API const mechanism_catalogue load_catalogue(const std::string&);

} // namespace arb
31 changes: 28 additions & 3 deletions arbor/mechcat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -592,8 +592,8 @@ std::pair<mechanism_ptr, mechanism_overrides> mechanism_catalogue::instance_impl

mechanism_catalogue::~mechanism_catalogue() = default;

ARB_ARBOR_API const mechanism_catalogue& load_catalogue(const std::string& fn) {
typedef const void* global_catalogue_t();
ARB_ARBOR_API const mechanism_catalogue load_catalogue(const std::string& fn) {
typedef void* global_catalogue_t(int*);
global_catalogue_t* get_catalogue = nullptr;
try {
get_catalogue = util::dl_get_symbol<global_catalogue_t*>(fn, "get_catalogue");
Expand All @@ -608,7 +608,32 @@ ARB_ARBOR_API const mechanism_catalogue& load_catalogue(const std::string& fn) {
* different lifetime than the actual catalogue itfself. This is not a leak,
* as `dlopen` caches handles for us.
*/
return *((const mechanism_catalogue*)get_catalogue());
int count = -1;
auto mechs = (arb_mechanism*)get_catalogue(&count);
if (count <= 0) {
throw bad_catalogue_error{util::pprintf("Invalid mechanism count {} in shared object '{}'", count, fn)};
}
mechanism_catalogue result;
for(int ix = 0; ix < count; ++ix) {
auto type = mechs[ix].type();
auto name = std::string{type.name};
if (name == "") {
throw bad_catalogue_error{util::pprintf("Empty name for mechanism in '{}'", fn)};
}
auto icpu = mechs[ix].i_cpu();
auto igpu = mechs[ix].i_gpu();
if (!icpu && !igpu) {
throw bad_catalogue_error{util::pprintf("Empty interfaces for mechanism '{}'", name)};
}
result.add(name, type);
if (icpu) {
result.register_implementation(name, std::make_unique<mechanism>(type, *icpu));
}
if (igpu) {
result.register_implementation(name, std::make_unique<mechanism>(type, *igpu));
}
}
return result;
}

} // namespace arb
3 changes: 2 additions & 1 deletion arbor/memory/gpu_wrappers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
#include <string>

#include <arbor/arbexcept.hpp>
#include <arbor/version.hpp>

#include "util.hpp"

#ifdef ARB_HAVE_GPU
#ifdef ARB_GPU_ENABLED

#include <arbor/gpu/gpu_api.hpp>

Expand Down
4 changes: 3 additions & 1 deletion arbor/util/config.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include <arbor/version.hpp>

namespace arb {
namespace config {

Expand Down Expand Up @@ -29,7 +31,7 @@ constexpr bool has_power_measurement = true;
constexpr bool has_power_measurement = false;
#endif

#ifdef ARB_HAVE_GPU
#ifdef ARB_GPU_ENABLED
constexpr bool has_gpu = true;
#else
constexpr bool has_gpu = false;
Expand Down
7 changes: 4 additions & 3 deletions arborenv/default_env.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#include <limits>
#include <optional>

#include <arbor/version.hpp>
#include <arborenv/arbenvexcept.hpp>
#include <arborenv/concurrency.hpp>
#include <arborenv/default_env.hpp>

#ifdef ARB_HAVE_GPU
#ifdef ARB_GPU_ENABLED
#include "gpu_api.hpp"
#endif

Expand All @@ -29,7 +30,7 @@ ARB_ARBORENV_API unsigned long get_env_num_threads() {
return *env_val;
}

#ifdef ARB_HAVE_GPU
#ifdef ARB_GPU_ENABLED

ARB_ARBORENV_API int default_gpu() {
constexpr const char* env_var = "ARBENV_GPU_ID";
Expand Down Expand Up @@ -60,7 +61,7 @@ ARB_ARBORENV_API int default_gpu() {
return -1;
}

#endif // def ARB_HAVE_GPU
#endif // def ARB_GPU_ENABLED

} // namespace arbenv

5 changes: 3 additions & 2 deletions arborenv/private_gpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@

#include <mpi.h>

#include <arbor/version.hpp>
#include <arborenv/arbenvexcept.hpp>
#include <arborenv/gpu_env.hpp>
#include "gpu_uuid.hpp"

namespace arbenv {

#ifdef ARB_HAVE_GPU
#ifdef ARB_GPU_ENABLED

template <>
ARB_ARBORENV_API int find_private_gpu(MPI_Comm comm) {
Expand Down Expand Up @@ -100,7 +101,7 @@ ARB_ARBORENV_API int find_private_gpu(MPI_Comm comm) {
return -1;
}

#endif // def ARB_HAVE_GPU
#endif // def ARB_GPU_ENABLED

} // namespace arbenv

Expand Down
34 changes: 27 additions & 7 deletions doc/dev/mechanism_abi.rst
Original file line number Diff line number Diff line change
Expand Up @@ -444,16 +444,36 @@ interface layer <simd>` for more information.
Making A Loadable Mechanism
---------------------------

Mechanisms interface with Arbor by providing three functions, one
returning the metadata portion, and one for each implemented backend (currently
two). The latter may return a NULL pointer, indicating that this backend is not
supported. The naming scheme is shown in the example below
Mechanisms interface with Arbor by providing a single function, returning
a structure

.. c:struct:: arb_mechanism
.. c:member:: arb_get_mechanism_type type
Pointer to a function ``arb_mechanism_type get_type()``
.. c:member:: arb_get_mechanism_interface i_cpu
Pointer to a function ``arb_mechanism_interface get_interface()``
that returns a pointer to the CPU interface which may be
``null``.
.. c:member:: arb_get_mechanism_interface i_gpu
Pointer to a function ``arb_mechanism_interace get_interface()``
that returns a pointer to the GPU interface which may be
``null``.
You can create mechanisms with both ``i_gpu`` and ``i_cpu`` returning ``null``,
but at least one of the interfaces must be provided or Arbor will refuse to load
the catalogue this mechanism.
The naming scheme is shown in the example below
.. code:: C
arb_mechanism_type make_arb_default_catalogue_pas();
arb_mechanism_interface* make_arb_default_catalogue_pas_interface_multicore();
arb_mechanism_interface* make_arb_default_catalogue_pas_interface_gpu();
arb_mechanism make_arb_default_catalogue_pas();
Writing Mechanisms Directly Against the ABI
-------------------------------------------
Expand Down
6 changes: 6 additions & 0 deletions mechanisms/BuildModules.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,17 @@ function("make_catalogue")
list(APPEND catalogue_${MK_CAT_NAME}_source ${MK_CAT_OUT_DIR}/${mech}_gpu.cpp ${MK_CAT_OUT_DIR}/${mech}_gpu.cu)
endif()
endforeach()

set(${MK_CAT_OUTPUT} ${catalogue_${MK_CAT_NAME}_source} PARENT_SCOPE)

if(${MK_CAT_STANDALONE})
add_library(${MK_CAT_NAME}-catalogue SHARED ${catalogue_${MK_CAT_NAME}_source})
target_compile_definitions(${MK_CAT_NAME}-catalogue PUBLIC STANDALONE=1)

if(ARB_WITH_GPU)
target_compile_definitions(${MK_CAT_NAME}-catalogue PUBLIC ARB_GPU_ENABLED)
endif()

target_compile_options(${MK_CAT_NAME}-catalogue PUBLIC ${MK_CAT_CXX_FLAGS_TARGET})
set_target_properties(${MK_CAT_NAME}-catalogue
PROPERTIES
Expand Down
Loading

0 comments on commit a9c52c4

Please sign in to comment.