Skip to content

Commit 2324a56

Browse files
committed
OMP Parallel
OMP functional parallel here
1 parent b3256d6 commit 2324a56

File tree

6 files changed

+47
-17
lines changed

6 files changed

+47
-17
lines changed

.vscode/c_cpp_properties.json

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"configurations": [
3+
{
4+
"name": "Win32",
5+
"includePath": [
6+
"${workspaceFolder}/**"
7+
],
8+
"defines": [
9+
"_DEBUG",
10+
"UNICODE",
11+
"_UNICODE"
12+
],
13+
"compilerPath": "/usr/bin/gcc",
14+
"cStandard": "gnu11",
15+
"cppStandard": "gnu++14",
16+
"intelliSenseMode": "linux-gcc-x64",
17+
"compileCommands": "${workspaceFolder}/compile_commands.json"
18+
}
19+
],
20+
"version": 4
21+
}

.vscode/settings.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"files.associations": {
3+
"complex": "cpp"
4+
}
5+
}

CMakeLists.txt

+2-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ cmake_minimum_required(VERSION 3.2)
44
set(CMAKE_CXX_STANDARD 17)
55
set(CMAKE_CXX_STANDARD_REQUIRED ON)
66
set(CMAKE_CXX_EXTENSIONS OFF)
7-
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pthread")
8-
9-
project(fractals)
7+
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -pthread")
8+
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -fopenmp")
109

1110
# Add dependencies
1211
find_package(Qt5 COMPONENTS Widgets PrintSupport REQUIRED)

exported/qcustomplot.cmake

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ if("${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}" LESS 2.5)
44
message(FATAL_ERROR "CMake >= 2.6.0 required")
55
endif()
66
cmake_policy(PUSH)
7-
cmake_policy(VERSION 2.6)
7+
cmake_policy(VERSION 2.6...3.18)
88
#----------------------------------------------------------------
99
# Generated CMake target import file.
1010
#----------------------------------------------------------------

include/fractals.hpp

+11-11
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <iostream>
44
#include <vector>
55
#include <thread>
6+
#include <omp.h>
67

78
namespace fractals {
89

@@ -27,37 +28,36 @@ ComplexDouble mandelbrot_process(ComplexDouble c, int boundary = 2,
2728
}
2829

2930
void mandelbrot(double start_val, double end_val, double step_val) {
31+
FractalSet temp_set;
3032
double step = step_val; // 0.01;
3133
double start = start_val; //-2.0;
3234
double end = end_val; //2.0;
33-
// FractalSet m_set;
3435
for (double i = start; i < end; i += step) {
3536
for (double j = start; j < end; j += step) {
3637
ComplexDouble c(i, j);
37-
m_set.push_back(mandelbrot_process(c));
38+
temp_set.push_back(mandelbrot_process(c));
3839
}
3940
}
41+
#pragma omp critical
42+
m_set.insert(m_set.end(), temp_set.begin(), temp_set.end());
4043

4144

4245
// return m_set;
4346
} // namespace fractals
4447

4548

4649
FractalSet thread(){
47-
// m_set.push_back(0);
48-
// Pre loop thread setup
49-
int num_threads = std::thread::hardware_concurrency();
50-
std::vector<std::thread> threads;
51-
// std::mutex critical;
50+
// // Pre loop thread setup
51+
int num_threads = omp_get_max_threads();
5252
std::cout << num_threads << std::endl;
53+
omp_set_num_threads(omp_get_max_threads());
54+
#pragma omp parallel
55+
#pragma omp for
5356
for(int t=0; t<num_threads; t++) {
5457
double start_here = -2.0+(t*4/num_threads);
5558
double end_here = -2.0+((t+1)*4/num_threads);
56-
threads.push_back(std::thread(mandelbrot, start_here, end_here, 0.01));
57-
threads[t].join();
59+
mandelbrot(start_here, end_here, 0.001);
5860
}
59-
// std::thread first(mandelbrot, -2.0, 2.0, 0.01, m_set);
60-
// first.join();
6161

6262
return m_set;
6363
}

src/mandelbrot.cpp

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#include <iostream>
2-
#include <thread>
32
#include <fstream>
43
#include <complex>
4+
#include <chrono>
5+
56

67

78
#include "../external/qcustomplot/qcustomplot.h"
@@ -25,7 +26,8 @@ using ComplexDouble = std::complex<double>;
2526
using FractalSet = std::vector<ComplexDouble>;
2627

2728
int main() {
28-
FractalSet m_set = fractals::thread();
29+
auto start = std::chrono::high_resolution_clock::now();
30+
auto m_set = fractals::thread();
2931
// std::cout << "{ ";
3032
// for (auto i : m_set) {
3133
// std::cout << i << " ";
@@ -44,6 +46,9 @@ int main() {
4446
// custom_plot.addGraph();
4547
// custom_plot.graph(0)->setPen(QPen(Qt::blue)); // line color blue for
4648
// first graph custom_plot.graph(0)->setBrush(QBrush(QColor(0, 0, 255,20))); // first graph will be filled with translucent blue
49+
auto stop = std::chrono::high_resolution_clock::now();
50+
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(stop - start);
51+
std::cout << "Runtime is " << duration.count() << " milliseconds" <<std::endl;
4752
return 0;
4853
}
4954

0 commit comments

Comments
 (0)