Hello all,
I'm having trouble with segfaults with my FLEXT external, I'm new to flext and C++ and I really don't have an idea how to narrow it down. I've got it down to two lines that commented out remove the segfault, but I can't find anything wrong with those lines.
I've attached my flext class (pso.cpp) and the SDL C program I'm re-implimenting. The SDL program has no segfault problems, and most of the code is copy-pasted with only variable and array subscript changes...
Any help would be greatly appreciated.
Thanks Ben
Hi Ben,
it would be great if you could send a patch that demonstrates some intended behavior of your external..... otherwise i can't really figure out what it does.... Anyway, having a short look at the code i spotted some things:
1) RAND_MAX is defined in stdlib.h to represent the largest number rand() will ever deliver. This means that it is guaranteed that 0 <= rand()/RAND_MAX <= 1. If you redefine RAND_MAX in your code to some different value this is no longer true. rand() doesn't know about the redefinition! So, if you want your random numbers to be in a range from a to b then you should use something like r = a+(float)rand()/RAND_MAX*(b-a). If the granularity of rand() is too large for your application you might also want to check out better algorithms (see for example http://www.library.cornell.edu/nr/bookcpdf.html , chapter 7).
2) You have some tricky array indexing in your code, which could be a potential cause of trouble (e.g. because of a simple typo). Maybe you would want to insert some assertion statements (or other means) into your code to verify that the indexing is correct.
for example: int ix = p*hoodsize+n; FLEXT_ASSERT(ix < hoodsize*popsize); // check if index is within the array bounds neighbors [ix] = p-1+n;
3) Are you aware of the fact that within class members the this-pointer can be omitted? This means that e.g. "popsize" means exactly the same as "this->popsize" in any member function of the pso class. On the other hand explicit usage of this can make things clearer, of course.
best greetings, Thomas
----- Original Message ----- From: "B. Bogart" ben@ekran.org To: pd-dev@iem.at Sent: Tuesday, August 05, 2003 5:10 AM Subject: [PD-dev] Segfaults with FLEXT code
Hello all,
I'm having trouble with segfaults with my FLEXT external, I'm new to flext and C++ and I really don't have an idea how to narrow it down. I've got it down to two lines that commented out remove the segfault, but I can't find anything wrong with those lines.
I've attached my flext class (pso.cpp) and the SDL C program I'm re-implimenting. The SDL program has no segfault problems, and most of the code is copy-pasted with only variable and array subscript changes...
Any help would be greatly appreciated.
Thanks Ben
Hey Thomas,
Thanks for the hints, I put in a few FLEXT_ASSERTS and I wrapped the array references with an if statement to make sure the values are in the valid range. Still no luck.
I wish I could send a patch that would explain the behavior, but the reason why I'm doing it as an external is that I think it would be too complicated to do as a patch, at least it would be just as time consuming. If you refer to the python code I sent you way back (attached again) thats the exact behavior the external requires. I'd love to get you more informarion but I only half-understand the algo myself. I'll give it a try...
Particle Swarm Optimizer:
Very similar to a Genetic Algo. You have a population of solutions to a problem, randomly generated. Then through an iterative process you try and steer the population towards the correct solution. In the case of the PSO The particles are attracted to the member with the best solution. So the whole population accelerates towards the best solution. The theory goes that the particles accelerating towards the best solution will find the solution and all the paricles will end up on the best solution. The python program just minimizes the distance from the particles initial position to the center. My external will translate the position of the "center" to match up with a target. this way the particles will all gravitate towards this target.
Does that help?
I fixed the rand() issue, not sure what I was thinking since I was already dividing by RAND_MAX!
Since its not the array indexing is there anything else that looks suspect in your eyes? I've been starring at it for days, along side the C version that works fine, and I just can't spot any problems. Since the problem is in the iterate function I really don't know how to approach trouble-shooting it, because any change I make will change the behavior of the pso and whatever is causing the fault may or may not realize itself. The system is supposed to be emergent by definition!
What is the standard way to deal with these problems?
I did not realize I could omit " this-> ", but I think i'll leave it all in place until I can get rid of this fault!
Thanks for your help.
Ben
----- Original Message ----- From: "Thomas Grill" t.grill@gmx.net To: "B. Bogart" ben@ekran.org; pd-dev@iem.at Sent: Tuesday, August 05, 2003 5:39 AM Subject: Re: [PD-dev] Segfaults with FLEXT code
Hi Ben,
it would be great if you could send a patch that demonstrates some
intended
behavior of your external..... otherwise i can't really figure out what it does.... Anyway, having a short look at the code i spotted some things:
- RAND_MAX is defined in stdlib.h to represent the largest number rand()
will ever deliver. This means that it is guaranteed that 0 <= rand()/RAND_MAX <= 1. If you redefine RAND_MAX in your code to some different value this is no longer true. rand() doesn't know about the redefinition! So, if you want your random numbers to be in a range from a
to
b then you should use something like r = a+(float)rand()/RAND_MAX*(b-a).
If
the granularity of rand() is too large for your application you might also want to check out better algorithms (see for example http://www.library.cornell.edu/nr/bookcpdf.html , chapter 7).
- You have some tricky array indexing in your code, which could be a
potential cause of trouble (e.g. because of a simple typo). Maybe you
would
want to insert some assertion statements (or other means) into your code
to
verify that the indexing is correct.
for example: int ix = p*hoodsize+n; FLEXT_ASSERT(ix < hoodsize*popsize); // check if index is within the array bounds neighbors [ix] = p-1+n;
- Are you aware of the fact that within class members the this-pointer
can
be omitted? This means that e.g. "popsize" means exactly the same as "this->popsize" in any member function of the pso class. On the other hand explicit usage of this can make things clearer, of course.
best greetings, Thomas
----- Original Message ----- From: "B. Bogart" ben@ekran.org To: pd-dev@iem.at Sent: Tuesday, August 05, 2003 5:10 AM Subject: [PD-dev] Segfaults with FLEXT code
Hello all,
I'm having trouble with segfaults with my FLEXT external, I'm new to
flext
and C++ and I really don't have an idea how to narrow it down. I've got
it
down to two lines that commented out remove the segfault, but I can't
find
anything wrong with those lines.
I've attached my flext class (pso.cpp) and the SDL C program I'm re-implimenting. The SDL program has no segfault problems, and most of
the
code is copy-pasted with only variable and array subscript changes...
Any help would be greatly appreciated.
Thanks Ben
Hi Ben,
I wish I could send a patch that would explain the behavior, but the
reason
why I'm doing it as an external is that I think it would be too
complicated
to do as a patch, at least it would be just as time consuming. If you
refer
to the python code I sent you way back (attached again) thats the exact behavior the external requires. I'd love to get you more informarion but I only half-understand the algo myself. I'll give it a try...
i rather meant if you could send me a patch that _uses_ the pso external....
What is the standard way to deal with these problems?
Use a debugger and have a look at the values...
best greetings, Thomas