finished openmp program

This commit is contained in:
Ziad Arafat committed 2023-11-04 21:50:09 -06:00
1 parent f092b3b621
commit a8c3a9589e
22 files changed
+1833 -110

No files matched your search

+7 -53
View File
@@ -5,60 +5,14 @@ count_threes: count_threes.c
gcc -fopenmp -o count_threes count_threes.c
run:
# run with 1 thread
export OMP_NUM_THREADS=1
./count_threes 10000000 >> results.txt
./count_threes 10000000 >> results.txt
./count_threes 10000000 >> results.txt
./count_threes 10000000 >> results.txt
./count_threes 10000000 >> results.txt
awk '{s+=$$1} END {print "1 thread: " s/5}' results.txt
# run with 2 threads
export OMP_NUM_THREADS=2
./count_threes 10000000 >> results.txt
./count_threes 10000000 >> results.txt
./count_threes 10000000 >> results.txt
./count_threes 10000000 >> results.txt
./count_threes 10000000 >> results.txt
awk '{s+=$$1} END {print "2 threads: " s/5}' results.txt
# run with 4 threads
export OMP_NUM_THREADS=4
./count_threes 10000000 >> results.txt
./count_threes 10000000 >> results.txt
./count_threes 10000000 >> results.txt
./count_threes 10000000 >> results.txt
./count_threes 10000000 >> results.txt
awk '{s+=$$1} END {print "4 threads: " s/5}' results.txt
# run with 8 threads
export OMP_NUM_THREADS=8
./count_threes 10000000 >> results.txt
./count_threes 10000000 >> results.txt
./count_threes 10000000 >> results.txt
./count_threes 10000000 >> results.txt
./count_threes 10000000 >> results.txt
awk '{s+=$$1} END {print "8 threads: " s/5}' results.txt
# run with 16 threads
export OMP_NUM_THREADS=16
./count_threes 10000000 >> results.txt
./count_threes 10000000 >> results.txt
./count_threes 10000000 >> results.txt
./count_threes 10000000 >> results.txt
./count_threes 10000000 >> results.txt
awk '{s+=$$1} END {print "16 threads: " s/5}' results.txt
# run with 32 threads
export OMP_NUM_THREADS=32
./count_threes 10000000 >> results.txt
./count_threes 10000000 >> results.txt
./count_threes 10000000 >> results.txt
./count_threes 10000000 >> results.txt
./count_threes 10000000 >> results.txt
awk '{s+=$$1} END {print "32 threads: " s/5}' results.txt
for threads in 1 2 4 8 16 32 64; do \
echo "$$threads threads" >> results.txt; \
for i in {1..5}; do \
OMP_NUM_THREADS=$$threads ./count_threes 1000000000 >> results.txt; \
done; \
done
clean:
rm -f count_threes results.txt
Binary file not shown.
+14 -5
View File
@@ -3,16 +3,25 @@
#include <time.h>
#include <omp.h>
int count3s(int* array, int length)
{
int count = 0;
int i;
#pragma omp parallel for reduction(+:count)
for (i = 0; i < length; i++) {
if (array[i] == 3) {
count++;
#pragma omp parallel shared(array, count, length) private(i)
{
int count_p = 0;
#pragma omp for
for (i = 0; i < length; i++) {
if (array[i] == 3) {
count_p++;
}
}
// #pragma omp critical
{
count += count_p;
}
}
@@ -0,0 +1,261 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Times for 1 thread: [1.503683, 1.503534, 1.504619, 1.504074, 1.504186]\n",
"Times for 2 threads: [0.753239, 0.761634, 0.752398, 0.754443, 0.752339]\n",
"Times for 4 threads: [0.432492, 0.380995, 0.684529, 0.43349, 0.427252]\n",
"Times for 8 threads: [0.238623, 0.24504, 0.241586, 0.240508, 0.189636]\n",
"Times for 16 threads: [0.09745, 0.096661, 0.128006, 0.125363, 0.125371]\n",
"Times for 32 threads: [0.08041, 0.069265, 0.078767, 0.069674, 0.086196]\n",
"Times for 64 threads: [0.085384, 0.087096, 0.064716, 0.061082, 0.062371]\n"
]
}
],
"source": [
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
" # Provided program output as a string\n",
"program_output = \"\"\"\n",
"1 threads\n",
"count: 99991099\n",
"Time taken: 1.503683 seconds\n",
"count: 99995163\n",
"Time taken: 1.503534 seconds\n",
"count: 99995800\n",
"Time taken: 1.504619 seconds\n",
"count: 99978792\n",
"Time taken: 1.504074 seconds\n",
"count: 100003511\n",
"Time taken: 1.504186 seconds\n",
"2 threads\n",
"count: 100014341\n",
"Time taken: 0.753239 seconds\n",
"count: 100001384\n",
"Time taken: 0.761634 seconds\n",
"count: 99994972\n",
"Time taken: 0.752398 seconds\n",
"count: 99980303\n",
"Time taken: 0.754443 seconds\n",
"count: 100003100\n",
"Time taken: 0.752339 seconds\n",
"4 threads\n",
"count: 99997517\n",
"Time taken: 0.432492 seconds\n",
"count: 100002006\n",
"Time taken: 0.380995 seconds\n",
"count: 100013130\n",
"Time taken: 0.684529 seconds\n",
"count: 100001312\n",
"Time taken: 0.433490 seconds\n",
"count: 99992179\n",
"Time taken: 0.427252 seconds\n",
"8 threads\n",
"count: 99990805\n",
"Time taken: 0.238623 seconds\n",
"count: 100001043\n",
"Time taken: 0.245040 seconds\n",
"count: 100005162\n",
"Time taken: 0.241586 seconds\n",
"count: 100011768\n",
"Time taken: 0.240508 seconds\n",
"count: 49995496\n",
"Time taken: 0.189636 seconds\n",
"16 threads\n",
"count: 37498358\n",
"Time taken: 0.097450 seconds\n",
"count: 31255893\n",
"Time taken: 0.096661 seconds\n",
"count: 81246280\n",
"Time taken: 0.128006 seconds\n",
"count: 93751286\n",
"Time taken: 0.125363 seconds\n",
"count: 87496828\n",
"Time taken: 0.125371 seconds\n",
"32 threads\n",
"count: 100003281\n",
"Time taken: 0.080410 seconds\n",
"count: 21877522\n",
"Time taken: 0.069265 seconds\n",
"count: 87495971\n",
"Time taken: 0.078767 seconds\n",
"count: 75019297\n",
"Time taken: 0.069674 seconds\n",
"count: 96885388\n",
"Time taken: 0.086196 seconds\n",
"64 threads\n",
"count: 71864758\n",
"Time taken: 0.085384 seconds\n",
"count: 82811032\n",
"Time taken: 0.087096 seconds\n",
"count: 84369914\n",
"Time taken: 0.064716 seconds\n",
"count: 76551863\n",
"Time taken: 0.061082 seconds\n",
"count: 84397515\n",
"Time taken: 0.062371 seconds\n",
"\"\"\"\n",
"\n",
"# Split the output into lines\n",
"lines = program_output.strip().split(\"\\n\")\n",
"\n",
"# Initialize lists to hold the times for each thread count\n",
"times_1_thread = []\n",
"times_2_threads = []\n",
"times_4_threads = []\n",
"times_8_threads = []\n",
"times_16_threads = []\n",
"times_32_threads = []\n",
"times_64_threads = []\n",
"\n",
"# Initialize a list of lists to make it easier to append in a loop\n",
"all_times = [\n",
" times_1_thread,\n",
" times_2_threads,\n",
" times_4_threads,\n",
" times_8_threads,\n",
" times_16_threads,\n",
" times_32_threads,\n",
" times_64_threads,\n",
"]\n",
"\n",
"# Current thread count index\n",
"thread_count_index = 0\n",
"\n",
"# Parse the output and fill the lists\n",
"for line in lines:\n",
" if \"Time taken:\" in line:\n",
" # Extract the time taken from the line\n",
" time_taken = float(line.split()[-2])\n",
" # Append the time to the appropriate list\n",
" all_times[thread_count_index].append(time_taken)\n",
" # Check if the current thread count's list is filled with 5 runs\n",
" if len(all_times[thread_count_index]) == 5:\n",
" # Move to the next thread count\n",
" thread_count_index += 1\n",
"\n",
"# Now all_times contains the lists of times for each thread count\n",
"print(\"Times for 1 thread:\", times_1_thread)\n",
"print(\"Times for 2 threads:\", times_2_threads)\n",
"print(\"Times for 4 threads:\", times_4_threads)\n",
"print(\"Times for 8 threads:\", times_8_threads)\n",
"print(\"Times for 16 threads:\", times_16_threads)\n",
"print(\"Times for 32 threads:\", times_32_threads)\n",
"print(\"Times for 64 threads:\", times_64_threads)\n",
"\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAmUAAAJwCAYAAADIlBYPAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjguMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/SrBM8AAAACXBIWXMAAA9hAAAPYQGoP6dpAABmvklEQVR4nO3dd3hTZf/H8c9J0qYtpaXsVbZSloCgCCiIIBURJ4qiMlzog4KgPIoLJ0OZ+qAoKjhAcYETFBFQXPzYqICykT27gI7k/P4oSRuatik0Tdq8X9fVS3JyTs43aYVP7/s+32OYpmkKAAAAAWUJdAEAAAAglAEAAAQFQhkAAEAQIJQBAAAEAUIZAABAECCUAQAABAFCGQAA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tK0cOFCn2tB0RDKUCSzZs3S9ddfr/DwcN1yyy36559/3P9ghYWF6brrrtO8efOUkZHhcdy8efOUnp6um2++WVL2P1xXX321xo8fr169eumVV17Rtddeq0mTJqlPnz55zvvDDz9o2LBh6tOnj6ZMmeKe3pgyZYpat26tZ599VqNHj5bNZtONN96or7/+2uP4AQMG6JVXXtGVV16pcePGKTIyUj179sxznv379+uiiy7S999/r/vvv19TpkxRo0aNdOedd2ry5Mln/LkdPnxYPXr0UKtWrTR58uQ80y2+euCBB7R27VqNGjVK9913n7788kuPhfKTJ09W7dq1lZCQoPfee0/vvfeeHn/88QJf8+jRo7rqqqvUrl07vfjii7Lb7br55ps1Z84c3Xzzzbryyis1duxYpaWlqXfv3kpJSXEfWxyf19KlS/Xggw/qtttu07PPPqvDhw/riiuu0B9//CEpOzjGx8d7/ALgMmvWLDVs2FDt27f36Vy+mD17tl566SUNGjRIzz//vLZv367rr79emZmZHvtlZWUpMTFRVatW1fjx43XDDTdIkgYNGqQRI0aoY8eOmjJligYOHKhZs2YpMTHR4zVmzpyp6OhoDR8+XFOmTFGbNm301FNP6dFHH/U4z1tvvaVBgwapevXqevHFF9WxY8d8w8Lp5s+fr6ysLN1+++0+vfc///xTl1xyidauXav//ve/evLJJ7Vt2zZdeuml+v333316DW82b96s3r176/LLL9eECRMUFxenAQMG6M8//5SUPbU+ZMgQSdJjjz3m/tlt0qRJvq/5yy+/yDAMtW7d2mP7l19+KUnq16+f1+NiY2N1zTXXaOPGjdq8ebPHcy+88IK+/vprPfLIIxoyZIgWLlyobt266cSJE+59fvjhB3Xq1EnJyckaNWqURo8erWPHjumyyy7T8uXL85zvpptuUkpKisaMGaObbrpJM2fO1DPPPOO1tr59+2r58uXasmWLe9vs2bPVu3dvhYWF5ftZnM51fKVKlfLd54svvpAkn382JKlp06aKjIzME0BRjAI9VIfSY8WKFaYkc+HChaZpmqbT6TRr165tDh061L3Pt99+a0oyv/zyS49jr7zySrNBgwbux++9955psVjyDJtPmzbNlG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ndd999Ho8//vhjxcbG6vLLL/f4PNq0aaPo6GiPn+vcP7spKSk6dOiQLrnkEh0/flwbN26UJK1YsUIHDhzQvffe63GBxoABAxQLine truncated
"text/plain": [
"<Figure size 700x700 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# Assuming the previous code has been executed and all_times is populated\n",
"\n",
"# Calculate the average time for each thread count\n",
"average_times = [sum(times) / len(times) for times in all_times]\n",
"\n",
"# max_threads = 64\n",
"# Thread counts\n",
"thread_counts = [1,2,4,8,16,32,64]\n",
"# Plotting the running time bar graph\n",
"plt.figure(figsize=(7, 7))\n",
"plt.plot(thread_counts, average_times, color='skyblue', )\n",
"\n",
"# Adding titles and labels\n",
"plt.title('Average runtime by Thread Count (OpenMP C)')\n",
"plt.xlabel('Number of Threads')\n",
"plt.ylabel('Running time (s)')\n",
"plt.xticks(thread_counts)\n",
"\n",
"# plt.xscale('log', base=2)\n",
"# Display the bar graph\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA1cAAAJwCAYAAABlHJvKAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjguMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/SrBM8AAAACXBIWXMAAA9hAAAPYQGoP6dpAABYHUlEQVR4nO3deVgV5f//8deRXQRcABFFxBUTt9QMNypNNMOlsjTNXVswM8uSNs0ltMWlckm/ppaZZi6llWamlqmVu5aSIgqmuJSAoKHB/P7ox/l0ZJGTg3D0+biuua7mnntm3nPOkXhxz9zHYhiGIQAAAADANSlV3AUAAAAAwI2AcAUAAAAAJiBcAQAAAIAJCFcAAAAAYALCFQAAAACYgHAFAAAAACYgXAEAAACACQhXAAAAAGACwhUAAAAAmIBwBQDXgcVi0ZgxY677eTdu3CiLxaJPP/30up87L/Pnz5fFYtH27duLu5RCKa73rSTJzs5WWFiYJkyY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qjjz5SnTp15O7uriZNmui7777Lte/vv/+uAQMGqGLFinJzc1O9evX0/vvv5+qXmZmp0aNHq2bNmnJzc1NQUJCee+45ZWZm5ur39NNPy8/PT15eXurcubOOHz+e63j9+vVTtWrVcrWPGTNGFovlP19PfrKysvTCCy8oICBAnp6e6ty5s5KSkqzbR48eLRcXF505cybXvkOGDFHZsmVNvVUpMzNTI0aMkJ+fnzw9PdWtW7dc565WrZruvfderV27Vk2bNpWHh4fee+89SVJKSoqGDx+uoKAgubm5qWbNmpo0aVKu27TefPNNtWjRQhUqVJCHh4eaNGmS5/NnhX3f8rJs2TLt2bNHL774olq1apVru7e3d67nlZYuXaomTZrIw8NDvr6+6t27t37//XebPnfccYfuuOOOXMe78rOTc8vbm2++qdmzZ6tGjRpyc3NTs2bN9PPPP9vsN336dEmyuc2tICtXrpSrq6vatGlj0/7GG2/owoULmj17dq4RSV9fX7333nvKyMjQ66+/bm3P+WwfPHhQDz74oLy9vVWhQgU99dRTeX62Fi5caH2Nypcvrx49eth8ZnNeo7CwMP3666+68847Vbp0aVWuXNnmvP8WHh6ukJAQLVq0yKb9o48+UocOHVS+fPkCX48cpUuX1u23366MjIw8/83keP3115Wenq65c+eqUqVKubbXrFlTTz31lE3b3Xffrc2bN+vPP/8sVC0AShbCFQCHEBwcrB07dmj//v2F6r9p0yYNHz5cvXv31tixY/XHH3+oQ4cONvufOnVKt99+u7755hsNHTpU06ZNU82aNTVw4EBNnTrV2i87O1udO3fWm2++qaioKL3zzjvq2rWrpkyZooceesjmvIMGDdLUqVPVvn17TZw4US4uLurUqdM1X39hrqcgEyZM0BdffKHnn39ew4YN07p169SuXTvr8x6PPPKI/v77by1ZssRmv0uXLunTTz/V/fffn+8tU//Fk08+qT179mj06NF6/PHHtWrVqjyf6YmLi1PPnj119913a9q0aWrUqJEuXLigiIgILVy4UH369NHbb7+tli1bKiYmRiNGjLDZf9q0aWrcuLHGjh2r1157Tc7Ozurevbu++OILm37X8r59/vnnkv55DQtj/vz5ev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2aWNerVq2qLl26aO3atcrKypJhGFq2bJmioqJkGIbOnj1rXSIjI5WamqqdO3dK+ucWrrp16yo0NNSm31133SVJ2rBhg6R/JguQpGHDhtnUMnz48Gu+/qtdz9X06dNHXl5e1vUHHnhAlSpVstac0+fHH39UfHy8te2jjz5SUFCQIiIirvka/m3IkCE2t6S1bt1aWVlZOnbsmE2/kJAQRUZG2rQtXbpUrVu3Vrly5Wzej3bt2ikrK8vmdkkPDw/rf587d06pqalq3bq19b2Vrv19S0tLs3ltC7J9+3adPn1aTzzxhM1IYKdOnRQaGprnSE9hPfTQQypXrpx1PWdU6ciRI//5mH/88YfNMSXp/PnzknTVa87ZnpaWZtMeHR1ts54zGU3O+7B8+XJlZ2frwQcftHl/AwICVKtWLeu/txxlypRR7969reuurq667bbb8r3uevXqqUGDBvr4448l/TPi16VLF5UuXTrfazl48KD8/Pzk5+enunXr6p133lGnTp3yvIU4R851F/azkSPn9T579qxd+wEoGZgtEIDDaNasmZYvX65Lly5pz549WrFihaZMmaIHHnhAu3fv1i233GLtW6tWrVz7165dWxcuXNCZM2dUqlQppaSkaPbs2daZuq50+vRpSdKhQ4d04MCBfGe7y+l37NgxlSpVSjVq1LDZXqdOnf90vf92tesJCAiwa3+LxaKaNWvaTI/+0EMPafjw4froo4/0yiuvKDU1VatXr9bTTz991Wdz7FW1alWb9ZxfKM+dO2fTHhISkmvfQ4cOae/evVd9PyRp9erVGj9+vHbv3m3zfNy/r+da3zdvb+9CB5ic8JjXsUNDQ7V58+ZCHScvhX1N7WX8/9vncuSEhZyQlZ/8QtiVn8UaNWqoVKlS1s/ioUOHZBhGnp95SbluUaxSpUquz2e5cuW0d+/efGt7+OGH9dZbb+npp5/Wli1b9MILLxR4LdWqVdOcOXNksVjk7u6uWrVq5XrW80re3t6Srv46XSnn9Tb73xyA64NwBcDhuLq6qlmzZmrWrJlq166t/v37a+nSpRo9enShj5Ez8UHv3r3Vt2/fPPs0aNDA2rd+/fqaPHlLine truncated
"text/plain": [
"<Figure size 1000x700 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# Calculate speedup compared to single-threaded execution\n",
"speedup = [average_times[0] / time for time in average_times]\n",
"\n",
"# Plotting the speedup bar graph\n",
"plt.figure(figsize=(10, 7))\n",
"plt.bar(thread_counts, speedup, color='skyblue')\n",
"\n",
"# Adding titles and labels\n",
"plt.title('Speedup by Thread Count (OpenMP C)')\n",
"plt.xlabel('Number of Threads')\n",
"plt.ylabel('Speedup Factor')\n",
"plt.xticks(thread_counts)\n",
"\n",
"\n",
"# Display the bar graph\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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.13"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
@@ -0,0 +1,218 @@
import matplotlib.pyplot as plt
# Provided program output as a string
program_output = """
Counting 3s sequentially...
finished counting 3s
count: 99994272
Time taken: 1.528885 seconds
Counting 3s sequentially...
finished counting 3s
count: 100004192
Time taken: 1.521651 seconds
Counting 3s sequentially...
finished counting 3s
count: 100000476
Time taken: 1.523760 seconds
Counting 3s sequentially...
finished counting 3s
count: 100014445
Time taken: 1.524873 seconds
Counting 3s sequentially...
finished counting 3s
count: 100024069
Time taken: 1.527630 seconds
Counting 3s sequentially...
finished counting 3s
count: 100009123
Time taken: 0.762074 seconds
Counting 3s sequentially...
finished counting 3s
count: 100015500
Time taken: 0.763967 seconds
Counting 3s sequentially...
finished counting 3s
count: 100008976
Time taken: 0.763413 seconds
Counting 3s sequentially...
finished counting 3s
count: 100010319
Time taken: 0.764237 seconds
Counting 3s sequentially...
finished counting 3s
count: 99981263
Time taken: 0.762605 seconds
Counting 3s sequentially...
finished counting 3s
count: 100003901
Time taken: 0.442879 seconds
Counting 3s sequentially...
finished counting 3s
count: 100003640
Time taken: 0.382068 seconds
Counting 3s sequentially...
finished counting 3s
count: 100020536
Time taken: 0.381934 seconds
Counting 3s sequentially...
finished counting 3s
count: 100000475
Time taken: 0.385530 seconds
Counting 3s sequentially...
finished counting 3s
count: 100004132
Time taken: 0.381731 seconds
Counting 3s sequentially...
finished counting 3s
count: 99996709
Time taken: 0.254787 seconds
Counting 3s sequentially...
finished counting 3s
count: 100003625
Time taken: 0.191506 seconds
Counting 3s sequentially...
finished counting 3s
count: 100003638
Time taken: 0.254230 seconds
Counting 3s sequentially...
finished counting 3s
count: 100003219
Time taken: 0.191384 seconds
Counting 3s sequentially...
finished counting 3s
count: 100012445
Time taken: 0.191317 seconds
Counting 3s sequentially...
finished counting 3s
count: 99987909
Time taken: 0.133211 seconds
Counting 3s sequentially...
finished counting 3s
count: 100006337
Time taken: 0.096643 seconds
Counting 3s sequentially...
finished counting 3s
count: 99997158
Time taken: 0.096577 seconds
Counting 3s sequentially...
finished counting 3s
count: 100008637
Time taken: 0.125293 seconds
Counting 3s sequentially...
finished counting 3s
count: 99994796
Time taken: 0.131650 seconds
Counting 3s sequentially...
finished counting 3s
count: 99977435
Time taken: 0.075728 seconds
Counting 3s sequentially...
finished counting 3s
count: 100004478
Time taken: 0.074724 seconds
Counting 3s sequentially...
finished counting 3s
count: 100004689
Time taken: 0.076790 seconds
Counting 3s sequentially...
finished counting 3s
count: 100000961
Time taken: 0.070120 seconds
Counting 3s sequentially...
finished counting 3s
count: 99989321
Time taken: 0.096495 seconds
Counting 3s sequentially...
finished counting 3s
count: 99991306
Time taken: 0.072446 seconds
Counting 3s sequentially...
finished counting 3s
count: 100012069
Time taken: 0.081696 seconds
Counting 3s sequentially...
finished counting 3s
count: 99989876
Time taken: 0.079536 seconds
Counting 3s sequentially...
finished counting 3s
count: 99990048
Time taken: 0.091937 seconds
Counting 3s sequentially...
finished counting 3s
count: 99999415
Time taken: 0.065238 seconds
"""
# Split the output into lines
lines = program_output.strip().split("\n")
# Initialize lists to hold the times for each thread count
times_1_thread = []
times_2_threads = []
times_4_threads = []
times_8_threads = []
times_16_threads = []
times_32_threads = []
times_64_threads = []
# Initialize a list of lists to make it easier to append in a loop
all_times = [
times_1_thread,
times_2_threads,
times_4_threads,
times_8_threads,
times_16_threads,
times_32_threads,
times_64_threads,
]
# Current thread count index
thread_count_index = 0
# Parse the output and fill the lists
for line in lines:
if "Time taken:" in line:
# Extract the time taken from the line
time_taken = float(line.split()[-2])
# Append the time to the appropriate list
all_times[thread_count_index].append(time_taken)
# Check if the current thread count's list is filled with 5 runs
if len(all_times[thread_count_index]) == 5:
# Move to the next thread count
thread_count_index += 1
# Now all_times contains the lists of times for each thread count
print("Times for 1 thread:", times_1_thread)
print("Times for 2 threads:", times_2_threads)
print("Times for 4 threads:", times_4_threads)
print("Times for 8 threads:", times_8_threads)
print("Times for 16 threads:", times_16_threads)
print("Times for 32 threads:", times_32_threads)
print("Times for 64 threads:", times_64_threads)
# Assuming the previous code has been executed and all_times is populated
# Calculate the average time for each thread count
average_times = [sum(times) / len(times) for times in all_times]
# Calculate speedup compared to single-threaded execution
speedup = [average_times[0] / time for time in average_times]
# Thread counts
thread_counts = [1, 2, 4, 8, 16, 32]
# Plotting the speedup bar graph
plt.figure(figsize=(10, 6))
plt.bar(thread_counts, speedup, color='skyblue')
# Adding titles and labels
plt.title('Speedup by Thread Count')
plt.xlabel('Number of Threads')
plt.ylabel('Speedup Factor')
plt.xticks(thread_counts)
# Display the bar graph
plt.show()
+77
View File
@@ -0,0 +1,77 @@
1 threads
count: 99991099
Time taken: 1.503683 seconds
count: 99995163
Time taken: 1.503534 seconds
count: 99995800
Time taken: 1.504619 seconds
count: 99978792
Time taken: 1.504074 seconds
count: 100003511
Time taken: 1.504186 seconds
2 threads
count: 100014341
Time taken: 0.753239 seconds
count: 100001384
Time taken: 0.761634 seconds
count: 99994972
Time taken: 0.752398 seconds
count: 99980303
Time taken: 0.754443 seconds
count: 100003100
Time taken: 0.752339 seconds
4 threads
count: 99997517
Time taken: 0.432492 seconds
count: 100002006
Time taken: 0.380995 seconds
count: 100013130
Time taken: 0.684529 seconds
count: 100001312
Time taken: 0.433490 seconds
count: 99992179
Time taken: 0.427252 seconds
8 threads
count: 99990805
Time taken: 0.238623 seconds
count: 100001043
Time taken: 0.245040 seconds
count: 100005162
Time taken: 0.241586 seconds
count: 100011768
Time taken: 0.240508 seconds
count: 49995496
Time taken: 0.189636 seconds
16 threads
count: 37498358
Time taken: 0.097450 seconds
count: 31255893
Time taken: 0.096661 seconds
count: 81246280
Time taken: 0.128006 seconds
count: 93751286
Time taken: 0.125363 seconds
count: 87496828
Time taken: 0.125371 seconds
32 threads
count: 100003281
Time taken: 0.080410 seconds
count: 21877522
Time taken: 0.069265 seconds
count: 87495971
Time taken: 0.078767 seconds
count: 75019297
Time taken: 0.069674 seconds
count: 96885388
Time taken: 0.086196 seconds
64 threads
count: 71864758
Time taken: 0.085384 seconds
count: 82811032
Time taken: 0.087096 seconds
count: 84369914
Time taken: 0.064716 seconds
count: 76551863
Time taken: 0.061082 seconds
count: 84397515
Time taken: 0.062371 seconds
@@ -1,11 +0,0 @@
all: testopenmp run
testopenmp:
gcc -fopenmp -o testopenmp testopenmp.c
run:
export OMP_NUM_THREADS=30
./testopenmp
clean:
rm -f testopenmp
@@ -1,30 +0,0 @@
#include <stdio.h>
#include <omp.h>
void main()
{
int i;
int num;
int sum = 0;
int tsum;
int x[10];
num = omp_get_num_procs();
x[0] = 0;
#pragma omp parallel for
for (i = 1; i < 200; i++) {
// #pragma omp critical
{
tsum = sum;
sum = tsum + i;
}
printf(" %d I am running on %d\n", i, omp_get_thread_num());
}
printf("the sum is %d\n", sum);
return 0;
}
Binary file not shown.
+10 -8
View File
@@ -1,9 +1,8 @@
import java.util.*;
public class CountThrees implements Runnable {
private static int ARRAY_LENGTH;
private static final int MAX_THREADS = 10;
private static int MAX_THREADS;
private static final int MAX_RANGE = 100;
private static final Random random = new Random();
private static int count = 0;
@@ -12,14 +11,15 @@ public class CountThrees implements Runnable {
private static Thread[] threads;
public static void main(String[] args) {
if (args.length != 1) {
System.err.println("Usage: java CountThrees <array_length>");
if (args.length != 2) {
System.err.println("Usage: java CountThrees <array_length> <max_threads>");
System.exit(1);
}
try {
ARRAY_LENGTH = Integer.parseInt(args[0]);
MAX_THREADS = Integer.parseInt(args[1]);
} catch (NumberFormatException e) {
System.err.println("Invalid argument: " + args[0] + " is not a valid integer");
System.err.println("Invalid argument: " + args[0] + " or " + args[1] + " is not a valid integer");
System.exit(1);
}
array = new int[ARRAY_LENGTH];
@@ -85,8 +85,10 @@ public class CountThrees implements Runnable {
}
}
synchronized (lock) {
count += myCount;
}
// synchronized (lock) {
// count += myCount;
// }
count += myCount;
}
}
+11 -3
View File
@@ -2,7 +2,15 @@ all: CountThrees.class
CountThrees.class:
javac CountThrees.java
run:
java CountThrees 1000000000
run:
for threads in 1 2 4 8 16 32 64; do \
echo "$$threads threads" >> results.txt; \
for i in {1..5}; do \
java CountThrees 1000000000 $$threads >> results.txt; \
done \
done
clean:
rm -f CountThrees.class
rm -f CountThrees.class results.txt
@@ -0,0 +1,261 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Times for 1 thread: [0.359876473, 0.361032238, 0.360004483, 0.359855544, 0.359787787]\n",
"Times for 2 threads: [0.373749715, 0.447702756, 0.464235889, 0.372059609, 0.371326817]\n",
"Times for 4 threads: [0.222983097, 0.216992067, 0.21719135, 0.21907073, 0.22178881]\n",
"Times for 8 threads: [0.167658194, 0.165304926, 0.163078056, 0.162809502, 0.165746493]\n",
"Times for 16 threads: [0.178607489, 0.172914077, 0.174337312, 0.176571896, 0.175561715]\n",
"Times for 32 threads: [0.190420432, 0.190276435, 0.178388981, 0.180038991, 0.149067537]\n",
"Times for 64 threads: [0.160374063, 0.229213367, 0.282740373, 0.19328029, 0.199149752]\n"
]
}
],
"source": [
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
" # Provided program output as a string\n",
"program_output = \"\"\"\n",
"1 threads\n",
"Number of threes: 9997807\n",
"Time taken: 0.359876473 seconds\n",
"Number of threes: 9998567\n",
"Time taken: 0.361032238 seconds\n",
"Number of threes: 9996883\n",
"Time taken: 0.360004483 seconds\n",
"Number of threes: 9996455\n",
"Time taken: 0.359855544 seconds\n",
"Number of threes: 9998718\n",
"Time taken: 0.359787787 seconds\n",
"2 threads\n",
"Number of threes: 9998562\n",
"Time taken: 0.373749715 seconds\n",
"Number of threes: 10004232\n",
"Time taken: 0.447702756 seconds\n",
"Number of threes: 10000537\n",
"Time taken: 0.464235889 seconds\n",
"Number of threes: 9997416\n",
"Time taken: 0.372059609 seconds\n",
"Number of threes: 9996787\n",
"Time taken: 0.371326817 seconds\n",
"4 threads\n",
"Number of threes: 10004804\n",
"Time taken: 0.222983097 seconds\n",
"Number of threes: 9997107\n",
"Time taken: 0.216992067 seconds\n",
"Number of threes: 10000709\n",
"Time taken: 0.21719135 seconds\n",
"Number of threes: 9996199\n",
"Time taken: 0.21907073 seconds\n",
"Number of threes: 10008067\n",
"Time taken: 0.22178881 seconds\n",
"8 threads\n",
"Number of threes: 10006502\n",
"Time taken: 0.167658194 seconds\n",
"Number of threes: 10004407\n",
"Time taken: 0.165304926 seconds\n",
"Number of threes: 9997402\n",
"Time taken: 0.163078056 seconds\n",
"Number of threes: 9999915\n",
"Time taken: 0.162809502 seconds\n",
"Number of threes: 9999852\n",
"Time taken: 0.165746493 seconds\n",
"16 threads\n",
"Number of threes: 9997128\n",
"Time taken: 0.178607489 seconds\n",
"Number of threes: 9995240\n",
"Time taken: 0.172914077 seconds\n",
"Number of threes: 10001920\n",
"Time taken: 0.174337312 seconds\n",
"Number of threes: 10000932\n",
"Time taken: 0.176571896 seconds\n",
"Number of threes: 10005193\n",
"Time taken: 0.175561715 seconds\n",
"32 threads\n",
"Number of threes: 9998869\n",
"Time taken: 0.190420432 seconds\n",
"Number of threes: 10000228\n",
"Time taken: 0.190276435 seconds\n",
"Number of threes: 9372724\n",
"Time taken: 0.178388981 seconds\n",
"Number of threes: 10000496\n",
"Time taken: 0.180038991 seconds\n",
"Number of threes: 9997442\n",
"Time taken: 0.149067537 seconds\n",
"64 threads\n",
"Number of threes: 9687065\n",
"Time taken: 0.160374063 seconds\n",
"Number of threes: 9996783\n",
"Time taken: 0.229213367 seconds\n",
"Number of threes: 9997292\n",
"Time taken: 0.282740373 seconds\n",
"Number of threes: 9530897\n",
"Time taken: 0.19328029 seconds\n",
"Number of threes: 9689583\n",
"Time taken: 0.199149752 seconds \n",
"\"\"\"\n",
"\n",
"# Split the output into lines\n",
"lines = program_output.strip().split(\"\\n\")\n",
"\n",
"# Initialize lists to hold the times for each thread count\n",
"times_1_thread = []\n",
"times_2_threads = []\n",
"times_4_threads = []\n",
"times_8_threads = []\n",
"times_16_threads = []\n",
"times_32_threads = []\n",
"times_64_threads = []\n",
"\n",
"# Initialize a list of lists to make it easier to append in a loop\n",
"all_times = [\n",
" times_1_thread,\n",
" times_2_threads,\n",
" times_4_threads,\n",
" times_8_threads,\n",
" times_16_threads,\n",
" times_32_threads,\n",
" times_64_threads,\n",
"]\n",
"\n",
"# Current thread count index\n",
"thread_count_index = 0\n",
"\n",
"# Parse the output and fill the lists\n",
"for line in lines:\n",
" if \"Time taken:\" in line:\n",
" # Extract the time taken from the line\n",
" time_taken = float(line.split()[-2])\n",
" # Append the time to the appropriate list\n",
" all_times[thread_count_index].append(time_taken)\n",
" # Check if the current thread count's list is filled with 5 runs\n",
" if len(all_times[thread_count_index]) == 5:\n",
" # Move to the next thread count\n",
" thread_count_index += 1\n",
"\n",
"# Now all_times contains the lists of times for each thread count\n",
"print(\"Times for 1 thread:\", times_1_thread)\n",
"print(\"Times for 2 threads:\", times_2_threads)\n",
"print(\"Times for 4 threads:\", times_4_threads)\n",
"print(\"Times for 8 threads:\", times_8_threads)\n",
"print(\"Times for 16 threads:\", times_16_threads)\n",
"print(\"Times for 32 threads:\", times_32_threads)\n",
"print(\"Times for 64 threads:\", times_64_threads)\n",
"\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAm4AAAJwCAYAAAAwdO34AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjguMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/SrBM8AAAACXBIWXMAAA9hAAAPYQGoP6dpAABvHklEQVR4nO3deXhTZf7+8Tt791IoFAoFWlA2WRQUQWURpCCiuA3MuAA67hviyvhVVBwBRwVXcAVHccB9Rn+KCwKKgiiIuAAKlF122nSjSZPz+6NN2tACDbSkSd6v6+pFe3Jy8kka2ruf5zzPMRmGYQgAAAD1njnUBQAAAKBmCG4AAABhguAGAAAQJghuAAAAYYLgBgAAECYIbgAAAGGC4AYAABAmCG4AAABhguAGAAAQJghuQIRbuHChTCaTFi5cGOpS/Ewmk26++eZQlyFJ2rhxo0wmkx5//PFQl1Ij/fr1U79+/UJdRsjdeOONOuecc0JdRp3o16+fTjrppFCXUSOzZs2SyWTSxo0ba3yfvXv3Kj4+Xh9//HHdFRbBCG6oseeff14mk0k9e/YMdSmoxvPPP69Zs2aFuox6oXXr1jKZTEf8iPTXy+PxaObMmerXr58aNmwoh8Oh1q1ba8yYMfrhhx9CXZ4k6bffftODDz4Y1C/+nJwcvfzyy/rHP/7h31afA/iDDz5Yo/djtATyRo0a6e9//7vuv//+UJcSlqyhLgDhY/bs2WrdurWWLVumdevWqW3btqEuCZU8//zzSk1N1ejRowO29+nTR8XFxbLb7aEpLASmTZumgoIC/9cff/yx/vOf/2jq1KlKTU31b+/du3coyjsuiouLddFFF2nevHnq06eP/vGPf6hhw4bauHGj3nrrLb322mvavHmzWrRoEdI6f/vtNz300EPq16+fWrduXaP7PPXUU8rMzFT//v3rtrhactFFFwX8vCwoKNANN9ygCy+8UBdddJF/e1paWijKC4nrr79eTz/9tL788kudffbZoS4nrBDcUCM5OTn69ttv9d577+m6667T7NmzNWHChONag9frlcvlUkxMzHF93NpUVFSkuLi44/qYZrM5rF+zozF8+PCAr3fs2KH//Oc/Gj58eJVwEEynpzqFhYWKj48/pmPUhbvuukvz5s3T1KlTNXbs2IDbJkyYoKlTp4amsGPkdrs1e/ZsXX/99aEupca6dOmiLl26+L/es2ePbrjhBnXp0kWXX355rT7WgQMHZLfbZTbX7wG1Dh066KSTTtKsWbMIbkGq399Z1BuzZ89WSkqKhg4dqksuuUSzZ8/23+Z2u9WwYUONGTOmyv2cTqdiYmJ05513+reVlJRowoQJatu2rRwOhzIyMnT33XerpKQk4L6+86Bmz56tTp06yeFwaN68eZKkxx9/XL1791ajRo0UGxur7t2765133qny+MXFxbr11luVmpqqxMREnX/++dq2bZtMJpMefPDBgH23bdumq666SmlpaXI4HOrUqZNeffXVo37NfOepLF++XH369FFcXJx/aKe6x5fKhvgqd8x854988803GjdunBo3bqz4+HhdeOGF2r17d8D9fv31Vy1atKjKsEt157j5alu1apX69u2ruLg4tW3b1v8aLlq0SD179lRsbKzatWunL774okqttfF6zZ49W+3atVNMTIy6d++ur776yn/bggULZDKZ9P7771e535tvvimTyaQlS5YE9XhH8uKLL6pNmzZyOBw69dRT9f333wfcPnr0aCUkJGj9+vU699xzlZiYqMsuu0xS2R8W06ZNU6dOnRQTE6O0tDRdd9112r9/f8Ax/vvf/2ro0KFKT0+Xw+FQmzZtNHHiRHk8nkPWExsbq9NOO01ff/11jZ7H1q1b9cILL+icc86pEtokyWKx6M477wzotv34448aMmSIkpKSlJCQoAEDBmjp0qUB9/MN+R2suvOcWrdurfPOO0+LFy/WaaedppiYGGVlZenf//53wP0uvfRSSVL//v39793DnY+5ePFi7dmzRwMHDjzi67Bv3z7deeed6ty5sxISEpSUlKQhQ4bop59+8u+zc+dOWa1WPfTQQ1Xuv3btWplMJj377LM1Pl5t+u2339S/f3/FxcWpefPmeuyxxwJu9/3fnjNnjv7v//5PzZs3V1xcnJxOpyTpu+++0+DBg5WcnKy4uDj17dtX33zzTcAxNm3apBtvvFHt2rVTbGysGjVqpEsvvbTaP2h+/fVXnX322YqNjVWLFi30yCOPyOv1Vtnvhx9+UHZ2tlJTUxUbG6vMzExdddVVVfY755xz9OGHH8owjGN4laIPHTfUyOzZs3XRRRfJbrfrr3/9q6ZPn67vv/9ep556qmw2my688EK99957euGFFwKG5D744AOVlJRo5MiRksp+uZ1//vlavHixrr32WnXo0EE///yzpk6dqt9//10ffPBBwON++eWXeuutt3TzzTcrNTXV3y156qmndP755+uyyy6Ty+XSnDlzdOmll+qjjz7S0KFD/fcfPXq03nrrLV1xxRU6/fTTtWjRooDbfXbu3KnTTz/dHxYbN26sTz75RFdffbWcTme1v/xqYu/evRoyZIhGjhypyy+//KiHQm655RalpKRowoQJ2rhxo6ZNm6abb75Zc+fOlVQ2NHjLLbcoISFB9913n6QjD7vs379f5513nkaOHKlLL71U06dP18iRIzV79myNHTtW119/vf72t7/pX//6ly655BJt2bJFiYmJkmrn9Vq0aJHmzp2rW2+9VQ6HQ88//7wGDx6sZcuW6aSTTlK/fv2UkZGh2bNn68ILLwy47+zZs9WmTRv16tXrKF7N6r355pvKz8/XddddJ5PJpMcee0wXXXSRNmzYIJvN5t+vtLRU2dnZOvPMM/X444/7O6jXXXedZs2apTFjxujWW29VTk6Onn32Wf3444/65ptv/MeYNWuWEhISNG7cOCUkJOjLL7/UAw88IKfTqX/961/+x3nllVd03XXXqXfv3ho7dqw2bNig888/Xw0bNlRGRsZhn8snn3yi0tJSXXHFFTV67r/++qvOOussJSUl6e6775bNZtMLL7ygfv36+UP80Vi3bp0uueQSXX311Ro1apReffVVjR49Wt27d1enTp3Up08f3XrrrXr66af1j3/8Qx06dJAk/7/V+fbbb2UymXTyyScf8fE3bNigDz74QJdeeqkyMzO1c+dOvfDCC+rbt69+++03paenKy0tTX379tVbb71VZRRh7ty5slgs/nBZk+PVlv3792vw4MG66KKL9Je//EXvvPOO7rnnHnXu3FlDhgwJ2HfixImy2+268847VVJSIrvdri+//FJDhgxR9+7dNWHCBJnNZs2cOVNnn322vv76a5122mmSpO+//17ffvutRo4cqRYtWmjjxo2aPn26+vXrp99++83//t6xY4f69++v0tJS3XvvvYqPj9eLL76o2NjYgFp27dqlQYMGqXHjxrr33nvVoEEDbdy4Ue+9916V59i9e3dNnTpVv/76a9hMxqgXDOAIfvjhB0OS8fnnnxuGYRher9do0aKFcdttt/n3+fTTTw1Jxocffhhw33PPPdfIysryf/36668bZrPZ+PrrrwP2mzFjhiHJ+Oabb/zbJBlms9n49ddfq9RUVFQU8LXL5TJOOukk4+yzz/ZvW758uSHJGDt2bMC+o0ePNiQZEyZM8G+7+uqrjWbNmhl79uwJ2HfkyJFGcnJylcerib59+xqSjBkzZlS57eDH92nVqpUxatQo/9czZ840JBkDBw40vF6vf/vtt99uWCwWIzc317+tU6dORt++fascc8GCBYYkY8GCBVVqe/PNN/3b1qxZ43/Nly5d6t/u+97OnDnTv+1YXy9JhiTjhx9+8G/btGmTERMTY1x44YX+bePHjzccDkfA89y1a5dhtVqrff0O5V//+pchycjJyalyW05OjiHJaNSokbFv3z7/9v/+979V3tOjRo0yJBn33ntvwDG+/vprQ5Ixe/bsgO3z5s2rsr261+a6664z4uLijAMHDhiGUfZ+btKkidGtWzejpKTEv9+LL75oSKr2+1zZ7bffbkgyfvzxx8Pu5zN8+HDDbrcb69ev92/bvn27kZiYaPTp08e/bcKECUZ1vzZ879PKr2+rVq0MScZXX33l37Zr1y7D4XAYd9xxh3/b22+/XeX9eTiXX3650ahRoyrbfd/Hf/3rX/5tBw4cMDweT5X9HA6H8fDDD/u3vfDCC4Yk4+effw7Yt2PHjgE/U2p6vCPZvXv3IX8GGEbF/89///vf/m0lJSVG06ZNjYsvvti/zfd/OysrK+B95fV6jRNOOMHIzs4O+LlRVFRkZGZmGuecc07AtoMtWbKkyuOPHTvWkGR89913/m27du0ykpOTA77377//viHJ+P7774/4Onz77beGJGPu3LlH3BcVGCrFEc2ePVtpaWn+E4FNJpNGjBihOXPm+Id3zj77bKWmpvo7QFLZX4yff/65RowY4d/29ttvq0OHDmrfvr327Nnj//Cd47BgwYKAx+7bt686duxYpabKf+Xt379feXl5Ouuss7RixQr/dt+w6o033hhw31tuuSXga8Mw9O6772rYsGEyDCOgruzsbOXl5QUcNxgOh6PaIeRgXXvttQFDVGeddZY8Ho82bdp01MdMSEjwd0IlqV27dmrQoIE6dOgQ0GHxfb5hwwZJtfd69erVS927d/d/3bJlS11wwQX69NNP/e+rK6+8UiUlJQHD4HPnzlVpaWmtnxs0YsQIpaSk+L8+66yzJFU878puuOGGgK/ffvttJScn65xzzgl4Pbp3766EhISA93Xl925+fr727Nmjs846S0VFRVqzZo2ksqGmXbt26frrrw/oYI8ePVrJyclHfC6+oTJfh/RwPB6PPvvsMw0fPlxZWVn+7c2aNdPf/vY3LV682H+8YHXs2NH/OkpS48aN1a5du2pf05rau3dvwPfpcBwOh/9cL4/Line truncated
"text/plain": [
"<Figure size 700x700 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# Assuming the previous code has been executed and all_times is populated\n",
"\n",
"# Calculate the average time for each thread count\n",
"average_times = [sum(times) / len(times) for times in all_times]\n",
"\n",
"# max_threads = 64\n",
"# Thread counts\n",
"thread_counts = [1,2,4,8,16,32,64]\n",
"# Plotting the running time bar graph\n",
"plt.figure(figsize=(7, 7))\n",
"plt.plot(thread_counts, average_times, color='skyblue', )\n",
"\n",
"# Adding titles and labels\n",
"plt.title('Average runtime by Thread Count (Java Threads)')\n",
"plt.xlabel('Number of Threads')\n",
"plt.ylabel('Running time (s)')\n",
"plt.xticks(thread_counts)\n",
"\n",
"# plt.xscale('log', base=2)\n",
"# Display the bar graph\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA04AAAJwCAYAAAC+pzHoAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjguMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/SrBM8AAAACXBIWXMAAA9hAAAPYQGoP6dpAABM+0lEQVR4nO3deXyM5/7/8ffILpFYE2tCbUEQWyu0opUKVUsXRbX2paexVdHm9LSKo6GtvZbqadGi1NpWa6va6VE7LVpqiZ2DRFAquX9/+JmvacKVYWKC1/PxmMejc93Xfc3nnrmTeue672tslmVZAgAAAADcVA53FwAAAAAA2R3BCQAAAAAMCE4AAAAAYEBwAgAAAAADghMAAAAAGBCcAAAAAMCA4AQAAAAABgQnAAAAADAgOAEAAACAAcEJADJgs9n07rvv3vXXXbFihWw2m2bPnn3XXzsjkydPls1m08aNG91dSqa463PLTtLS0hQREaHBgwe7u5QsYbPZ1K1bN3eXkSnt2rVT8eLFndpn0aJFCggI0KlTp7KmKAC3jeAEIMvs2LFDzz//vMLCwuTr66siRYroySef1JgxY9xd2gPrwIEDstlsmXocOHDA3eVmqeTkZA0YMECVK1dWQECA/Pz8FBERoTfeeENHjx51d3mSpO+//97pIPjll18qMTHRIVxk5wBct27dTJ2PD0ogbtCggUqVKqWEhAR3lwLgbzzdXQCA+9O6dev0+OOPKzQ0VJ07d1bBggWVmJion376SaNGjVL37t3dXeIDqUCBAvriiy8c2oYNG6bDhw9rxIgR6frer/744w/FxMTo0KFDat68ubp06SJvb29t375dn376qebNm6fffvvN3WXq+++/19ixY50KDR988IFatmypoKCgrCvMhd566y116tTJ/vznn3/W6NGj9c9//lPlypWzt1eqVMkd5blF165d1adPHw0YMEC5cuVydzkA/j+CE4AsMXjwYAUFBennn39W7ty5HbadPHnSPUVB/v7+eumllxzaZsyYobNnz6Zrv1OWZenPP/+Un5+fS8e9U1evXtWzzz6rEydOaMWKFXr00Ucdtg8ePFhDhw51U3V3ZsuWLdq2bZuGDRvm7lIy7cknn3R47uvrq9GjR+vJJ59U3bp1XfpaFy5ckL+/v0vHzArPPfecunfvrlmzZqlDhw7uLgfA/8elegCyxL59+1ShQoV0oUmSgoODHZ5fv2dh2rRpKlu2rHx9fVWtWjWtWrUq3b5HjhxRhw4dFBISIh8fH1WoUEGfffZZun6XL19W//79VapUKfn4+KhYsWLq16+fLl++nK7fa6+9pgIFCihXrlxq0qSJDh8+nG68m92r8O6778pms9328dxMamqq/vnPf6pgwYLy9/dXkyZNlJiYaN/ev39/eXl5ZXgfRJcuXZQ7d279+eefmX49k8uXL6t3794qUKCA/P399cwzz6R77eLFi+vpp5/W4sWLVb16dfn5+enjjz+WJJ07d069evVSsWLF5OPjo1KlSmno0KFKS0tzGOPDDz9UrVq1lC9fPvn5+alatWoZ3u+V2c8tI3PmzNG2bdv01ltvpQtNkhQYGJju/qBZs2apWrVq8vPzU/78+fXSSy/pyJEjDn3q1q2b4T/0/37uXL9c8sMPP9TEiRNVsmRJ+fj4qEaNGvr5558d9hs7dqwkOVyydivz58+Xt7e36tSpY3obtH37drVr104PPfSQfH19VbBgQXXo0EH/+9//7H1mz54tm82mlStXptv/448/ls1m086dOzM9nivNnz9fERER9t8DixYtcth+/Wfz119/1Ysvvqg8efI4fN5Tp061f6Z58+ZVy5YtHX7GJGn16tVq3ry5QkND7b9HXnvtNV26dOmm9fj6+ioiIkLz5s3LsO4ZM2aoWrVqypUrlwIDA1WxYkWNGjXKoU9wcLAqVaqkr7/++nbfHgBZgBknAFkiLCxM69ev186dOxUREWHsv3LlSs2cOVM9evSQj4+Pxo0bpwYNGmjDhg32/U+cOKGaNWvag0mBAgW0cOFCdezYUcnJyerVq5ekazfHN2nSRGvWrFGXLl1Urlw57dixQyNGjNBvv/2m+fPn21+3U6dOmjp1ql588UXVqlVLP/74oxo1anTHx5+Z47mVwYMHy2az6Y033tDJkyc1cuRIxcTEaOvWrfLz89PLL7+sgQMHaubMmQ73sly5ckWzZ8/Wc889J19f3zs+juu6d++uPHnyqH///jpw4IBGjhypbt26aebMmQ799uzZo1atWqlr167q3Lm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VygoODrauXr1qbzt27JiVI0cOa+DAgU6PZzJr1qxbnsOSLG9vb2vv3r32tm3btlmSrDFjxtjbrv9stmrVymH/AwcOWB4eHtbgwYMd2nfs2GF5eno6tGd0TAkJCZbNZrMOHjxob4uMjLQKFSrk8POwZMkSS5LDZ9+zZ08rMDDQ4b28mffee8+SZJ04ccLYF8DdwaV6ALLEk08+qfXr16tJkybatm2b3n//fcXGxqpIkSL2v/jfKCoqStWqVbM/Dw0NVdOmTbV48WKlpqbKsizNmTNHjRs3lmVZOn36tP0RGxurpKQkbd68WdK1vxKXK1dO4eHhDv2eeOIJSdLy5cslXbvxXpJ69OjhUMv1mas7YToekzZt2jjcFP7888+rUKFC9pqv9/nvf/+rffv22dumTZumYsWKpZsduFNdunRxmDF47LHHlJqaqoMHDzr0K1GihGJjYx3aZs2apccee0x58uRx+DxiYmKUmprqcAnjjfdDnT17VklJSXrsscfsn610559bcnJypm+437hxo06ePKlXX33VYQavUaNGCg8P13fffZepcTLSokUL5cmTx/78sccek3Rt4Yrb9b///c9hzFu58b3+888/dfr0adWsWVOSHN7vFi1a6OTJk1qxYoW9bfbs2UpLS1OLFi2cHs8VYmJiVLJkSfvzSpUqKTAwMMP37pVXXnF4PnfuXKWlpemFF15wOB8LFiyo0qVL238//P2YLly4oNOnT6tWrVqyLEtbtmyRJB07dkxbt25V27ZtHRbkePLJJ1W+fHmH186dO7cuXLigpUuXGo/x+ud4+vRpY18AdwfBCUCWqVGjhubOnauzZ89qw4YNio+P1/nz5/X888/r119/dehbunTpdPuXKVNGFy9e1KlTp3Tq1CmdO3dOEydOVIECBRwe7du3l/R/i078/vvv+uWXX9L1K1OmjEO/gwcPKkeOHA7/AJOksmXL3vGxm47H2f1tNptKlSrlsER4ixYt5OPjo2nTpkmSkpKStGDBArVu3dqll0VJ14Lfja7/o+7s2bMO7SVKlEi37++//65Fixal+zxiYmIkOS4WsmDBAtWsWVO+vr7KmzevChQooPHjxyspKcne504/t8DAQJ0/fz5Tfa8Hw4zGDg8PTxccnZHZ99RZ1v+/HM3kzJkz6tmzp0JCQuTn56cCBQrYP78b3+8GDRooKCjI4bLMmTNnKjIy0v4z5cx4rvD390669v5l9N79/Zz8/fffZVmWSpcune6c3LVrl8P5eOjQIbVr10558+ZVQECAChQoYP+jxPVjun4OZPQz//fz5tVXX1WZMmXUsGFDFS1aVB06dEh3b9Z11z9HV/8sA7h93OMEIMt5e3urRo0aqlGjhsqUKaP27dtr1qxZ6t+/f6bHuL6IwEsvvaS2bdtm2Of6csVpaWmqWLGihg8fnmG/YsWKOXkEN//HS2Zmj7JKnjx59PTTT2vatGl65513NHv2bF2+fNnlq+NJkoeHR4btf/9HekYr6KWlpenJJ59Uv379Mhzj+j++V69erSZNmqhOnToaN26cChUqJC8vL02aNEnTp0+/wyP4P+HLine truncated
"text/plain": [
"<Figure size 1000x700 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# Calculate speedup compared to single-threaded execution\n",
"speedup = [average_times[0] / time for time in average_times]\n",
"\n",
"# Plotting the speedup bar graph\n",
"plt.figure(figsize=(10, 7))\n",
"plt.bar(thread_counts, speedup, color='skyblue')\n",
"\n",
"# Adding titles and labels\n",
"plt.title('Speedup by Thread Count (Java Threads)')\n",
"plt.xlabel('Number of Threads')\n",
"plt.ylabel('Speedup Factor')\n",
"plt.xticks(thread_counts)\n",
"\n",
"\n",
"# Display the bar graph\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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.13"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
+77
View File
@@ -0,0 +1,77 @@
1 threads
Number of threes: 9997807
Time taken: 0.359876473 seconds
Number of threes: 9998567
Time taken: 0.361032238 seconds
Number of threes: 9996883
Time taken: 0.360004483 seconds
Number of threes: 9996455
Time taken: 0.359855544 seconds
Number of threes: 9998718
Time taken: 0.359787787 seconds
2 threads
Number of threes: 9998562
Time taken: 0.373749715 seconds
Number of threes: 10004232
Time taken: 0.447702756 seconds
Number of threes: 10000537
Time taken: 0.464235889 seconds
Number of threes: 9997416
Time taken: 0.372059609 seconds
Number of threes: 9996787
Time taken: 0.371326817 seconds
4 threads
Number of threes: 10004804
Time taken: 0.222983097 seconds
Number of threes: 9997107
Time taken: 0.216992067 seconds
Number of threes: 10000709
Time taken: 0.21719135 seconds
Number of threes: 9996199
Time taken: 0.21907073 seconds
Number of threes: 10008067
Time taken: 0.22178881 seconds
8 threads
Number of threes: 10006502
Time taken: 0.167658194 seconds
Number of threes: 10004407
Time taken: 0.165304926 seconds
Number of threes: 9997402
Time taken: 0.163078056 seconds
Number of threes: 9999915
Time taken: 0.162809502 seconds
Number of threes: 9999852
Time taken: 0.165746493 seconds
16 threads
Number of threes: 9997128
Time taken: 0.178607489 seconds
Number of threes: 9995240
Time taken: 0.172914077 seconds
Number of threes: 10001920
Time taken: 0.174337312 seconds
Number of threes: 10000932
Time taken: 0.176571896 seconds
Number of threes: 10005193
Time taken: 0.175561715 seconds
32 threads
Number of threes: 9998869
Time taken: 0.190420432 seconds
Number of threes: 10000228
Time taken: 0.190276435 seconds
Number of threes: 9372724
Time taken: 0.178388981 seconds
Number of threes: 10000496
Time taken: 0.180038991 seconds
Number of threes: 9997442
Time taken: 0.149067537 seconds
64 threads
Number of threes: 9687065
Time taken: 0.160374063 seconds
Number of threes: 9996783
Time taken: 0.229213367 seconds
Number of threes: 9997292
Time taken: 0.282740373 seconds
Number of threes: 9530897
Time taken: 0.19328029 seconds
Number of threes: 9689583
Time taken: 0.199149752 seconds
+627
View File
@@ -0,0 +1,627 @@
<!DOCTYPE html>
<html>
<head>
<title>report.md</title>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<style>
/* https://github.com/microsoft/vscode/blob/master/extensions/markdown-language-features/media/markdown.css */
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
body {
font-family: var(--vscode-markdown-font-family, -apple-system, BlinkMacSystemFont, "Segoe WPC", "Segoe UI", "Ubuntu", "Droid Sans", sans-serif);
font-size: var(--vscode-markdown-font-size, 14px);
padding: 0 26px;
line-height: var(--vscode-markdown-line-height, 22px);
word-wrap: break-word;
}
#code-csp-warning {
position: fixed;
top: 0;
right: 0;
color: white;
margin: 16px;
text-align: center;
font-size: 12px;
font-family: sans-serif;
background-color:#444444;
cursor: pointer;
padding: 6px;
box-shadow: 1px 1px 1px rgba(0,0,0,.25);
}
#code-csp-warning:hover {
text-decoration: none;
background-color:#007acc;
box-shadow: 2px 2px 2px rgba(0,0,0,.25);
}
body.scrollBeyondLastLine {
margin-bottom: calc(100vh - 22px);
}
body.showEditorSelection .code-line {
position: relative;
}
body.showEditorSelection .code-active-line:before,
body.showEditorSelection .code-line:hover:before {
content: "";
display: block;
position: absolute;
top: 0;
left: -12px;
height: 100%;
}
body.showEditorSelection li.code-active-line:before,
body.showEditorSelection li.code-line:hover:before {
left: -30px;
}
.vscode-light.showEditorSelection .code-active-line:before {
border-left: 3px solid rgba(0, 0, 0, 0.15);
}
.vscode-light.showEditorSelection .code-line:hover:before {
border-left: 3px solid rgba(0, 0, 0, 0.40);
}
.vscode-light.showEditorSelection .code-line .code-line:hover:before {
border-left: none;
}
.vscode-dark.showEditorSelection .code-active-line:before {
border-left: 3px solid rgba(255, 255, 255, 0.4);
}
.vscode-dark.showEditorSelection .code-line:hover:before {
border-left: 3px solid rgba(255, 255, 255, 0.60);
}
.vscode-dark.showEditorSelection .code-line .code-line:hover:before {
border-left: none;
}
.vscode-high-contrast.showEditorSelection .code-active-line:before {
border-left: 3px solid rgba(255, 160, 0, 0.7);
}
.vscode-high-contrast.showEditorSelection .code-line:hover:before {
border-left: 3px solid rgba(255, 160, 0, 1);
}
.vscode-high-contrast.showEditorSelection .code-line .code-line:hover:before {
border-left: none;
}
img {
max-width: 100%;
max-height: 100%;
}
a {
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
a:focus,
input:focus,
select:focus,
textarea:focus {
outline: 1px solid -webkit-focus-ring-color;
outline-offset: -1px;
}
hr {
border: 0;
height: 2px;
border-bottom: 2px solid;
}
h1 {
padding-bottom: 0.3em;
line-height: 1.2;
border-bottom-width: 1px;
border-bottom-style: solid;
}
h1, h2, h3 {
font-weight: normal;
}
table {
border-collapse: collapse;
}
table > thead > tr > th {
text-align: left;
border-bottom: 1px solid;
}
table > thead > tr > th,
table > thead > tr > td,
table > tbody > tr > th,
table > tbody > tr > td {
padding: 5px 10px;
}
table > tbody > tr + tr > td {
border-top: 1px solid;
}
blockquote {
margin: 0 7px 0 5px;
padding: 0 16px 0 10px;
border-left-width: 5px;
border-left-style: solid;
}
code {
font-family: Menlo, Monaco, Consolas, "Droid Sans Mono", "Courier New", monospace, "Droid Sans Fallback";
font-size: 1em;
line-height: 1.357em;
}
body.wordWrap pre {
white-space: pre-wrap;
}
pre:not(.hljs),
pre.hljs code > div {
padding: 16px;
border-radius: 3px;
overflow: auto;
}
pre code {
color: var(--vscode-editor-foreground);
tab-size: 4;
}
/** Theming */
.vscode-light pre {
background-color: rgba(220, 220, 220, 0.4);
}
.vscode-dark pre {
background-color: rgba(10, 10, 10, 0.4);
}
.vscode-high-contrast pre {
background-color: rgb(0, 0, 0);
}
.vscode-high-contrast h1 {
border-color: rgb(0, 0, 0);
}
.vscode-light table > thead > tr > th {
border-color: rgba(0, 0, 0, 0.69);
}
.vscode-dark table > thead > tr > th {
border-color: rgba(255, 255, 255, 0.69);
}
.vscode-light h1,
.vscode-light hr,
.vscode-light table > tbody > tr + tr > td {
border-color: rgba(0, 0, 0, 0.18);
}
.vscode-dark h1,
.vscode-dark hr,
.vscode-dark table > tbody > tr + tr > td {
border-color: rgba(255, 255, 255, 0.18);
}
</style>
<style>
/* Tomorrow Theme */
/* http://jmblog.github.com/color-themes-for-google-code-highlightjs */
/* Original theme - https://github.com/chriskempson/tomorrow-theme */
/* Tomorrow Comment */
.hljs-comment,
.hljs-quote {
color: #8e908c;
}
/* Tomorrow Red */
.hljs-variable,
.hljs-template-variable,
.hljs-tag,
.hljs-name,
.hljs-selector-id,
.hljs-selector-class,
.hljs-regexp,
.hljs-deletion {
color: #c82829;
}
/* Tomorrow Orange */
.hljs-number,
.hljs-built_in,
.hljs-builtin-name,
.hljs-literal,
.hljs-type,
.hljs-params,
.hljs-meta,
.hljs-link {
color: #f5871f;
}
/* Tomorrow Yellow */
.hljs-attribute {
color: #eab700;
}
/* Tomorrow Green */
.hljs-string,
.hljs-symbol,
.hljs-bullet,
.hljs-addition {
color: #718c00;
}
/* Tomorrow Blue */
.hljs-title,
.hljs-section {
color: #4271ae;
}
/* Tomorrow Purple */
.hljs-keyword,
.hljs-selector-tag {
color: #8959a8;
}
.hljs {
display: block;
overflow-x: auto;
color: #4d4d4c;
padding: 0.5em;
}
.hljs-emphasis {
font-style: italic;
}
.hljs-strong {
font-weight: bold;
}
</style>
<style>
/*
* Markdown PDF CSS
*/
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe WPC", "Segoe UI", "Ubuntu", "Droid Sans", sans-serif, "Meiryo";
padding: 0 12px;
}
pre {
background-color: #f8f8f8;
border: 1px solid #cccccc;
border-radius: 3px;
overflow-x: auto;
white-space: pre-wrap;
overflow-wrap: break-word;
}
pre:not(.hljs) {
padding: 23px;
line-height: 19px;
}
blockquote {
background: rgba(127, 127, 127, 0.1);
border-color: rgba(0, 122, 204, 0.5);
}
.emoji {
height: 1.4em;
}
code {
font-size: 14px;
line-height: 19px;
}
/* for inline code */
:not(pre):not(.hljs) > code {
color: #C9AE75; /* Change the old color so it seems less like an error */
font-size: inherit;
}
/* Page Break : use <div class="page"/> to insert page break
-------------------------------------------------------- */
.page {
page-break-after: always;
}
</style>
<script src="https://unpkg.com/mermaid/dist/mermaid.min.js"></script>
</head>
<body>
<script>
mermaid.initialize({
startOnLoad: true,
theme: document.body.classList.contains('vscode-dark') || document.body.classList.contains('vscode-high-contrast')
? 'dark'
: 'default'
});
</script>
<h1 id="lab-report-performance-comparison-of-openmp-java-threads-and-pthreads">Lab Report: Performance Comparison of OpenMP, Java Threads, and Pthreads</h1>
<h2 id="abstract">Abstract</h2>
<p>OpenMP is a parallel programming model that simplifies the development of concurrent applications in C, C++, and Fortran. Unlike traditional threading models, OpenMP uses compiler directives to abstract platform-specific threading mechanisms, allowing for portable and scalable parallelism. This experiment compares the performance of OpenMP in C with Java threads and POSIX threads (pthreads) in C. The objective is to evaluate the scalability of OpenMP on a multi-processor system and its impact on program execution.</p>
<h2 id="methods">Methods</h2>
<h3 id="environment">Environment</h3>
<ul>
<li><strong>System</strong>: Dell PowerEdge R7525 on OpenSUSE Linux Leap (&quot;kaiju&quot; in the CS lab)</li>
<li><strong>Specifications</strong>:
<ul>
<li>RAM: 512 GiB</li>
<li>CPU Model: AMD EPYC 7313</li>
<li>CPUs: 2</li>
<li>Speed: 3.9 GHz</li>
<li>Cores: 32 (16 per CPU)</li>
<li>Threads: 64 (32 per CPU)</li>
</ul>
</li>
</ul>
<h3 id="procedure">Procedure</h3>
<ul>
<li><strong>Execution</strong>: Java and C OpenMP programs were run using a makefile with thread counts of 1, 2, 4, 8, 16, 32, and 64.</li>
<li><strong>Timing</strong>: Programs recorded execution times, outputting to a file.</li>
<li><strong>Analysis</strong>: Running times parsed by a Python notebook and visualized using matplotlib graphs.</li>
<li><strong>Note</strong>: No synchronization was used, focusing on parallel performance benchmarking.</li>
<li><strong>Comparison</strong>: C pthreads results were referenced from a previous similar experiment.</li>
<li><strong>Code</strong> (see appendix)</li>
</ul>
<h2 id="results">Results</h2>
<p>The OpenMP implementation in C demonstrated superior scalability in terms of running time and speedup, particularly evident up to 32 threads. Beyond this point, performance plateaued, likely due to inter-CPU cache sharing inefficiencies. In contrast, Java threads and pthreads showed less stable scaling, with diminishing returns observed at lower thread counts, indicating less efficient use of available processors.</p>
<ul>
<li>
<p>OpenMP Runtimes
<img src="./runtimes.png" alt="OpenMP Runtimes"></p>
</li>
<li>
<p>OpenMP Speedup
<img src="./speedup.png" alt="OpenMP Speedup"></p>
</li>
<li>
<p>Java Runtimes
<img src="./runtimes_java.png" alt="OpenMP Runtimes"></p>
</li>
<li>
<p>Java Speedup
<img src="./speedup_java.png" alt="OpenMP Speedup"></p>
</li>
<li>
<p>C Pthreads Runtimes and comparison to serial</p>
</li>
</ul>
<p><img src="runtimes_pthreads.png" alt="Pthreads Runtimes"></p>
<h2 id="conclusion">Conclusion</h2>
<p>The experiment underscores OpenMP's efficiency in scaling across multiple processors within a single CPU, as evidenced by the performance plateau at 32 threads corresponding to the single CPU's thread capacity. The diminishing returns observed with Java threads and pthreads suggest that OpenMP's model of parallelism, with its shared memory architecture and compiler optimizations, is more adept at exploiting the computational resources for parallel tasks. This is particularly relevant in systems with a high number of cores and threads, where efficient scaling is crucial. Future work could explore the impact of using differnt algorithms and paradigms such as thread synchronization and semaphores or the use of generalized &quot;reduce/scan&quot; functions.</p>
<h2 id="appendix">Appendix</h2>
<h3 id="code">Code</h3>
<h4 id="openmp">OpenMP</h4>
<h5 id="makefile">Makefile</h5>
<pre class="hljs"><code><div>
<span class="hljs-section">all: count_threes</span>
<span class="hljs-section">count_threes: count_threes.c</span>
gcc -fopenmp -o count_threes count_threes.c
<span class="hljs-section">run:</span>
for threads in 1 2 4 8 16 32 64; do \
echo <span class="hljs-string">"$$threads threads"</span> &gt;&gt; results.txt; \
for i in {1..5}; do \
OMP_NUM_THREADS=$$threads ./count_threes 1000000000 &gt;&gt; results.txt; \
done; \
done
<span class="hljs-section">clean:</span>
rm -f count_threes results.txt
</div></code></pre>
<h5 id="program">Program</h5>
<pre class="hljs"><code><div><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdlib.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;time.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;omp.h&gt;</span></span>
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">count3s</span><span class="hljs-params">(<span class="hljs-keyword">int</span>* <span class="hljs-built_in">array</span>, <span class="hljs-keyword">int</span> length)</span>
</span>{
<span class="hljs-keyword">int</span> count = <span class="hljs-number">0</span>;
<span class="hljs-keyword">int</span> i;
<span class="hljs-meta">#<span class="hljs-meta-keyword">pragma</span> omp parallel shared(array, count, length) private(i)</span>
{
<span class="hljs-keyword">int</span> count_p = <span class="hljs-number">0</span>;
<span class="hljs-meta">#<span class="hljs-meta-keyword">pragma</span> omp for</span>
<span class="hljs-keyword">for</span> (i = <span class="hljs-number">0</span>; i &lt; length; i++) {
<span class="hljs-keyword">if</span> (<span class="hljs-built_in">array</span>[i] == <span class="hljs-number">3</span>) {
count_p++;
}
}
<span class="hljs-comment">// #pragma omp critical</span>
{
count += count_p;
}
}
<span class="hljs-keyword">return</span> count;
}
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">int</span> argc, <span class="hljs-keyword">char</span> <span class="hljs-keyword">const</span> *argv[])</span>
</span>{
<span class="hljs-keyword">if</span> (argc != <span class="hljs-number">2</span>) {
<span class="hljs-built_in">printf</span>(<span class="hljs-string">"Usage: %s &lt;array_size&gt;\n"</span>, argv[<span class="hljs-number">0</span>]);
<span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;
}
<span class="hljs-keyword">int</span> length = atoi(argv[<span class="hljs-number">1</span>]);
<span class="hljs-keyword">if</span> (length &lt;= <span class="hljs-number">0</span>) {
<span class="hljs-built_in">printf</span>(<span class="hljs-string">"Error: array size must be a positive integer\n"</span>);
<span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;
}
<span class="hljs-keyword">int</span>* <span class="hljs-built_in">array</span> = (<span class="hljs-keyword">int</span>*) <span class="hljs-built_in">malloc</span>(length * <span class="hljs-keyword">sizeof</span>(<span class="hljs-keyword">int</span>));
<span class="hljs-keyword">int</span> i;
srand(time(<span class="hljs-literal">NULL</span>));
<span class="hljs-keyword">for</span> (i = <span class="hljs-number">0</span>; i &lt; length; i++) {
<span class="hljs-built_in">array</span>[i] = rand() % <span class="hljs-number">10</span> + <span class="hljs-number">1</span>;
}
<span class="hljs-keyword">double</span> start_time, end_time;
start_time = omp_get_wtime();
<span class="hljs-keyword">int</span> count = count3s(<span class="hljs-built_in">array</span>, length);
end_time = omp_get_wtime();
<span class="hljs-built_in">printf</span>(<span class="hljs-string">"count: %d\n"</span>, count);
<span class="hljs-built_in">printf</span>(<span class="hljs-string">"Time taken: %f seconds\n"</span>, end_time - start_time);
<span class="hljs-built_in">free</span>(<span class="hljs-built_in">array</span>);
<span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
</div></code></pre>
<h4 id="java-threads">Java Threads</h4>
<h5 id="makefile">Makefile</h5>
<pre class="hljs"><code><div><span class="hljs-section">all: CountThrees.class</span>
<span class="hljs-section">CountThrees.class:</span>
javac CountThrees.java
<span class="hljs-section">run: </span>
for threads in 1 2 4 8 16 32 64; do \
echo <span class="hljs-string">"$$threads threads"</span> &gt;&gt; results.txt; \
for i in {1..5}; do \
java CountThrees 1000000000 $$threads &gt;&gt; results.txt; \
done \
done
<span class="hljs-section">clean:</span>
rm -f CountThrees.class results.txt
</div></code></pre>
<h5 id="program">Program</h5>
<pre class="hljs"><code><div><span class="hljs-keyword">import</span> java.util.*;
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">CountThrees</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">Runnable</span> </span>{
<span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">int</span> ARRAY_LENGTH;
<span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">int</span> MAX_THREADS;
<span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">int</span> MAX_RANGE = <span class="hljs-number">100</span>;
<span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> Random random = <span class="hljs-keyword">new</span> Random();
<span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">int</span> count = <span class="hljs-number">0</span>;
<span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> Object lock = <span class="hljs-keyword">new</span> Object();
<span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">int</span>[] array;
<span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> Thread[] threads;
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{
<span class="hljs-keyword">if</span> (args.length != <span class="hljs-number">2</span>) {
System.err.println(<span class="hljs-string">"Usage: java CountThrees &lt;array_length&gt; &lt;max_threads&gt;"</span>);
System.exit(<span class="hljs-number">1</span>);
}
<span class="hljs-keyword">try</span> {
ARRAY_LENGTH = Integer.parseInt(args[<span class="hljs-number">0</span>]);
MAX_THREADS = Integer.parseInt(args[<span class="hljs-number">1</span>]);
} <span class="hljs-keyword">catch</span> (NumberFormatException e) {
System.err.println(<span class="hljs-string">"Invalid argument: "</span> + args[<span class="hljs-number">0</span>] + <span class="hljs-string">" or "</span> + args[<span class="hljs-number">1</span>] + <span class="hljs-string">" is not a valid integer"</span>);
System.exit(<span class="hljs-number">1</span>);
}
array = <span class="hljs-keyword">new</span> <span class="hljs-keyword">int</span>[ARRAY_LENGTH];
<span class="hljs-comment">// Initialize the elements in the array</span>
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; array.length; i++) {
array[i] = random.nextInt(MAX_RANGE);
}
<span class="hljs-keyword">long</span> startTime = System.nanoTime();
<span class="hljs-comment">// Create the threads</span>
CountThrees[] counters = <span class="hljs-keyword">new</span> CountThrees[MAX_THREADS];
<span class="hljs-keyword">int</span> lengthPerThread = ARRAY_LENGTH / MAX_THREADS;
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; counters.length; i++) {
counters[i] = <span class="hljs-keyword">new</span> CountThrees(
i * lengthPerThread,
lengthPerThread
);
}
<span class="hljs-comment">// Run the threads</span>
threads = <span class="hljs-keyword">new</span> Thread[MAX_THREADS];
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; counters.length; i++) {
threads[i] = <span class="hljs-keyword">new</span> Thread(counters[i]);
threads[i].start();
}
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; counters.length; i++) {
<span class="hljs-keyword">try</span> {
threads[i].join();
} <span class="hljs-keyword">catch</span> (InterruptedException e) {
<span class="hljs-comment">// Do nothing</span>
}
}
<span class="hljs-keyword">long</span> endTime = System.nanoTime();
<span class="hljs-comment">// Print the number of threes</span>
System.out.println(<span class="hljs-string">"Number of threes: "</span> + count);
<span class="hljs-comment">// compute the elapsed time in nanoseconds</span>
<span class="hljs-keyword">long</span> duration = (endTime - startTime);
<span class="hljs-comment">// convert to seconds</span>
<span class="hljs-keyword">double</span> seconds = (<span class="hljs-keyword">double</span>)duration / <span class="hljs-number">1_000_000_000.0</span>;
System.out.println(<span class="hljs-string">"Time taken: "</span> + seconds + <span class="hljs-string">" seconds"</span>);
}
<span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> startIndex;
<span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> elements;
<span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> myCount = <span class="hljs-number">0</span>;
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">CountThrees</span><span class="hljs-params">(<span class="hljs-keyword">int</span> start, <span class="hljs-keyword">int</span> elem)</span> </span>{
startIndex = start;
elements = elem;
}
<span class="hljs-comment">// Overload of run method in the Thread class</span>
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">run</span><span class="hljs-params">()</span> </span>{
<span class="hljs-comment">// Count the number of threes</span>
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; elements; i++) {
<span class="hljs-keyword">if</span> (array[startIndex + i] == <span class="hljs-number">3</span>) {
myCount++;
}
}
<span class="hljs-comment">// synchronized (lock) {</span>
<span class="hljs-comment">// count += myCount;</span>
<span class="hljs-comment">// }</span>
count += myCount;
}
}
</div></code></pre>
</body>
</html>
+270
View File
@@ -0,0 +1,270 @@
# Lab Report: Performance Comparison of OpenMP, Java Threads, and Pthreads
## Abstract
OpenMP is a parallel programming model that simplifies the development of concurrent applications in C, C++, and Fortran. Unlike traditional threading models, OpenMP uses compiler directives to abstract platform-specific threading mechanisms, allowing for portable and scalable parallelism. This experiment compares the performance of OpenMP in C with Java threads and POSIX threads (pthreads) in C. The objective is to evaluate the scalability of OpenMP on a multi-processor system and its impact on program execution.
## Methods
### Environment
- **System**: Dell PowerEdge R7525 on OpenSUSE Linux Leap ("kaiju" in the CS lab)
- **Specifications**:
- RAM: 512 GiB
- CPU Model: AMD EPYC 7313
- CPUs: 2
- Speed: 3.9 GHz
- Cores: 32 (16 per CPU)
- Threads: 64 (32 per CPU)
### Procedure
- **Execution**: Java and C OpenMP programs were run using a makefile with thread counts of 1, 2, 4, 8, 16, 32, and 64.
- **Timing**: Programs recorded execution times, outputting to a file.
- **Analysis**: Running times parsed by a Python notebook and visualized using matplotlib graphs.
- **Note**: No synchronization was used, focusing on parallel performance benchmarking.
- **Comparison**: C pthreads results were referenced from a previous similar experiment.
- **Code** (see appendix)
## Results
The OpenMP implementation in C demonstrated superior scalability in terms of running time and speedup, particularly evident up to 32 threads. Beyond this point, performance plateaued, likely due to inter-CPU cache sharing inefficiencies. In contrast, Java threads and pthreads showed less stable scaling, with diminishing returns observed at lower thread counts, indicating less efficient use of available processors.
- OpenMP Runtimes
![OpenMP Runtimes](./runtimes.png)
- OpenMP Speedup
![OpenMP Speedup](./speedup.png)
- Java Runtimes
![OpenMP Runtimes](./runtimes_java.png)
- Java Speedup
![OpenMP Speedup](./speedup_java.png)
- C Pthreads Runtimes and comparison to serial
![Pthreads Runtimes](runtimes_pthreads.png)
## Conclusion
The experiment underscores OpenMP's efficiency in scaling across multiple processors within a single CPU, as evidenced by the performance plateau at 32 threads corresponding to the single CPU's thread capacity. The diminishing returns observed with Java threads and pthreads suggest that OpenMP's model of parallelism, with its shared memory architecture and compiler optimizations, is more adept at exploiting the computational resources for parallel tasks. This is particularly relevant in systems with a high number of cores and threads, where efficient scaling is crucial. Future work could explore the impact of using differnt algorithms and paradigms such as thread synchronization and semaphores or the use of generalized "reduce/scan" functions.
## Appendix
### Code
#### OpenMP
##### Makefile
```makefile
all: count_threes
count_threes: count_threes.c
gcc -fopenmp -o count_threes count_threes.c
run:
for threads in 1 2 4 8 16 32 64; do \
echo "$$threads threads" >> results.txt; \
for i in {1..5}; do \
OMP_NUM_THREADS=$$threads ./count_threes 1000000000 >> results.txt; \
done; \
done
clean:
rm -f count_threes results.txt
```
##### Program
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <omp.h>
int count3s(int* array, int length)
{
int count = 0;
int i;
#pragma omp parallel shared(array, count, length) private(i)
{
int count_p = 0;
#pragma omp for
for (i = 0; i < length; i++) {
if (array[i] == 3) {
count_p++;
}
}
// #pragma omp critical
{
count += count_p;
}
}
return count;
}
int main(int argc, char const *argv[])
{
if (argc != 2) {
printf("Usage: %s <array_size>\n", argv[0]);
return 1;
}
int length = atoi(argv[1]);
if (length <= 0) {
printf("Error: array size must be a positive integer\n");
return 1;
}
int* array = (int*) malloc(length * sizeof(int));
int i;
srand(time(NULL));
for (i = 0; i < length; i++) {
array[i] = rand() % 10 + 1;
}
double start_time, end_time;
start_time = omp_get_wtime();
int count = count3s(array, length);
end_time = omp_get_wtime();
printf("count: %d\n", count);
printf("Time taken: %f seconds\n", end_time - start_time);
free(array);
return 0;
}
```
#### Java Threads
##### Makefile
```makefile
all: CountThrees.class
CountThrees.class:
javac CountThrees.java
run:
for threads in 1 2 4 8 16 32 64; do \
echo "$$threads threads" >> results.txt; \
for i in {1..5}; do \
java CountThrees 1000000000 $$threads >> results.txt; \
done \
done
clean:
rm -f CountThrees.class results.txt
```
##### Program
```java
import java.util.*;
public class CountThrees implements Runnable {
private static int ARRAY_LENGTH;
private static int MAX_THREADS;
private static final int MAX_RANGE = 100;
private static final Random random = new Random();
private static int count = 0;
private static final Object lock = new Object();
private static int[] array;
private static Thread[] threads;
public static void main(String[] args) {
if (args.length != 2) {
System.err.println("Usage: java CountThrees <array_length> <max_threads>");
System.exit(1);
}
try {
ARRAY_LENGTH = Integer.parseInt(args[0]);
MAX_THREADS = Integer.parseInt(args[1]);
} catch (NumberFormatException e) {
System.err.println("Invalid argument: " + args[0] + " or " + args[1] + " is not a valid integer");
System.exit(1);
}
array = new int[ARRAY_LENGTH];
// Initialize the elements in the array
for (int i = 0; i < array.length; i++) {
array[i] = random.nextInt(MAX_RANGE);
}
long startTime = System.nanoTime();
// Create the threads
CountThrees[] counters = new CountThrees[MAX_THREADS];
int lengthPerThread = ARRAY_LENGTH / MAX_THREADS;
for (int i = 0; i < counters.length; i++) {
counters[i] = new CountThrees(
i * lengthPerThread,
lengthPerThread
);
}
// Run the threads
threads = new Thread[MAX_THREADS];
for (int i = 0; i < counters.length; i++) {
threads[i] = new Thread(counters[i]);
threads[i].start();
}
for (int i = 0; i < counters.length; i++) {
try {
threads[i].join();
} catch (InterruptedException e) {
// Do nothing
}
}
long endTime = System.nanoTime();
// Print the number of threes
System.out.println("Number of threes: " + count);
// compute the elapsed time in nanoseconds
long duration = (endTime - startTime);
// convert to seconds
double seconds = (double)duration / 1_000_000_000.0;
System.out.println("Time taken: " + seconds + " seconds");
}
private int startIndex;
private int elements;
private int myCount = 0;
public CountThrees(int start, int elem) {
startIndex = start;
elements = elem;
}
// Overload of run method in the Thread class
public void run() {
// Count the number of threes
for (int i = 0; i < elements; i++) {
if (array[startIndex + i] == 3) {
myCount++;
}
}
// synchronized (lock) {
// count += myCount;
// }
count += myCount;
}
}
```
Binary file not shown.
Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 28 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB