diff --git a/javacount3openmp/c_openmp/Makefile b/javacount3openmp/c_openmp/Makefile new file mode 100644 index 0000000..0f93c26 --- /dev/null +++ b/javacount3openmp/c_openmp/Makefile @@ -0,0 +1,11 @@ +all: count_threes + +count_threes: count_threes.c + gcc -fopenmp -o count_threes count_threes.c + +run: + export OMP_NUM_THREADS=30 + ./count_threes + +clean: + rm -f count_threes \ No newline at end of file diff --git a/javacount3openmp/c_openmp/count_threes b/javacount3openmp/c_openmp/count_threes new file mode 100755 index 0000000..9ea75be Binary files /dev/null and b/javacount3openmp/c_openmp/count_threes differ diff --git a/javacount3openmp/c_openmp/count_threes.c b/javacount3openmp/c_openmp/count_threes.c new file mode 100644 index 0000000..dd85e07 --- /dev/null +++ b/javacount3openmp/c_openmp/count_threes.c @@ -0,0 +1,43 @@ +#include +#include + +int count3s(int* array, int length) +{ + 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 + { + 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; + + 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); + + return 0; +} diff --git a/javacount3openmp/c_openmp/testopenmp/Makefile b/javacount3openmp/c_openmp/testopenmp/Makefile new file mode 100644 index 0000000..2358d3e --- /dev/null +++ b/javacount3openmp/c_openmp/testopenmp/Makefile @@ -0,0 +1,11 @@ +all: testopenmp run + +testopenmp: + gcc -fopenmp -o testopenmp testopenmp.c + +run: + export OMP_NUM_THREADS=30 + ./testopenmp + +clean: + rm -f testopenmp \ No newline at end of file diff --git a/javacount3openmp/c_openmp/testopenmp/testopenmp.c b/javacount3openmp/c_openmp/testopenmp/testopenmp.c new file mode 100644 index 0000000..e0c1f2d --- /dev/null +++ b/javacount3openmp/c_openmp/testopenmp/testopenmp.c @@ -0,0 +1,30 @@ +#include +#include + +void main() +{ + int i; + int num; + int sum = 0; + int tsum; + int x[10]; + + num = omp_get_num_procs(); + + x[0] = 0; + +#pragma omp parallel for + + for (i = 1; i < 200; i++) { + // #pragma omp critical + { + tsum = sum; + sum = tsum + i; + } + + printf(" %d I am running on %d\n", i, omp_get_thread_num()); + } + + printf("the sum is %d\n", sum); + return 0; +} diff --git a/javacount3openmp/java_code/CountThrees.java b/javacount3openmp/java_code/CountThrees.java new file mode 100644 index 0000000..6587c37 --- /dev/null +++ b/javacount3openmp/java_code/CountThrees.java @@ -0,0 +1,81 @@ +import java.util.*; + +public class CountThrees implements Runnable { + private static final int ARRAY_LENGTH = 1000000; + private static final int MAX_THREADS = 10; + private static final int MAX_RANGE = 100; + private static final Random random = new Random(); + private static int count = 0; + private static final Object lock = new Object(); + private static int[] array; + private static Thread[] threads; + + public static void main(String[] args) { + array = new int[ARRAY_LENGTH]; + + // Initialize the elements in the array + for (int i = 0; i < array.length; i++) { + array[i] = random.nextInt(MAX_RANGE); + } + long startTime = System.nanoTime(); + + // Create the threads + CountThrees[] counters = new CountThrees[MAX_THREADS]; + int lengthPerThread = ARRAY_LENGTH / MAX_THREADS; + for (int i = 0; i < counters.length; i++) { + counters[i] = new CountThrees( + i * lengthPerThread, + lengthPerThread + ); + } + + // Run the threads + threads = new Thread[MAX_THREADS]; + for (int i = 0; i < counters.length; i++) { + threads[i] = new Thread(counters[i]); + threads[i].start(); + } + + for (int i = 0; i < counters.length; i++) { + try { + threads[i].join(); + } catch (InterruptedException e) { + // Do nothing + } + } + long endTime = System.nanoTime(); + // Print the number of threes + System.out.println("Number of threes: " + count); + + // compute the elapsed time in nanoseconds + long duration = (endTime - startTime); + + // convert to seconds + double seconds = (double)duration / 1_000_000_000.0; + + System.out.println("Time taken: " + seconds + " seconds"); + } + + private int startIndex; + private int elements; + private int myCount = 0; + + public CountThrees(int start, int elem) { + startIndex = start; + elements = elem; + } + + // Overload of run method in the Thread class + public void run() { + // Count the number of threes + for (int i = 0; i < elements; i++) { + if (array[startIndex + i] == 3) { + myCount++; + } + } + + synchronized (lock) { + count += myCount; + } + } +} \ No newline at end of file diff --git a/javacount3openmp/java_code/Makefile b/javacount3openmp/java_code/Makefile new file mode 100644 index 0000000..87e03e9 --- /dev/null +++ b/javacount3openmp/java_code/Makefile @@ -0,0 +1,8 @@ +all: CountThrees.class run + +CountThrees.class: + javac CountThrees.java +run: + java CountThrees +clean: + rm -f CountThrees.class \ No newline at end of file