Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HPC tutorial: update python scripts #592

Merged
merged 3 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions intro-HPC/examples/Program-examples/01_Python/file3.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
#
def primes(top):
result = []
for n in range(2, top):
for x in range(2, n/2+1):
for n in range(2, top+1):
for x in range(2, n):
if n % x == 0:
break
else:
Expand All @@ -33,11 +33,11 @@ def primes(top):
#
scratch_dir = os.environ.get('VSC_SCRATCH')
filename_p1 = scratch_dir + "/primes_1.txt"
print "Output File: ", filename_p1
print("Output File: ", filename_p1)
f_out = open(filename_p1, 'w+')
for i in range(1, 30000):
# We take a random integer between 1 and 2000
top = random.randrange(2000)
top = random.randrange(2,2000)
# and we calculate all primes up to that limit
l = primes(top)
f_out.write('TOP=')
Expand All @@ -60,8 +60,8 @@ def primes(top):
filename_p2 = scratch_dir + "/primes_2.txt"
f_in=open(filename_p1, "r")
f_out=open(filename_p2, "w")
print "Input File: ", filename_p1
print "Output File: ", filename_p2
print("Input File: ", filename_p1)
print("Output File: ", filename_p2)
in_lines=f_in.readlines() #reads it line by line
for line in in_lines:
delim = line.find("[")
Expand All @@ -82,5 +82,5 @@ def primes(top):

end_time = int(time.time())
duration = end_time - start_time
print "Duration = " + str(duration) + " seconds."
print("Duration = " + str(duration) + " seconds.")

14 changes: 7 additions & 7 deletions intro-HPC/examples/Program-examples/01_Python/primes.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def ask_int(prompt, retries=3):
if num > 1:
return num
else:
print "Please enter an integer which is bigger as one."
print('Please enter an integer which is bigger as one.')
retries = retries - 1
if retries <= 0:
print('Too many attempts, try again later.')
Expand All @@ -27,23 +27,23 @@ def ask_int(prompt, retries=3):
#
# PRIMES
#
print "This program calculates all primes between 1 and your upper limit."
print('This program calculates all primes between 1 and your upper limit.')
num = ask_int('Enter your upper limit (>1): ')
start_time = print_time("Start Time: ")
print "[Prime#1] = 1"
print('[Prime#1] = 1')
ctr = 1
for n in range(2, int(num)):
for x in range(2, n/2+1):
for n in range(2, int(num)+1):
for x in range(2, n):
if n % x == 0:
break
else:
# loop fell through without finding a factor
ctr += 1
print "[Prime#%i] = %i" % (ctr,n)
print('[Prime#%i] = %i' % (ctr,n))
end_time = print_time("End Time: ")
duration = end_time - start_time
s = "Duration: " + str(duration) + " seconds."
print s
print(s)



2 changes: 1 addition & 1 deletion intro-HPC/examples/Running-interactive-jobs/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def print_cowsay(cowsay):
||----w | \n\
|| ||\
"
print cowtxt
print(cowtxt)

# X-Message
cmd = "xmessage -buttons yes:1,no:0 -center -timeout 60 \" Do you want to see a cow? \""
Expand Down
16 changes: 8 additions & 8 deletions intro-HPC/examples/Running-interactive-jobs/primes.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
def print_time(str):
now = time.time()
st = datetime.datetime.fromtimestamp(now).strftime('%Y-%m-%d %H:%M:%S')
print str, st
print(str, st)
return int(now)

def ask_int(prompt, retries=3):
Expand All @@ -21,27 +21,27 @@ def ask_int(prompt, retries=3):
if num > 1:
return num
else:
print "Please enter an integer which is bigger as one."
print("Please enter an integer which is bigger as one.")
retries = retries - 1
if retries <= 0:
print('Too many attempts, try again later.')
sys.exit()

# PRIMES
print "This program calculates all primes between 1 and your upper limit."
print("This program calculates all primes between 1 and your upper limit.")
num = ask_int('Enter your upper limit (>1): ')
start_time = print_time("Start Time: ")
print "[Prime#1] = 1"
print("[Prime#1] = 1")
ctr = 1
for n in range(2, int(num)):
for x in range(2, n/2+1):
for n in range(2, int(num)+1):
for x in range(2, n):
if n % x == 0:
break
else:
# loop fell through without finding a factor
ctr += 1
print "[Prime#%i] = %i" % (ctr,n)
print("[Prime#%i] = %i" % (ctr,n))
end_time = print_time("End Time: ")
duration = end_time - start_time
s = "Duration: " + str(duration) + " seconds."
print s
print(s)
Loading