Skip to content

Commit

Permalink
fixed memory leak ... problem with coeffs in tree
Browse files Browse the repository at this point in the history
  • Loading branch information
wsttiger committed Apr 22, 2016
1 parent ce66219 commit a3b6460
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 42 deletions.
6 changes: 4 additions & 2 deletions Matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class VectorT {

VectorT(int k) {
create(k);
for (auto i = 0; i < v.size(); i++) _p[i] = T(0);
for (auto i = 0; i < k; i++) _p[i] = T(0);
}

void create(int d0) {
Expand Down Expand Up @@ -509,7 +509,9 @@ class MatrixT {
// multiply with a VectorT
VectorT<T> operator* (const VectorT<T>& v) const {
int vsz = v.size();
VectorT<T> r(vsz);
// VectorT<T> r(vsz);
VectorT<T> r;
r.create(vsz);
assert(vsz == _dim1);
for (int i = 0; i < _dim0; i++) {
T s = T(0);
Expand Down
33 changes: 21 additions & 12 deletions function1d.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ double normf(Vector v) {

class Function1D {
private:
bool debug = true;
int k = 8;
double thresh = 1e-6;
int maxlevel = 30;
int initiallevel = 2;
int initiallevel = 4;
std::map<Key,Vector> tree;
Vector quad_x;
Vector quad_w;
Expand All @@ -34,7 +35,7 @@ class Function1D {
init_quadrature(k);
}

Function1D(double (*f) (double), int k, double thresh, int maxlevel = 30, int initiallevel = 2)
Function1D(double (*f) (double), int k, double thresh, int maxlevel = 30, int initiallevel = 4)
: k(k), thresh(thresh), maxlevel(maxlevel), initiallevel(initiallevel) {
init_twoscale(k);
init_quadrature(k);
Expand Down Expand Up @@ -82,23 +83,24 @@ class Function1D {
}

void refine(double (*f)(double), int n, int l) {
printf("\nrefine---> n: %d l: %d\n", n, l);
if (debug) printf("\nrefine---> n: %d l: %d\n", n, l);
Vector s0 = project_box(f, n+1, 2*l);
printf(" computed s0\n");
if (debug) printf(" computed s0\n");
Vector s1 = project_box(f, n+1, 2*l+1);
printf(" computed s1\n");
if (debug) printf(" computed s1\n");
Vector s(2*k);
for (auto i = 0; i < k; i++) s[i] = s0[i];
printf(" copied s0 to s\n");
for (auto i = k; i < 2*k; i++) s[i+k] = s1[i];
printf(" copied s1 to s\n");
if (debug) printf(" copied s0 and s1 to s\n");
for (auto i = 0; i < k; i++) {
s[i] = s0[i];
s[i+k] = s1[i];
}
Vector d = hg*s;
printf(" d = hg*s\n");
if (debug) printf(" d = hg*s\n");
if (normf(Vector(d.slice(k,2*k-1))) < thresh || n >= maxlevel-1) {
tree[Key(n+1,2*l)] = s0;
printf("set n+1 2*l coeff\n");
if (debug) printf("set n+1 2*l coeff (%d %d)\n",n+1,2*l);
tree[Key(n+1,2*l+1)] = s1;
printf("set n+1 2*l+1 coeff\n");
if (debug) printf("set n+1 2*l+1 coeff (%d %d)\n",n+1,2*l+1);
} else {
refine(f, n+1, 2*l);
refine(f, n+1, 2*l+1);
Expand All @@ -113,4 +115,11 @@ class Function1D {
}
printf(")\n");
}

void print_tree() {
for (auto c : tree) {
Key k = c.first;
printf("[%d %d]\n", k.n, k.l);
}
}
};
3 changes: 2 additions & 1 deletion test_function.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ double func(double x) {

int main(int argc, char** argv) {

Function1D f(func, 8, 1e-6, 3);
Function1D f(func, 8, 1e-6, 300);
f.print_tree();
return 0;
}
27 changes: 0 additions & 27 deletions tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,7 @@ struct Key {
Key(int n, int l) : n(n), l(l) {}

bool operator< (const Key &k) const {
printf("n: %d l: %d k.n: %d k.l: %d\n", n, l, k.n, k.l);
return ((1<<n)+l < (k.n<<n)+k.l);
}

// bool operator< (const Key& key) const {
// if ((n < key.n) || (n == key.n && l < key.l)) return true;
// return false;
// }
};

struct MyHashCompare {
static size_t hash( const Key& x ) {
size_t prime = 31;
size_t result = 1;
result = prime * result + x.l;
result = prime * result + x.n;
return result;
}
//! True if strings are equal
static bool equal( const Key& x, const Key& y ) {
return ((x.n == y.n) && (x.l == y.l));
}
};

// class Tree {
// private:
// std::map<Key,real_matrix,MyHashCompare> _map;
// public:
// Tree() {}
// ~Tree() {}
// };

0 comments on commit a3b6460

Please sign in to comment.