Skip to content

Commit abaa531

Browse files
committed
fix matrix coiercion
1 parent 1a5c426 commit abaa531

File tree

24 files changed

+559
-174
lines changed

24 files changed

+559
-174
lines changed

Makefile

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
clean:
2-
@clear
32
@Rscript -e 'devtools::clean_dll("extended-tests/cpp4rtest");'
43
@Rscript -e 'devtools::clean_dll("extended-tests/cpp11benchmark")'
54
@Rscript -e 'devtools::clean_dll("extended-tests/cpp4rbenchmark")'
65
@Rscript -e 'devtools::clean_dll("extended-tests/Rcppbenchmark")'
76

7+
register:
8+
@Rscript -e 'cpp4r::register("extended-tests/cpp4rtest")'
9+
@Rscript -e 'cpp4r::register("extended-tests/cpp4rbenchmark")'
10+
811
install:
9-
@clear
1012
@Rscript -e 'devtools::install("./")'
1113

1214
docs:
13-
@clear
1415
@Rscript -e 'devtools::document("./"); pkgsite::build_site("./")'
1516

1617
check:
17-
@clear
1818
@echo "==============================="
1919
@echo "Checking R code"
20+
@$(MAKE) clean
2021
@$(MAKE) install
22+
@$(MAKE) register
2123
@Rscript -e 'cpp4r::register("extended-tests/cpp4rtest")'
2224
@Rscript -e 'devtools::check("./", error_on = "error")'
2325
@echo "==============================="
@@ -29,9 +31,11 @@ check:
2931
@echo "==============================="
3032

3133
bench:
32-
@clear
3334
@rm -f extended-tests-results/*.rds
3435
@rm -f extended-tests-results/bench_summary.md
36+
@$(MAKE) clean
37+
@$(MAKE) install
38+
@$(MAKE) register
3539
@export -p USE_CLANG; /bin/bash -euo pipefail -c './scripts/bench_loop.sh'
3640
@Rscript './scripts/combine-benchmarks.R'
3741

R/register.R

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -349,38 +349,8 @@ convert_cpp_default_to_r <- function(cpp_default) {
349349

350350
# Helper function to generate type checking/coercion code
351351
generate_type_check <- function(param_name, param_type) {
352-
# Map C++ types to R coercion functions
353-
if (param_type == "int" || grepl("^int[[:space:]]*$", param_type)) {
354-
return(glue::glue("{param_name} <- as.integer({param_name})"))
355-
} else if (param_type == "double" || grepl("^double[[:space:]]*$", param_type)) {
356-
return(glue::glue("{param_name} <- as.numeric({param_name})"))
357-
} else if (param_type == "bool" || grepl("^bool[[:space:]]*$", param_type)) {
358-
return(glue::glue("{param_name} <- as.logical({param_name})"))
359-
} else if (grepl("string", param_type, ignore.case = TRUE)) {
360-
return(glue::glue("{param_name} <- as.character({param_name})"))
361-
}
362-
363-
# Handle cpp4r matrix types - set proper storage mode
364-
if (grepl("integers_matrix", param_type)) {
365-
return(glue::glue("storage.mode({param_name}) <- \"integer\""))
366-
} else if (grepl("doubles_matrix", param_type)) {
367-
return(glue::glue("storage.mode({param_name}) <- \"double\""))
368-
} else if (grepl("logicals_matrix", param_type)) {
369-
return(glue::glue("storage.mode({param_name}) <- \"logical\""))
370-
}
371-
372-
# Handle cpp4r vector types - set proper storage mode as well
373-
if (grepl("^integers[^_]", param_type) || param_type == "integers") {
374-
return(glue::glue("storage.mode({param_name}) <- \"integer\""))
375-
} else if (grepl("^doubles[^_]", param_type) || param_type == "doubles") {
376-
return(glue::glue("storage.mode({param_name}) <- \"double\""))
377-
} else if (grepl("^logicals[^_]", param_type) || param_type == "logicals") {
378-
return(glue::glue("storage.mode({param_name}) <- \"logical\""))
379-
} else if (grepl("^strings[^_]", param_type) || param_type == "strings") {
380-
return(glue::glue("storage.mode({param_name}) <- \"character\""))
381-
}
382-
383-
# For other cpp4r types, don't add checks (they handle conversion internally)
352+
# No type checking or coercion - match cpp11's approach
353+
# The C++ side handles all type validation and conversion
384354
return("")
385355
}
386356

extended-tests/cpp4rbenchmark/R/cpp4r.R

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,44 +3,29 @@
33
#' @title Add Two Matrices
44
#' @export
55
add_two_cpp4r <- function(a, b) {
6-
storage.mode(a) <- "double"
7-
storage.mode(b) <- "double"
86
.Call(`_cpp4rbenchmark_add_two_cpp4r`, a, b)
97
}
108

119
#' @title Add Four Matrices
1210
#' @export
1311
add_four_cpp4r <- function(a, b, c, d) {
14-
storage.mode(a) <- "double"
15-
storage.mode(b) <- "double"
16-
storage.mode(c) <- "double"
17-
storage.mode(d) <- "double"
1812
.Call(`_cpp4rbenchmark_add_four_cpp4r`, a, b, c, d)
1913
}
2014

2115
#' @title Multiply Four Matrices
2216
#' @export
2317
multiply_four_cpp4r <- function(a, b, c, d) {
24-
storage.mode(a) <- "double"
25-
storage.mode(b) <- "double"
26-
storage.mode(c) <- "double"
27-
storage.mode(d) <- "double"
2818
.Call(`_cpp4rbenchmark_multiply_four_cpp4r`, a, b, c, d)
2919
}
3020

3121
#' @title Submatrix Manipulation
3222
#' @export
3323
submatrix_manipulation_cpp4r <- function(a, b) {
34-
storage.mode(a) <- "double"
35-
storage.mode(b) <- "double"
3624
.Call(`_cpp4rbenchmark_submatrix_manipulation_cpp4r`, a, b)
3725
}
3826

3927
#' @title Multi-Operation Expression
4028
#' @export
4129
multi_operation_cpp4r <- function(a, b, c) {
42-
storage.mode(a) <- "double"
43-
storage.mode(b) <- "double"
44-
storage.mode(c) <- "double"
4530
.Call(`_cpp4rbenchmark_multi_operation_cpp4r`, a, b, c)
4631
}

0 commit comments

Comments
 (0)