Compare commits

..
4 Commits
Author SHA256 Message Date
OkunElya 123e5b8661 add omp parrallel sum with critical section 2026-11-26 13:15:08 +10:00
OkunElya d90415c892 init uv project, add new sum implementations 2026-11-26 13:01:03 +10:00
OkunElya 6b025a81f8 utils update 2026-11-26 13:00:41 +10:00
OkunElya bfa18c90ec add debugger config 2026-11-26 13:00:30 +10:00
13 changed files with 241 additions and 20 deletions
-1
View File
@@ -1,2 +1 @@
*.out
.zed
+1
View File
@@ -0,0 +1 @@
3.13
+36
View File
@@ -0,0 +1,36 @@
// Project-local debug tasks
//
// For more documentation on how to configure debug tasks,
// see: https://zed.dev/docs/debugger
[
{
"label": "Build Main",
"adapter": "CodeLLDB",
"program": "a.out",
"request": "launch",
"build": {
"command": "g++",
"args": ["-std=c++20", "-fopenmp", "./src/main.cpp", "-o3"]
}
},
{
"label": "Build & Debug Main",
"adapter": "CodeLLDB",
"request": "launch",
"program": "$ZED_WORKTREE_ROOT/a.out",
"cwd": "$ZED_WORKTREE_ROOT",
"stopOnEntry": false,
"build": {
"command": "g++",
"args": [
"-std=c++20",
"-fopenmp",
"-g",
"-O0",
"./src/main.cpp",
"-o",
"a.out"
]
}
}
]
+8
View File
@@ -0,0 +1,8 @@
{
"languages": {
"C++": {
"tab_size": 4,
"format_on_save": "on"
}
}
}
Executable
BIN
View File
Binary file not shown.
View File
+10
View File
@@ -0,0 +1,10 @@
#include <stdlib.h>
#include<utility>
auto get_range(std::size_t n , unsigned t, unsigned T){
size_t s = n/T, b = n%T;
if(t<b)
b=++s*t;
else
b+=s*t;
return std::pair{b, b+s};
}
-10
View File
@@ -1,10 +0,0 @@
from plotly.express import scatter
from os import mkdir,listdir
from pandas import read_csv
MEASUREMENTS_DIR= "./measurements/"
mkdir(MEASUREMENTS_DIR, exists_ok = True)
files = os.listdir(MEASUREMENTS_DIR)
files = list(filter(lambda x :str(x).endswith(".csv")))
+9
View File
@@ -0,0 +1,9 @@
[project]
name = "parallel-compute"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.13"
dependencies = [
"plotly>=7.2.0",
]
+27
View File
@@ -0,0 +1,27 @@
from os import listdir, makedirs
from csv import DictReader
from typing_extensions import Dict
from plotly.express import scatter
MEASUREMENTS_DIR = "./measurements/"
makedirs(MEASUREMENTS_DIR, exist_ok=True)
files = listdir(MEASUREMENTS_DIR)
files = list(filter(lambda x: str(x).endswith(".csv"), files))
readings=dict()
for file in files:
with open(MEASUREMENTS_DIR+file) as F:
data = DictReader(F)
times = [ float(x["time"]) for x in data]
speedups = [ float(x["speedups"])for x in data]
threads = [ float(x["T"]) for x in data]
result = {"x": threads, "y":times}
readings[file[:-4]]= result
for name, data in readings.items():
+1 -1
View File
@@ -32,4 +32,4 @@ public:
}
};
int main() {}
// int main() {}
+104 -8
View File
@@ -1,4 +1,6 @@
#include "../include/num_thread.cpp"
#include "../include/ranges.cpp"
#include "barrier.cpp"
#include <cstddef>
#include <cstdio>
#include <cstdlib>
@@ -80,7 +82,7 @@ unsigned sum_par_rr_no_cache(const unsigned *V, std::size_t n) {
// lab 3
unsigned sum_loc_cpp(const unsigned *V, std::size_t n) {
unsigned T = std::thread::hardware_concurrency();
unsigned T = get_num_threads();
struct alignas(std::hardware_destructive_interference_size)
p_t { // destructive as we want to minimize the distructive effect of
// cache interference
@@ -88,19 +90,15 @@ unsigned sum_loc_cpp(const unsigned *V, std::size_t n) {
};
auto p = std::make_unique<p_t[]>(T);
auto worker_proc = [V, n, T, &p](const unsigned t) {
size_t s = n / T, b = n % T;
if (t < b)
b = ++s * t;
else
b += s * t;
unsigned e = b + s;
auto [beg, end] = get_range(n, t, T);
unsigned sum = 0;
for (unsigned i = b; i < e; i++) {
for (unsigned i = beg; i < end; i++) {
sum += V[i];
// p[t].v+= V[i];
}
p[t].v = sum;
};
unsigned sum = 0;
std::vector<std::thread> workers(T);
for (unsigned t = 0; t < T; t++) {
workers[t] = std::thread(worker_proc, t);
@@ -115,6 +113,90 @@ unsigned sum_loc_cpp(const unsigned *V, std::size_t n) {
return result;
}
unsigned sum_cpp_bar(const unsigned *V, size_t n) {
unsigned T = get_num_threads();
Barrier b(T);
struct alignas(std::hardware_destructive_interference_size)
p_t { // destructive as we want to minimize the distructive effect of
// cache interference
unsigned v;
};
auto p = std::make_unique<p_t[]>(T);
auto worker_proc = [T, &b, &p, V, n](unsigned t) {
auto [beg, end] = get_range(n, t, T);
unsigned U = 0;
for (size_t i = beg; i < end; ++i) {
U += V[i];
}
p[t].v = U;
b.arrive_and_wait();
for (size_t step = 1, next = 2; step < T; step = next, next += next) {
if (t + step < T && !(t % next)) {
p[t].v += p[t + step].v;
}
b.arrive_and_wait();
}
};
std::vector<std::thread> workers(T - 1);
for (size_t t = 0; t < T - 1; ++t) {
workers[t] = std::thread(worker_proc, t + 1);
}
worker_proc(0);
for (auto &w : workers) {
w.join();
}
return p[0].v;
}
// cpp parallel with custom num threads with mutex
unsigned sum_cpp_mtx(const unsigned *V, size_t n) {
unsigned T = get_num_threads();
std::mutex m;
unsigned result = 0;
auto worker_proc = [T, &m, &result, V, n](unsigned t) {
auto [beg, end] = get_range(n, t, T);
unsigned U = 0;
for (size_t i = beg; i < end; ++i)
U += V[i];
{
std::scoped_lock(m);
result += U;
}
};
std::vector<std::thread> workers(T - 1);
for (size_t t = 0; t < T - 1; ++t) {
workers[t] = std::thread(worker_proc, t + 1);
}
worker_proc(0);
for (auto &w : workers) {
w.join();
}
return result;
}
unsigned sum_omp_mtx(const unsigned *V, size_t n) {
unsigned T = get_num_threads();
unsigned result = 0;
#pragma omp parallel shared(result)
{
unsigned t = omp_get_thread_num();
auto [beg, end] = get_range(n, t, T);
unsigned v = 0;
for (size_t i = beg; i < end; i++) {
v += V[i];
}
#pragma omp critical
{
result = ++v;
}
}
return result;
}
int main(int argc, char **argv) {
std::size_t n = 1u << 28;
auto data = std::make_unique<unsigned[]>(n);
@@ -157,5 +239,19 @@ int main(int argc, char **argv) {
printf("sum_loc_cpp: :0x%x\n It took %g s \n", r, t1 - t0);
}
{
auto t0 = omp_get_wtime();
auto r = sum_cpp_bar(data.get(), n);
auto t1 = omp_get_wtime();
printf("sum_cpp_bar: :0x%x\n It took %g s \n", r, t1 - t0);
}
{
auto t0 = omp_get_wtime();
auto r = sum_cpp_bar(data.get(), n);
auto t1 = omp_get_wtime();
printf("sum_cpp_mtx: :0x%x\n It took %g s \n", r, t1 - t0);
}
return 0;
}
Generated
+45
View File
@@ -0,0 +1,45 @@
version = 1
revision = 3
requires-python = ">=3.13"
[[package]]
name = "narwhals"
version = "2.29.0"
source = { registry = "https://pypi.org/simple" }
sdist = { url = "https://files.pythonhosted.org/packages/c4/20/e2cce41bfa17909205b39c8a4aa3ec730d87f1c4e77453efd3f5aaf50b81/narwhals-2.29.0.tar.gz", hash = "sha256:eb19c877d9a705300bcc9414499a07b32a52db703369414e57e474ca5fc63c42", size = 773295, upload-time = "2026-11-17T15:30:43.03Z" }
wheels = [
{ url = "https://files.pythonhosted.org/packages/9e/2b/20b4b463c3795dc390db8fa6ec2b5c3e0aea90a760f813305da4c33dd814/narwhals-2.29.0-py3-none-any.whl", hash = "sha256:1c69b3dc30240f6e848c5a673cafc86affa44184ecce721e25b7194663ad10dc", size = 501718, upload-time = "2026-11-17T15:30:41.263Z" },
]
[[package]]
name = "packaging"
version = "26.4"
source = { registry = "https://pypi.org/simple" }
sdist = { url = "https://files.pythonhosted.org/packages/c3/75/ffb1dc0ed12352139fb4ca0e3fde4325a23e3d0f3f40efe89717bc4f557b/packaging-26.4.tar.gz", hash = "sha256:c4b671c41ddb9bf26e543c37723610d28fb6bf7affc4da17cf048d419d2c062a", size = 355877, upload-time = "2026-11-23T21:44:11.152Z" }
wheels = [
{ url = "https://files.pythonhosted.org/packages/ba/7b/5504f91c9c2733f693d7759e50ede5d27119d4da7e5836c46d3b5ff81d56/packaging-26.4-py3-none-any.whl", hash = "sha256:81383611a09c1bc0b08b76e0fd5829d7c6824c65b27a6aee339da133f4233ea2", size = 140049, upload-time = "2026-11-23T21:44:09.531Z" },
]
[[package]]
name = "parallel-compute"
version = "0.1.0"
source = { virtual = "." }
dependencies = [
{ name = "plotly" },
]
[package.metadata]
requires-dist = [{ name = "plotly", specifier = ">=7.2.0" }]
[[package]]
name = "plotly"
version = "7.2.0"
source = { registry = "https://pypi.org/simple" }
dependencies = [
{ name = "narwhals" },
{ name = "packaging" },
]
sdist = { url = "https://files.pythonhosted.org/packages/40/23/ed969530891a2fd44991f9d4c733ff349d87903b11d75366f1dee607f3ef/plotly-7.2.0.tar.gz", hash = "sha256:0c8299da0556ba9bf18d157f23982d189b627492a6c3cafba9d010b1b6ccb746", size = 6699266, upload-time = "2026-11-24T21:33:02.618Z" }
wheels = [
{ url = "https://files.pythonhosted.org/packages/f8/43/86c04582164c54476d4dae5c053af82ba70b975f5babbf183ae6e51159b9/plotly-7.2.0-py3-none-any.whl", hash = "sha256:51ed86a5e71b0abfe4df9679a1da0564f9e7e93d5dc7cb35d433256b2cfce854", size = 9702562, upload-time = "2026-11-24T21:33:00.154Z" },
]