Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
ahomoudi committed Mar 15, 2024
1 parent 44759f9 commit b7477c1
Show file tree
Hide file tree
Showing 12 changed files with 391 additions and 298 deletions.
11 changes: 2 additions & 9 deletions QuartoBook/AquaFortR_Codes/conv2D.f90
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ subroutine conv2d_f(m, n, p, q, k, l, a, b, conv)
implicit none
integer :: m, n, p, q, k, l, i, j
double precision, dimension(m, n) :: a
double precision, dimension(p, q) :: b, b_flipped
double precision, dimension(p, q) :: b
double precision, dimension(k, l) :: conv
! dummy vars
integer :: min_row_shift, min_col_shift
Expand All @@ -17,13 +17,6 @@ subroutine conv2d_f(m, n, p, q, k, l, a, b, conv)
min_col_shift = -1*(q - 1)
max_col_shift = n - 1

! Flip the kernel i.e. B
b_flipped = 0.0
do i = 1, p
do j = 1, q
b_flipped(p - i + 1, q - j + 1) = b(i, j)
end do
end do

! Padded arrray
rows_padded = abs(min_row_shift) + m + abs(max_row_shift)
Expand All @@ -42,7 +35,7 @@ subroutine conv2d_f(m, n, p, q, k, l, a, b, conv)
iconv = irow + ((icol - 1)*k)
icol2 = icol + q - 1
irow2 = irow + p - 1
padded_b(irow:irow2, icol:icol2) = b_flipped
padded_b(irow:irow2, icol:icol2) = b(p:1:-1,q:1:-1)
conv(irow, icol) = sum(padded_a*padded_b)
padded_b = 0.0
end do
Expand Down
46 changes: 25 additions & 21 deletions QuartoBook/AquaFortR_Codes/conv2D_f.R
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
conv2D_f0 <- function(a, b) {
require(dotCall64)
dyn.load("AquaFortR_Codes/conv2D.so")


m <- nrow(a)
n <- ncol(b)

p <- nrow(b)
q <- ncol(b)
# the full convolution matrix
conv_row <- nrow(a) + nrow(b) - 1
conv_col <- ncol(a) + ncol(b) - 1
conv <- matrix(1:c(conv_row * conv_col),
byrow = FALSE, ncol = conv_col
)

result <- .Fortran("conv2d_f",
m = as.integer(dim(a)[1]),
n = as.integer(dim(a)[2]),
p = as.integer(dim(b)[1]),
q = as.integer(dim(b)[2]),
k = as.integer(conv_row),
l = as.integer(conv_col),
a = as.double(a),
b = as.double(b),
conv = as.double(conv)
)$conv

return(matrix(result, nrow = conv_row, ncol = conv_col))
}
conv_row <- m + p - 1
conv_col <- n + q - 1
conv <- matrix(0,
ncol = conv_col,
nrow = conv_row)

conv <- .C64("conv2d_f",
SIGNATURE = c(rep("integer",6),
rep("double",3)),
INTENT = c(rep("r",8), "rw"),
m, n, p, q,
k = conv_row,
l = conv_col,
a = a, b = b,
conv = conv)$conv

return(conv)
}
1 change: 1 addition & 0 deletions QuartoBook/_quarto.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ book:
- scripts.qmd
- package.qmd
- summary.qmd
- further_reading.qmd
- references.qmd
page-navigation: true

