From a33cf14dccf277dc0ab73635b5f7d2ae97de87d0 Mon Sep 17 00:00:00 2001 From: Adam Gibson Date: Sun, 17 Sep 2017 15:47:25 +0800 Subject: [PATCH] Added Python 2/3 compatibility for exercises (print functions) --- Exercises/Exercise01/Python/DeviceInfo.py | 39 ++++++++++++----------- Exercises/Exercise03/Python/vadd.py | 7 ++-- Exercises/Exercise04/Python/vadd.py | 7 ++-- Exercises/Exercise05/Python/vadd.py | 7 ++-- Exercises/Exercise06/Python/helper.py | 5 +-- Exercises/Exercise06/Python/matmul.py | 7 ++-- Exercises/Exercise09/Python/pi.py | 8 ++--- 7 files changed, 43 insertions(+), 37 deletions(-) diff --git a/Exercises/Exercise01/Python/DeviceInfo.py b/Exercises/Exercise01/Python/DeviceInfo.py index 631aeb5..c233515 100644 --- a/Exercises/Exercise01/Python/DeviceInfo.py +++ b/Exercises/Exercise01/Python/DeviceInfo.py @@ -7,6 +7,7 @@ # History: C++ version written by Tom Deakin, 2012 # Ported to Python by Tom Deakin, July 2013 # +from __future__ import print_function # Import the Python OpenCL API import pyopencl as cl @@ -14,37 +15,37 @@ # Create a list of all the platform IDs platforms = cl.get_platforms() -print "\nNumber of OpenCL platforms:", len(platforms) +print("\nNumber of OpenCL platforms:", len(platforms)) -print "\n-------------------------" +print("\n-------------------------") # Investigate each platform for p in platforms: - # Print out some information about the platforms - print "Platform:", p.name - print "Vendor:", p.vendor - print "Version:", p.version + # print out some information about the platforms + print("Platform:", p.name) + print("Vendor:", p.vendor) + print("Version:", p.version) # Discover all devices devices = p.get_devices() - print "Number of devices:", len(devices) + print("Number of devices:", len(devices)) # Investigate each device for d in devices: - print "\t-------------------------" - # Print out some information about the devices - print "\t\tName:", d.name - print "\t\tVersion:", d.opencl_c_version - print "\t\tMax. Compute Units:", d.max_compute_units - print "\t\tLocal Memory Size:", d.local_mem_size/1024, "KB" - print "\t\tGlobal Memory Size:", d.global_mem_size/(1024*1024), "MB" - print "\t\tMax Alloc Size:", d.max_mem_alloc_size/(1024*1024), "MB" - print "\t\tMax Work-group Total Size:", d.max_work_group_size + print("\t-------------------------") + # print out some information about the devices + print("\t\tName:", d.name) + print("\t\tVersion:", d.opencl_c_version) + print("\t\tMax. Compute Units:", d.max_compute_units) + print("\t\tLocal Memory Size:", d.local_mem_size/1024, "KB") + print("\t\tGlobal Memory Size:", d.global_mem_size/(1024*1024), "MB") + print("\t\tMax Alloc Size:", d.max_mem_alloc_size/(1024*1024), "MB") + print("\t\tMax Work-group Total Size:", d.max_work_group_size) # Find the maximum dimensions of the work-groups dim = d.max_work_item_sizes - print "\t\tMax Work-group Dims:(", dim[0], " ".join(map(str, dim[1:])), ")" + print("\t\tMax Work-group Dims:(", dim[0], " ".join(map(str, dim[1:])), ")") - print "\t-------------------------" + print("\t-------------------------") - print "\n-------------------------" + print("\n-------------------------") diff --git a/Exercises/Exercise03/Python/vadd.py b/Exercises/Exercise03/Python/vadd.py index 17234e9..188a922 100644 --- a/Exercises/Exercise03/Python/vadd.py +++ b/Exercises/Exercise03/Python/vadd.py @@ -8,6 +8,7 @@ # C version Updated by Tom Deakin and Simon McIntosh-Smith, October 2012 # Ported to Python by Tom Deakin, July 2013 # +from __future__ import print_function # Import the Python OpenCL API import pyopencl as cl @@ -90,7 +91,7 @@ # Wait for the commands to finish before reading back queue.finish() rtime = time() - rtime -print "The kernel ran in", rtime, "seconds" +print("The kernel ran in", rtime, "seconds") # Read back the results from the compute device cl.enqueue_copy(queue, h_c, d_c) @@ -106,7 +107,7 @@ if tmp*tmp < TOL*TOL: correct += 1 else: - print "tmp", tmp, "h_a", a, "h_b", b, "h_c", c + print("tmp", tmp, "h_a", a, "h_b", b, "h_c", c) # Summarize results -print "C = A+B:", correct, "out of", LENGTH, "results were correct." +print("C = A+B:", correct, "out of", LENGTH, "results were correct.") diff --git a/Exercises/Exercise04/Python/vadd.py b/Exercises/Exercise04/Python/vadd.py index 17234e9..188a922 100644 --- a/Exercises/Exercise04/Python/vadd.py +++ b/Exercises/Exercise04/Python/vadd.py @@ -8,6 +8,7 @@ # C version Updated by Tom Deakin and Simon McIntosh-Smith, October 2012 # Ported to Python by Tom Deakin, July 2013 # +from __future__ import print_function # Import the Python OpenCL API import pyopencl as cl @@ -90,7 +91,7 @@ # Wait for the commands to finish before reading back queue.finish() rtime = time() - rtime -print "The kernel ran in", rtime, "seconds" +print("The kernel ran in", rtime, "seconds") # Read back the results from the compute device cl.enqueue_copy(queue, h_c, d_c) @@ -106,7 +107,7 @@ if tmp*tmp < TOL*TOL: correct += 1 else: - print "tmp", tmp, "h_a", a, "h_b", b, "h_c", c + print("tmp", tmp, "h_a", a, "h_b", b, "h_c", c) # Summarize results -print "C = A+B:", correct, "out of", LENGTH, "results were correct." +print("C = A+B:", correct, "out of", LENGTH, "results were correct.") diff --git a/Exercises/Exercise05/Python/vadd.py b/Exercises/Exercise05/Python/vadd.py index 17234e9..188a922 100644 --- a/Exercises/Exercise05/Python/vadd.py +++ b/Exercises/Exercise05/Python/vadd.py @@ -8,6 +8,7 @@ # C version Updated by Tom Deakin and Simon McIntosh-Smith, October 2012 # Ported to Python by Tom Deakin, July 2013 # +from __future__ import print_function # Import the Python OpenCL API import pyopencl as cl @@ -90,7 +91,7 @@ # Wait for the commands to finish before reading back queue.finish() rtime = time() - rtime -print "The kernel ran in", rtime, "seconds" +print("The kernel ran in", rtime, "seconds") # Read back the results from the compute device cl.enqueue_copy(queue, h_c, d_c) @@ -106,7 +107,7 @@ if tmp*tmp < TOL*TOL: correct += 1 else: - print "tmp", tmp, "h_a", a, "h_b", b, "h_c", c + print("tmp", tmp, "h_a", a, "h_b", b, "h_c", c) # Summarize results -print "C = A+B:", correct, "out of", LENGTH, "results were correct." +print("C = A+B:", correct, "out of", LENGTH, "results were correct.") diff --git a/Exercises/Exercise06/Python/helper.py b/Exercises/Exercise06/Python/helper.py index 4a255b5..9c9d771 100644 --- a/Exercises/Exercise06/Python/helper.py +++ b/Exercises/Exercise06/Python/helper.py @@ -1,3 +1,4 @@ +from __future__ import print_function from definitions import * import numpy @@ -25,7 +26,7 @@ def error(N, C): # Function to analyze and output results def results(N, C, run_time): mflops = 2.0 * N * N * N/(1000000.0* run_time) - print run_time, "seconds at", mflops, "MFLOPS" + print(run_time, "seconds at", mflops, "MFLOPS") errsq = error(N, C) if numpy.isnan(errsq) or errsq > TOL: - print "Errors in multiplication:", errsq + print("Errors in multiplication:", errsq) diff --git a/Exercises/Exercise06/Python/matmul.py b/Exercises/Exercise06/Python/matmul.py index b9e15af..b559939 100644 --- a/Exercises/Exercise06/Python/matmul.py +++ b/Exercises/Exercise06/Python/matmul.py @@ -15,6 +15,7 @@ # Ported to Python by Tom Deakin, July 2013 # Modified to assume square matrices by Simon McIntosh-Smith, Sep 2014 # +from __future__ import print_function from helper import * from definitions import * @@ -51,13 +52,13 @@ # C matrix h_C = numpy.empty(size).astype(numpy.float32) -print "\n===== Sequential, matrix mult (dot prod), order", ORDER, "on host CPU ======\n" +print("\n===== Sequential, matrix mult (dot prod), order", ORDER, "on host CPU ======\n") for i in range(COUNT): h_C.fill(0.0) start_time = time() - print "Skipping as this takes a long time to run!" + print("Skipping as this takes a long time to run!") #seq_mat_mul_sdot(N, h_A, h_B, h_C) run_time = time() - start_time @@ -84,7 +85,7 @@ mmul = program.mmul mmul.set_scalar_arg_dtypes([numpy.int32, None, None, None]) -print "\n===== OpenCL, matrix mult, C(i,j) per work item, order", N, "======\n" +print("\n===== OpenCL, matrix mult, C(i,j) per work item, order", N, "======\n") # Do the multiplication COUNT times for i in range(COUNT): diff --git a/Exercises/Exercise09/Python/pi.py b/Exercises/Exercise09/Python/pi.py index 2c119bc..ad46dad 100644 --- a/Exercises/Exercise09/Python/pi.py +++ b/Exercises/Exercise09/Python/pi.py @@ -11,16 +11,16 @@ # History: Written in C by Tim Mattson, 11/99 # Ported to Python by Tom Deakin, July 2013 # - +from __future__ import print_function from time import time num_steps = 100000000 -print "\nNote: Wanted to do", num_steps, "steps, but this is very slow in Python." +print("\nNote: Wanted to do", num_steps, "steps, but this is very slow in Python.") num_steps = 1000000 -print "Doing", num_steps, "steps instead." +print("Doing", num_steps, "steps instead.") integral_sum = 0.0 @@ -36,5 +36,5 @@ run_time = time() - start_time; -print "\npi with", num_steps, "steps is", pi, "in", run_time, "seconds\n" +print("\npi with", num_steps, "steps is", pi, "in", run_time, "seconds\n")