-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update new exercises and descriptions
- Loading branch information
1 parent
84b5ec1
commit 3da2b60
Showing
11 changed files
with
276 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,5 +2,6 @@ | |
_site | ||
.jekyll-cache | ||
CodingTutorialC++/cmake-build-debug | ||
CodingTutorialC++/misc.xml | ||
ruby | ||
.bundle |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters