Skip to content

Commit

Permalink
Add OpenMP guards for MacOS. Add github workflow for mac testing and …
Browse files Browse the repository at this point in the history
…fix issues with pipeline. Bump version number
  • Loading branch information
shail-choksi committed Jul 31, 2024
1 parent a8bec7e commit 2f1d232
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 40 deletions.
39 changes: 10 additions & 29 deletions .github/workflows/cran-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ permissions:

jobs:
cran-check:
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
strategy:
fail-fast: false
matrix:
Expand All @@ -18,13 +20,9 @@ jobs:
{os: "windows-latest", r: "release", compiler_version: 15},
{os: "windows-latest", r: "devel", compiler_version: 15},
{os: "macos-latest", r: "release", compiler_version: 15},
{os: "macos-latest", r: "devel", compiler_version: 15},
# {os: "macos-latest", r: "devel", compiler_version: 15},
{os: "macos-latest", r: "release", compiler_version: 14},
]
# os: [ubuntu-latest]
# r: ['release']
# compiler: ['clang']
# compiler_version: [14, 17]
runs-on: ${{matrix.os}}
name: ${{ matrix.os }}_${{ matrix.r}}
steps:
Expand Down Expand Up @@ -56,18 +54,13 @@ jobs:
with:
extra-packages: any::rcmdcheck, any::roxygen2
needs: check
- name: Setup compiler
- name: Install mac-r-tools
if: runner.os == 'macOs'
uses: KyleMayes/install-llvm-action@v2
with:
version: ${{matrix.compiler_version}}
env: true
directory: ".."
- name: Setup Xcode
if: runner.os == 'macOs'
uses: maxim-lobanov/setup-xcode@v1
with:
xcode-version: latest
run: |
install.packages("remotes")
remotes::install_github("coatless-mac/macrtools")
macrtools::macos_rtools_install()
shell: Rscript {0}
- name: Build and Check ToxicR
if: runner.os != 'Windows'
run: |
Expand All @@ -79,10 +72,6 @@ jobs:
package_name=$(echo "$r_output" | sed -n "s/.*building ‘\([^’]*\.tar\.gz\)’.*/\1/p")
echo "$package_name"
R CMD check --as-cran --no-manual $package_name
file ToxicR.Rcheck/00install.log
file ToxicR/ToxicR.Rcheck/00install.log
cat ToxicR.Rcheck/00install.log
cat ToxicR.Rcheck/00check.log
shell: bash
working-directory: "."
- name: Build and Check ToxicR
Expand All @@ -98,12 +87,4 @@ jobs:
cat ToxicR.Rcheck/00install.log
cat ToxicR.Rcheck/00check.log
shell: bash
working-directory: "."
# - name: Show testthat output
# if: always()
# run: |
# echo ::group::Show testthat output
# find check -name 'testthat.Rout*' -exec cat '{}' \; || true
# echo ::endgroup::
# shell: bash
# working-directory: "."
working-directory: "."
81 changes: 81 additions & 0 deletions .github/workflows/mac-check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
name: MacOS check

on:
push:
branches: [ "bk_omp" ]
workflow_dispatch:

permissions:
contents: read

jobs:
cran-check:
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
strategy:
fail-fast: false
matrix:
include: [
{os: "macos-latest", r: "release", compiler_version: 15},
{os: "macos-latest", r: "release", compiler_version: 14},
]
runs-on: ${{matrix.os}}
name: ${{ matrix.os }}_${{ matrix.r}}_${{matrix.compiler_version}}
steps:
- uses: actions/checkout@v4
- uses: r-lib/actions/setup-r@v2
with:
r-version: ${{matrix.r}}
- uses: awalsh128/cache-apt-pkgs-action@latest
if: runner.os == 'Linux'
with:
packages: |
cmake cmake-data gsl dh-elpa-helper libcmark-gfm-extensions0.29.0.gfm.3
libcmark-gfm0.29.0.gfm.3 libcurl4-openssl-dev libjsoncpp25 librhash0
pandoc pandoc-data texlive-latex-base texlive-latex-recommended
texlive-fonts-recommended
version: 1.0
- name: Restore R package cache (Linux)
if: runner.os == 'Linux'
id: cache-packages
uses: actions/cache@v4
with:
key: r-${{ hashFiles('DESCRIPTION') }}
path: ${{ env.R_LIBS_USER }}
- name: Install GSL (macOS)
if: runner.os == 'macOs'
run: |
brew install gsl openssl
- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: any::rcmdcheck, any::roxygen2
needs: check
- name: Install mac-r-tools
if: runner.os == 'macOs'
run: |
install.packages("remotes")
remotes::install_github("coatless-mac/macrtools")
macrtools::macos_rtools_install()
shell: Rscript {0}
- name: Build and Check ToxicR
if: runner.os == 'Linux'
run: |
R CMD build .
r_output=$(R CMD build .)
echo "$r_output"
package_name=$(echo "$r_output" | sed -n "s/.*building ‘\([^’]*\.tar\.gz\)’.*/\1/p")
echo "$package_name"
R CMD check --as-cran --no-manual $package_name
shell: bash
working-directory: "."
- name: Build and Check ToxicR (MacOs)
if: runner.os == 'macOs'
run: |
R CMD build .
r_output=$(R CMD build .)
echo "$r_output"
package_name=$(echo "$r_output" | sed -n "s/.*building ‘\([^’]*\.tar\.gz\)’.*/\1/p")
echo "$package_name"
R CMD check --as-cran --no-manual $package_name
shell: bash
working-directory: "."
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Changes
## Version 23.10.1.2.0
## The following cumulative fixes are in version 23.10.1.2.0
## Version 23.10.1.2.3
## The following cumulative fixes are in version 23.10.1.2.3
- Add the ability to set a seed for nlopt and GSL to allow for consistent results
- Set additional checks and guards for openMP
## New Features
- Added setseedGSL function with a default seed value of 12331
## Version 23.10.1.1.1
Expand Down
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: ToxicR
Type: Package
Title: Analyzing Toxicology Dose-Response Data
Version: 24.1.1.2.2
Date: 2024-07-17
Version: 24.1.1.2.3
Date: 2024-07-31
Authors@R:
c(
person(given = "Matt",
Expand Down
2 changes: 1 addition & 1 deletion R/opening_messages.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
| |/ _ \\ \\/ / |/ __| _ /
| | (_) > <| | (__| | \\ \\
|_|\\___/_/\\_\\_|\\___|_| \\_\\
24.1.1.2.2
24.1.1.2.3
___
| |
/ \\ ____()()
Expand Down
31 changes: 30 additions & 1 deletion src/include/seeder.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
#ifndef SEEDER
#define SEEDER

#include "omp.h"
#ifndef NO_OMP
#include <omp.h>
#endif
#include <Rcpp.h>
#include <gsl/gsl_randist.h>
#include <gsl/gsl_rng.h>
Expand All @@ -30,7 +32,11 @@ class Seeder {
std::lock_guard<std::mutex> lock(instanceMutex);
if (!instance) {
instance = new Seeder();
#ifndef NO_OMP
instance->max_threads = omp_get_num_threads();
#else
instance->max_threads = 1;
#endif
instance->T = gsl_rng_mt19937;
instance->currentSeed = 0;
instance->rngs.reserve(instance->max_threads);
Expand All @@ -40,6 +46,7 @@ class Seeder {
}

void reset_max_threads(int threads) {
#ifndef NO_OMP
if(max_threads < threads) {
int num_prev_threads = max_threads;
max_threads = threads;
Expand All @@ -56,6 +63,7 @@ class Seeder {
rngs[thread_num] = r_local;
}
}
#endif
}

~Seeder() {
Expand All @@ -68,6 +76,8 @@ class Seeder {
if (seed < 0) {
Rcpp::stop("Error: Seed must be a positive integer.");
}

#ifndef NO_OMP
if(rngs.empty()) {
#pragma omp parallel for
for (int i = 0; i < max_threads; i++) {
Expand All @@ -87,25 +97,44 @@ class Seeder {
rngs[thread_num] = r_local;
}
}
#else // MacOs or non-OpenMP architecture
int thread_num = 0; // master thread
gsl_rng_free(rngs[thread_num]);
gsl_rng* r_local = gsl_rng_alloc(gsl_rng_mt19937);
gsl_rng_set(r_local, seed);
rngs[thread_num] = r_local;
#endif
}

double get_uniform() {
double r_val;
#ifndef NO_OMP
int thread_num = omp_get_thread_num();
#else
int thread_num = 0;
#endif
r_val = gsl_rng_uniform(rngs[thread_num]);
return r_val;
}

double get_gaussian_ziggurat() {
double r_val;
#ifndef NO_OMP
int thread_num = omp_get_thread_num();
#else
int thread_num = 0;
#endif
r_val = gsl_ran_gaussian_ziggurat(rngs[thread_num], 1.0);
return r_val;
}

double get_ran_flat() {
double r_val;
#ifndef NO_OMP
int thread_num = omp_get_thread_num();
#else
int thread_num = 0;
#endif
r_val = gsl_ran_flat(rngs[thread_num], -1, 1);
return r_val;
}
Expand Down
20 changes: 15 additions & 5 deletions src/set_omp_threads.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include "omp.h"
#ifndef NO_OMP
#include <omp.h>
#endif

#include <Rcpp.h>
#include "seeder.h"
Expand All @@ -12,9 +14,17 @@ using namespace Rcpp;
// output: none
// [[Rcpp::export(".set_threads")]]
void set_threads(int num_threads) {
if (num_threads > omp_get_num_threads()) {
Seeder* s = Seeder::getInstance();
omp_set_num_threads(num_threads);
s->reset_max_threads(num_threads);
#ifndef NO_OMP
if(omp_get_max_threads() > 1){
if (num_threads > omp_get_num_threads()) {
Seeder* s = Seeder::getInstance();
omp_set_num_threads(num_threads);
s->reset_max_threads(num_threads);
}
}else{
Rcout << "OMP will not be used for parallelization.";
}
#else
Rcpp::Rcout << "OpenMP not supported on this architecure." << std::endl;
#endif
}

0 comments on commit 2f1d232

Please sign in to comment.