Skip to content

Commit 034445b

Browse files
Cpp code organization (#72)
* Reorganize C++ sources into subdirectories - Move bgm- and bgmCompare-specific C++ code into src/bgm/ and src/bgmCompare/ - Update build system to support C++ sources in subdirectories - Generate src/sources.mk at configure time to list all .cpp files - Include sources.mk from Makevars.in using portable (CRAN-compliant) rules * Finalize C++ refactor: path-qualified headers and single include root - Reorganize remaining C++ files into src/ subdirectories - Include headers relative to src/ and drop per-module -I flags * Use ARMA_MY_LOG instead of arma::log in compute_Vn_mfm_sbm() * Cleanup Makevars.in * Final commit
1 parent 2354706 commit 034445b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+340
-277
lines changed

.Rbuildignore

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,35 @@
1-
^renv$
2-
^renv\.lock$
1+
# RStudio / IDE
32
^.*\.Rproj$
43
^\.Rproj\.user$
4+
^\.vscode$
55

6-
^Readme.Rmd$
7-
^\.github$
8-
^_pkgdown\.yml$
6+
# renv
7+
^renv$
8+
^renv\.lock$
9+
10+
# pkgdown / docs
911
^docs$
1012
^pkgdown$
11-
^vignettes/introduction_cache
13+
^_pkgdown\.yml$
14+
^Readme\.Rmd$
15+
^vignettes/introduction_cache$
16+
17+
# GitHub / CI
18+
^\.github$
19+
20+
# R CMD build artifacts
1221
^doc$
1322
^Meta$
14-
^\.vscode$
15-
^dev/
23+
24+
# Development helpers
25+
^dev$
26+
27+
# ---- C/C++ build artifacts (REQUIRED) ----
28+
^src/.*\.o$
29+
^src/.*\.so$
30+
^src/.*\.dll$
31+
32+
# ---- Generated build files ----
33+
^src/Makevars$
34+
^src/Makevars\.win$
35+
^src/sources\.mk$

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
src/*.o
66
src/*.so
77
src/*.dll
8+
src/**/*.o
9+
src/**/*.so
10+
src/**/*.dll
811
.DS_Store
912
/doc/
1013
/Meta/

R/RcppExports.R

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,6 @@ run_bgm_parallel <- function(observations, num_categories, pairwise_scale, edge_
99
.Call(`_bgms_run_bgm_parallel`, observations, num_categories, pairwise_scale, edge_prior, inclusion_probability, beta_bernoulli_alpha, beta_bernoulli_beta, beta_bernoulli_alpha_between, beta_bernoulli_beta_between, dirichlet_alpha, lambda, interaction_index_matrix, iter, warmup, counts_per_category, blume_capel_stats, main_alpha, main_beta, na_impute, missing_index, is_ordinal_variable, baseline_category, edge_selection, update_method, pairwise_effect_indices, target_accept, pairwise_stats, hmc_num_leapfrogs, nuts_max_depth, learn_mass_matrix, num_chains, nThreads, seed, progress_type)
1010
}
1111

12-
get_explog_switch <- function() {
13-
.Call(`_bgms_get_explog_switch`)
14-
}
15-
16-
rcpp_ieee754_exp <- function(x) {
17-
.Call(`_bgms_rcpp_ieee754_exp`, x)
18-
}
19-
20-
rcpp_ieee754_log <- function(x) {
21-
.Call(`_bgms_rcpp_ieee754_log`, x)
22-
}
23-
2412
sample_omrf_gibbs <- function(no_states, no_variables, no_categories, interactions, thresholds, iter) {
2513
.Call(`_bgms_sample_omrf_gibbs`, no_states, no_variables, no_categories, interactions, thresholds, iter)
2614
}

R/generate_makevars_sources.R

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
cpp <- list.files(
2+
"src",
3+
pattern = "\\.cpp$",
4+
recursive = TRUE,
5+
full.names = TRUE
6+
)
7+
8+
# strip leading "src/"
9+
cpp <- sub("^src/", "", cpp)
10+
11+
con <- file("src/sources.mk", open = "w")
12+
13+
writeLines(c(
14+
"# ------------------------------------------------------------------",
15+
"# THIS FILE IS AUTO-GENERATED - DO NOT EDIT",
16+
"# Generated by configure",
17+
"# To add C++ code, place .cpp files anywhere under src/",
18+
"# ------------------------------------------------------------------",
19+
"SOURCES = \\"
20+
), con)
21+
22+
writeLines(paste0(" ", cpp, " \\"), con)
23+
writeLines("", con)
24+
25+
close(con)
26+

configure

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
#!/bin/sh
22

3-
# Get flags from RcppParallel
3+
# RcppParallel flags
44
RCPP_PARALLEL_CPPFLAGS=`"${R_HOME}/bin/Rscript" -e "cat(RcppParallel::CxxFlags())"`
55
RCPP_PARALLEL_LIBS=`"${R_HOME}/bin/Rscript" -e "cat(RcppParallel::LdFlags())"`
66

7+
# Generate sources.mk using R
8+
"${R_HOME}/bin/Rscript" R/generate_makevars_sources.R > src/sources.mk
9+
710
# Substitute into Makevars
811
sed -e "s|@RCPP_PARALLEL_CPPFLAGS@|${RCPP_PARALLEL_CPPFLAGS}|" \
912
-e "s|@RCPP_PARALLEL_LIBS@|${RCPP_PARALLEL_LIBS}|" \

configure.win

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
#!/bin/sh
22

3+
# RcppParallel flags
34
RCPP_PARALLEL_CPPFLAGS=`"${R_HOME}/bin${R_ARCH_BIN}/Rscript.exe" -e "cat(RcppParallel::CxxFlags())"`
45
RCPP_PARALLEL_LIBS=`"${R_HOME}/bin${R_ARCH_BIN}/Rscript.exe" -e "cat(RcppParallel::LdFlags())"`
56

7+
# Generate sources.mk using R
8+
"${R_HOME}/bin/Rscript" R/generate_makevars_sources.R > src/sources.mk
9+
610
# Substitute into Makevars.win
711
sed -e "s|@RCPP_PARALLEL_CPPFLAGS@|${RCPP_PARALLEL_CPPFLAGS}|" \
812
-e "s|@RCPP_PARALLEL_LIBS@|${RCPP_PARALLEL_LIBS}|" \

src/Makevars.in

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
CXX_STD = CXX20
22

3-
PKG_CPPFLAGS = @RCPP_PARALLEL_CPPFLAGS@ -DARMA_NO_DEBUG
3+
include sources.mk
4+
5+
OBJECTS = $(SOURCES:.cpp=.o)
6+
7+
PKG_CPPFLAGS = @RCPP_PARALLEL_CPPFLAGS@ -DARMA_NO_DEBUG -I.
48

59
PKG_LIBS = $(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS) @RCPP_PARALLEL_LIBS@

src/RcppExports.cpp

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -101,38 +101,6 @@ BEGIN_RCPP
101101
return rcpp_result_gen;
102102
END_RCPP
103103
}
104-
// get_explog_switch
105-
Rcpp::String get_explog_switch();
106-
RcppExport SEXP _bgms_get_explog_switch() {
107-
BEGIN_RCPP
108-
Rcpp::RObject rcpp_result_gen;
109-
Rcpp::RNGScope rcpp_rngScope_gen;
110-
rcpp_result_gen = Rcpp::wrap(get_explog_switch());
111-
return rcpp_result_gen;
112-
END_RCPP
113-
}
114-
// rcpp_ieee754_exp
115-
Rcpp::NumericVector rcpp_ieee754_exp(Rcpp::NumericVector x);
116-
RcppExport SEXP _bgms_rcpp_ieee754_exp(SEXP xSEXP) {
117-
BEGIN_RCPP
118-
Rcpp::RObject rcpp_result_gen;
119-
Rcpp::RNGScope rcpp_rngScope_gen;
120-
Rcpp::traits::input_parameter< Rcpp::NumericVector >::type x(xSEXP);
121-
rcpp_result_gen = Rcpp::wrap(rcpp_ieee754_exp(x));
122-
return rcpp_result_gen;
123-
END_RCPP
124-
}
125-
// rcpp_ieee754_log
126-
Rcpp::NumericVector rcpp_ieee754_log(Rcpp::NumericVector x);
127-
RcppExport SEXP _bgms_rcpp_ieee754_log(SEXP xSEXP) {
128-
BEGIN_RCPP
129-
Rcpp::RObject rcpp_result_gen;
130-
Rcpp::RNGScope rcpp_rngScope_gen;
131-
Rcpp::traits::input_parameter< Rcpp::NumericVector >::type x(xSEXP);
132-
rcpp_result_gen = Rcpp::wrap(rcpp_ieee754_log(x));
133-
return rcpp_result_gen;
134-
END_RCPP
135-
}
136104
// sample_omrf_gibbs
137105
IntegerMatrix sample_omrf_gibbs(int no_states, int no_variables, IntegerVector no_categories, NumericMatrix interactions, NumericMatrix thresholds, int iter);
138106
RcppExport SEXP _bgms_sample_omrf_gibbs(SEXP no_statesSEXP, SEXP no_variablesSEXP, SEXP no_categoriesSEXP, SEXP interactionsSEXP, SEXP thresholdsSEXP, SEXP iterSEXP) {
@@ -185,9 +153,6 @@ END_RCPP
185153
static const R_CallMethodDef CallEntries[] = {
186154
{"_bgms_run_bgmCompare_parallel", (DL_FUNC) &_bgms_run_bgmCompare_parallel, 36},
187155
{"_bgms_run_bgm_parallel", (DL_FUNC) &_bgms_run_bgm_parallel, 34},
188-
{"_bgms_get_explog_switch", (DL_FUNC) &_bgms_get_explog_switch, 0},
189-
{"_bgms_rcpp_ieee754_exp", (DL_FUNC) &_bgms_rcpp_ieee754_exp, 1},
190-
{"_bgms_rcpp_ieee754_log", (DL_FUNC) &_bgms_rcpp_ieee754_log, 1},
191156
{"_bgms_sample_omrf_gibbs", (DL_FUNC) &_bgms_sample_omrf_gibbs, 6},
192157
{"_bgms_sample_bcomrf_gibbs", (DL_FUNC) &_bgms_sample_bcomrf_gibbs, 8},
193158
{"_bgms_compute_Vn_mfm_sbm", (DL_FUNC) &_bgms_compute_Vn_mfm_sbm, 4},
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include <RcppArmadillo.h>
2-
#include "bgm_helper.h"
3-
#include "common_helpers.h"
2+
#include "bgm/bgm_helper.h"
3+
#include "utils/common_helpers.h"
44

55

66

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#pragma once
22

33
#include <RcppArmadillo.h>
4-
#include "rng_utils.h"
4+
#include "rng/rng_utils.h"
55

66
// Vectorize main_effect matrix
77
arma::vec vectorize_main_effects_bgm(

0 commit comments

Comments
 (0)