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 0000000..690d595 Binary files /dev/null and b/javacount3openmp/java_code/CountThrees.class differ diff --git a/javacount3openmp/java_code/CountThrees.java b/javacount3openmp/java_code/CountThrees.java index 6587c37..ceef142 100644 --- a/javacount3openmp/java_code/CountThrees.java +++ b/javacount3openmp/java_code/CountThrees.java @@ -1,7 +1,8 @@ import java.util.*; + public class CountThrees implements Runnable { - private static final int ARRAY_LENGTH = 1000000; + private static int ARRAY_LENGTH; private static final int MAX_THREADS = 10; private static final int MAX_RANGE = 100; private static final Random random = new Random(); @@ -11,6 +12,16 @@ public class CountThrees implements Runnable { private static Thread[] threads; public static void main(String[] args) { + if (args.length != 1) { + System.err.println("Usage: java CountThrees "); + 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