void nested_mpi::choose_cpu_step(){ for (int mc_step=0; mc_step<numProcs; mc_step++){ double cpu_log_like = 0.0; choose_step(); for (int j=0;j<nb_var;++j) cpu_chain[j].set(trial_vector[j]); if (mc_step == myid) cpu_log_like=log_like(trial_vector); cpu_log_posterior.set(cpu_log_like); } MPI_Status status; MPI_Barrier(MPI_COMM_WORLD); // redistribute answers around... for (int mc_step=0; mc_step<numProcs; mc_step++){ double value; if (mc_step == myid){ value = cpu_log_posterior.x_data(mc_step); for (int ii=0; ii<numProcs; ++ii) if (ii != mc_step) MPI_Send((void *)&value,1,MPI_DOUBLE,ii,1,MPI_COMM_WORLD); } if (mc_step != myid) { MPI_Recv((void *)&value,1,MPI_DOUBLE,mc_step,1,MPI_COMM_WORLD,&status); cpu_log_posterior.set(mc_step,value); } } MPI_Barrier(MPI_COMM_WORLD); }
void generate_points(){ mpoints.setRandom(mndim,msize); for(int ii=0; ii < mlikelihoods.size(); ii++){ mlikelihoods(ii) = log_like(point(ii)); } }
void nested_mpi::choose_cpu_pre_step(){ // Calculate with all processors... for (int mc_step=1; mc_step<=nb_pre_steps; mc_step++){ choose_step_box(); for (int j=0;j<nb_var;++j) pre_chain[j].set(trial_vector[j]); if (mc_step % numProcs == myid){ trial_log_like=log_like(trial_vector); } else trial_log_like=0.0; pre_log_posterior.set(trial_log_like); if (mc_step % numProcs == myid) if (verbose >=2) { int iii=pre_chain[0].size_data(); for (int j=0;j<nb_var;++j) {std::cout<<pre_chain[j].x_data(iii-1)<<"\t";} std::cout<<pre_log_posterior.x_data(iii-1)<<"\n"<<std::flush; } } MPI_Status status; status.MPI_ERROR; MPI_Barrier(MPI_COMM_WORLD); for (int mc_step=1; mc_step<=nb_pre_steps; mc_step++){ double value; if (mc_step % numProcs == myid){ value = pre_log_posterior.x_data(mc_step-1); for (int ii=0;ii<numProcs;++ii) if (ii != myid) { MPI_Send((void *)&value,1,MPI_DOUBLE,ii,1,MPI_COMM_WORLD); } } if (mc_step % numProcs != myid) { MPI_Recv((void *)&value,1,MPI_DOUBLE,mc_step%numProcs,1, MPI_COMM_WORLD,&status); pre_log_posterior.set(mc_step-1,value); } } MPI_Barrier(MPI_COMM_WORLD); }
static void seq_MAP_routine(unsigned char ***sf_pym, /* pyramid of segmentations */ struct Region *region, /* specifies image subregion */ LIKELIHOOD **** ll_pym, /* pyramid of class statistics */ int M, /* number of classes */ double *alpha_dec /* decimation parameters returned by seq_MAP */ ) { int j, k; /* loop index */ int wd, ht; /* width and height at each resolution */ int *period; /* sampling period at each resolution */ int D; /* number of resolutions -1 */ double ***N; /* transition probability statistics; N[2][3][2] */ double alpha[3]; /* transition probability parameters */ double tmp[3]; /* temporary transition probability parameters */ double diff1; /* change in parameter estimates */ double diff2; /* change in log likelihood */ struct Region *regionary; /* array of region stuctures */ /* determine number of resolutions */ D = levels_reg(region); /* allocate memory */ if ((N = (double ***)multialloc(sizeof(double), 3, 2, 3, 2)) == NULL) G_fatal_error(_("Unable to allocate memory")); regionary = (struct Region *)G_malloc((D + 1) * sizeof(struct Region)); period = (int *)G_malloc(D * sizeof(int)); /* Compute the image region at each resolution. */ k = 0; copy_reg(region, &(regionary[k])); reg_to_wdht(&(regionary[k]), &wd, &ht); while ((wd > 2) && (ht > 2)) { copy_reg(&(regionary[k]), &(regionary[k + 1])); dec_reg(&(regionary[k + 1])); reg_to_wdht(&(regionary[k + 1]), &wd, &ht); k++; } /* Compute sampling period for EM algorithm at each resolution. */ for (k = 0; k < D; k++) { period[k] = (int)pow(2.0, (D - k - 2) / 2.0); if (period[k] < 1) period[k] = 1; } /* Compute Maximum Likelihood estimate at coarsest resolution */ MLE(sf_pym[D], ll_pym[D], &(regionary[D]), M); /* Initialize the transition parameters */ alpha[0] = 0.5 * (3.0 / 7.0); alpha[1] = 0.5 * (2.0 / 7.0); alpha[2] = 0.0; /* Interpolate the classification at each resolution */ for (D--; D >= 0; D--) { G_debug(1, "Resolution = %d; period = %d", D, period[D]); for (j = 0; j < 3; j++) alpha[j] *= (1 - EM_PRECISION * 10); print_alpha(alpha); /* Apply EM algorithm to estimate alpha. Continue for * * fixed number of iterations or until convergence. */ do { interp(sf_pym[D], &(regionary[D]), sf_pym[D + 1], ll_pym[D], M, alpha, period[D], N, 1); print_N(N); G_debug(4, "log likelihood = %f", log_like(N, alpha, M)); for (j = 0; j < 3; j++) tmp[j] = alpha[j]; alpha_max(N, alpha, M, ML_PRECISION); print_alpha(alpha); G_debug(4, "log likelihood = %f", log_like(N, alpha, M)); for (diff1 = j = 0; j < 3; j++) diff1 += fabs(tmp[j] - alpha[j]); diff2 = log_like(N, alpha, M) - log_like(N, tmp, M); } while ((diff1 > EM_PRECISION) && (diff2 > 0)); interp(sf_pym[D], &(regionary[D]), sf_pym[D + 1], ll_pym[D], M, alpha, 1, N, 0); alpha_dec[D] = alpha_dec_max(N); print_N(N); alpha_max(N, alpha, M, ML_PRECISION); print_alpha(alpha); } /* free up N */ G_free((char *)regionary); G_free((char *)period); multifree((char *)N, 3); }