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

Aria Moradi #3

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,4 @@ cython_debug/
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
.idea/
1 change: 1 addition & 0 deletions CPU GPU Computing Evaluation.ipynb

Large diffs are not rendered by default.

Binary file added CPU GPU Computing Evaluation.pdf
Binary file not shown.
132 changes: 100 additions & 32 deletions Scenario1.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,42 @@
# ************************ Written by Alireza ************************************
""" The Explanation of the Code:
In this code, we start by defining the size of the square matrices to be multiplied (matrix_size).
We then generate two random matrices (matrix_a and matrix_b) using the np.random.rand function.

We define two functions: cpu_matrix_multiplication and gpu_matrix_multiplication,
which perform matrix multiplication using CPU and GPU, respectively.

In the CPU function, we use the NumPy np.dot function for matrix multiplication.
In the GPU function, we use the cp.asarray function from the cupy library
to transfer the matrices to the GPU, perform matrix multiplication using cp.dot,
and then transfer the result back to the CPU using cp.asnumpy.

After running the CPU and GPU matrix multiplication functions,
we can optionally compare the results and print the execution times.

Please note that for the GPU matrix multiplication to work,
you need to have the cupy library installed, which provides
a NumPy-compatible interface for GPU computations.
Also, make sure you have a compatible GPU and CUDA drivers installed.

Feel free to adjust the matrix size and modify the code according to your specific requirements.
"""

import numpy as np
import cupy as cp
import tensorflow as tf
import torch
import time

# Define the matrix sizes
matrix_size = 1000
matrix_a = np.random.rand(matrix_size, matrix_size)
matrix_b = np.random.rand(matrix_size, matrix_size)
test_count = 100
matrix_pairs = []

for i in range(test_count):
matrix_a = np.random.rand(matrix_size, matrix_size)
matrix_b = np.random.rand(matrix_size, matrix_size)
matrix_pairs.append([matrix_a, matrix_b])


# CPU Matrix Multiplication
def cpu_matrix_multiplication(matrix_a, matrix_b):
Expand All @@ -18,8 +49,9 @@ def cpu_matrix_multiplication(matrix_a, matrix_b):
execution_time = end_time - start_time
return cpu_result, execution_time


# GPU Matrix Multiplication
def gpu_matrix_multiplication(matrix_a, matrix_b):
def gpu_matrix_multiplication_cupy(matrix_a, matrix_b):
start_time = time.time()

# Perform matrix multiplication using GPU
Expand All @@ -32,41 +64,77 @@ def gpu_matrix_multiplication(matrix_a, matrix_b):
execution_time = end_time - start_time
return cpu_result, execution_time


def gpu_matrix_multiplication_tensorflow(matrix_a, matrix_b):
start_time = time.time()

with tf.device('/GPU:0'):
gpu_result = tf.matmul(matrix_a, matrix_b)

end_time = time.time()
execution_time = end_time - start_time
return gpu_result, execution_time


def gpu_matrix_multiplication_pytorch(matrix_a, matrix_b):
start_time = time.time()

gpu_matrix_a = torch.from_numpy(matrix_a)
gpu_matrix_b = torch.from_numpy(matrix_b)
gpu_result = torch.matmul(gpu_matrix_a, gpu_matrix_b)
gpu_result = gpu_result.numpy()

end_time = time.time()
execution_time = end_time - start_time
return gpu_result, execution_time


def cpu_test():
test_result = []

for i in range(test_count):
result, execution_time = cpu_matrix_multiplication(matrix_a, matrix_b)
# test_result.append([result, execution_time])
test_result.append(execution_time)

return test_result


def gpu_test(mul_function):
test_result = []

for i in range(test_count):
result, execution_time = mul_function(matrix_a, matrix_b)
# test_result.append([result, execution_time])
test_result.append(execution_time)

return test_result


# Run CPU Matrix Multiplication
cpu_result, cpu_execution_time = cpu_matrix_multiplication(matrix_a, matrix_b)
cpu_test_result = cpu_test()

# Run GPU Matrix Multiplication
gpu_result, gpu_execution_time = gpu_matrix_multiplication(matrix_a, matrix_b)
gpu_test_result_cupy = gpu_test(gpu_matrix_multiplication_cupy)
gpu_test_result_tensorflow = gpu_test(gpu_matrix_multiplication_tensorflow)
gpu_test_result_pytorch = gpu_test(gpu_matrix_multiplication_pytorch)

