From fc5a4f6910485d44f1df8eaf93e43def233baf79 Mon Sep 17 00:00:00 2001 From: Sudaraka88 Date: Thu, 18 Jul 2024 16:43:22 +1000 Subject: [PATCH] openMP_checker --- .gitignore | 1 - R/BacGWES.R | 3 ++- R/RcppExports.R | 4 ++++ R/performPopulationStuctureCorrection.R | 10 +++++----- src/Makevars | 13 +++++++++++++ src/RcppExports.cpp | 10 ++++++++++ src/openMP_tester.cpp | 16 ++++++++++++++++ 7 files changed, 50 insertions(+), 7 deletions(-) create mode 100644 src/Makevars create mode 100644 src/openMP_tester.cpp diff --git a/.gitignore b/.gitignore index d62b672..b3ec624 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,3 @@ *.o .DS_Store *.Rmd -Makevars diff --git a/R/BacGWES.R b/R/BacGWES.R index e23bf7c..a6bfc6f 100644 --- a/R/BacGWES.R +++ b/R/BacGWES.R @@ -243,7 +243,8 @@ LDWeaver = function(dset, aln_path, aln_has_all_bases = T, pos = NULL, gbk_path ######## Welcome message ######## { timestamp() - cat("\n ***** This is LDWeaver", as.character(packageVersion(pkg = "LDWeaver")), " *****") + cat("\n ***** This is LDWeaver", as.character(packageVersion(pkg = "LDWeaver")), " *****\n") + test_openmp() if(ncores > 1) cat(paste("\n\n Performing GWES analysis on:", dset, " - using", ncores, "cores\n\n")) if(ncores == 1) cat(paste("\n\n Performing GWES analysis on:", dset, "\n\n")) if(perform_SR_analysis_only) cat("Only short-range analysis requested. \n") diff --git a/R/RcppExports.R b/R/RcppExports.R index e2c4e3d..1346efa 100644 --- a/R/RcppExports.R +++ b/R/RcppExports.R @@ -37,6 +37,10 @@ .Call('_LDWeaver_extractRef', PACKAGE = 'LDWeaver', file) } +test_openmp <- function() { + invisible(.Call('_LDWeaver_test_openmp', PACKAGE = 'LDWeaver')) +} + .readFasta <- function(file, pos_len) { .Call('_LDWeaver_readFasta', PACKAGE = 'LDWeaver', file, pos_len) } diff --git a/R/performPopulationStuctureCorrection.R b/R/performPopulationStuctureCorrection.R index 7a875e1..8666f16 100644 --- a/R/performPopulationStuctureCorrection.R +++ b/R/performPopulationStuctureCorrection.R @@ -52,20 +52,20 @@ estimate_Hamming_distance_weights = function(snp.dat, threshold = 0.1, mega_dset } } else { - snpmat_t = as(snp.dat$snp.matrix_A, 'dgCMatrix') + snpmat_t = as(snp.dat$snp.matrix_A, 'lgeMatrix') # shared.snps = MatrixExtra::crossprod((snpmat_t)) shared.snps = crossprod((snpmat_t)) - snpmat_t = as(snp.dat$snp.matrix_C, 'dgCMatrix') + snpmat_t = as(snp.dat$snp.matrix_C, 'lgeMatrix') shared.snps = shared.snps + crossprod((snpmat_t)) - snpmat_t = as(snp.dat$snp.matrix_G, 'dgCMatrix') + snpmat_t = as(snp.dat$snp.matrix_G, 'lgeMatrix') shared.snps = shared.snps + crossprod((snpmat_t)) - snpmat_t = as(snp.dat$snp.matrix_T, 'dgCMatrix') + snpmat_t = as(snp.dat$snp.matrix_T, 'lgeMatrix') shared.snps = shared.snps + crossprod((snpmat_t)) - snpmat_t = as(snp.dat$snp.matrix_N, 'dgCMatrix') + snpmat_t = as(snp.dat$snp.matrix_N, 'lgeMatrix') shared.snps = shared.snps + crossprod((snpmat_t)) hdw = 1/( Matrix::colSums( (snp.dat$nsnp - shared.snps) < thresh) + 1) diff --git a/src/Makevars b/src/Makevars new file mode 100644 index 0000000..30483de --- /dev/null +++ b/src/Makevars @@ -0,0 +1,13 @@ +CXX_STD=CXX11 + +CXX1X=$(shell ${R_HOME}/bin/R CMD config CXX11) + +PKG_LIBS = $(shell $(R_HOME)/bin/Rscipt.exe -e "Rcpp:::LdFlags()") -lz + +ifeq ($(shell $(CXX1X) -fopenmp -E -xc++ - 2>&1 >/dev/null && echo 'true'), true) + PKG_CXXFLAGS=$(SHLIB_OPENMP_CXXFLAGS) -DARMA_64BIT_WORD + PKG_LIBS=$(SHLIB_OPENMP_CXXFLAGS) $(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS) +else + PKG_CXXFLAGS=-DARMA_64BIT_WORD + PKG_LIBS=$(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS) +endif diff --git a/src/RcppExports.cpp b/src/RcppExports.cpp index a342f74..8b29f0c 100644 --- a/src/RcppExports.cpp +++ b/src/RcppExports.cpp @@ -129,6 +129,15 @@ BEGIN_RCPP return rcpp_result_gen; END_RCPP } +// test_openmp +void test_openmp(); +RcppExport SEXP _LDWeaver_test_openmp() { +BEGIN_RCPP + Rcpp::RNGScope rcpp_rngScope_gen; + test_openmp(); + return R_NilValue; +END_RCPP +} // readFasta List readFasta(std::string file, int pos_len); RcppExport SEXP _LDWeaver_readFasta(SEXP fileSEXP, SEXP pos_lenSEXP) { @@ -152,6 +161,7 @@ static const R_CallMethodDef CallEntries[] = { {"_LDWeaver_extractAlnParam", (DL_FUNC) &_LDWeaver_extractAlnParam, 4}, {"_LDWeaver_extractSNPs", (DL_FUNC) &_LDWeaver_extractSNPs, 4}, {"_LDWeaver_extractRef", (DL_FUNC) &_LDWeaver_extractRef, 1}, + {"_LDWeaver_test_openmp", (DL_FUNC) &_LDWeaver_test_openmp, 0}, {"_LDWeaver_readFasta", (DL_FUNC) &_LDWeaver_readFasta, 2}, {NULL, NULL, 0} }; diff --git a/src/openMP_tester.cpp b/src/openMP_tester.cpp new file mode 100644 index 0000000..c07551e --- /dev/null +++ b/src/openMP_tester.cpp @@ -0,0 +1,16 @@ +#include +#ifdef _OPENMP +#include +// [[Rcpp::plugins(openmp)]] +#endif + + +// [[Rcpp::export]] +void test_openmp() { +#ifdef _OPENMP + int nthreads = omp_get_max_threads(); + Rcpp::Rcout << "with OpenMP support - system supports up to: " << nthreads << " cores" << std::endl; +#else + Rcpp::Rcout << "without OpenMP support - WARNING! Some functions will run slower..." << std::endl; +#endif +}