Rcpp_matrix_blockSubstract
C++ Function Reference
1 Signature
Rcpp::RObject BigDataStatMeth::Rcpp_matrix_blockSubstract(T A, T B, Rcpp::Nullable< int > threads=R_NilValue)2 Description
Block-based matrix subtraction.
3 Parameters
A(T): First input matrixB(T): Second input matrix (subtracted from A)threads(Rcpp::Nullable< int >): Number of threads for parallel computation
4 Returns
Result of matrix subtraction (A - B)
5 Details
Implements block-based matrix subtraction with optional parallel processing.
6 Call Graph
7 Source Code
NoteImplementation
File: inst/include/memAlgebra/memSubstract.hpp • Lines 256-334
inline Rcpp::RObject Rcpp_matrix_blockSubstract ( T A, T B, Rcpp::Nullable<int> threads)
{
// static_assert(std::is_same<T, Eigen::MatrixXd >::value ||
// std::is_same<T, Eigen::Map< Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>> >::value ||
// std::is_same<T, Eigen::Map< Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor>> >::value ,
// "Error - type not allowed");
Rcpp::NumericMatrix X = Rcpp::as<Rcpp::NumericMatrix>(A);
Rcpp::NumericMatrix Y = Rcpp::as<Rcpp::NumericMatrix>(B);
hsize_t N = X.rows();
hsize_t M = X.cols();
Rcpp::NumericMatrix C = Rcpp::no_init( N, M);
try {
// unsigned int ithreads;
hsize_t block_size;
std::vector<hsize_t> vsizetoRead;
std::vector<hsize_t> vstart;
std::vector<hsize_t> blockSize = getMatrixBlockSize( N, M);
if(N < M) {
block_size = blockSize.at(0);
} else {
block_size = blockSize.at(1);
}
if(block_size > 0 ) {
// if( N == Y.rows() && M == Y.cols())
if (N == static_cast<hsize_t>(Y.rows()) && M == static_cast<hsize_t>(Y.cols()))
{
// ithreads = get_number_threads(threads, R_NilValue);
getBlockPositionsSizes( N*M, block_size, vstart, vsizetoRead );
// int chunks = vstart.size()/ithreads;
#pragma omp parallel num_threads( get_number_threads(threads, R_NilValue) ) shared(A, B, C) //, chunks)
{
#pragma omp for schedule (dynamic)
for (hsize_t ii = 0; ii < vstart.size(); ii ++)
{
if( vstart[ii] + vsizetoRead[ii] >= N*M ) {
std::transform (X.begin() + vstart[ii], X.end(),
Y.begin() + vstart[ii], C.begin() + vstart[ii], std::minus<double>());
} else {
std::transform (X.begin() + vstart[ii], X.begin() + vstart[ii] + vsizetoRead[ii],
Y.begin() + vstart[ii], C.begin() + vstart[ii], std::minus<double>());
}
}
}
} else {
Rcpp::Rcout<<"matrix sum error: non-conformable arguments\n";
return(R_NilValue);
}
} else{
Rcpp::Rcout<<"matrix sum error: Error whent computing block sizes\n";
return(R_NilValue);
}
} catch(std::exception &ex) {
Rcpp::Rcout<< ex.what();
return(R_NilValue);
}
C.attr("dim") = Rcpp::Dimension( N, M);
return(C);
}8 Usage Example
#include "BigDataStatMeth.hpp"
// Example usage
auto result = Rcpp_matrix_blockSubstract(...);