From e95d902ddaf3a61cae10601d304629c693fac6a9 Mon Sep 17 00:00:00 2001 From: Ziad Arafat Date: Thu, 2 Nov 2023 20:49:59 -0600 Subject: [PATCH] started openmp project --- javacount3openmp/c_openmp/count_threes.c | 76 ++++++++++++------- javacount3openmp/java_code/CountThrees.class | Bin 0 -> 2742 bytes javacount3openmp/java_code/CountThrees.java | 15 +++- javacount3openmp/java_code/Makefile | 4 +- 4 files changed, 63 insertions(+), 32 deletions(-) create mode 100644 javacount3openmp/java_code/CountThrees.class diff --git a/javacount3openmp/c_openmp/count_threes.c b/javacount3openmp/c_openmp/count_threes.c index dd85e07..2aa2ce6 100644 --- a/javacount3openmp/c_openmp/count_threes.c +++ b/javacount3openmp/c_openmp/count_threes.c @@ -1,43 +1,63 @@ #include +#include +#include #include int count3s(int* array, int length) { - int count = 0; - int i; + int count = 0; + int i; - #pragma omp parallel shared(array, count, length) private(i) - { - int count_p = 0; - - #pragma omp for - for (i = 0; i < length; i++) { - if (array[i] == 3) { - count_p++; - } - } - - #pragma omp critical + #pragma omp parallel shared(array, count, length) private(i) { - count += count_p; - } - } + int count_p = 0; - return count; + #pragma omp for + for (i = 0; i < length; i++) { + if (array[i] == 3) { + count_p++; + } + } + + #pragma omp critical + { + count += count_p; + } + } + + return count; } int main(int argc, char const *argv[]) { - int array[] = {1, 2, 3, 4, 5, 3, 3, 3, 3, 3}; - int length = 10; - double start_time, end_time; + if (argc != 2) { + printf("Usage: %s \n", argv[0]); + return 1; + } - start_time = omp_get_wtime(); - int count = count3s(array, length); - end_time = omp_get_wtime(); + int length = atoi(argv[1]); + if (length <= 0) { + printf("Error: array size must be a positive integer\n"); + return 1; + } - printf("count: %d\n", count); - printf("Time taken: %f seconds\n", end_time - start_time); + int* array = (int*) malloc(length * sizeof(int)); + int i; - return 0; -} + srand(time(NULL)); + for (i = 0; i < length; i++) { + array[i] = rand() % 10 + 1; + } + + double start_time, end_time; + + start_time = omp_get_wtime(); + int count = count3s(array, length); + end_time = omp_get_wtime(); + + printf("count: %d\n", count); + printf("Time taken: %f seconds\n", end_time - start_time); + + free(array); + return 0; +} \ No newline at end of file diff --git a/javacount3openmp/java_code/CountThrees.class b/javacount3openmp/java_code/CountThrees.class new file mode 100644 index 0000000000000000000000000000000000000000..690d59518c18a19c9f77f7ba68212282a7a95c5a GIT binary patch literal 2742 zcmZ`)O>A3L7XEH*=h?5XO>mpENlIf%+euuKK%gzHLug3S#?(nWcE~hMA$hSc$!qNQ z%=2?efEk#X&R^%B0mK3c#HI^03ljRHmMIcQvlt1DgjkIRv6;neSRi&)MfvWt)3`Kv z;rs5n=boSMeCOPMeS7sffWvqrf;t2=1Pz2xFVJ|#JZtvkO?$2f{jOR+TkO(6zu;;0gIVTUrltOIKDcj!oyenlX)@Qn|xip=Z_MA80uS3TM z4Tgb@*d(xd9eV;=QCZL1bl6exSc%1>crMX7p)=e@1NR^*5R{9S$E72ww+fPc=;(Y>XDE*vIEX{^*=O07*Uy+b zlB|S=zE$IX=@b{toQl2Qz~eYfH=ZwI;+v;xvaBdF%mkiLrUwaGEimb_ZJV=s>9Zh# zVFO2SlwOIV%bGGQvLJz^fhUzbc*e184Jm;gYeiCIaNTmzleyb9QXMd6;JBiHz$tqw z)E^s2D-MLRiq268jH@a>rKpqatO=TZpom#N@adV);!Sg{UNBT6REL&EZWn7h$<};p|T^Kcs{$9fi-y=rV zhK82}Hch2!+A_h4xn)-Q<<)tzN;&8GitTV;HeaQuo^i@XRC@DHuGCPEU+MTYexu>Hs-(XY*tG_hWuJ8xWKSiyN?RW>v!3HF3A|a$+p1#4 z=m>i;FH5^pjNS65bNMU#k!uhUcqr4u zSwMV^disXYhUsKKGavRJJ8p* zjNRAYMKc$Eb~ffNq3ifLAuind{}nuZxKaNHTF0o~sOR?w1N%cwp)RVobv1>qp>?t; z)D)a=3SP#6%()Qn{7>m8R`A$u&HI<}n9@y5@=EVz3}k#k?45n?vpLfYz*?-+~F}V1$R;B3E(JKYO3ZMbSpA+VM~P8=v7A zCC?MN^VrG%1|rsoP3ryTgWZO0)NG<|GrIV2KZF(z#qAhmHBWGz;K?c8n_=_KV;5_= z8|S#5XANKFjkoxs{T)37wF@7jmmVLX&ja+j51(`YA6om8Pr_H|$2SEW8eS9vxPcuvIp%I)&rQ^cw1#8+4zcij^D5;>A5&Aa zZ%h0#rs9E%n2xty#B6-~MacIl7k=W+yahfKJy6fM;#`xq1m=4a+g-?wymN7q>`j|zb&h*R*PVD{U0>YzvKY8FI zRf29N-A{Y{Oj@m#66{2n-)WLRL*T~=Y?`D$N$p8yb&9Z0`Ow^pkjlB8`QBo6>NK=! zs?#+5>;}^a"); + System.exit(1); + } + try { + ARRAY_LENGTH = Integer.parseInt(args[0]); + } catch (NumberFormatException e) { + System.err.println("Invalid argument: " + args[0] + " is not a valid integer"); + System.exit(1); + } array = new int[ARRAY_LENGTH]; // Initialize the elements in the array @@ -78,4 +89,4 @@ public class CountThrees implements Runnable { count += myCount; } } -} \ No newline at end of file +} diff --git a/javacount3openmp/java_code/Makefile b/javacount3openmp/java_code/Makefile index 87e03e9..55f2c50 100644 --- a/javacount3openmp/java_code/Makefile +++ b/javacount3openmp/java_code/Makefile @@ -1,8 +1,8 @@ -all: CountThrees.class run +all: CountThrees.class CountThrees.class: javac CountThrees.java run: - java CountThrees + java CountThrees 1000000000 clean: rm -f CountThrees.class \ No newline at end of file