SIMD Mandelbrot implementation












-2















#include <vector>
template<typename T, typename SOURCE, typename DEST, std::size_t SIZE=8>
void calculate(const T &_dLimit, const unsigned int _iMax, SOURCE &_rSource, DEST &_rDest)
{ // starting point -- does not change during iteration
float adX0[SIZE], adY0[SIZE];
// coordinates of point -- for reporting back number of iterations
unsigned int aiX[SIZE], aiY[SIZE];
// current value of iteration
float adX[SIZE], adY[SIZE];
// number of iterations
unsigned int aiIt[SIZE];
// is the point a valid point
bool ab[SIZE];
// number of valid points currently being calculated
unsigned int iValid = 0;
// initial -- get SIZE points
for (std::size_t i = 0; i < SIZE; ++i)
{ const auto s = _rSource.getPoint();
if (!(ab[i] = s.b))
if (iValid == 0)
return;
else
;
else
++iValid;
// the coordinates of the point
aiX[i] = s.ix;
aiY[i] = s.iy;
// the complex value of the point
adX[i] = adX0[i] = s.dx;
adY[i] = adY0[i] = s.dy;
// number of iterations
aiIt[i] = 0;
}
while (1)
{ // square values
float ad2X[SIZE], ad2Y[SIZE];
// stop condition fulfilled?
bool abl[SIZE];
// x square
for (std::size_t i = 0; i < SIZE; ++i)
ad2X[i] = adX[i]*adX[i];
// y square
for (std::size_t i = 0; i < SIZE; ++i)
ad2Y[i] = adY[i]*adY[i];
// stop condition met
for (std::size_t i = 0; i < SIZE; ++i)
if (aiIt[i] > _iMax || ad2Y[i] + ad2X[i] > _dLimit)
abl[i] = false;
else
abl[i] = true;
// new x value
for (std::size_t i = 0; i < SIZE; ++i)
ad2X[i] = ad2X[i] - ad2Y[i] + adX0[i];
// new y value
for (std::size_t i = 0; i < SIZE; ++i)
ad2Y[i] = 2*adX[i]*adY[i] + adY0[i];
// copy over to x
for (std::size_t i = 0; i < SIZE; ++i)
adX[i] = ad2X[i];
// copy over to y
for (std::size_t i = 0; i < SIZE; ++i)
adY[i] = ad2Y[i];
// if condition is met, report results and request new value
for (std::size_t i = 0; i < SIZE; ++i)
if (!abl[i] && ab[i])
{ _rDest.report(aiX[i], aiY[i], aiIt[i]);
const auto s = _rSource.getPoint();
if (!(ab[i] = s.b))
if (!--iValid)
return;
aiX[i] = s.ix;
aiY[i] = s.iy;
adX[i] = adX0[i] = s.dx;
adY[i] = adY0[i] = s.dy;
aiIt[i] = 0;
}
else
++aiIt[i];
}
}
struct point
{ float dx, dy;
unsigned int ix, iy;
bool b;
};
struct source
{ point getPoint(void);
void report(unsigned int, unsigned int, unsigned int);
};
int main()
{
source sSource;
calculate(10.0, 100, sSource, sSource);
}









share|improve this question









New contributor




GrapschKnutsch is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 2





    Please add some context. Talk about what your code does. Explain some of the design decisions. Help the reviewers understand what they are reviewing.

    – bruglesco
    23 hours ago











  • @bruglesco This code assumes that you know the Mandelbrot formula. Without this, there is no point of looking at this code.

    – GrapschKnutsch
    12 hours ago






  • 2





    there's always a point. We still expect more than a code dump here.

    – bruglesco
    11 hours ago






  • 3





    If you're wondering what kind of explanation to add to the question, see How to Ask and Text minimum in questions.

    – 200_success
    10 hours ago











  • I need an explanation of how this is SIMD. I don't see a single SIMD instruction in the code.

    – user1118321
    4 hours ago
















-2















#include <vector>
template<typename T, typename SOURCE, typename DEST, std::size_t SIZE=8>
void calculate(const T &_dLimit, const unsigned int _iMax, SOURCE &_rSource, DEST &_rDest)
{ // starting point -- does not change during iteration
float adX0[SIZE], adY0[SIZE];
// coordinates of point -- for reporting back number of iterations
unsigned int aiX[SIZE], aiY[SIZE];
// current value of iteration
float adX[SIZE], adY[SIZE];
// number of iterations
unsigned int aiIt[SIZE];
// is the point a valid point
bool ab[SIZE];
// number of valid points currently being calculated
unsigned int iValid = 0;
// initial -- get SIZE points
for (std::size_t i = 0; i < SIZE; ++i)
{ const auto s = _rSource.getPoint();
if (!(ab[i] = s.b))
if (iValid == 0)
return;
else
;
else
++iValid;
// the coordinates of the point
aiX[i] = s.ix;
aiY[i] = s.iy;
// the complex value of the point
adX[i] = adX0[i] = s.dx;
adY[i] = adY0[i] = s.dy;
// number of iterations
aiIt[i] = 0;
}
while (1)
{ // square values
float ad2X[SIZE], ad2Y[SIZE];
// stop condition fulfilled?
bool abl[SIZE];
// x square
for (std::size_t i = 0; i < SIZE; ++i)
ad2X[i] = adX[i]*adX[i];
// y square
for (std::size_t i = 0; i < SIZE; ++i)
ad2Y[i] = adY[i]*adY[i];
// stop condition met
for (std::size_t i = 0; i < SIZE; ++i)
if (aiIt[i] > _iMax || ad2Y[i] + ad2X[i] > _dLimit)
abl[i] = false;
else
abl[i] = true;
// new x value
for (std::size_t i = 0; i < SIZE; ++i)
ad2X[i] = ad2X[i] - ad2Y[i] + adX0[i];
// new y value
for (std::size_t i = 0; i < SIZE; ++i)
ad2Y[i] = 2*adX[i]*adY[i] + adY0[i];
// copy over to x
for (std::size_t i = 0; i < SIZE; ++i)
adX[i] = ad2X[i];
// copy over to y
for (std::size_t i = 0; i < SIZE; ++i)
adY[i] = ad2Y[i];
// if condition is met, report results and request new value
for (std::size_t i = 0; i < SIZE; ++i)
if (!abl[i] && ab[i])
{ _rDest.report(aiX[i], aiY[i], aiIt[i]);
const auto s = _rSource.getPoint();
if (!(ab[i] = s.b))
if (!--iValid)
return;
aiX[i] = s.ix;
aiY[i] = s.iy;
adX[i] = adX0[i] = s.dx;
adY[i] = adY0[i] = s.dy;
aiIt[i] = 0;
}
else
++aiIt[i];
}
}
struct point
{ float dx, dy;
unsigned int ix, iy;
bool b;
};
struct source
{ point getPoint(void);
void report(unsigned int, unsigned int, unsigned int);
};
int main()
{
source sSource;
calculate(10.0, 100, sSource, sSource);
}









share|improve this question









New contributor




GrapschKnutsch is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 2





    Please add some context. Talk about what your code does. Explain some of the design decisions. Help the reviewers understand what they are reviewing.

    – bruglesco
    23 hours ago











  • @bruglesco This code assumes that you know the Mandelbrot formula. Without this, there is no point of looking at this code.

    – GrapschKnutsch
    12 hours ago






  • 2





    there's always a point. We still expect more than a code dump here.

    – bruglesco
    11 hours ago






  • 3





    If you're wondering what kind of explanation to add to the question, see How to Ask and Text minimum in questions.

    – 200_success
    10 hours ago











  • I need an explanation of how this is SIMD. I don't see a single SIMD instruction in the code.

    – user1118321
    4 hours ago














-2












-2








-2








