Initial
This commit is contained in:
64
uebung_02/main_matrix.c
Normal file
64
uebung_02/main_matrix.c
Normal file
@ -0,0 +1,64 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int compute(int N) {
|
||||
|
||||
int i_0, i_1;
|
||||
// claim space
|
||||
double **A = (double **)malloc(sizeof(double *) * N);
|
||||
for (i_0 = 0; i_0 < N; i_0++) {
|
||||
A[i_0] = (double *)malloc(sizeof(double) * N);
|
||||
}
|
||||
double *x = (double *)malloc(sizeof(double) * N);
|
||||
double *y = (double *)malloc(sizeof(double) * N);
|
||||
double *y_sol = (double *)malloc(sizeof(double) * N);
|
||||
|
||||
// fill matrix and vector
|
||||
for (i_0 = 0; i_0 < N; i_0++) {
|
||||
x[i_0] = i_0 + 1;
|
||||
y_sol[i_0] = (i_0 + 1) * N;
|
||||
for (i_1 = 0; i_1 < N; i_1++) {
|
||||
A[i_0][i_1] = (i_0 + 1.0) / (i_1 + 1.0);
|
||||
}
|
||||
}
|
||||
|
||||
// for (i_0 = 0; i_0 < N; i_0++){
|
||||
// printf("[ ");
|
||||
// for(i_1 = 0; i_1 < N; i_1++){
|
||||
// printf("%.3f ",A[i_0][i_1]);
|
||||
// }
|
||||
// printf("]");
|
||||
// printf("[ %.1f ] = [ %.1f ][ %.1f ]\n",x[i_0],y[i_0],y_sol[i_0]);
|
||||
// }
|
||||
|
||||
// compute matrix-vector product
|
||||
for (i_0 = 0; i_0 < N; i_0++) {
|
||||
y[i_0] = 0;
|
||||
for (i_1 = 0; i_1 < N; i_1++) {
|
||||
y[i_0] += A[i_0][i_1] * x[i_1];
|
||||
}
|
||||
}
|
||||
|
||||
// compare result to solution
|
||||
double sum = 0.0;
|
||||
for (i_0 = 0; i_0 < N; i_0++) {
|
||||
sum += (y[i_0] - y_sol[i_0]) * (y[i_0] - y_sol[i_0]);
|
||||
}
|
||||
|
||||
return sum;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
|
||||
int N;
|
||||
if (argc != 2) {
|
||||
N = 1000;
|
||||
} else {
|
||||
N = atoi(argv[1]); // Convert the argument to an integer
|
||||
}
|
||||
|
||||
double err = compute(N);
|
||||
printf("Error for N = %d: %.8f\n", N, err);
|
||||
|
||||
return 0;
|
||||
}
|
19
uebung_02/main_openmp.c
Normal file
19
uebung_02/main_openmp.c
Normal file
@ -0,0 +1,19 @@
|
||||
#include <omp.h> // load Open-MP library
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main() {
|
||||
int N = 5;
|
||||
int num;
|
||||
|
||||
#pragma omp parallel for private(num) // start Open-MP section
|
||||
// here the program (1 thread) is split into multiple threads
|
||||
// each thread has its own copy of i (loop) and num (private)
|
||||
// all threads share N (by default, because defined outside)
|
||||
for (int i = 0; i < N; i++) {
|
||||
num = omp_get_thread_num();
|
||||
printf("Thread %d does iteration %d\n", num, i);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
BIN
uebung_02/matrix_mul
Executable file
BIN
uebung_02/matrix_mul
Executable file
Binary file not shown.
BIN
uebung_02/openmp_hello
Executable file
BIN
uebung_02/openmp_hello
Executable file
Binary file not shown.
Reference in New Issue
Block a user