Skip to content

Commit

Permalink
update new exercises and descriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
weiguangcui committed May 10, 2024
1 parent 84b5ec1 commit 3da2b60
Show file tree
Hide file tree
Showing 11 changed files with 276 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
_site
.jekyll-cache
CodingTutorialC++/cmake-build-debug
CodingTutorialC++/misc.xml
ruby
.bundle
5 changes: 5 additions & 0 deletions CodingTutorialC++/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,9 @@ add_executable(ACO_practice overflow.cpp
different_arrays.cpp
vector.cpp
Sort_index.cpp
openmp-no-parallel.cpp
openmp-sections.cpp
openmp-nested-for-loop.cpp
openmp-parallel-output.cpp
problemtic_program_for_gdb.cpp
)
21 changes: 19 additions & 2 deletions CodingTutorialC++/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@ TARGET = ACO_practice
# Source files: SRCS is a list of source files
SRCS = overflow.cpp MainArgs.cpp openmp-example.cpp pi.cpp pointer-dam.cpp pointer-dam3D.cpp \
pointer-update-parameter.cpp IO.cpp class.cpp structure.cpp Sort_index.cpp vector.cpp \
different_arrays.cpp
different_arrays.cpp openmp-nested-for-loop.cpp openp-no-parallel.cpp openmp-parallle-output.cpp \
openmp-sections.cpp problemtic_program_for_gdb.cpp

# Object files
# The $(SRCS:.cpp=.o) is a substitution reference which replaces the .cpp extension with .o in SRCS
OBJS = $(SRCS:.cpp=.o)

# Executables
EXECS = overflow MainArgs openmp-example pi pointer-dam pointer-dam3D IO pointer-update-parameter \
class structure Sort_index vector different_arrays
class structure Sort_index vector different_arrays openmp-nested-for-loop openmp-no-parallel \
openmp-parallle-output openmp-sections problemtic_program_for_gdb

# Number of threads for OpenMP
# OMP_NUM_THREADS is an environment variable that specifies the number of threads to use for OpenMP
Expand Down Expand Up @@ -78,6 +80,21 @@ vector: vector.o
different_arrays: different_arrays.o
$(CXX) $(CXXFLAGS) -o different_arrays different_arrays.o

openmp-nested-for-loop: openmp-nested-for-loop.o
$(CXX) $(CXXFLAGS) -o openmp-nested-for-loop openmp-nested-for-loop.o

openmp-no-parallel: openmp-no-parallel.o
$(CXX) $(CXXFLAGS) -o openmp-no-parallel openmp-no-parallel.o

openmp-parallle-output: openmp-parallle-output.o
$(CXX) $(CXXFLAGS) -o openmp-parallle-output openmp-parallle-output.o

openmp-sections: openmp-sections.o
$(CXX) $(CXXFLAGS) -o openmp-sections openmp-sections.o

problemtic_program_for_gdb: problemtic_program_for_gdb.o
$(CXX) $(CXXFLAGS) -o problemtic_program_for_gdb problemtic_program_for_gdb.o


# Rule for building object files from source files
# The $(CXX) $(CXXFLAGS) -c $< -o $@ command compiles the source file into an object file
Expand Down
29 changes: 29 additions & 0 deletions CodingTutorialC++/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,27 @@ This file contains a program that prints the name of the program and all the arg

This file contains a program that uses OpenMP to calculate the sum of numbers from 1 to 1e5. It sets the number of threads to the number of processors available, and each thread calculates a portion of the sum.

### [`openmp-nested-for-loop.cpp`](./openmp-nested-for-loop.cpp)

This file contains a program that demonstrates the use of nested for loops in OpenMP. It shows how to parallelize both outer and inner loops for efficient computation.

### [`openmp-no-parallel.cpp`](./openmp-no-parallel.cpp)

This file contains a program that demonstrates the cases that OpenMP parallelization can not be used. It shows how a program behaves when run on a single thread.

### [`openmp-parallel-output.cpp`](./openmp-parallel-output.cpp)

This file contains a program that demonstrates the use of OpenMP for parallel output. It shows how to write to different files in parallel using OpenMP.

### [`openmp-sections.cpp`](./openmp-sections.cpp)

This file contains a program that demonstrates the use of sections in OpenMP. It shows how to divide a program into sections that can be executed in parallel.

### [`problemtic_program_for_gdb.cpp`](./problemtic_program_for_gdb.cpp)

This file contains a program that is designed to be debugged using GDB. It includes intentional errors that can be investigated using the debugger.


### [`pi.cpp`](./pi.cpp)

