You are on page 1of 3

Teks 1

%%file hello_world.c
#include <stdio.h>
#include <omp.h>

int main(){
int rank;

printf("Sabrina Aziz Aulia\n");

#pragma omp parallel


{
rank = omp_get_thread_num();
int num_threads = omp_get_num_threads();
printf("Saya tehread %i dari %i threads\n", rank,
num_threads);
}

printf("End of program\n");
}

Overwriting hello_world.c

!gcc hello_world.c -o test -fopenmp

!./test

Sabrina Aziz Aulia


Saya tehread 1 dari 2 threads
Saya tehread 0 dari 2 threads
End of program

!export OMP_NUM_THREADS=8; ./test

Sabrina Aziz Aulia


Saya tehread 0 dari 8 threads
Saya tehread 7 dari 8 threads
Saya tehread 6 dari 8 threads
Saya tehread 5 dari 8 threads
Saya tehread 4 dari 8 threads
Saya tehread 3 dari 8 threads
Saya tehread 2 dari 8 threads
Saya tehread 1 dari 8 threads
End of program

#Open MP (VECTOR ADD)


%%file vector_add.c
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
#define CHUNKSIZE 202
#define N 2020

int main(int argc, char** argv){

int i, chunk;
float a[N], b[N], c[N];

// some initialization
for(i = 0; i < N; i++){
a[i] = i*10;
b[i] = 2020.0 - a[i];
}
chunk = CHUNKSIZE;

#pragma omp parallel shared(a,b,c,chunk) private(i)


{
#pragma omp for schedule(dynamic, chunk) nowait
for(i = 0; i <N; i++)
c[i] = a[i] + b[i];
}

// end of parallel section

for(i = 0; i <= N; i++){


if(i % 101 == 0){
printf("%.5f + %.5f = %.5f\n", a[i], b[i], c[i]);
}
}
}

Overwriting vector_add.c

!gcc vector_add.c -o test1 -fopenmp

!export OMP_NUM_THREADS=8; time ./test1

0.00000 + 2020.00000 = 2020.00000


1010.00000 + 1010.00000 = 2020.00000
2020.00000 + 0.00000 = 2020.00000
3030.00000 + -1010.00000 = 2020.00000
4040.00000 + -2020.00000 = 2020.00000
5050.00000 + -3030.00000 = 2020.00000
6060.00000 + -4040.00000 = 2020.00000
7070.00000 + -5050.00000 = 2020.00000
8080.00000 + -6060.00000 = 2020.00000
9090.00000 + -7070.00000 = 2020.00000
10100.00000 + -8080.00000 = 2020.00000
11110.00000 + -9090.00000 = 2020.00000
12120.00000 + -10100.00000 = 2020.00000
13130.00000 + -11110.00000 = 2020.00000
14140.00000 + -12120.00000 = 2020.00000
15150.00000 + -13130.00000 = 2020.00000
16160.00000 + -14140.00000 = 2020.00000
17170.00000 + -15150.00000 = 2020.00000
18180.00000 + -16160.00000 = 2020.00000
19190.00000 + -17170.00000 = 2020.00000
2020.00000 + 2020.00000 = 0.00000

real 0m0.004s
user 0m0.002s
sys 0m0.000s

You might also like