Optimizing my 2D Ising model code in Julia The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)Neural Network in JuliaJulia RPN calculator algorithm ported from Python versionKernel density regression in JuliaImplementing cat function in JuliaComputing the double Integral using MonteCarlo techniques using JuliaSearch algorithms in juliaSort Algorithms in JuliaParsing simple log files in Julia vs PythonWeighted logistic regression in JuliaWebSocket based API library in Julia
The variadic template constructor of my class cannot modify my class members, why is that so?
Single author papers against my advisor's will?
Problems with Ubuntu mount /tmp
how can a perfect fourth interval be considered either consonant or dissonant?
I could not break this equation. Please help me
Can smartphones with the same camera sensor have different image quality?
Road tyres vs "Street" tyres for charity ride on MTB Tandem
Wall plug outlet change
How to remove this toilet supply line that seems to have no nut?
How to pronounce 1ターン?
If the empty set is a subset of every set, why write ... ∪ ∅?
How to delete random line from file using Unix command?
system() function string length limit
Why does the Event Horizon Telescope (EHT) not include telescopes from Africa, Asia or Australia?
In horse breeding, what is the female equivalent of putting a horse out "to stud"?
Can the DM override racial traits?
Sort a list of pairs representing an acyclic, partial automorphism
How to copy the contents of all files with a certain name into a new file?
What is special about square numbers here?
Segmentation fault output is suppressed when piping stdin into a function. Why?
Can withdrawing asylum be illegal?
How did passengers keep warm on sail ships?
Why can't devices on different VLANs, but on the same subnet, communicate?
Why can't wing-mounted spoilers be used to steepen approaches?
Optimizing my 2D Ising model code in Julia
The 2019 Stack Overflow Developer Survey Results Are In
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)Neural Network in JuliaJulia RPN calculator algorithm ported from Python versionKernel density regression in JuliaImplementing cat function in JuliaComputing the double Integral using MonteCarlo techniques using JuliaSearch algorithms in juliaSort Algorithms in JuliaParsing simple log files in Julia vs PythonWeighted logistic regression in JuliaWebSocket based API library in Julia
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
$begingroup$
I'm just starting to learn Julia, I work primarily in physics and am used to writing most of my code in Fortran90 and occasionally Python for Tensorflow (also Mathematica but that's less relevant). Julia has been recommended to me and I started checking it out; I like it a lot in theory as a middle ground between the speed of Fortran and the syntax of Python. To test it out I wrote a simple 2D Ising model code implementing a basic single-spin-flip Metropolis Monte Carlo algorithm. However, this code runs very slowly compared to an equivalent code in Fortran. Am I doing something wrong which is significantly affecting the performance of the code? I know almost nothing beyond what I've done here. I am using the Juno IDE in Atom on Windows 10. As an aside, I would also like to know how I can make multiple plots in the Atom plot tab, but that's secondary.
using Printf
using Plots
L = 20
n_sweep = 20
n_therm = 1000
n_data = 100
temps = 4.0:-0.3:0.1
e1 = Array(1:n_data)
m1 = Array(1:n_data)
et = []
mt = []
energy = e1
magnetization = m1
s = ones(Int32,L,L)
function measure(i)
en = 0
m = 0
for x = 1:L
for y = 1:L
u = 1+mod(y,L)
r = 1+mod(x,L)
en -= s[x,y]*(s[x,u]+s[r,y])
m += s[x,y]
end
end
energy[i] = en
magnetization[i] = abs(m)
end
function sweep(n,T)
for i = 1:n
for x = 1:L
for y = 1:L
flip(x,y,T)
end
end
end
end
function flip(x,y,T)
u = 1+mod(y,L)
d = 1+mod(y-2,L)
r = 1+mod(x,L)
l = 1+mod(x-2,L)
de = 2*s[x,y]*(s[x,u]+s[x,d]+s[l,y]+s[r,y])
if (de < 0)
s[x,y] = -s[x,y]
else
p = rand()
if (p < exp(-de/T))
s[x,y] = -s[x,y]
end
end
end
for T in temps
sweep(n_therm, T)
energy = e1
magnetization = m1
for i = 1:n_data
sweep(n_sweep, T)
measure(i)
end
en1 = sum(energy)/n_data
ma1 = sum(magnetization)/n_data
push!(et,en1/(L*L))
push!(mt,ma1/(L*L))
@printf("%8.3f %8.3f n", en1/(L*L), ma1/(L*L))
end
plot(temps,mt)
performance beginner julia
New contributor
$endgroup$
add a comment |
$begingroup$
I'm just starting to learn Julia, I work primarily in physics and am used to writing most of my code in Fortran90 and occasionally Python for Tensorflow (also Mathematica but that's less relevant). Julia has been recommended to me and I started checking it out; I like it a lot in theory as a middle ground between the speed of Fortran and the syntax of Python. To test it out I wrote a simple 2D Ising model code implementing a basic single-spin-flip Metropolis Monte Carlo algorithm. However, this code runs very slowly compared to an equivalent code in Fortran. Am I doing something wrong which is significantly affecting the performance of the code? I know almost nothing beyond what I've done here. I am using the Juno IDE in Atom on Windows 10. As an aside, I would also like to know how I can make multiple plots in the Atom plot tab, but that's secondary.
using Printf
using Plots
L = 20
n_sweep = 20
n_therm = 1000
n_data = 100
temps = 4.0:-0.3:0.1
e1 = Array(1:n_data)
m1 = Array(1:n_data)
et = []
mt = []
energy = e1
magnetization = m1
s = ones(Int32,L,L)
function measure(i)
en = 0
m = 0
for x = 1:L
for y = 1:L
u = 1+mod(y,L)
r = 1+mod(x,L)
en -= s[x,y]*(s[x,u]+s[r,y])
m += s[x,y]
end
end
energy[i] = en
magnetization[i] = abs(m)
end
function sweep(n,T)
for i = 1:n
for x = 1:L
for y = 1:L
flip(x,y,T)
end
end
end
end
function flip(x,y,T)
u = 1+mod(y,L)
d = 1+mod(y-2,L)
r = 1+mod(x,L)
l = 1+mod(x-2,L)
de = 2*s[x,y]*(s[x,u]+s[x,d]+s[l,y]+s[r,y])
if (de < 0)
s[x,y] = -s[x,y]
else
p = rand()
if (p < exp(-de/T))
s[x,y] = -s[x,y]
end
end
end
for T in temps
sweep(n_therm, T)
energy = e1
magnetization = m1
for i = 1:n_data
sweep(n_sweep, T)
measure(i)
end
en1 = sum(energy)/n_data
ma1 = sum(magnetization)/n_data
push!(et,en1/(L*L))
push!(mt,ma1/(L*L))
@printf("%8.3f %8.3f n", en1/(L*L), ma1/(L*L))
end
plot(temps,mt)
performance beginner julia
New contributor
$endgroup$
add a comment |
$begingroup$
I'm just starting to learn Julia, I work primarily in physics and am used to writing most of my code in Fortran90 and occasionally Python for Tensorflow (also Mathematica but that's less relevant). Julia has been recommended to me and I started checking it out; I like it a lot in theory as a middle ground between the speed of Fortran and the syntax of Python. To test it out I wrote a simple 2D Ising model code implementing a basic single-spin-flip Metropolis Monte Carlo algorithm. However, this code runs very slowly compared to an equivalent code in Fortran. Am I doing something wrong which is significantly affecting the performance of the code? I know almost nothing beyond what I've done here. I am using the Juno IDE in Atom on Windows 10. As an aside, I would also like to know how I can make multiple plots in the Atom plot tab, but that's secondary.
using Printf
using Plots
L = 20
n_sweep = 20
n_therm = 1000
n_data = 100
temps = 4.0:-0.3:0.1
e1 = Array(1:n_data)
m1 = Array(1:n_data)
et = []
mt = []
energy = e1
magnetization = m1
s = ones(Int32,L,L)
function measure(i)
en = 0
m = 0
for x = 1:L
for y = 1:L
u = 1+mod(y,L)
r = 1+mod(x,L)
en -= s[x,y]*(s[x,u]+s[r,y])
m += s[x,y]
end
end
energy[i] = en
magnetization[i] = abs(m)
end
function sweep(n,T)
for i = 1:n
for x = 1:L
for y = 1:L
flip(x,y,T)
end
end
end
end
function flip(x,y,T)
u = 1+mod(y,L)
d = 1+mod(y-2,L)
r = 1+mod(x,L)
l = 1+mod(x-2,L)
de = 2*s[x,y]*(s[x,u]+s[x,d]+s[l,y]+s[r,y])
if (de < 0)
s[x,y] = -s[x,y]
else
p = rand()
if (p < exp(-de/T))
s[x,y] = -s[x,y]
end
end
end
for T in temps
sweep(n_therm, T)
energy = e1
magnetization = m1
for i = 1:n_data
sweep(n_sweep, T)
measure(i)
end
en1 = sum(energy)/n_data
ma1 = sum(magnetization)/n_data
push!(et,en1/(L*L))
push!(mt,ma1/(L*L))
@printf("%8.3f %8.3f n", en1/(L*L), ma1/(L*L))
end
plot(temps,mt)
performance beginner julia
New contributor
$endgroup$
I'm just starting to learn Julia, I work primarily in physics and am used to writing most of my code in Fortran90 and occasionally Python for Tensorflow (also Mathematica but that's less relevant). Julia has been recommended to me and I started checking it out; I like it a lot in theory as a middle ground between the speed of Fortran and the syntax of Python. To test it out I wrote a simple 2D Ising model code implementing a basic single-spin-flip Metropolis Monte Carlo algorithm. However, this code runs very slowly compared to an equivalent code in Fortran. Am I doing something wrong which is significantly affecting the performance of the code? I know almost nothing beyond what I've done here. I am using the Juno IDE in Atom on Windows 10. As an aside, I would also like to know how I can make multiple plots in the Atom plot tab, but that's secondary.
using Printf
using Plots
L = 20
n_sweep = 20
n_therm = 1000
n_data = 100
temps = 4.0:-0.3:0.1
e1 = Array(1:n_data)
m1 = Array(1:n_data)
et = []
mt = []
energy = e1
magnetization = m1
s = ones(Int32,L,L)
function measure(i)
en = 0
m = 0
for x = 1:L
for y = 1:L
u = 1+mod(y,L)
r = 1+mod(x,L)
en -= s[x,y]*(s[x,u]+s[r,y])
m += s[x,y]
end
end
energy[i] = en
magnetization[i] = abs(m)
end
function sweep(n,T)
for i = 1:n
for x = 1:L
for y = 1:L
flip(x,y,T)
end
end
end
end
function flip(x,y,T)
u = 1+mod(y,L)
d = 1+mod(y-2,L)
r = 1+mod(x,L)
l = 1+mod(x-2,L)
de = 2*s[x,y]*(s[x,u]+s[x,d]+s[l,y]+s[r,y])
if (de < 0)
s[x,y] = -s[x,y]
else
p = rand()
if (p < exp(-de/T))
s[x,y] = -s[x,y]
end
end
end
for T in temps
sweep(n_therm, T)
energy = e1
magnetization = m1
for i = 1:n_data
sweep(n_sweep, T)
measure(i)
end
en1 = sum(energy)/n_data
ma1 = sum(magnetization)/n_data
push!(et,en1/(L*L))
push!(mt,ma1/(L*L))
@printf("%8.3f %8.3f n", en1/(L*L), ma1/(L*L))
end
plot(temps,mt)
performance beginner julia
performance beginner julia
New contributor
New contributor
New contributor
asked 12 mins ago
KaiKai
1012
1012
New contributor
New contributor
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
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
);
);
Kai is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f217421%2foptimizing-my-2d-ising-model-code-in-julia%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
Kai is a new contributor. Be nice, and check out our Code of Conduct.
Kai is a new contributor. Be nice, and check out our Code of Conduct.
Kai is a new contributor. Be nice, and check out our Code of Conduct.
Kai 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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f217421%2foptimizing-my-2d-ising-model-code-in-julia%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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