#include <vector>
template<typename T, typename SOURCE, typename DEST, std::size_t SIZE=8>
void calculate(const T &_dLimit, const unsigned int _iMax, SOURCE &_rSource, DEST &_rDest)
{ // starting point -- does not change during iteration
float adX0[SIZE], adY0[SIZE];
// coordinates of point -- for reporting back number of iterations
unsigned int aiX[SIZE], aiY[SIZE];
// current value of iteration
float adX[SIZE], adY[SIZE];
// number of iterations
unsigned int aiIt[SIZE];
// is the point a valid point
bool ab[SIZE];
// number of valid points currently being calculated
unsigned int iValid = 0;
// initial -- get SIZE points
for (std::size_t i = 0; i < SIZE; ++i)
{ const auto s = _rSource.getPoint();
if (!(ab[i] = s.b))
if (iValid == 0)
return;
else
;
else
++iValid;
// the coordinates of the point
aiX[i] = s.ix;
aiY[i] = s.iy;
// the complex value of the point
adX[i] = adX0[i] = s.dx;
adY[i] = adY0[i] = s.dy;
// number of iterations
aiIt[i] = 0;
}
while (1)
{ // square values
float ad2X[SIZE], ad2Y[SIZE];
// stop condition fulfilled?
bool abl[SIZE];
// x square
for (std::size_t i = 0; i < SIZE; ++i)
ad2X[i] = adX[i]*adX[i];
// y square
for (std::size_t i = 0; i < SIZE; ++i)
ad2Y[i] = adY[i]*adY[i];
// stop condition met
for (std::size_t i = 0; i < SIZE; ++i)
if (aiIt[i] > _iMax || ad2Y[i] + ad2X[i] > _dLimit)
abl[i] = false;
else
abl[i] = true;
// new x value
for (std::size_t i = 0; i < SIZE; ++i)
ad2X[i] = ad2X[i] - ad2Y[i] + adX0[i];
// new y value
for (std::size_t i = 0; i < SIZE; ++i)
ad2Y[i] = 2*adX[i]*adY[i] + adY0[i];
// copy over to x
for (std::size_t i = 0; i < SIZE; ++i)
adX[i] = ad2X[i];
// copy over to y
for (std::size_t i = 0; i < SIZE; ++i)
adY[i] = ad2Y[i];
// if condition is met, report results and request new value
for (std::size_t i = 0; i < SIZE; ++i)
if (!abl[i] && ab[i])
{ _rDest.report(aiX[i], aiY[i], aiIt[i]);
const auto s = _rSource.getPoint();
if (!(ab[i] = s.b))
if (!--iValid)
return;
aiX[i] = s.ix;
aiY[i] = s.iy;
adX[i] = adX0[i] = s.dx;
adY[i] = adY0[i] = s.dy;
aiIt[i] = 0;
}
else
++aiIt[i];
}
}
struct point
{ float dx, dy;
unsigned int ix, iy;
bool b;
};
struct source
{ point getPoint(void);
void report(unsigned int, unsigned int, unsigned int);
};
int main()
{
source sSource;
calculate(10.0, 100, sSource, sSource);
}









share|improve this question









New contributor




GrapschKnutsch is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












#include <vector>
template<typename T, typename SOURCE, typename DEST, std::size_t SIZE=8>
void calculate(const T &_dLimit, const unsigned int _iMax, SOURCE &_rSource, DEST &_rDest)
{ // starting point -- does not change during iteration
float adX0[SIZE], adY0[SIZE];
// coordinates of point -- for reporting back number of iterations
unsigned int aiX[SIZE], aiY[SIZE];
// current value of iteration
float adX[SIZE], adY[SIZE];
// number of iterations
unsigned int aiIt[SIZE];
// is the point a valid point
bool ab[SIZE];
// number of valid points currently being calculated
unsigned int iValid = 0;
// initial -- get SIZE points
for (std::size_t i = 0; i < SIZE; ++i)
{ const auto s = _rSource.getPoint();
if (!(ab[i] = s.b))
if (iValid == 0)
return;
else
;
else
++iValid;
// the coordinates of the point
aiX[i] = s.ix;
aiY[i] = s.iy;
// the complex value of the point
adX[i] = adX0[i] = s.dx;
adY[i] = adY0[i] = s.dy;
// number of iterations
aiIt[i] = 0;
}
while (1)
{ // square values
float ad2X[SIZE], ad2Y[SIZE];
// stop condition fulfilled?
bool abl[SIZE];
// x square
for (std::size_t i = 0; i < SIZE; ++i)
ad2X[i] = adX[i]*adX[i];
// y square
for (std::size_t i = 0; i < SIZE; ++i)
ad2Y[i] = adY[i]*adY[i];
// stop condition met
for (std::size_t i = 0; i < SIZE; ++i)
if (aiIt[i] > _iMax || ad2Y[i] + ad2X[i] > _dLimit)
abl[i] = false;
else
abl[i] = true;
// new x value
for (std::size_t i = 0; i < SIZE; ++i)
ad2X[i] = ad2X[i] - ad2Y[i] + adX0[i];
// new y value
for (std::size_t i = 0; i < SIZE; ++i)
ad2Y[i] = 2*adX[i]*adY[i] + adY0[i];
// copy over to x
for (std::size_t i = 0; i < SIZE; ++i)
adX[i] = ad2X[i];
// copy over to y
for (std::size_t i = 0; i < SIZE; ++i)
adY[i] = ad2Y[i];
// if condition is met, report results and request new value
for (std::size_t i = 0; i < SIZE; ++i)
if (!abl[i] && ab[i])
{ _rDest.report(aiX[i], aiY[i], aiIt[i]);
const auto s = _rSource.getPoint();
if (!(ab[i] = s.b))
if (!--iValid)
return;
aiX[i] = s.ix;
aiY[i] = s.iy;
adX[i] = adX0[i] = s.dx;
adY[i] = adY0[i] = s.dy;
aiIt[i] = 0;
}
else
++aiIt[i];
}
}
struct point
{ float dx, dy;
unsigned int ix, iy;
bool b;
};
struct source
{ point getPoint(void);
void report(unsigned int, unsigned int, unsigned int);
};
int main()
{
source sSource;
calculate(10.0, 100, sSource, sSource);
}






