Similar functions produce different outputs - c++

I am trying to compile the files below. The PosLin.cpp contains the SurTriAuto and getSphere functions below. Although they are similar, I am not getting the same results. Is it because the "namespace TPiecesNS" causes them to be different?
I have a tpieces.h file
namespace TPiecesNS
{
class TPieces
{
public:
TPieces();
//other stuff
}
}
tpieces.cpp has:
void TPieces::addPoint(Vertex* point)
{
Vertex* p = new Vertex();
p->Point[0] = point->Point[0]; //similar for Point[1],[2]
p->Normal[0] = point->Normal[0]; //same for 1,2
m_Vertices.push_back(p);
}
geopar.h file has
#include "tpieces.h"
#include "Geo/Geo.h"
class Geo;
namespace TPiecesNS
{
class GeoPar;
{
public:
GeoPar();
TPieces* getSphere(Geo* geo);
TPieces* getSphere(Geo* geo, int permu);
private:
TPieces* SurTriAuto(TPieces* boundary, Geo* geo,int permu);
}
}
geopar.cpp file has
#include "tpieces/geo.h"
#include "tpieces.h"
#include "Geo/Geo.h"
using namespace TPiecesNS;
TPieces* GeoPar::getSphere(Geo* geo) {
return getSphere(geo, 0);
}
TPieces* GeoPar::getSphere(Geo* geo, int permu)
{
TPieces* boundary = new Sphere();
return SurTriAuto(boundary,geo,permu);
}
TPieces* GeoPar::SurTriAuto(TPieces* boundary, Geo* geo, int permu)
{
double maxx, maxy, maxz, minx, miny, minz;
double x,y,z,f,nx,ny,nz;
int number = 6;
ofstream file;
file.open("output.txt");
boundary->numbpts = geo->m_NumTriVerts;
boundary->numbtris = geo->m_NumTris;
file<<"NumVertices "<<boundary->numbpts<<endl;
file<<"NumTrianlges "<<boundary->numbtris<<endl;
for (i = 0 ; i < boundary->numbpts; i++)
{
x = geometry->m_TriVerts[i*3+0];
//also equalities for y,z, but I don't want to type here in order to save space
nx = geometry->m_TriVertNormals[i*3+0];
//ny, nz also
if (x < minx) minx = x;
//comparisons for y,z also and comparing to maxx, maxy, maxz
Vertex* point = new Vertex();
point->Point[0] = x;
point->Point[1] = y;
point->Point[2] = z;
point->Normal[0] = nx; //also assignments for ny, nz
file<<"xyz normals: "<<point->Point[0]<<endl;
//I also printed out y,z,nx,ny,nz
boundary->addPoint(point);
}
for (i = 0 ; i < boundary->numbtris; i++)
{
ii = geo->m_Tris[i*3+0]; //assignments for jj, kk also
if (ii < jj && jj < kk) { i1 = ii; i2 = jj; i3 = kk; }
//similar comparisons for jj and kk also here, but I want to save space
//...
if (kk < ii && ii < jj) { i1 = kk; i2 = ii; i3 = jj; } // result in i1 <= i2 <= i3
Face* facet = new Face();
facet->Index[0] = i1; //i2, i3 are also assigned
facet->IndexInR[0] = ii; //jj, kk also
boundary->addFacet(facet);
} /* end facet (i) loop */
for (i = 0 ; i <boundary->numbtris; i++)
{
for(int j=0;j<3;j++)
{
int index = boundary->m_Faces[i]->Index[j];
for(int k=0;k<3;k++)
{
file<<boundary->m_Faces[i]->Normal[k]<<" "<<boundary->m_Vertices[index]->Normal[k]<<endl;
boundary->m_Faces[i]->Normal[k] += boundary->m_Vertices[index]->Normal[k];
//ERROR IS HERE
file<<boundary->m_Faces[i]->Normal[k]<<endl;
}
}
}
return boundary;
}
and PosLin.h has
#include "TPieces/tpieces.h"
#include "TPieces/geoPar.h"
#include "Geo/Geo.h"
struct PosRotAndQ {
TPiecesNS::TPieces* boundary;
};
class PS{
public:
PosExCode computation(Geo* geo, POpinion* opinion, PositionRotation* matterboundary)
PositionRotation* matterboundary;
}
and PosLin.cpp has
#include "tpieces/tpieces.h"
#include "Geo/Geo.h"
PosExCode PS::computation(Geo* geo, POpinion* opinion, PositionRotation* matterboundary)
{
TPiecesNS::GeoPar* perform = new TPiecesNS::GeoPar();
TPiecesNS::TPieces* boundary = new TPiecesNS::Sphere();
boundary->sphere = perform->SurTriAuto(boundary, geo,0);//if I comment this line out and the line below and un-comment the 2 getSphere lines below, they do not produce the same output
boundary->sphereDark[0] = perform->SurTriAuto(boundary, geo,0); \
//boundary->sphere = perform->getSphere(geo,0);
//boundary->sphereDark[0] = perform->getSphere(geo,0);
}
I noticed that the getSphere and SurTriAuto get different outputs, specifically at the line surface->m_Faces[i]->Normal[k] +=
surface->m_Vertices[index]->Normal[k];
In the outputted textfile, before the += operation takes place, the values surface->m_Faces[i]->Normal[k] and surface->m_Vertices[index]->Normal[k] and are not the same for getSphere and SurTriAuto, even though all the other values (such as the x,y,z,index values) are the same.
I suspect this is because one of the boundary pointers loses values in TPieces* boundary = new Sphere(); in getSphere in GeoPar.cpp and/or TPiecesNS::TPieces* boundary = new TPiecesNS::Sphere(); in PosLin.cpp

