From ff3494ec716c593e27cd67df025edb21c96dfaea Mon Sep 17 00:00:00 2001
From: boeschf <48126478+boeschf@users.noreply.github.com>
Date: Thu, 17 Nov 2022 15:24:22 +0100
Subject: [PATCH] BUGFIX: add ARB_CUDA flag to example catalogue (#2039)

fix ornstein_uhlenbeck example on gpu:
- use make_catalogue instead of make_catalogue_standalone
- link to arbor-private-deps
- mark all files as c++ sources when using cuda-clang or hip-clang
---
 example/ornstein_uhlenbeck/CMakeLists.txt     | 29 ++++++++--------
 .../ornstein_uhlenbeck.mod                    |  0
 example/ornstein_uhlenbeck/ou.cpp             | 33 ++-----------------
 3 files changed, 19 insertions(+), 43 deletions(-)
 rename example/ornstein_uhlenbeck/{ => ornstein_uhlenbeck}/ornstein_uhlenbeck.mod (100%)

diff --git a/example/ornstein_uhlenbeck/CMakeLists.txt b/example/ornstein_uhlenbeck/CMakeLists.txt
index aa4d7ef9..70981afa 100644
--- a/example/ornstein_uhlenbeck/CMakeLists.txt
+++ b/example/ornstein_uhlenbeck/CMakeLists.txt
@@ -1,17 +1,21 @@
-make_catalogue_standalone(
-    NAME ou
-    SOURCES "${CMAKE_CURRENT_SOURCE_DIR}"
-    MOD ornstein_uhlenbeck
-    CXX
-    CXX_FLAGS_TARGET ${ARB_CXX_FLAGS_TARGET_FULL}
-    VERBOSE ON)
+include(${PROJECT_SOURCE_DIR}/mechanisms/BuildModules.cmake)
 
-add_executable(ou EXCLUDE_FROM_ALL ou.cpp)
-add_dependencies(ou ou-catalogue)
-target_compile_options(ou PRIVATE ${ARB_CXX_FLAGS_TARGET_FULL})
-target_include_directories(ou PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/generated/ou")
+make_catalogue(
+    NAME    ornstein_uhlenbeck
+    MOD     ornstein_uhlenbeck
+    VERBOSE ${ARB_CAT_VERBOSE}
+    ADD_DEPS OFF)
 
-target_link_libraries(ou PRIVATE arbor arborio ou-catalogue)
+add_executable(ou EXCLUDE_FROM_ALL ou.cpp ${catalogue-ornstein_uhlenbeck-mechanisms})
+add_dependencies(ou catalogue-ornstein_uhlenbeck-target)
+
+if(ARB_WITH_CUDA_CLANG OR ARB_WITH_HIP_CLANG)
+    set_source_files_properties(${catalogue-ornstein_uhlenbeck-meachanisms} PROPERTIES LANGUAGE CXX)
+endif()
+target_include_directories(ou PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/generated/ornstein_uhlenbeck")
+
+target_link_libraries(ou PRIVATE arbor-private-deps) # compiler flags, GPU options etc
+target_link_libraries(ou PRIVATE arbor arborio)
 if (ARB_USE_BUNDLED_FMT)
     target_include_directories(ou PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/../../ext/fmt/include")
     target_compile_definitions(ou PRIVATE FMT_HEADER_ONLY)
@@ -19,5 +23,4 @@ else()
     find_package(fmt REQUIRED)
     target_link_libraries(ou PRIVATE fmt::fmt-header-only)
 endif()
-
 add_dependencies(examples ou)
diff --git a/example/ornstein_uhlenbeck/ornstein_uhlenbeck.mod b/example/ornstein_uhlenbeck/ornstein_uhlenbeck/ornstein_uhlenbeck.mod
similarity index 100%
rename from example/ornstein_uhlenbeck/ornstein_uhlenbeck.mod
rename to example/ornstein_uhlenbeck/ornstein_uhlenbeck/ornstein_uhlenbeck.mod
diff --git a/example/ornstein_uhlenbeck/ou.cpp b/example/ornstein_uhlenbeck/ou.cpp
index ad8f9c47..4f7db1a0 100644
--- a/example/ornstein_uhlenbeck/ou.cpp
+++ b/example/ornstein_uhlenbeck/ou.cpp
@@ -1,5 +1,7 @@
 #include <fmt/format.h>
 
+#include "ornstein_uhlenbeck_catalogue.hpp"
+
 #include <arborio/label_parse.hpp>
 
 #include <arbor/assert.hpp>
@@ -8,9 +10,6 @@
 #include <arbor/cable_cell.hpp>
 #include <arbor/simulation.hpp>
 
-// forward declaration
-arb::mechanism_catalogue build_catalogue();
-
 // a single-cell recipe with probes
 class recipe: public arb::recipe {
 public:
@@ -20,7 +19,7 @@ public:
 
         // build catalogue with stochastic mechanism
         cell_gprop_.catalogue = global_default_catalogue();
-        cell_gprop_.catalogue.import(build_catalogue(), "");
+        cell_gprop_.catalogue.import(arb::global_ornstein_uhlenbeck_catalogue(), "");
         cell_gprop_.default_parameters = neuron_parameter_defaults;
         
         // paint the process on the whole cell
@@ -153,29 +152,3 @@ int main(int argc, char** argv) {
     }
     return 0;
 }
-
-// load mechanisms from library and add to new catalogue
-// =====================================================
-
-extern "C" {
-const void* get_catalogue(int*);
-}
-
-arb::mechanism_catalogue build_catalogue() {
-    arb::mechanism_catalogue cat;
-    int n=0;
-    const void* ptr = get_catalogue(&n);
-    const auto* mechs = reinterpret_cast<const arb_mechanism*>(ptr);
-    for (int i=0; i<n; ++i) {
-        const auto& mech = mechs[i];
-        auto ty = mech.type();
-        auto nm = ty.name;
-        auto ig = mech.i_gpu();
-        auto ic = mech.i_cpu();
-        arb_assert(ic || ig);
-        cat.add(nm, ty);
-        if (ic) cat.register_implementation(nm, std::make_unique<arb::mechanism>(ty, *ic));
-        if (ig) cat.register_implementation(nm, std::make_unique<arb::mechanism>(ty, *ig));
-    }
-    return cat;
-}
-- 
GitLab