Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
84 lines (68 loc) · 2.88 KB
/
Copy pathapp.py
File metadata and controls
84 lines (68 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import time
import numpy as np
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor
import tensorflow as tf
# Removed: import cupy as cp
# Simulate a computationally intensive task (e.g., matrix multiplication)
def cpu_task(data):
result = np.dot(data, data.T) # CPU-based task
return result
# Originally meant for GPU; now replaced with NumPy for CPU fallback
def pseudo_gpu_task(data):
# Simulate GPU task using NumPy on CPU
result = np.dot(data, data.T)
return result
# Simulate a deep learning task using TensorFlow (uses CPU if GPU unavailable)
def deep_learning_task(data):
model = tf.keras.Sequential([
tf.keras.layers.Dense(64, input_dim=data.shape[1], activation='relu'),
tf.keras.layers.Dense(32, activation='relu'),
tf.keras.layers.Dense(1, activation='sigmoid')
])
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
labels = np.random.randint(0, 2, size=(data.shape[0], 1))
model.fit(data, labels, epochs=1, batch_size=32, verbose=0)
return model.evaluate(data, labels, verbose=0)
# Task Offloading decision logic
def offload_task(data, task_type):
if task_type == 'cpu':
print("Using CPU for the task")
return cpu_task(data)
elif task_type == 'gpu':
print("Simulated GPU task using CPU (no GPU available)")
return pseudo_gpu_task(data)
elif task_type == 'deep_learning':
print("Using TensorFlow for deep learning task")
return deep_learning_task(data)
else:
print("Invalid task type")
return None
# Auto-Parallelization System (APTOS)
class APTOS:
def __init__(self):
self.executor_cpu = ThreadPoolExecutor(max_workers=4)
self.executor_gpu = ProcessPoolExecutor(max_workers=2)
self.executor_dl = ThreadPoolExecutor(max_workers=1)
def run_parallel_tasks(self, data, task_type):
if task_type == 'cpu':
future = self.executor_cpu.submit(cpu_task, data)
elif task_type == 'gpu':
future = self.executor_gpu.submit(pseudo_gpu_task, data)
elif task_type == 'deep_learning':
future = self.executor_dl.submit(deep_learning_task, data)
else:
raise ValueError("Unknown task type.")
return future.result()
# Example usage
if __name__ == "__main__":
data = np.random.rand(1000, 100)
aptos = APTOS()
start_time = time.time()
cpu_result = aptos.run_parallel_tasks(data, 'cpu')
print(f"CPU Task Completed in {time.time() - start_time:.4f} seconds.")
start_time = time.time()
gpu_result = aptos.run_parallel_tasks(data, 'gpu')
print(f"Simulated GPU Task Completed in {time.time() - start_time:.4f} seconds.")
start_time = time.time()
dl_result = aptos.run_parallel_tasks(data, 'deep_learning')
print(f"Deep Learning Task Completed in {time.time() - start_time:.4f} seconds.")