Commit 4297c938 authored by mohamad's avatar mohamad

Creating SIMD Program

parents
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing>
<profile name="Maven default annotation processors profile" enabled="true">
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<module name="Lab1" />
</profile>
</annotationProcessing>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RemoteRepositoriesConfiguration">
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Central Repository" />
<option name="url" value="https://repo.maven.apache.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Maven Central repository" />
<option name="url" value="https://repo1.maven.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="jboss.community" />
<option name="name" value="JBoss Community repository" />
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
</remote-repository>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_19" default="true" project-jdk-name="openjdk-19 (2)" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="AutoImportSettings">
<option name="autoReloadType" value="SELECTIVE" />
</component>
<component name="ChangeListManager">
<list default="true" id="18737100-4b11-4578-9356-e2f0341259ce" name="Changes" comment="" />
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="FileTemplateManagerImpl">
<option name="RECENT_TEMPLATES">
<list>
<option value="Interface" />
<option value="Class" />
</list>
</option>
</component>
<component name="MarkdownSettingsMigration">
<option name="stateVersion" value="1" />
</component>
<component name="ProjectId" id="2XMVGtfupLN8AA5FR24PXykDy1N" />
<component name="ProjectViewState">
<option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" />
</component>
<component name="PropertiesComponent"><![CDATA[{
"keyToString": {
"RunOnceActivity.OpenProjectViewOnStart": "true",
"RunOnceActivity.ShowReadmeOnStart": "true"
}
}]]></component>
<component name="RunManager">
<configuration name="Main" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="threading.runners.Main" />
<module name="Lab1" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="threading.runners.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<recent_temporary>
<list>
<item itemvalue="Application.Main" />
</list>
</recent_temporary>
</component>
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
<component name="TaskManager">
<task active="true" id="Default" summary="Default task">
<changelist id="18737100-4b11-4578-9356-e2f0341259ce" name="Changes" comment="" />
<created>1698438843315</created>
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1698438843315</updated>
</task>
<servers />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>Lab1</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>19</maven.compiler.source>
<maven.compiler.target>19</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
\ No newline at end of file
package threading.abstractions;
import java.util.Collection;
public interface IPrimeNumbersExtractor {
Collection<Integer> extract(int start, int end);
}
package threading.implementation;
import threading.abstractions.IPrimeNumbersExtractor;
import java.util.ArrayList;
import java.util.Collection;
public class ParallelPrimeNumbersExtractor implements IPrimeNumbersExtractor {
private final int _numberOfThreads;
public ParallelPrimeNumbersExtractor(int numberOfThreads) {
_numberOfThreads = numberOfThreads;
}
@Override
public Collection<Integer> extract(int start, int end) {
Collection<Integer> result = new ArrayList<>();
Collection<Thread> threads = new ArrayList<>();
Collection<RunnablePrimeNumbersExtractor> extractors = new ArrayList<>();
System.out.println(_numberOfThreads + " threads launched");
int base = start;
int last = 0;
int range = (end - start) /_numberOfThreads;
for(int i = 0; i < _numberOfThreads; i++) {
if(i == _numberOfThreads - 1) {
last = end;
}
else{
last = base+range;
}
RunnablePrimeNumbersExtractor extractor = new RunnablePrimeNumbersExtractor(base,last);
Thread thread = new Thread(extractor);
base = base+range+1;
thread.start();
extractors.add(extractor);
threads.add(thread);
}
for (Thread thread : threads) {
try {
thread.join();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
for (RunnablePrimeNumbersExtractor extractor : extractors) {
result.addAll(extractor.get_primeNumbers());
}
return result;
}
}
package threading.implementation;
import java.util.ArrayList;
import java.util.Collection;
class RunnablePrimeNumbersExtractor implements Runnable {
private final int _start;
private final int _end;
private Collection<Integer> _primeNumbers;
public Collection<Integer> get_primeNumbers() {
return _primeNumbers;
}
public RunnablePrimeNumbersExtractor(int start, int end) {
_start = start;
_end = end;
_primeNumbers = new ArrayList<>();
}
@Override
public void run() {
for (int i = _start; i < _end; i++) {
boolean isPrime = true;
for (int j = 2; j < Math.sqrt(i); j++) {
if (i % j == 0) {
isPrime = false;
break;
}
}
if(isPrime) {
_primeNumbers.add(i);
}
}
}
}
package threading.runners;
import threading.abstractions.IPrimeNumbersExtractor;
import threading.implementation.ParallelPrimeNumbersExtractor;
import java.util.Collection;
public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
IPrimeNumbersExtractor extractor = new ParallelPrimeNumbersExtractor(4);
Collection<Integer> collection = extractor.extract(0,1000000000);
// for (Integer i : collection) {
// System.out.println(i);
// }
System.out.println(collection.size());
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment