Pushing everything again, accidentally deleted my remote repository

This commit is contained in:
jakob.schratter 2025-12-09 22:06:13 +01:00
commit 1bee3e8e5b
101 changed files with 9428 additions and 0 deletions

30
ex5/ex5_3/Makefile Normal file
View file

@ -0,0 +1,30 @@
#
# use GNU-Compiler tools
COMPILER=GCC_
# alternatively from the shell
# export COMPILER=GCC_
# or, alternatively from the shell
# make COMPILER=GCC_
# use Intel compilers
#COMPILER=ICC_
# use PGI compilers
# COMPILER=PGI_
SOURCES = main.cpp goldbach.cpp
OBJECTS = $(SOURCES:.cpp=.o)
PROGRAM = main.${COMPILER}
# uncomment the next to lines for debugging and detailed performance analysis
CXXFLAGS += -g
LINKFLAGS += -g
# do not use -pg with PGI compilers
ifndef COMPILER
COMPILER=GCC_
endif
include ../${COMPILER}default.mk

46
ex5/ex5_3/goldbach.cpp Normal file
View file

@ -0,0 +1,46 @@
#include "goldbach.h"
#include <iostream>
#include <iterator>
#include <omp.h>
size_t single_goldbach(size_t k)
{
const std::vector<size_t> relevant_primes = get_primes(k);
size_t m = relevant_primes.size();
size_t counter = 0;
#pragma omp parallel for shared(relevant_primes, m, k) reduction(+:counter)
for(size_t i = 0; i < m; ++i)
{
for(size_t j = i; j < m; ++j)
{
if(relevant_primes[i] + relevant_primes[j] == k)
++counter;
}
}
return counter;
}
std::vector<size_t> count_goldbach(size_t n)
{
const std::vector<size_t> relevant_primes = get_primes(n);
size_t m = relevant_primes.size();
std::vector<size_t> counter_vector(n + 1, 0);
#pragma omp parallel for shared(relevant_primes, m, n) reduction(VecAdd:counter_vector)
for(size_t i = 0; i < m; ++i)
{
for(size_t j = i; j < m; ++j)
{
size_t sum = relevant_primes[i] + relevant_primes[j];
if(sum <= n)
++counter_vector[relevant_primes[i] + relevant_primes[j]];
}
}
return counter_vector;
}

45
ex5/ex5_3/goldbach.h Normal file
View file

@ -0,0 +1,45 @@
#pragma once
#include "mayer_primes.h"
#include <cassert>
#include <vector>
/**
This function returns the number of possible decompositions of an integer into a sum of two prime numbers.
@param[in] k first integer
@param[out] count number of decompositions
*/
size_t single_goldbach(size_t k);
/**
This function returns the number of possible decompositions into a sum of two prime numbers of all even integers in the interval [4,n].
@param[in] n upper integer bound
@param[out] count_vector vector containing the number of decompositions for a natural number the corresponding index
*/
std::vector<size_t> count_goldbach(size_t n);
/** Vector @p b adds its elements to vector @p a .
@param[in] a vector
@param[in] b vector
@return a+=b componentwise
*/
template<class T>
std::vector<T> &operator+=(std::vector<T> &a, std::vector<T> const &b)
{
assert(a.size()==b.size());
for (size_t k = 0; k < a.size(); ++k) {
a[k] += b[k];
}
return a;
}
// Declare the reduction operation in OpenMP for an STL-vector
// omp_out += omp_in requires operator+=(vector<int> &, vector<int> const &) from above
// ------------------------------------------------------------
// https://scc.ustc.edu.cn/zlsc/tc4600/intel/2016.0.109/compiler_c/common/core/GUID-7312910C-D175-4544-99C5-29C12D980744.htm
// https://gist.github.com/eruffaldi/7180bdec4c8c9a11f019dd0ba9a2d68c
// https://stackoverflow.com/questions/29633531/user-defined-reduction-on-vector-of-varying-size
// see also p.74ff in https://www.fz-juelich.de/ias/jsc/EN/AboutUs/Staff/Hagemeier_A/docs-parallel-programming/OpenMP-Slides.pdf
#pragma omp declare reduction(VecAdd : std::vector<size_t> : omp_out += omp_in) initializer (omp_priv=omp_orig)

45
ex5/ex5_3/main.cpp Normal file
View file

@ -0,0 +1,45 @@
#include "goldbach.h"
#include <algorithm>
#include <iostream>
#include <omp.h>
using namespace std;
int main()
{
cout << "Check: 694 has "<< single_goldbach(694) << " decompositions." << endl << "----------------------------------------" << endl;
for(size_t n : {10000, 100000, 400000, 1000000, 2000000})
{
double t_start = omp_get_wtime();
auto goldbach_vector = count_goldbach(n);
auto max_it = max_element(goldbach_vector.begin(), goldbach_vector.end());
size_t max_number = distance(goldbach_vector.begin(), max_it);
double t_end = omp_get_wtime() - t_start;
cout << "The number " << max_number << " has " << *max_it << " decompositions. Duration: " << t_end << endl;
}
/*
###### WITHOUT PARALLELIZATION ######
The number 9240 has 329 decompositions. Duration: 0.00307696
The number 99330 has 2168 decompositions. Duration: 0.189839
The number 390390 has 7094 decompositions. Duration: 1.3042
The number 990990 has 15594 decompositions. Duration: 5.45034
The number 1981980 has 27988 decompositions. Duration: 47.1807
###### WITH PARALLELIZATION ######
The number 9240 has 329 decompositions. Duration: 0.000734854
The number 99330 has 2168 decompositions. Duration: 0.0251322
The number 390390 has 7094 decompositions. Duration: 0.487375
The number 990990 has 15594 decompositions. Duration: 6.16972
The number 1981980 has 27988 decompositions. Duration: 31.5699
*/
return 0;
}

73
ex5/ex5_3/mayer_primes.h Normal file
View file

@ -0,0 +1,73 @@
#pragma once
#include <cstring> //memset
#include <vector>
//using namespace std;
/** \brief Determines all prime numbers in interval [2, @p max].
*
* The sieve of Eratosthenes is used.
*
* The implementation originates from <a href="http://code.activestate.com/recipes/576559-fast-prime-generator/">Florian Mayer</a>.
*
* \param[in] max end of interval for the prime number search.
* \return vector of prime numbers @f$2,3,5, ..., p<=max @f$.
*
* \copyright
* Copyright (c) 2008 Florian Mayer (adapted by Gundolf Haase 2018)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
template <class T>
std::vector<T> get_primes(T max)
{
std::vector<T> primes;
char *sieve;
sieve = new char[max / 8 + 1];
// Fill sieve with 1
memset(sieve, 0xFF, (max / 8 + 1) * sizeof(char));
for (T x = 2; x <= max; x++)
{
if (sieve[x / 8] & (0x01 << (x % 8))) {
primes.push_back(x);
// Is prime. Mark multiplicates.
for (T j = 2 * x; j <= max; j += x)
{
sieve[j / 8] &= ~(0x01 << (j % 8));
}
}
}
delete[] sieve;
return primes;
}
//---------------------------------------------------------------
//int main() // by Florian Mayer
//{g++ -O3 -std=c++14 -fopenmp main.cpp && ./a.out
// vector<unsigned long> primes;
// primes = get_primes(10000000);
// // return 0;
// // Print out result.
// vector<unsigned long>::iterator it;
// for(it=primes.begin(); it < primes.end(); it++)
// cout << *it << " ";
//
// cout << endl;
// return 0;
//}