mirror of
https://github.com/RustyRaptor/parallel-programming.git
synced 2026-12-03 15:55:15 -07:00
started openmp project
This commit is contained in:
1 parent
acbb814247
commit
e95d902dda
4 files changed
+63
-32
No files matched your search
@@ -1,43 +1,63 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <omp.h>
|
||||
|
||||
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 <array_size>\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;
|
||||
}
|
||||
Binary file not shown.
@@ -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 <array_length>");
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
Reference in new issue
Block a user