Expand Down
23 changes: 23 additions & 0 deletions QuartoBook/further_reading.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Further Reading
* [Fortran and R – Speed Things Up](https://www.r-bloggers.com/2014/04/fortran-and-r-speed-things-up/) by Steve Pittard
* [The Need for Speed Part 1: Building an R Package with Fortran (or C)](https://www.r-bloggers.com/2018/12/the-need-for-speed-part-1-building-an-r-package-with-fortran-or-c/) by Avraham Adler
* [The Need for Speed Part 2: C++ vs. Fortran vs. C](https://www.avrahamadler.com/2018/12/23/the-need-for-speed-part-2-c-vs-fortran-vs-c/) by Avraham Adler
* [Writing R Extensions](https://cran.r-project.org/doc/manuals/r-release/R-exts.html#Writing-R-Extensions) by R Core Team
* [Modern Fortran Tutorial](https://masuday.github.io/fortran_tutorial/index.html) by Yutaka Masuda
* [Extend R with Fortran](https://masuday.github.io/fortran_tutorial/r.html) by Yutaka Masuda
* [Advanced R](http://adv-r.had.co.nz/) by Hadley Wickham
* [Fortran 90 Tutorial](http://web.stanford.edu/class/me200c/tutorial_90/) by Stanford University
* [Fortran Libraries](https://fortranwiki.org/fortran/show/Libraries) by Fortran Wiki
* [Fortran Best Practices](https://fortran-lang.org/en/learn/best_practices/#fortran-best-practices) by Fortran Community
* [Fortran 90 Reference Card](https://web.pa.msu.edu/people/duxbury/courses/phy480/fortran90_refcard.pdf) by Michael Goerz.
* [Hands-On Programming with R](https://rstudio-education.github.io/hopr/) by Garrett Grolemund
* [r-spatial](https://r-spatial.org/) by Edzer Pebesma, Marius Appel, and Daniel Nüst
* [Spatial Data Science](https://rspatial.org/index.html)
* [R for Data Science (2e)](https://r4ds.hadley.nz/) by Hadley Wickham, Mine Çetinkaya-Rundel, and Garrett Grolemund
* [Introduction to Environmental Data Science](https://bookdown.org/igisc/EnvDataSci/) by Jerry Davis

<!-- https://cran.r-project.org/doc/manuals/r-release/R-exts.html#System-and-foreign-language-interfaces-1 -->

<!-- Fortran is primarily intended for Fortran 77 code, and long precedes any support for Fortran 9x. Now current implementations of Fortran 9x support the Fortran 2003 module iso_c_binding, a better way to interface modern Fortran code to R is to use .C and write a C interface using use iso_c_binding. -->

<!-- The header files R_ext/BLAS.h, R_ext/Lapack.h and R_ext/Linpack.h contains declarations of the BLAS, LAPACK and LINPACK linear algebra functions included in R. -->
10 changes: 5 additions & 5 deletions QuartoBook/index.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

Welcome to the enlightening journey through the pages of **"AquaFortR: Streamlining
Atmospheric Science, Oceanography, Climate, and Water Research with Fortran-accelerated R"**.
In this book, you will gain invaluable insights into seamlessly integrating Fortran
into R scripts by harnessing the power of Fortran. You will acquire
invaluable insights into how to speed up your package using simple C and Fortran codes.
Furthermore, you will accumulate tweaks to further accelerate your scripts or packages,
and further reading will prove to be both advantageous and highly beneficial for further
In this book, you will gain invaluable insights into seamlessly speeding up R scripts
by harnessing the power of Fortran. You will acquire
essential perspectives into speeding up your package using simple C and Fortran codes.
Furthermore, you will accumulate tweaks to accelerate your scripts or packages,
and supplementary reading will prove to be both advantageous and highly beneficial for further
optimization, and efficiency.

&nbsp;
Expand Down
8 changes: 4 additions & 4 deletions QuartoBook/package.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ This chapter will focus on wrapping the developed routines from the previous cha
<!-- https://www.mzes.uni-mannheim.de/socialsciencedatalab/article/r-package/ -->
## Developing AquaFortR Package


### R function

### Fortran subroutine

- iso_c_binidng
iso_c_binidng
<!-- - https://gcc.gnu.org/onlinedocs/gfortran/Intrinsic-Types.html -->

### R function

### C funtions
- C to Fortran
Expand Down
Loading

0 comments on commit b7477c1

Please sign in to comment.