Skip to content

Commit 0618e61

Browse files
authored
Add files via upload
1 parent 8b7db45 commit 0618e61

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

FastVectorAddition/arr_openmp.c

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <stdlib.h>
2+
#include <stdio.h>
3+
#include <sys/time.h>
4+
5+
#define ARRSIZE 1000000
6+
7+
int main() {
8+
int *A, *B, *C;
9+
A = (int *)malloc(ARRSIZE*sizeof(int));
10+
B = (int *)malloc(ARRSIZE*sizeof(int));
11+
C = (int *)malloc(ARRSIZE*sizeof(int));
12+
13+
for (int i = 0; i < ARRSIZE; i++) {
14+
A[i] = i;
15+
B[i] = i;
16+
}
17+
18+
struct timeval start, end;
19+
gettimeofday(&start, NULL);
20+
#pragma omp parallel for default(none) shared(A,B,C)
21+
for (int i = 0; i < ARRSIZE; i++) {
22+
C[i] = A[i] + B[i];
23+
}
24+
gettimeofday(&end, NULL);
25+
26+
double time_taken = (end.tv_sec) + (end.tv_usec/1e6) - ((start.tv_sec) + (start.tv_usec/1e6));
27+
printf("Time taken to add integer arrays of length %d: %g (seconds)\n", ARRSIZE, time_taken);
28+
29+
free(A);
30+
free(B);
31+
free(C);
32+
}

0 commit comments

Comments
 (0)