In one case you have both functions using the same boundary object. In the other case each function uses a fresh boundary object.
You haven't shown what the TPieces class does, but I assume that addPoint and addFacet change the contents of the TPieces class, such that when you write the faces to the file in the second call, you end up getting the faces that were saved in the first call.
To make the two cases work the same, try using a different boundary object in the second call. Something like this:
TPiecesNS::GeoPar* perform = new TPiecesNS::GeoPar();
TPiecesNS::TPieces* boundary = new TPiecesNS::Sphere();
boundary->sphere = perform->SurTriAuto(boundary, geo,0);//if I comment this line out and the line below and un-comment the 2 getSphere lines below, they do not produce the same output
TPiecesNS::TPieces* boundary2 = new TPiecesNS::Sphere();
boundary->sphereDark[0] = perform->SurTriAuto(boundary2, geo,0);
//boundary->sphere = perform->getSphere(geo,0);
//boundary->sphereDark[0] = perform->getSphere(geo,0);

Related

Error C2678 C++ A* Pathfinding

I'm currently working on implementing the A* pathfinding algorithm in C++. I tried to run my code to see if the display grid function was working but got the C2678 error: binary '<': no operator found which takes a left-hand operand of type 'const Coord' (or there is no acceptable conversion).
I know that my program is messy and probably not efficient at all however i was trying to get a basic version working before optimising. Is the error because I'm trying to output a boolean value of a Coord structure?
Code:
#include <iostream>
#include <fstream>
#include <chrono>
#include <thread>
#include <vector>
#include <set>
using std::chrono::milliseconds;
using std::chrono::duration_cast;
using std::this_thread::sleep_for;
typedef std::chrono::steady_clock the_clock;
struct Location {
int g = 0; // Distance covered so far
int h = 0; // Estimate of distance to goal
float f = 0; // Estimated cost of the complete path
bool walkable = 0; // 0 = Walkable, 1 = Wall
};
// Structure
struct Coord {
int x;
int y;
Location location;
};
// Declare size of grid
#define WIDTH 10
#define HEIGHT 10
typedef Location Array[HEIGHT][WIDTH];
Location grid[HEIGHT][WIDTH]; // Create an array of locations
void displayGrid() {
/* Displays the Grid to the console! */
system("CLS");
for (int y = 0; y < HEIGHT; y++) {
for (int x = 0; x < WIDTH; x++) {
std::cout << grid[y][x].walkable;
}
std::cout << "\n";
}
sleep_for(milliseconds(100)); // Visual delay
}
void initialiseGrid() {
/* Fills the Grid array with values */
srand((unsigned)time(0));
for (int y = 0; y < HEIGHT; y++) {
for (int x = 0; x < WIDTH; x++) {
grid[y][x].walkable = 0;
}
}
/* Test grid */
grid[4][2].walkable = 1;
grid[5][2].walkable = 1;
grid[4][3].walkable = 1;
grid[5][3].walkable = 1;
grid[4][5].walkable = 1;
grid[5][5].walkable = 1;
grid[4][6].walkable = 1;
grid[5][6].walkable = 1;
}
void Astar(Coord startPoint, Coord endPoint) {
/**/
std::set<Coord> closedSet = {}; // Nodes that do not have to be considered again
std::set<Coord> openSet = {}; // Nodes still to be considered to find the shortest path
Coord currentNode; // Current node
currentNode.x = startPoint.x;
currentNode.y = startPoint.y;
currentNode.location.g = 0; // 0 Distance from starting point
openSet.insert(currentNode); // Insert starting node
while (openSet.empty() == false) { // Loop while open list is not empty
for (std::set<Coord>::iterator it = openSet.begin(); it != openSet.end(); it++) { // Iterate through each element in the open set to find the lowest F value
if ((*it).location.f < currentNode.location.f) { // Check if iterator f value is smaller than the current value
currentNode = *it; // Update the current node
}
}
openSet.erase(currentNode); // Drop from the open set since been checked
closedSet.insert(currentNode); // Add to the closed set
}
}
int main(int argc, char *argv[]) {
// Set start and end points
Coord start;
start.x = 3;
start.y = 3;
Coord end;
end.x = 5;
end.y = 6;
initialiseGrid(); // Put -1 (empty) in
// Start timing
the_clock::time_point startTime = the_clock::now();
// Stop timing
the_clock::time_point endTime = the_clock::now();
// Compute the difference between the two times in milliseconds
auto time_taken = duration_cast<milliseconds>(endTime - startTime).count();
displayGrid();
std::cout << "That took: " << time_taken << " ms" << std::endl;
return 0;
}
The easiest way to solve the issue with std::set requiring a strict-weak-ordering and your Coord class is to provide an operator < comparing the x and y values in Coord, and returning whether one Coord is less than another Coord using these values.
You can do this with std::tie
#include <tuple>
//...
struct Coord {
int x;
int y;
Location location;
bool operator <(const Coord& c) const
// returns true if this->x and this->y < c.x and c.y, false otherwise
{ return std::tie(x,y) < std::tie(c.x,c.y); }
};
The std::tie compares the x components, then if equal, compares the y components. The result of the comparison is returned (either true if the first set of x,y components is less than the second set of x,y components, or false otherwise).
Live Example here

