Last active
June 28, 2018 04:52
-
-
Save bqcuong/a47ce4b225d295f4ec228921fed22b27 to your computer and use it in GitHub Desktop.
oscar_sample.c
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
#include<stdio.h> | |
#include<time.h> | |
#include<math.h> | |
#include<stdlib.h> | |
#define Max 1000 | |
void genMatrix(int** a,int n) { | |
srand(time(NULL)); | |
for(int i=0; i< n; i++) { | |
for (int j=0; j<n; j++) { | |
a[i][j] = rand()% 1000; | |
} | |
} | |
} | |
void output(char* name, int **a, int n) { | |
FILE *f ; | |
f = fopen (name, "w"); | |
for(int i=0;i<n;i++) { | |
for(int j=0;j<n;j++) { | |
fprintf(f, "%d ",a[i][j]); | |
} | |
fprintf(f, "\n"); | |
} | |
fclose(f); | |
} | |
int main () { | |
int** a; | |
int n = 5000; | |
a = malloc(sizeof(int*) * n); | |
size_t i; | |
for (i = 0; i < n; i++) { | |
a[i] = malloc(sizeof(int) * n); | |
} | |
genMatrix (a,n); | |
output("a.txt", a, n); | |
genMatrix (a,n); | |
output("b.txt", a, n); | |
genMatrix (a,n); | |
output("c.txt", a, n); | |
} |
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
#include <stdio.h> | |
#include <stdlib.h> | |
#include <time.h> | |
#include <omp.h> | |
int fun(double** A, double** B, double** C, int N) { | |
double *E = malloc(sizeof(double) * N); | |
double *X = malloc(sizeof(double) * N); | |
double *Y = malloc(sizeof(double) * N); | |
double *Z = malloc(sizeof(double) * N); | |
double O = 0, P = 0; | |
int i, j; | |
//------------------ MT 1 | |
for (i = 1; i < N; i++) | |
{ | |
O += A[i][i] / N; | |
P += B[i][i] / N; | |
for (j = 1; j < N; j++) { | |
X[i] = A[i][j] * B[i][j]; | |
} | |
} | |
//------------------ MT 2 | |
double Q = (A[1][1] + C[1][1]) * 0.5; | |
if (Q > 1.0) { | |
//------------------ MT 3 | |
int flag = 0; | |
do { | |
O *= 0.5; | |
P *= 0.5; | |
if (O <= 10.0) { | |
flag = 1; | |
break; | |
} | |
} | |
while (P > 10.0); | |
//------------------ MT 4 | |
double R = P * O; | |
if (flag || R <= 100.0) { | |
for (i = 1; i < N; i++) { | |
Y[i] = C[i][ i] * O; | |
} | |
} | |
else { | |
{ | |
for (i = 1; i < N; i++) { | |
printf("Thread %d => %d", omp_get_thread_num(), i); | |
E[i] = C[i][i] + Q; | |
Y[i] = C[i][i] * R; | |
} | |
for (i = 1; i < N; i++) { | |
printf("Thread %d => %d", omp_get_thread_num(), i); | |
Z[i] = C[i][i] * E[i]; | |
} | |
} | |
} | |
} | |
//------------------ MT 8 | |
else { | |
for (i = 1; i < N; i++) { | |
Z[i] = X[i] * 20; | |
} | |
} | |
//------------------ MT 9 | |
for (i = 1; i < N - 1; i++) { | |
Z[i] = Z[i] + Z[i + 1]; | |
} | |
//------------------ MT 10 | |
return 0; | |
} | |
void read_matrix(char* file, double** A, int M, int N) { | |
FILE *f = fopen(file, "r"); | |
int i, j; | |
for (i = 0; i < M; i++) { | |
for (j = 0; j < N; j++) { | |
if(!fscanf(f, "%lf", &A[i][j])) break; | |
} | |
} | |
fclose(f); | |
} | |
void init_matrix(double** A, int N) { | |
A = malloc(sizeof(double*) * N); | |
size_t i; | |
for (i = 0; i < N; i++) { | |
A[i] = malloc(sizeof(double) * N); | |
} | |
} | |
int main() { | |
// assign value for below matrices and integer | |
int N = 5000; | |
double **A; | |
double **B; | |
double **C; | |
// init_matrix(A, N); | |
// init_matrix(B, N); | |
// init_matrix(C, N); | |
A = malloc(sizeof(double*) * N); | |
size_t i; | |
for (i = 0; i < N; i++) { | |
A[i] = malloc(sizeof(double) * N); | |
} | |
B = malloc(sizeof(double*) * N); | |
for (i = 0; i < N; i++) { | |
B[i] = malloc(sizeof(double) * N); | |
} | |
C = malloc(sizeof(double*) * N); | |
for (i = 0; i < N; i++) { | |
C[i] = malloc(sizeof(double) * N); | |
} | |
// stop here | |
read_matrix("a.txt", A, N, N); | |
read_matrix("b.txt", B, N, N); | |
read_matrix("c.txt", C, N, N); | |
printf("fun() starts\n"); | |
double t = omp_get_wtime(); | |
fun(A, B, C, N); | |
double time_taken = (omp_get_wtime() - t) * 1000; | |
printf("fun() ends\n"); | |
printf("fun() took %f ms to execute\n", time_taken); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment