Coder Social home page Coder Social logo

slwu89 / romp Goto Github PK

View Code? Open in Web Editor NEW

This project forked from wrathematics/romp

0.0 2.0 0.0 29 KB

Basic examples using OpenMP with R, for C, C++, F77, and Fortran 2003.

License: Other

R 33.09% Shell 0.96% C++ 30.51% C 18.66% Fortran 16.77%

romp's Introduction

Romp

Basic examples using OpenMP with R, for C, C++, F77, and Fortran 2003.

Examples

There are 3 examples, using each of C, C++ (Rcpp), F77, and F2003.

Hello World

A basic OpenMP hello world. Note that the order of thread printing is not guaranteed.

Sum

Sums up a numeric vector.

Sweep

Sweeps a numeric vector from a numeric matrix of the same length as the number of rows of that matrix. Equivalent to calling sweep(x, STATS=vec, MARGIN=1, FUN="-") in R.

Benchmarks

I wouldn't take the numbers here too seriously, especially for the sum example, where they appear to be statistically identical. The languages separate on the sweep example, though possibly for implementation reasons.

The benchmarks are located at Romp/inst/benchmarks/ of the source tree. All tests were performed using:

  • R 3.2.0
  • gcc 4.9.1
  • 4 cores of a Core i5-2500K CPU @ 3.30GHz

Sum

         test replications elapsed relative
5  f90_sum(x)          100   1.178    1.000
4  f77_sum(x)          100   1.179    1.001
2    c_sum(x)          100   1.186    1.007
3 rcpp_sum(x)          100   1.196    1.015
1      sum(x)          100   2.605    2.211

Sweep

                test replications elapsed relative
4  f77_sweep(x, vec)          100   5.242    1.000
5  f90_sweep(x, vec)          100   5.315    1.014
2    c_sweep(x, vec)          100   5.354    1.021
3 rcpp_sweep(x, vec)          100  14.966    2.855
1    r_sweep(x, vec)          100  52.330    9.983

Primes Below an Integer

                 test replications elapsed relative
1    c_primesbelow(n)          100   9.054    1.000
3  f90_primesbelow(n)          100   9.629    1.064
2  f77_primesbelow(n)          100   9.656    1.066
4 rcpp_primesbelow(n)          100  10.391    1.148

Integration with R

If you are unfamiliar with integrating C, C++, or Fortran into R, then the following will hopefully be of use to you.

C

We use the .Call() interface. There is a (nearly) deprecated .C() interface, which you should not use, as it has serious performance loss compared to the .Call() interface.

If you are interested in some simplifications of R's C interface but don't want to jump to C++, you might consider taking a look at the C project RNACI, which is also available as a header-only library.

C++

Here we use Rcpp. Using RcppAttributes (noted by the // [[Rcpp::export]] calls), we can write something that looks very much like C++, and use Rcpp's (R function) compileAttributes() to generate C-level and R-level R wrappers. The script Romp/resrc does this.

Fortran

For both F77 and F90+, integration with R is non-trivial. As with C, there is a (nearly) deprecated interface .Fortran() which you should not use due to its large performance overhead. Instead, you should:

  1. Write your Fortran code.
  2. Write a C wrapper of the Fortran code using R's .Call() interface (or Rcpp if you prefer).
  3. Call the C code from R using .Call(().

As noted, you can use Rcpp in lieu of R's basic C interface for wrapping Fortran code, though I seriously recommend against it. Bringing in C++ can complicate linking, among other things, and for wrapping C/Fortran code, in my opinion, Rcpp brings little to the table (C++ is another story!). If you are interested in some simplifications of R's C interface but don't want to jump to use Rcpp, you might consider taking a look at RNACI.

It is difficult to reliably use F90+ functions and subroutines which live in modules without the use of the F2003 iso_c_binding module (which has been supported by every compiler for ages). This package gives some nice examples of how to use module code.

A final note about F90+ in particular, is that some of the advice in Writing R Extensions is not always entirely accurate.

romp's People

Contributors

wrathematics avatar slwu89 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.