OpenMP generate segfault in Rcpp code for the SEIR model

I wrote a (probably-inefficient, but anyway..) Rcpp code using inline to simulate a stochastic SEIR model.
The serial version compiles and works perfectly, but since I need to simulate from it a large number of times and since it seems to me like an embarrassingly parallel problem (just need to simulate again for other parameter values and return a matrix with the results) I tried to add #pragma omp parallel for and to compile with -fopenmp -lgomp but ... boom!
I get a segfault even for very small examples!
I tried to add setenv("OMP_STACKSIZE","24M",1); and values well over 24M but still the segfault happens.
I'll explain briefly the code since it's a bit long (I tried to shorten it but the result change and I can't reproduce it..):
I have two nested loops, the inner one execute the model for a given parameter set and the outer one changes the parameters.
The only reason a race condition might happen is if the code were trying to execute set of instructions inside inner the loop in parallel (which cannot be done because of the model structure, on iteration t it depends on iteration t-1) and not to parallelize the outer, but if I'm not mistaken that is what the parallel for constructor does for default if put just outside the outer...
This is basically the form of the code I'm trying to run:
mat result(n_param,T_MAX);
#pragma omp parallel for
for(int i=0,i<n_param_set;i++){
t=0;
rowvec jnk(T_MAX);
while(t < T_MAX){
...
jnk(t) = something(jnk(t-1));
...
t++;
}
result.row(i)=jnk;
}
return wrap(result);
And my question is: How I tell the compiler that I just want to compute in parallel the outer loop (even distributing them statically like n_loops/n_threads for each thread) and not the inner one (which is actually non-parallelizable)?
The real code is a bit more involved and I'll present it here for the sake of reproducibility if you're really willing, but I'm only asking about the behavior of OpenMP. Please notice that the only OpenMP instruction appears at line 122.
library(Rcpp);library(RcppArmadillo);library(inline)
misc='
#include <math.h>
#define _USE_MATH_DEFINES
#include <omp.h>
using namespace arma;
template <typename T> int sgn(T val) {
return (T(0) < val) - (val < T(0));
}
uvec rmultinomial(int n,vec prob)
{
int K = prob.n_elem;
uvec rN = zeros<uvec>(K);
double p_tot = sum(prob);
double pp;
for(int k = 0; k < K-1; k++) {
if(prob(k)>0) {
pp = prob[k] / p_tot;
rN(k) = ((pp < 1.) ? (rbinom(1,(double) n, pp))(0) : n);
n -= rN[k];
} else
rN[k] = 0;
if(n <= 0) /* we have all*/
return rN;
p_tot -= prob[k]; /* i.e. = sum(prob[(k+1):K]) */
}
rN[K-1] = n;
return rN;
}
'
model_and_summary='
mat SEIR_sim_plus_summaries()
{
vec alpha;
alpha << 0.002 << 0.0045;
vec beta;
beta << 0.01 << 0.01;
vec gamma;
gamma << 1.0/14.0 << 1.0/14.0;
vec sigma;
sigma << 1.0/(3.5) << 1.0/(3.5);
vec phi;
phi << 0.8 << 0.8;
int S_0 = 800;
int E_0 = 100;
int I_0 = 100;
int R_0 = 0;
int pop = 1000;
double tau = 0.01;
double t_0 = 0;
vec obs_time;
obs_time << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10 << 11 << 12 << 13 << 14 << 15 << 16 << 17 << 18 << 19 << 20 << 21 << 22 << 23 << 24;
const int n_obs = obs_time.n_elem;
const int n_part = alpha.n_elem;
mat stat(n_part,6);
//#pragma omp parallel for
for(int k=0;k<n_part;k++) {
ivec INC_i(n_obs);
ivec INC_o(n_obs);
// Event variables
double alpha_t;
int nX; //current number of people moving
vec rates(8);
uvec trans(4); // current transitions, e.g. from S to E,I,R,Universe
vec r(4); // rates e.g. from S to E, I, R, Univ.
/*********************** Initialize **********************/
int S_curr = S_0;
int S_prev = S_0;
int E_curr = E_0;
int E_prev = E_0;
int I_curr = I_0;
int I_prev = I_0;
int R_curr = R_0;
int R_prev = R_0;
int IncI_curr = 0;
int IncI_prev = 0;
int IncO_curr = 0;
int IncO_prev = 0;
double t_curr = t_0;
int t_idx =0;
while( t_idx < n_obs ) {
// next time preparation
t_curr += tau;
S_prev = S_curr;
E_prev = E_curr;
I_prev = I_curr;
R_prev = R_curr;
IncI_prev = IncI_curr;
IncO_prev = IncO_curr;
/*********************** description (rates) of the events **********************/
alpha_t = alpha(k)*(1+phi(k)*sin(2*M_PI*(t_curr+0)/52)); //real contact rate, time expressed in weeks
rates(0) = (alpha_t * ((double)I_curr / (double)pop ) * ((double)S_curr)); //e+1, s-1, r,i one s get infected (goes in E, not yey infectous)
rates(1) = (sigma(k) * E_curr); //e-1, i+1, r,s one exposed become infectous (goes in I) INCIDENCE!!
rates(2) = (gamma(k) * I_curr); //i-1, s,e, r+1 one i recover
rates(3) = (beta(k) * I_curr); //i-1, s, r,e one i dies
rates(4) = (beta(k) * R_curr); //i,e, s, r-1 one r dies
rates(5) = (beta(k) * E_curr); //e-1, s, r,i one e dies
rates(6) = (beta(k) * S_curr); //s-1 e, i ,r one s dies
rates(7) = (beta(k) * pop); //s+1 one susc is born
// Let the events occour
/*********************** S compartement **********************/
if((rates(0)+rates(6))>0){
nX = rbinom(1,S_prev,1-exp(-(rates(0)+rates(6))*tau))(0);
r(0) = rates(0)/(rates(0)+rates(6)); r(1) = 0.0; r(2) = 0; r(3) = rates(6)/(rates(0)+rates(6));
trans = rmultinomial(nX, r);
S_curr -= nX;
E_curr += trans(0);
I_curr += trans(1);
R_curr += trans(2);
//trans(3) contains dead individual, who disappear...we could avoid this using sequential conditional binomial
}
/*********************** E compartement **********************/
if((rates(1)+rates(5))>0){
nX = rbinom(1,E_prev,1-exp(-(rates(1)+rates(5))*tau))(0);
r(0) = 0.0; r(1) = rates(1)/(rates(1)+rates(5)); r(2) = 0.0; r(3) = rates(5)/(rates(1)+rates(5));
trans = rmultinomial(nX, r);
S_curr += trans(0);
E_curr -= nX;
I_curr += trans(1);
R_curr += trans(2);
IncI_curr += trans(1);
}
/*********************** I compartement **********************/
if((rates(2)+rates(3))>0){
nX = rbinom(1,I_prev,1-exp(-(rates(2)+rates(3))*tau))(0);
r(0) = 0.0; r(1) = 0.0; r(2) = rates(2)/(rates(2)+rates(3)); r(3) = rates(3)/(rates(2)+rates(3));
trans = rmultinomial(nX, r);
S_curr += trans(0);
E_curr += trans(1);
I_curr -= nX;
R_curr += trans(2);
IncO_curr += trans(2);
}
/*********************** R compartement **********************/
if(rates(4)>0){
nX = rbinom(1,R_prev,1-exp(-rates(4)*tau))(0);
r(0) = 0.0; r(1) = 0.0; r(2) = 0.0; r(3) = rates(4)/rates(4);
trans = rmultinomial(nX, r);
S_curr += trans(0);
E_curr += trans(1);
I_curr += trans(2);
R_curr -= nX;
}
/*********************** Universe **********************/
S_curr += pop - (S_curr+E_curr+I_curr+R_curr); //it should be poisson, but since the pop is fixed...
/*********************** Save & Continue **********************/
// Check if the time is interesting for us
if(t_curr > obs_time[t_idx]){
INC_i(t_idx) = IncI_curr;
INC_o(t_idx) = IncO_curr;
IncI_curr = IncI_prev = 0;
IncO_curr = IncO_prev = 0;
t_idx++;
}
//else just go on...
}
/*********************** Finished - Starting w/ stats **********************/
// INC_i is the useful variable, how can I change its reference withour copying it?
ivec incidence = INC_i; //just so if I want to use INC_o i have to change just this...
//Scan the epidemics to recover the summary stats (naively divide the data each 52 weeks)
double n_years = ceil((double)obs_time(n_obs-1)/52.0);
vec mu_attack(n_years);
vec ratio_attack(n_years-1);
vec peak(n_years);
vec atk(52);
peak(0)=0.0;
vec tmpExplo(52); //explosiveness
vec explo(n_years);
int year=0;
int week;
for(week=0 ; week<n_obs ; week++){
if(week - 52*year > 51){
mu_attack(year) = sum( atk )/(double)pop;
if(year>0)
ratio_attack(year-1) = mu_attack(year)/mu_attack(year-1);
for(int i=0;i<52;i++){
if(atk(i)>(peak(year)/2.0)){
tmpExplo(i) = 1.0;
} else {
tmpExplo(i) = 0.0;
}
}
explo(year) = sum(tmpExplo);
year++;
peak(year)=0.0;
}
atk(week-52*year) = incidence(week);
if( peak(year) < incidence(week) )
peak(year)=incidence(week);
}
if(week - 52*year > 51){
mu_attack(year) = sum( atk )/(double)pop;
} else {
ivec idx(52);
for(int i=0;i<52;i++)
{ idx(i) = i; } //take just the updated ones...
vec tmp = atk.elem(find(idx<(week - 52*year)));
mu_attack(year) = sum( tmp )/((double)pop * (tmp.n_elem/52.0));
ratio_attack(year-1) = mu_attack(year)/mu_attack(year-1);
for(int i=0;i<tmp.n_elem;i++){
if(tmp(i)>(peak(year)/2.0)){
tmpExplo(i) = 1.0;
} else {
tmpExplo(i) = 0.0;
}
}
for(int i=tmp.n_elem;i<52;i++)
tmpExplo(i) = 0.0; //to reset the others
explo(year) = sum(tmpExplo);
}
double correlation2;
double correlation4;
vec autocorr = acf(peak);
/***** ACF *****/
if(n_years<3){
correlation2=0.0;
correlation4=0.0;
} else {
if(n_years<5){
correlation2 = autocorr(1);
correlation4 = 0.0;
} else {
correlation2 = autocorr(1);
correlation4 = autocorr(3);
}
}
rowvec jnk(6);
jnk << sum(mu_attack)/(year+1.0)
<< (sum( log(ratio_attack)%log(ratio_attack) )/(n_years-1)) - (pow(sum( log(ratio_attack) )/(n_years-1),2))
<< correlation2 << correlation4 << max(peak) << sum(explo)/n_years;
stat.row(k) = jnk;
}
return stat;
}
'
main='
std::cout << "max_num_threads " << omp_get_max_threads() << std::endl;
RNGScope scope;
mat summaries = SEIR_sim_plus_summaries();
return wrap(summaries);
'
plug = getPlugin("RcppArmadillo")
## modify the plugin for Rcpp to support OpenMP
plug$env$PKG_CXXFLAGS <- paste('-fopenmp', plug$env$PKG_CXXFLAGS)
plug$env$PKG_LIBS <- paste('-fopenmp -lgomp', plug$env$PKG_LIBS)
SEIR_sim_summary = cxxfunction(sig=signature(),main,settings=plug,inc = paste(misc,model_and_summary),verbose=TRUE)
SEIR_sim_summary()
Thanks for the help!
NB: before you ask, I slightly modified the Rcpp multinomial sampling function just because I liked that way more than the one using pointer...not any other particular reason! :)
The core pseudo-random number generators (PRNGs) in R are not designed to be used in multithreaded environments. That is, their state is stored in a static array (dummy from src/main/PRNG.c) and therefore is shared among all threads. Moreover several other static structures are used to store states for the higher-level interfaces to the core PRNGs.
A possible solution could be that you put each call to rnorm() or other sampling functions inside named critical sections with all having the same name, e.g.:
...
#pragma omp critical(random)
rN(k) = ((pp < 1.) ? (rbinom(1,(double) n, pp))(0) : n);
...
if((rates(0)+rates(6))>0){
#pragma omp critical(random)
nX = rbinom(1,S_prev,1-exp(-(rates(0)+rates(6))*tau))(0);
...
Note that the critical construct operates on the structured block following it and therefore locks the entire statement. If a random number is being drawn inline inside a call to a time-consuming function, e.g.
#pragma omp critical(random)
x = slow_computation(rbinom(...));
this is better transformed to:
#pragma omp critical(random)
rb = rbinom(...);
x = slow_computation(rb);
That way only the rb = rbinom(...); statement will be protected.

array "not used in this scope". Why?

I'm getting an error saying that "adjacencymatrix' was not used in this scope" right at the end of main (before the function makebond at the end) (the commented line 112 "BROKEN LINE"). Why? Sorry about this being simple. I'm compiling with g++ ($ g++ a.c -o f).
Heres the code:
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
using namespace std;
#define PI 3.1415926535897932384626433832795
#define sqr(x) ((x)*(x))
#define count 500
double density;
double volume;
int N;
double beta = 0.1;
double R = 5;
double rob = 1;
int dimension = 2;
double eps=0.1; // Increase in density
double mindensity = 0; // Minimum density
double maxdensity = 8; // max.dens (scaled for the sake of ensuring int()
int makebond(double x);
int main(){
srand(time(0));
for (int rho=mindensity;rho<=(maxdensity/eps);density++){
N = floor(density*volume);
double nodepositions[N][dimension];
// Place nodes in volume (square side L, circle volume *R and obstacle *rob)
for (int i=0;i<N;i++){
int L = 5;
double distancefromorigin;
double x = (L*(rand()/RAND_MAX))-(L/2);
double y = (L*(rand()/RAND_MAX))-(L/2);
distancefromorigin = sqrt((x*x)+(y*y));
if(distancefromorigin<R){
if(distancefromorigin>rob){
nodepositions[i][0] = x;
nodepositions[i][1] = y;
}
}
}
double adjacencymatrix [N][N];
double itzhak; //distance of node 1 from the centre
double isaac; //distance of node 2 from the centre
double vivaldi; //distance between node 1 and node 2
double phi; // a function of the above 3 doubles (see later usage)
double rubicon; // maximum distance nodes within the icecream can be apart before becoming visually indepdendent
double maxtheta; // "in the icecream" means theta < maxtheta
double theta; // angular displacement of inner point from the line bisecting the icecream
// Create adjacency matrix (note alternative implementation using incidence lists)
for (int i=0;i<N;i++){
for (int j=0;j<N;j++){
double x0 = nodepositions[i][0];
double y0 = nodepositions[i][1];
double x1 = nodepositions[j][0];
double y1 = nodepositions[j][1];
itzhak = sqrt(sqr(x0) + sqr(y0));
isaac = sqrt(sqr(x1) + sqr(y1));
vivaldi = sqrt(sqr(x0-x1) + sqr(y0-y1));
phi = ((sqr(vivaldi)+sqr(itzhak)-sqr(isaac))/(2*vivaldi*itzhak));
rubicon = ((itzhak*phi) - sqrt((sqr(rob)) - ((sqr(itzhak))*(1-sqr(phi)))));
maxtheta = asin(rob/itzhak);
theta = acos(phi);
if (x0==x1 && y0==y1){
adjacencymatrix[i][j] = 0;
}
else{
if (isaac<itzhak && theta<maxtheta) {
if (vivaldi>rubicon){
adjacencymatrix[i][j] = 0;}
else {
adjacencymatrix[i][j] = makebond(vivaldi);}
}
else{adjacencymatrix[i][j] = makebond(vivaldi);}
}
}
}
}
FILE *datafc1;
datafc1 = fopen("matrix.dat", "w");
for (int ii = 0; ii<N; ii++){
for (int jj = 0; jj<N; jj++){
int aaa;
aaa = adjacencymatrix[ii][jj];///////////////*******BROKEN LINE******
fprintf(datafc1,"%i", aaa);
}
}
fclose(datafc1);
return 0;
}
/////////////////////////////
////////////////
/////// --End Main--
////////////////
////////////////////////////
int makebond(double x){
// This function takes in the euc. dist. between two nodes and draws a link with prob. H(r)
double randomnumber = (rand()/RAND_MAX); // Random number between 0 and 1
double hr = exp(-beta*sqr(x));// ***Connection function***
int a = 1; // Number to be put into adjacency matrix
if (randomnumber > hr){
a = 0;
}
return a; //Returns 0 or 1 depending on prob. dist.
}
adjacencymatrix is declared in your first for loop, so it's out of scope before the last spot you're using it, in the print-out loop at the bottom.
In addition, you have a useless using namespace std; line. Your code doesn't include any headers that contain std namespace symbols.
Your code in line 57:
double adjacencymatrix [N][N];
is inside a for loop, outside that loop, adjacencymatrix is undefined.
You matrix is defined in the for loop on line 11. Therefore it is out of scope on line 112.
FILE *datafc1;
datafc1 = fopen("matrix.dat", "w");
for (int ii = 0; ii<N; ii++){
for (int jj = 0; jj<N; jj++){
int aaa;
//error adjacencymatrix is declared in your first for loop
aaa = adjacencymatrix[ii][jj];///////////////*******BROKEN LINE******
fprintf(datafc1,"%i", aaa);
}
}

Thread 1: EXC_Bad_Access (Code=1, address=0x7fff55056148) While Trying to Use Ofstream and an integer data type named count

I am currently working to solve Project Euler's Problem #60: http://projecteuler.net/problem=60 (Just in case if you want to try and follow my logic).
The issue is that after I build my code (Which it completes without errors) and then run it, I get the error code "Thread 1: EXC_Bad_Access (Code=1, address=0x7fff55056148)" from the IDE I was using while running it (The IDE's built in debugger I think). More Specifically the error occurs only within my "Combinations" Function. The lines that get highlighted are disabled with "//" comment lines within my combinations function. Thus, currently, my code will run without any errors because all the error-causing lines are disabled as comments. if you de-comment any of those lines or any combination of those lines thereof, the code runs into the same error code listed above.
Personal Comments from Experimentation:
What I found was that any line that has something to do with either ofstream or the integer that I initialized called count causes the error. ofstream kind of makes sense, but even after disabling all lines of code related to ofstream, suddenly the integer count starts creating the error.
Any help would be much appreciated! I am still a beginner with C++, (started about two to three weeks ago.)
#include <iostream>
#include <cmath>
#include <fstream>
using namespace std;
/* double x = 2 , y = 2 , b = 3, s = 2; */
/* int z, c = 1, v = 3000; */
int AllPrimes[3000];
/* int AllCombos[2018257871250650][5]; */ // disabled this line for now.
//Used to be within Combinations; Moved here to circumvent "Bad Access" Error
int FindPrimes();
int TestforPrime(double y);
int Combinations();
int WriteArrayToFile(int *ArrayPointer,int ArrayLength, string FileName, char Append);
int main()
{
cout<<FindPrimes();
cout<<Combinations();
}
int Combinations() {
int i1, i2, i3, i4, i5, /* ai */ bi, ci, di, ei;
int ZeroPointBreaker=0;
//ofstream BufferFlushed ("/Users/Yash/Xcode/Projects/Project Euler Programs/Project Euler Problem 60 (Weird Prime Cocatenate Property Problem)/I:O Files/");
int count=0;
int Buffer[9000000][5];
for (i1=0; i1<2996; i1++) {
count++;
// cout<<"Index 1 Iteration: "<<i1<<" || Count Value: "<<count<<"\n";
bi = i1 + 1;
for (i2=bi; i2<2997; i2++) {
count++;
// cout<<"Index 2 Iteration: "<<i2<<" || Count Value: "<<count<<"\n";
ci = i2+ 1;
for (i3=ci; i3<2998; i3++) {
count++;
di = i3 + 1;
for (i4=di; i4<2999; i4++) {
count++;
ei = i4 + 1;
for (i5=ei; i5<3000; i5++) {
count++;
// Buffer[count][0]=AllPrimes[i1];
// Buffer[count][1]=AllPrimes[i2];
// Buffer[count][2]=AllPrimes[i3];
// Buffer[count][3]=AllPrimes[i4];
// Buffer[count][4]=AllPrimes[i5];
}
}
}
//Flush Here
// count=0;
/* for (int i=0; i<9000000; i++) {
if (Buffer[i][1]==0) {ZeroPointBreaker=i; break;}
} */
// for (int i=0; i<ZeroPointBreaker; i++) {
// BufferFlushed<<Buffer[i][1]<<','<<Buffer[i][2]<<','<<Buffer[i][3]<<','<<Buffer[i][4]<<','<<Buffer[i][5]<<'\n';
// }
}
}
//End of Code Statements
//BufferFlushed.close();
return 0;
}
int FindPrimes() {
cout.precision(0);
AllPrimes[0]=2;
double b = 3, s = 2;
int z, c = 1, v = 3000;
while ( c != v ) {
z = TestforPrime(b);
if ( z == 1 ) {
AllPrimes[c]=b;
c = c + 1;
s = s + b;
if ( c == v ) {
cout<<fixed<<" Prime="<<b<<" Count="<<c<<" "<<"Sum="<<s<<"\n";
int success = WriteArrayToFile(AllPrimes,3000,"/Users/Yash/Xcode/Projects/Project Euler Programs/Project Euler Problem 60 (Weird Prime Cocatenate Property Problem)/I:O Files/AllPrimes.txt",'n');
cout<<"\n Write Success (0=Successful): "<<success<<"\n";
if (success == 0) {return 0;}
else {return 1;}
}
else {
};
}
else {
};
b = b + 2;
}
}
int WriteArrayToFile(int *ArrayPointer,int ArrayLength, string FileName, char Append) {
if (Append == 'y') {
ofstream OutputFile (FileName, ios::app);
for ( unsigned long long i1=0 ; i1 < ArrayLength ; i1++) {
OutputFile<<ArrayPointer[i1]<<"\n";
}
OutputFile.close();
return 0;}
else if (Append == 'n') {
ofstream OutputFile (FileName);
for ( unsigned long long i1=0 ; i1 < ArrayLength ; i1++) {
OutputFile<<ArrayPointer[i1]<<"\n";
}
OutputFile.close();
return 0;}
}
int TestforPrime (double y) {
double x = 2;
while ( x <= y ) {
if ( (( y / x ) - int( y / x )) == 0 ) {
if ( y == x ) {
return 1;
}
else {
return 0;
}
}
x = x + 1;
}
}
This variable:
int Buffer[9000000][5];
takes up 45000000 * 4 Bytes. That's 180MB. You can't fit that on the stack. Use a global variable or dynamic allocation (or, more likely, another solution - I haven't looked at the problem itself, so don't know if your solution is "right").

structs within structs, dynamic memory allocation

I am making a 3D application where a boat has to drive through buoy tracks. I also need to store the tracks in groups or "layouts". The buoys class is basically a list of "buoy layouts" inside of which is a list of "buoy tracks", inside of which is a list of buoys.
I checked the local variable watcher and all memory allocations in the constructor appear to work. Later when the calculateCoordinates function is called it enters a for loop. On the first iteration of the for loop the functions pointer is used and works fine, but then on this line
ctMain[j+1][1] = 0;
the function pointers are set to NULL. I am guessing it has something to with the structs not being allocated or addressed correctly. I am not sure what to do from here. Maybe I am not understanding how malloc is working.
Update
I replaced the M3DVector3d main_track with double ** main_track, thinking maybe malloc is not handling the typedefs correctly. But I am getting the same error when trying to access the main_track variable later in calculateCoordinates.
Update
It ended up being memory corruption caused by accessing a pointer wrong in the line
rotatePointD(&(cTrack->main_track[j]), rotation);
It only led to an error later when I tried to access it.
// Buoys.h
////////////////////////////////////////////
struct buoy_layout_t;
struct buoy_track_t;
typedef double M3DVector3d[3];
class Buoys {
public:
Buoys();
struct buoy_layout_t ** buoyLayouts;
int nTrackLayouts;
int currentLayoutID;
void calculateCoordinates();
};
struct buoy_track_t {
int nMain, nYellow, nDistract;
M3DVector3d * main_track,
yellow_buoys,
distraction_buoys;
double (*f)(double x);
double (*fp)(double x);
double thickness;
M3DVector3d start, end;
};
struct buoy_layout_t {
int nTracks;
buoy_track_t ** tracks;
};
// Buoys.cpp
/////////////////////////////
// polynomial and its derivative, for shape of track
double buoyfun1(double x) {return (1.0/292.0)*x*(x-12.0)*(x-24.0);}
double buoyfun1d(double x) {return (1.0/292.0)*((3.0*pow(x,2))-(72.0*x)+288.0);}
// ... rest of buoy shape functions go here ...
Buoys::Buoys() {
struct buoy_layout_t * cLayout;
struct buoy_track_t * cTrack;
nTrackLayouts = 1;
buoyLayouts = (buoy_layout_t **) malloc(nTrackLayouts*sizeof(*buoyLayouts));
for (int i = 0; i < nTrackLayouts; i++) {
buoyLayouts[i] = (buoy_layout_t *) malloc(sizeof(*(buoyLayouts[0])));
}
currentLayoutID = 0;
// ** Layout 1 **
cLayout = buoyLayouts[0];
cLayout->nTracks = 1;
cLayout->tracks = (buoy_track_t **) malloc(sizeof(*(cLayout->tracks)));
for (int i = 0; i < 1; i++) {
cLayout->tracks[i] = (buoy_track_t *) malloc (sizeof(*(cLayout->tracks)));
}
cTrack = cLayout->tracks[0];
cTrack->main_track = (M3DVector3d *) malloc(30*sizeof(*(cTrack->main_track)));
cTrack->nMain = 30;
cTrack->f = buoyfun1;
cTrack->fp = buoyfun1d;
cTrack->thickness = 5.5;
cTrack->start[0] = 0; cTrack->start[1] = 0; cTrack->start[2] = 0;
cTrack->end[0] = 30; cTrack->end[1] = 0; cTrack->end[2] = -19;
// ... initialize rest of layouts here ...
// ** Layout 2 **
// ** Layout 3 **
// ...
// ** Layout N **
calculateCoordinates();
}
void Buoys::calculateCoordinates()
{
int i, j;
buoy_layout_t * cLayout = buoyLayouts[0];
for (i = 0; i < (cLayout->nTracks); i++) {
buoy_track_t * cTrack = cLayout->tracks[i];
M3DVector3d * ctMain = cTrack->main_track;
double thickness = cTrack->thickness;
double rotation = getAngleD(cTrack->start[0], cTrack->start[2],
cTrack->end[0], cTrack->end[2]);
double full_disp = sqrt(pow((cTrack->end[0] - cTrack->start[0]), 2)
+ pow((cTrack->end[2] - cTrack->start[2]), 2));
// nBuoys is nBuoys per side. So one side has nBuoys/2 buoys.
for (j=0; j < cTrack->nMain; j+=2) {
double id = j*((full_disp)/(cTrack->nMain));
double y = (*(cTrack->f))(id);
double yp = (*(cTrack->fp))(id);
double normal, normal_a;
if (yp!=0) {
normal = -1.0/yp;
}
else {
normal = 999999999;
}
if (normal > 0) {
normal_a = atan(normal);
}
else {
normal_a = atan(normal) + PI;
}
ctMain[j][0] = id + ((thickness/2.0)*cos(normal_a));
ctMain[j][1] = 0;
ctMain[j][2] = y + ((thickness/2.0)*sin(normal_a));
ctMain[j+1][0] = id + ((thickness/2.0)*cos(normal_a+PI));
ctMain[j+1][1] = 0; // function pointers get set to null here
ctMain[j+1][2] = y + ((thickness/2.0)*sin(normal_a+PI));
}
for (j=0; j < cTrack->nMain; j++) {
rotatePointD(&(cTrack->main_track[j]), rotation);
}
}
}
Unless there are requirements for learning pointers or you cannot use STL, given you are using C++ I'd strongly recommend you use more STL, it is your friend. But anyways...
First, the type of ctMain is *M3DVector3D. So you can safely access ctMain[0], but you cannot access ctMain[1], maybe you meant for the type of ctMain to be **M3DVector3D, in which case the line for initialization you had written which is:
cTrack->main_track = (M3DVector3d *) malloc(30*sizeof(*(cTrack->main_track)));
would make sense.
More Notes
Why are you allocating 30 of these here?
cTrack->main_track = (M3DVector3d *) malloc(30*sizeof(*(cTrack->main_track)));
Given the type of main_track, you only need:
cTrack->main_track = (M3DVector3d *) malloc(sizeof(M3DVector3d));
In addition, for organizational purposes, when doing sizeof you may want to give the actual type to check the sizeof, as opposed to the variable (there should be no difference, just organizational), these two changes:
buoyLayouts = (buoy_layout_t **) malloc(nTrackLayouts*sizeof(buoy_layout_t*));
for (int i = 0; i < nTrackLayouts; i++) {
buoyLayouts[i] = (buoy_layout_t *) malloc(sizeof(buoy_layout_t));
}
cLayout->tracks = (buoy_track_t **) malloc(clayout->nTracks * sizeof(buoy_track_t*));
for (int i = 0; i < 1; i++) {
cLayout->tracks[i] = (buoy_track_t *) malloc(sizeof(buoy_track_t));
}