This file contains a program that calculates the value of pi using the Leibniz formula for π. The program uses OpenMP for parallel computation, allowing it to calculate pi to a high degree of precision in a shorter amount of time. The number of threads used for the computation can be specified as a command-line argument when running the program.
Expand Down Expand Up @@ -92,6 +113,14 @@ make pointer-update-parameter
make IO
make class
make structure
make different_arrays
make vector
make Sort_index
make openmp-nested-for-loop
make openmp-no-parallel
make openmp-parallel-output
make openmp-sections
make problemtic_program_for_gdb
```

To clean up the built files, use the make clean command:
Expand Down
1 change: 1 addition & 0 deletions CodingTutorialC++/different_arrays.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ int main() {
// test array out of scope
test_array_out_of_scope();
int* arr2 = new int[100000000];
int arr3[100000];
test_array_out_of_scope(); // run twice to see if the memory location is the same
delete[] arr2;

Expand Down
22 changes: 22 additions & 0 deletions CodingTutorialC++/openmp-nested-for-loop.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// Created by weiguang on 9/05/24.
//
#include <omp.h>
#include <iostream>

int main() {
int N = 1000;
int M = 10;
int matrix[N][M];

// #pragma omp parallel for collapse(2)
#pragma omp parallel for
for(int i = 0; i < N; i++) {
// #pragma omp parallel for
for(int j = 0; j < M; j++) {
matrix[i][j] = i * j;
}
}

return 0;
}
84 changes: 84 additions & 0 deletions CodingTutorialC++/openmp-no-parallel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
//
// Created by weiguang on 9/05/24.
//
#include <omp.h>
#include <iostream>
#include <vector>

// Sequential tasks
void sequentialTasks() {
int arr[10];
arr[0] = 1;
int brr[10];
brr[0] = 1;

//#pragma omp parallel sections
for(int i = 1; i < 10; i++) {
//#pragma omp parallel sections // any difference?
// #pragma omp section
arr[i] = arr[i-1] * 2; // Each iteration depends on the previous one
// #pragma omp section
brr[i] = brr[i-1] * 3;
}

for(int i = 0; i < 10; i++) {
std::cout << arr[i] << " ";
}
std::cout << std::endl;
}

// Shared resource access
void sharedResourceAccess() {
std::vector<int> sharedVector;

// #pragma omp parallel for
for(int i = 0; i < 100; i++) {
sharedVector.push_back(i); // Race condition here
}

std::cout << "Vector size: " << sharedVector.size() << std::endl;
}

// Recursion
int fibonacci(int n) {
if(n <= 1) {
return n;
} else {
return fibonacci(n-1) + fibonacci(n-2); // Recursive calls
}
}

void testFibonacci() {
std::cout << "Fibonacci(10): " << fibonacci(10) << std::endl;
}

// Complex dependencies
void complexDependencies() {
std::vector<int> numbers = {1, 2, 3, 4, 5};
std::vector<int> results(numbers.size());

for(int i = 1; i < numbers.size() - 1; i++) {
results[i] = numbers[i-1] * numbers[i+1]; // Each operation depends on the previous and next number
}

for(int i = 0; i < results.size(); i++) {
std::cout << results[i] << " ";
}
std::cout << std::endl;
}

int main() {
std::cout << "Sequential tasks example:\n";
sequentialTasks();

std::cout << "\nShared resource access example:\n";
sharedResourceAccess();

std::cout << "\nRecursion example:\n";
testFibonacci();

std::cout << "\nComplex dependencies example:\n";
complexDependencies();

return 0;
}
49 changes: 49 additions & 0 deletions CodingTutorialC++/openmp-parallel-output.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//
// Created by weiguang on 9/05/24.
//
#include <omp.h>
#include <fstream>
#include <sstream>

int main() {
int N = omp_get_max_threads();
int data[1000]; // Assuming you have 1000 data points

// Initialize the data array
for(int i = 0; i < 1000; i++) {
data[i] = i * i; // Just an example, replace with your actual data
}

int chunk_size = 1000 / N; // Size of each chunk of data

// Write data to files in parallel
#pragma omp parallel num_threads(N)
{
int thread_id = omp_get_thread_num();

// Create a unique filename for each thread
std::stringstream ss;
ss << "output" << thread_id << ".txt";

// Open the file and write to it
std::ofstream file(ss.str());

// Calculate the start and end indices for this thread's chunk of data
int start = thread_id * chunk_size;
int end = start + chunk_size;

// If this is the last thread, make sure it processes all remaining data
if(thread_id == N - 1) {
end = 1000;
}

for(int i = start; i < end; i++) {
file << "Data: " << data[i] << std::endl;
}

// Close the file
file.close();
}

return 0;
}
48 changes: 48 additions & 0 deletions CodingTutorialC++/openmp-sections.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//
// Created by weiguang on 9/05/24.
//
#include <omp.h>
#include <iostream>

void task1() {
std::cout << "Task 1 executed by thread " << omp_get_thread_num() << std::endl;
}

void task2() {
std::cout << "Task 2 executed by thread " << omp_get_thread_num() << std::endl;
}

void task3() {
std::cout << "Task 3 executed by thread " << omp_get_thread_num() << std::endl;
}

void task4() {
std::cout << "Task 4 executed by thread " << omp_get_thread_num() << std::endl;
}

int main() {
#pragma omp parallel sections
{
#pragma omp section
{
task1();
}

#pragma omp section
{
task2();
}

#pragma omp section
{
task3();
}

#pragma omp section
{
task4();
}
}

return 0;
}
16 changes: 16 additions & 0 deletions CodingTutorialC++/problemtic_program_for_gdb.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// Created by weiguang on 10/05/24.
//
#include <iostream>

int main() {
int arr[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

// Attempt to access the array out of its bounds
for (int i = 0; i <= 10; i++) {
arr[i] += i;
std::cout << arr[i] << std::endl; // This will cause a segmentation fault when i == 10
}

return 0;
}
4 changes: 2 additions & 2 deletions Exercises/Mandelbrot/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ CXX = g++
CXXFLAGS = -DHIGH_TIME_PRECISION

# Compiler flags
CXXFLAGS += -Wall -Wextra -O2 -fopenmp
CXXFLAGS += -pg -ftest-coverage -lgcov -Wall -Wextra -O2 -fopenmp



Expand All @@ -30,4 +30,4 @@ $(EXEC): $(OBJS)
$(CXX) $(CXXFLAGS) -c $< -o $@

clean:
rm -f $(OBJS) $(EXEC)
rm -f $(OBJS) $(EXEC)

0 comments on commit 3da2b60

Please sign in to comment.