#include
#include
#include
#include
double *function_a(const double *A, const double *x, const int N) {
double *y = new double[N];
for (unsigned int i = 0; i < N; i++) {
for (unsigned int i = 0; i < N; i++) {
for (unsigned int j = 0; j < N; j++) {
y[i] += A[i * N + j] * x[i];
double *function_b(const double a, const double *u, const double *v, const int N) {
double *x = new double[N];
for (unsigned int i = 0; i < N; i++) {
x[i] = a * u[i] + v[i];
double *function_c(const double s, const double *x, const double *y,
const int N) {
double *z = new double[N];
for (unsigned int i = 0; i < N; i++) {
if (i % 2 == 0) {
z[i] = s * x[i] + y[i];
z[i] = x[i] + y[i];
double function_d(const double *u, const double *v, const int N) {
double s = 0;
for (unsigned int i = 0; i < N; i++) {
s += u[i] * v[i];
void init_datastructures(double *u, double *v, double *A, const int N) {
for (unsigned int i = 0; i < N; i++) {
u[i] = static_cast
v[i] = static_cast
for (unsigned int i = 0; i < N * N; i++) {
A[i] = static_cast
void print_results_to_file(const double s, const double *x, const double *y,
const double *z, const double *A, const long long n,
std::ofstream &File) {
unsigned int N = std::min(n, static_cast
File << "N: " << n << "\n"; File << "s: " << std::fixed << std::setprecision(1) << s << "\n"; File << "x: " for (unsigned int i = 0; i < N; i++) { File << x[i] << " "; File << "\n"; File << "y: " for (unsigned int i = 0; i < N; i++) { File << y[i] << " "; File << "\n"; File << "z: " for (unsigned int i = 0; i < N; i++) { File << z[i] << " "; File << "\n"; int main(int argc, char **argv) { long long N; if (argc == 2) { N = std::stoi(argv[1]); std::cout << "Error: Missing problem size N. Please provide N as " "commandline parameter. Usage example for N=10: " "./number_crunching 10" << std::endl; double *u = new double[N]; double *v = new double[N]; double *A = new double[N * N]; init_datastructures(u, v, A, N); double s = function_d(u, v, N); double *x = function_b(2, u, v, N); double *y = function_a(A, x, N); double *z = function_c(s, x, y, N); std::ofstream File("partial_results.out"); print_results_to_file(s, x, y, z, A, N, File); std::cout << "For correctness checking, partial results have been written to " "partial_results.out" << std::endl; delete[] u; delete[] v; delete[] A; delete[] x; delete[] y; delete[] z; EXIT_SUCCESS;