# Compare the results (optional)
print("CPU Result:")
print(cpu_result)
print("GPU Result:")
print(gpu_result)
# print("CPU Result:")
# print(cpu_result)
# print("GPU Result:")
# print(gpu_result)

# Print the execution times
print("CPU Execution Time:", cpu_execution_time, "seconds")
print("GPU Execution Time:", gpu_execution_time, "seconds")

""" The Explanation of the Code:
In this code, we start by defining the size of the square matrices to be multiplied (matrix_size).
We then generate two random matrices (matrix_a and matrix_b) using the np.random.rand function.

We define two functions: cpu_matrix_multiplication and gpu_matrix_multiplication,
which perform matrix multiplication using CPU and GPU, respectively.
print("CPU Execution Time: ", cpu_test_result, "seconds")
print("GPU Execution Time (CuPy): ", gpu_test_result_cupy, "seconds")
print("GPU Execution Time (TensorFlow): ", gpu_test_result_tensorflow, "seconds")
print("GPU Execution Time (PyTorch): ", gpu_test_result_pytorch, "seconds")

In the CPU function, we use the NumPy np.dot function for matrix multiplication.
In the GPU function, we use the cp.asarray function from the cupy library
to transfer the matrices to the GPU, perform matrix multiplication using cp.dot,
and then transfer the result back to the CPU using cp.asnumpy.

After running the CPU and GPU matrix multiplication functions,
we can optionally compare the results and print the execution times.
print("\n")

Please note that for the GPU matrix multiplication to work,
you need to have the cupy library installed, which provides
a NumPy-compatible interface for GPU computations.
Also, make sure you have a compatible GPU and CUDA drivers installed.
print("CPU Execution Time Average: ", sum(cpu_test_result) / test_count, "seconds")
print("GPU Execution Time Average (CuPy): ", sum(gpu_test_result_cupy) / test_count, "seconds")
print("GPU Execution Time Average (TensorFlow): ", sum(gpu_test_result_tensorflow) / test_count, "seconds")
print("GPU Execution Time Average (PyTorch): ", sum(gpu_test_result_pytorch) / test_count, "seconds")

Feel free to adjust the matrix size and modify the code according to your specific requirements.
"""
168 changes: 138 additions & 30 deletions Scenario2.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,38 @@
# ****************************************** written by Alireza ***************************************
""" Code Definition to use:
In this code, we start by loading an image for processing using the cv2.imread function.
Then, we define two functions: cpu_image_processing and gpu_image_processing,
which perform image processing operations using CPU and GPU, respectively.

In the example, we convert the image to grayscale using the cv2.cvtColor function
for both CPU and GPU processing.

The cpu_image_processing function measures the execution time using the time module.
Similarly, the gpu_image_processing function measures the execution time
but with GPU-accelerated operations using the OpenCV CUDA module.

After running the CPU and GPU image processing functions,
the results are displayed using cv2.imshow, and the execution times are printed.

Please note that for this code to work, you need to have OpenCV installed with CUDA support.
Additionally, you may need to modify the image processing operations based on
your specific requirements.

Remember to replace "path_to_your_image.jpg" with the actual path to your image file
"""

import cv2
import numpy as np
import time
import cupy as cp

# Load an image for processing
image_path = "path_to_your_image.jpg"
image = cv2.imread(image_path)
image_paths = [
"drive/MyDrive/3.jpg",
"drive/MyDrive/1.jpg",
"drive/MyDrive/bigImage.png"
]

test_count = 50

# CPU Image Processing
def cpu_image_processing(image):
Expand All @@ -18,8 +46,22 @@ def cpu_image_processing(image):
execution_time = end_time - start_time
return gray_image, execution_time

def cpu_image_processing_numpy(image):
start_time = time.time()

second = np.array([0.299, 0.587, 0.114])
gray_image = np.dot(image, second)


end_time = time.time()

# end_time = time.time()

execution_time = end_time - start_time
return gray_image, execution_time

# GPU Image Processing
def gpu_image_processing(image):
def gpu_image_processing_cv2_cuda(image):
start_time = time.time()

# Perform image processing operations using GPU
Expand All @@ -33,38 +75,104 @@ def gpu_image_processing(image):
execution_time = end_time - start_time
return gray_image, execution_time

