You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello, I am writing because I am experiencing troubles with parallel programming.
The following code is a parallel version of a stochastic model for an actuarial simulation.
Even with OpenMP there are unsolvable errors I am unable to fix (may be useful: win11, R 4.5.1, Rstudio 2025.05.1 Build 513)
Very briefly, I would like to repeat in parallel the same stochastic simulation model for evolving a popolution.
The sequential version works (EvoluzionePopolazione_v01.cpp), while the parallel version crashes
(EvoluzionePopolazione_par_v03_Gemini.cpp).
Here is my parallel code...
#include<Rcpp.h>// [[Rcpp::depends(dqrng)]]
#include<dqrng.h>
#include<omp.h>// [[Rcpp::plugins(openmp)]]usingnamespaceRcpp;inline Rcpp::NumericMatrix random_mtx(int n, int m) {
Rcpp::NumericVector res = dqrng::dqrunif(n * m, 0.0, 1.0);
res.attr("dim") = Rcpp::Dimension(n, m);
return Rcpp::as<Rcpp::NumericMatrix>(res);
}
inline Rcpp::IntegerMatrix permutazioni(int n,Rcpp::IntegerVector x) {
Rcpp::IntegerMatrix perm(n,x.size());
for(int i = 0; i < n; ++i) {
Rcpp::IntegerVector indices = dqrng::dqsample_int(x.size(), x.size());
for(int j = 0; j <x.size();j++){
perm(i,j)=x[indices[j]];
}
}
return perm;
}
inline Rcpp::NumericVector DeterminazioniEvento(
int evento, int stato, int tp_sesso,
double prob_q, double prob_tavM
){
NumericVector mDetEvento(3);
// 0. evento, 1. accadimento, 2. statomDetEvento[0] = evento;
switch(evento) {
case1:
{
if (prob_q <= prob_tavM) {
mDetEvento[1] = 1; //mortemDetEvento[2] = 5;
} else {
mDetEvento[1] = 0; //sopravvivenzamDetEvento[2] = stato; //non cambia lo stato
}
}
break;
default:
{
}
break;
}
returnmDetEvento;
}
inline Rcpp::NumericMatrix SintesiSimulazione(NumericMatrix mSim, int nR, int nAnni){
Rcpp::NumericMatrix Sintesi(1, 4 * nAnni);
for(int i = 0; i < nR; i++) {
if(mSim(i,12) != 0) {Sintesi(0, mSim(i,12)-1)++; } //conteggio decessi per epocaif(mSim(i,13) != 0) {Sintesi(0, nAnni + mSim(i,13)-1)++; } //conteggio reversibilità per epocaif(mSim(i,14) != 0) {Sintesi(0,2 * nAnni + mSim(i,14)-1)++; } //conteggio indirette per epocaif(mSim(i,15) != 0) {Sintesi(0,3 * nAnni + mSim(i,15)-1)++; } //conteggio estinzioni per epoca
} //next ireturn Sintesi;
}
// [[Rcpp::export]]
Rcpp::NumericMatrix popolazione_par_v02(NumericMatrix Iscritti, NumericMatrix Tmorte, NumericMatrix Teta,
int nRip, int nAnni, int nThreads) {
int nR = Iscritti.nrow();
int nC = Iscritti.ncol();
Rcpp::NumericMatrix mSintesi(nRip, 1 + 4 * nAnni); //4 eventiif (nThreads > 0) {
omp_set_num_threads(nThreads);
}
#pragma omp parallel for
for(int r = 0; r < nRip; r++) {
Rcpp::NumericMatrix mSim(nR, 16);
Rcpp::NumericMatrix mpcMorte = random_mtx(nR, nAnni);
Rcpp::NumericMatrix SintesiAnno(1, 4 * nAnni);
double prob_q=0.0;
double prob_tavM=0.0;
int evento = 0;
for(int s = 0; s < nC; s++) {
for(int z = 0; z < nR; z++) {
mSim(z,s) = Iscritti(z,s);
}
}
int eta_anag =0;
int eta_q=0;
int tp_sesso=0;
int stato=0;
int inC= -1; //incremento epocafor(int t = 1; t <= nAnni; t++) {
inC = inC + 1;
for(int i = 0; i < nR; i++) {
NumericVector eventoRis(3);
tp_sesso = mSim(i,5);
stato = mSim(i,6);
if (t == 1) {
eta_anag = mSim(i,1);
eta_q = mSim(i,4);
} else {
eta_anag = mSim(i,1) + inC; //incremento età anagrafica
eta_q = mSim(i,4) + inC; //incremento età aggiustata
}
evento = 1; //unico evento previstoif(mSim(i,6) != 5) {
prob_q = mpcMorte(i, t - 1);
prob_tavM = Tmorte(eta_q, tp_sesso);
eventoRis = DeterminazioniEvento (evento, stato, tp_sesso, prob_q, prob_tavM);
if (eventoRis[0] == 1 && eventoRis[1] == 1) {
mSim(i,6) = eventoRis[2]; //nuovo statomSim(i,12) = t; //epoca decesso mSim(i,15) = t; //epoca estinzione
}
}
} //next i
} //next t
SintesiAnno = SintesiSimulazione(mSim, nR, nAnni);
mSintesi(r,0) = r;
for(int g = 0; g < SintesiAnno.ncol(); g++) {
mSintesi(r,g + 1) = SintesiAnno(0,g);
}
} //next rreturnmSintesi;
}
it is possible to test the simulation and to reproduce the errors using the following R code:
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello, I am writing because I am experiencing troubles with parallel programming.
The following code is a parallel version of a stochastic model for an actuarial simulation.
Even with OpenMP there are unsolvable errors I am unable to fix (may be useful: win11, R 4.5.1, Rstudio 2025.05.1 Build 513)
Very briefly, I would like to repeat in parallel the same stochastic simulation model for evolving a popolution.
The sequential version works (EvoluzionePopolazione_v01.cpp), while the parallel version crashes
(EvoluzionePopolazione_par_v03_Gemini.cpp).
Here is my parallel code...
it is possible to test the simulation and to reproduce the errors using the following R code:
Beta Was this translation helpful? Give feedback.
All reactions