c++ fractals simd






share|improve this question









New contributor




GrapschKnutsch is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




GrapschKnutsch is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 23 hours ago









200_success

129k15152414




129k15152414






New contributor




GrapschKnutsch is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked yesterday









GrapschKnutschGrapschKnutsch

971




971




New contributor




GrapschKnutsch is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





GrapschKnutsch is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






GrapschKnutsch is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 2





    Please add some context. Talk about what your code does. Explain some of the design decisions. Help the reviewers understand what they are reviewing.

    – bruglesco
    23 hours ago











  • @bruglesco This code assumes that you know the Mandelbrot formula. Without this, there is no point of looking at this code.

    – GrapschKnutsch
    12 hours ago






  • 2





    there's always a point. We still expect more than a code dump here.

    – bruglesco
    11 hours ago






  • 3





    If you're wondering what kind of explanation to add to the question, see How to Ask and Text minimum in questions.

    – 200_success
    10 hours ago











  • I need an explanation of how this is SIMD. I don't see a single SIMD instruction in the code.

    – user1118321
    4 hours ago














  • 2





    Please add some context. Talk about what your code does. Explain some of the design decisions. Help the reviewers understand what they are reviewing.

    – bruglesco
    23 hours ago











  • @bruglesco This code assumes that you know the Mandelbrot formula. Without this, there is no point of looking at this code.

    – GrapschKnutsch
    12 hours ago






  • 2





    there's always a point. We still expect more than a code dump here.

    – bruglesco
    11 hours ago






  • 3





    If you're wondering what kind of explanation to add to the question, see How to Ask and Text minimum in questions.

    – 200_success
    10 hours ago











  • I need an explanation of how this is SIMD. I don't see a single SIMD instruction in the code.

    – user1118321
    4 hours ago








2




2





Please add some context. Talk about what your code does. Explain some of the design decisions. Help the reviewers understand what they are reviewing.

– bruglesco
23 hours ago





Please add some context. Talk about what your code does. Explain some of the design decisions. Help the reviewers understand what they are reviewing.

– bruglesco
23 hours ago













@bruglesco This code assumes that you know the Mandelbrot formula. Without this, there is no point of looking at this code.

– GrapschKnutsch
12 hours ago





@bruglesco This code assumes that you know the Mandelbrot formula. Without this, there is no point of looking at this code.

– GrapschKnutsch
12 hours ago




2




2





there's always a point. We still expect more than a code dump here.

– bruglesco
11 hours ago





there's always a point. We still expect more than a code dump here.

– bruglesco
11 hours ago




3




3





If you're wondering what kind of explanation to add to the question, see How to Ask and Text minimum in questions.

– 200_success
10 hours ago





If you're wondering what kind of explanation to add to the question, see How to Ask and Text minimum in questions.

– 200_success
10 hours ago













I need an explanation of how this is SIMD. I don't see a single SIMD instruction in the code.

– user1118321
4 hours ago





I need an explanation of how this is SIMD. I don't see a single SIMD instruction in the code.

– user1118321
4 hours ago










0






active

oldest

votes











Your Answer





StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");

StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});






GrapschKnutsch is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f211394%2fsimd-mandelbrot-implementation%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes








GrapschKnutsch is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















GrapschKnutsch is a new contributor. Be nice, and check out our Code of Conduct.













GrapschKnutsch is a new contributor. Be nice, and check out our Code of Conduct.












GrapschKnutsch is a new contributor. Be nice, and check out our Code of Conduct.
















Thanks for contributing an answer to Code Review Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


Use MathJax to format equations. MathJax reference.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f211394%2fsimd-mandelbrot-implementation%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Список кардиналов, возведённых папой римским Каликстом III

Deduzione

Mysql.sock missing - “Can't connect to local MySQL server through socket”