# Run CPU Image Processing
cpu_result, cpu_execution_time = cpu_image_processing(image)

# Run GPU Image Processing
gpu_result, gpu_execution_time = gpu_image_processing(image)
def gpu_image_processing_cupy(image):
# start_time = time.time()

# Display the results and execution times
cv2.imshow("CPU Result", cpu_result)
cv2.imshow("GPU Result", gpu_result)
cv2.waitKey(0)
# gray_image = np.dot(image, [0.2989, 0.5870, 0.1140]).astype(np.uint8)
gpu_image = cp.asarray(image)
second = cp.asarray([0.299, 0.587, 0.114])

print("CPU Execution Time:", cpu_execution_time, "seconds")
print("GPU Execution Time:", gpu_execution_time, "seconds")
start_time = time.time()

""" Code Definition to use:
In this code, we start by loading an image for processing using the cv2.imread function.
Then, we define two functions: cpu_image_processing and gpu_image_processing,
which perform image processing operations using CPU and GPU, respectively.

In the example, we convert the image to grayscale using the cv2.cvtColor function
for both CPU and GPU processing.
gpu_result = cp.dot(gpu_image, second)
x = gpu_result[0][0]

The cpu_image_processing function measures the execution time using the time module.
Similarly, the gpu_image_processing function measures the execution time
but with GPU-accelerated operations using the OpenCV CUDA module.
end_time = time.time()

After running the CPU and GPU image processing functions,
the results are displayed using cv2.imshow, and the execution times are printed.

Please note that for this code to work, you need to have OpenCV installed with CUDA support.
Additionally, you may need to modify the image processing operations based on
your specific requirements.
gray_image = cp.asnumpy(gpu_result)

# end_time = time.time()

execution_time = end_time - start_time
return gray_image, execution_time


def cpu_test(image):
test_result = []

for i in range(test_count):
result, execution_time = cpu_image_processing(image)
test_result.append(execution_time)

return test_result

def cpu_test_numpy(image):
test_result = []

for i in range(test_count):
result, execution_time = cpu_image_processing_numpy(image)
test_result.append(execution_time)

return test_result

def gpu_test_cupy(image):
test_result = []

for i in range(test_count):
result, execution_time = gpu_image_processing_cupy(image)
test_result.append(execution_time)

return test_result

def gpu_test_cuda(image):
test_result = []

for i in range(test_count):
result, execution_time = gpu_image_processing_cv2_cuda(image)
test_result.append(execution_time)

return test_result

for image_path in image_paths:
print("Testing image ", image_path)

image = cv2.imread(image_path)

print("Image size is ", image.shape)

# Run CPU Image Processing
# cpu_result, cpu_execution_time = cpu_image_processing(image)
cpu_test_result = cpu_test(image)
cpu_test_result_numpy = cpu_test_numpy(image)

# Run GPU Image Processing
# gpu_result, gpu_execution_time = gpu_image_processing_2(image)
gpu_test_result_cupy = gpu_test_cupy(image)
# gpu_test_result_cuda = gpu_test_cuda(image)

# print(cpu_result)
# print(gpu_result)

# Display the results and execution times
# cv2.imshow("CPU Result", cpu_result)
# cv2.imshow("GPU Result", gpu_result)
# cv2.waitKey(0)

print("CPU Execution Time: ", cpu_test_result, "seconds")
print("CPU Execution Time (numPy): ", cpu_test_result_numpy, "seconds")
print("GPU Execution Time (CuPy): ", gpu_test_result_cupy, "seconds")
# print("GPU Execution Time (CV2_CUDA): ", gpu_test_result_cuda, "seconds")

print("\n")

print("CPU Execution Time Average: ", sum(cpu_test_result) / test_count, "seconds")
print("CPU Execution Time Average (NumPy): ", sum(cpu_test_result_numpy) / test_count, "seconds")
print("GPU Execution Time Average (CuPy): ", sum(gpu_test_result_cupy) / test_count, "seconds")
# print("GPU Execution Time Average (CUDA): ", sum(gpu_test_result_cuda) / test_count, "seconds")

print("#################\n\n\n")


Remember to replace "path_to_your_image.jpg" with the actual path to your image file
"""
Binary file added image/drive/MyDrive/1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/drive/MyDrive/3.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/drive/MyDrive/bigImage.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
numpy==1.25.1
cupy==12.1.0
tensorflow==2.13.0