setup code for java and openmp program

This commit is contained in:
Ziad Arafat committed 2023-11-02 00:59:48 -06:00
1 parent b3674f6752
commit acbb814247
7 files changed
+184

No files matched your search

+11
View File
@@ -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
Binary file not shown.
+43
View File
@@ -0,0 +1,43 @@
#include <stdio.h>
#include <omp.h>
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;
}
@@ -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
@@ -0,0 +1,30 @@
#include <stdio.h>
#include <omp.h>
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;
}
@@ -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;
}
}
}
+8
View File
@@ -0,0 +1,8 @@
all: CountThrees.class run
CountThrees.class:
javac CountThrees.java
run:
java CountThrees
clean:
rm -f CountThrees.class