Skip to content
Snippets Groups Projects
Commit f42ffe0d authored by Armin Co's avatar Armin Co
Browse files

Fix segfault.

Trying to fix some segmentation faults when creating large arrays on the stack.
Creating arrays on the heap and switching pointers.
Not done.
parent 9738b98b
Branches
No related tags found
No related merge requests found
build
.vscode
\ No newline at end of file
......@@ -19,17 +19,18 @@ bool ArrayCanvas::on_draw(const Cairo::RefPtr<Cairo::Context>& cr)
{
for (unsigned x = 0; x < horizontalArraySize; x++)
{
constexpr unsigned scale_factor = 4;
// Print local
QColor c;
c = m_bufLocalRepresentation.getValue(x, y);
cr->set_source_rgba(c.r, c.g, c.b, c.a);
cr->rectangle(x, y, 1, 1);
cr->rectangle(x*scale_factor, y*scale_factor, scale_factor, scale_factor);
cr->fill();
// Print impulse
c = m_bufImpulseRepresentation.getValue(x, y);
cr->set_source_rgba(c.r, c.g, c.b, c.a);
cr->rectangle((x + horizontalArraySize), y, 1, 1);
cr->rectangle((x * scale_factor+ horizontalArraySize*scale_factor), y * scale_factor, scale_factor, scale_factor);
cr->fill();
}
}
......
......@@ -40,20 +40,14 @@ public:
m_dispatcher.emit();
}
///
/// @brief Copy buffer.
///
void updateLocalRepresentation(const ParticleImage localRepresentation)
ParticleImage &getLocalImage()
{
m_bufLocalRepresentation = localRepresentation;
return m_bufLocalRepresentation;
}
///
/// @brief Copy buffer.
///
void updateImpulseRepresentation(const ParticleImage impulseRepresentation)
ParticleImage &getImpulseImage()
{
m_bufImpulseRepresentation = impulseRepresentation;
return m_bufImpulseRepresentation;
}
protected:
......
......@@ -11,7 +11,7 @@
///
/// @brief Width of the drawing area in pixels.
///
constexpr unsigned long k_drawingAreaWidth = 256; // crashes at 418
constexpr unsigned long k_drawingAreaWidth = 128; // crashes at 418
///
/// @brief Height of the drawing area in pixels.
......
......@@ -17,13 +17,15 @@ const Complex Ci(0.0, 1.0);
void Particle::initImpulse()
{
#pragma omp parallel for
for (unsigned long iy = 0; iy < ny; iy++)
{
double y = yAt(iy);
for (unsigned long ix = 0; ix < nx; ix++)
{
double x = xAt(ix);
int index = nx * iy + ix;
unsigned long index = nx * iy + ix;
m_psi[index] = A0 * exp(Ci / hbar * (x * k_px0 + y * k_py0) - lambda * (square(x - k_x0) + square(y - k_y0)));
}
}
......@@ -54,7 +56,6 @@ Particle::~Particle()
void Particle::propagate()
{
fftw_execute (m_planForward); // transform into impulse repr.
#pragma omp parallel for
for (unsigned long iy = 0; iy < ny; iy++)
{
......@@ -77,12 +78,13 @@ void Particle::propagate()
void Particle::updateImpulseImage()
{
std::array<Complex, valuesInArray> array;
ParticleImage &impulseImage = m_arrayCanvas.getImpulseImage();
unsigned px = 0;
unsigned py = 0;
unsigned long impulse_index = 0;
unsigned long array_index = 0;
#pragma omp parallel for
for (unsigned y = 0; y < ny; y++)
{
for (unsigned x = 0; x < nx; x++)
......@@ -106,22 +108,24 @@ void Particle::updateImpulseImage()
}
impulse_index = py * nx + px;
array_index = y * nx + x;
array[array_index] = (m_psi[impulse_index] * 142.42) + 0.03;
impulseImage.updateBuffer(array_index, (m_psi[impulse_index] * 142.42));
}
}
m_arrayCanvas.updateImpulseRepresentation(ParticleImage(array));
impulseImage.swapBuffers();
}
void Particle::updateLocalImage()
{
std::array<Complex, valuesInArray> array;
ParticleImage &localImage = m_arrayCanvas.getLocalImage();
#pragma omp parallel for
for (unsigned long y = 0; y < ny; y++)
{
for (unsigned long x = 0; x < nx; x++)
{
int index = y * nx + x;
array[index] = m_psi[index];
localImage.updateBuffer(index, m_psi[index]);
}
}
m_arrayCanvas.updateLocalRepresentation(ParticleImage(array));
localImage.swapBuffers();
}
......@@ -26,10 +26,10 @@ public:
void propagate();
private:
fftw_plan m_planForward;
fftw_plan m_planBackward;
Complex *m_psi;
ArrayCanvas &m_arrayCanvas;
fftw_plan m_planForward;
fftw_plan m_planBackward;
///
/// @brief Init the array with initial impulse.
......
......@@ -9,6 +9,7 @@
#define QPONG_PARTICLE_IMAGE_HPP
#include <array>
#include <memory>
#include <cairomm/context.h>
#include "Particle.hpp"
......@@ -31,6 +32,7 @@ struct QColor
float b = 0;
float a = 0.95;
};
using Qarray = **Complex;
///
/// @brief Wraps an one dimensional array.
......@@ -39,14 +41,9 @@ struct QColor
class ParticleImage
{
public:
ParticleImage()
ParticleImage() : m_bufferIndex{0}
{
}
ParticleImage(std::array<Complex, valuesInArray> array) : m_buffer{array}
{
m_buffer = new (std::array<Qarray, 2>);
}
///
......@@ -56,7 +53,7 @@ public:
{
unsigned long index = y * nx + x;
constexpr double scale_factor = 1.0;
Complex val = m_buffer[index] * scale_factor;
Complex val = m_buffer[index][m_bufferIndex] * scale_factor;
double r = real(val);
double i = imag(val);
QColor c;
......@@ -66,11 +63,27 @@ public:
return c;
}
void updateBuffer(unsigned long index, Complex val)
{
m_buffer[index][m_bufferIndex] = val;
}
void swapBuffers()
{
m_bufferIndex++;
if (m_bufferIndex >= 2)
{
m_bufferIndex = 0;
}
}
private:
///
/// @brief Buffer for array with complex values from simulation.
///
std::array<Complex, valuesInArray> m_buffer;
std::unique_ptr<std::array<Qarray, 2>> m_buffer;
unsigned m_bufferIndex;
};
#endif
\ No newline at end of file
......@@ -30,7 +30,8 @@ const unsigned long nx = k_drawingAreaWidth;
const unsigned long ny = k_drawingAreaHeight;
// Number of all points/pixels.
const unsigned long n = nx * ny;
constexpr unsigned long n = nx * ny;
const double dx = 2.0 / nx;
const double dy = 2.0 / ny;
......
......@@ -19,6 +19,7 @@
void simulation(ArrayCanvas & ac)
{
Particle p(ac);
std::cout << "Particle initialised" << std::endl;
do
{
p.propagate();
......@@ -45,6 +46,8 @@ void render(ArrayCanvas &ac)
///
int main(int argc, char* argv[])
{
fprintf(stderr, "Hallo\n");
fflush(stderr);
// App and window
auto app = Gtk::Application::create(argc, argv, "An exmaple");
Gtk::Window window;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment