Files
strategy-lab/research/tests/ray_test.ipynb
David Brazda 48db2bc9de daily update
2024-10-03 17:05:49 +02:00

127 lines
3.5 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"2024-10-03 09:43:41,741\tINFO worker.py:1786 -- Started a local Ray instance.\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"waiting for ray\n",
"ray returned all\n",
"ray finsihed 0.4031927839969285\n",
"worker 0 started\n",
"worker 0 finsihed\n",
"worker 1 started\n",
"worker 1 finsihed\n",
"worker 2 started\n",
"worker 2 finsihed\n",
"worker 3 started\n",
"worker 3 finsihed\n",
"serial function finsihed 0.21200023603159934\n",
"Ray with 4 parts: 0.4031927839969285 seconds\n",
"Serial: 0.21200023603159934 seconds\n",
"Serial computation is faster than Ray with 4 parts\n"
]
}
],
"source": [
"import numpy as np\n",
"import timeit\n",
"import ray\n",
"\n",
"# Define the expensive function\n",
"@ray.remote\n",
"def expensive_function(n):\n",
" # Generate a large random matrix\n",
" A = np.random.rand(1000, 1000)\n",
" B = np.random.rand(1000, 1000)\n",
"\n",
" # Perform the matrix multiplication\n",
" C = np.dot(A, B)\n",
" # Return the result\n",
" return C\n",
"\n",
"def expensive_function_serial(n):\n",
" print(f\"worker {n} started\")\n",
" # Generate a large random matrix\n",
" A = np.random.rand(1000, 1000)\n",
" B = np.random.rand(1000, 1000)\n",
"\n",
" # Perform the matrix multiplication\n",
" C = np.dot(A, B)\n",
"\n",
" # Return the result\n",
" print(f\"worker {n} finsihed\")\n",
" return C\n",
"\n",
"# Initialize Ray\n",
"ray.init()\n",
"\n",
"# Create 4 remote actors to distribute the work\n",
"futures = [expensive_function.remote(_) for _ in range(4)]\n",
"\n",
"# Time the function using Ray with 4 parts\n",
"start_time = timeit.default_timer()\n",
"print(\"waiting for ray\")\n",
"results = ray.get(futures)\n",
"print(\"ray returned all\")\n",
"end_time = timeit.default_timer()\n",
"ray_time = end_time - start_time\n",
"print(\"ray finsihed\", ray_time)\n",
"\n",
"# Time the function serially\n",
"start_time = timeit.default_timer()\n",
"results = [expensive_function_serial(_) for _ in range(4)]\n",
"end_time = timeit.default_timer()\n",
"serial_time = end_time - start_time\n",
"print(\"serial function finsihed\", serial_time)\n",
"\n",
"\n",
"# Print the results\n",
"print(f\"Ray with 4 parts: {ray_time} seconds\")\n",
"print(f\"Serial: {serial_time} seconds\")\n",
"\n",
"# Compare the results\n",
"if ray_time < serial_time:\n",
" print(\"Ray with 4 parts is faster than serial computation\")\n",
"else:\n",
" print(\"Serial computation is faster than Ray with 4 parts\")\n",
"\n",
"# Shutdown Ray\n",
"ray.shutdown()\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.11"
}
},
"nbformat": 4,
"nbformat_minor": 2
}